From a58af379e16060dd1d59e1d10e652fa300d223cf Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Tue, 20 Aug 2024 11:49:04 +0100 Subject: [PATCH] login: add e2e tests for oauth + escape hatch Signed-off-by: Laura Brehm (cherry picked from commit a327476f7fdf7fe0bcaf1e9695eec1d00ee0f1f5) Signed-off-by: Laura Brehm --- e2e/registry/login_test.go | 56 ++++++++++++++++++++++++++++++++++++++ e2e/registry/main_test.go | 17 ++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 e2e/registry/login_test.go create mode 100644 e2e/registry/main_test.go diff --git a/e2e/registry/login_test.go b/e2e/registry/login_test.go new file mode 100644 index 0000000000..523e00d065 --- /dev/null +++ b/e2e/registry/login_test.go @@ -0,0 +1,56 @@ +package registry + +import ( + "io" + "os/exec" + "strings" + "syscall" + "testing" + "time" + + "github.com/creack/pty" + "gotest.tools/v3/assert" +) + +func TestOauthLogin(t *testing.T) { + t.Parallel() + loginCmd := exec.Command("docker", "login") + + p, err := pty.Start(loginCmd) + assert.NilError(t, err) + defer func() { + _ = loginCmd.Wait() + _ = p.Close() + }() + + time.Sleep(1 * time.Second) + pid := loginCmd.Process.Pid + t.Logf("terminating PID %d", pid) + err = syscall.Kill(pid, syscall.SIGTERM) + assert.NilError(t, err) + + output, _ := io.ReadAll(p) + assert.Check(t, strings.Contains(string(output), "USING WEB BASED LOGIN"), string(output)) +} + +func TestLoginWithEscapeHatch(t *testing.T) { + t.Parallel() + loginCmd := exec.Command("docker", "login") + loginCmd.Env = append(loginCmd.Env, "DOCKER_CLI_DISABLE_OAUTH_LOGIN=1") + + p, err := pty.Start(loginCmd) + assert.NilError(t, err) + defer func() { + _ = loginCmd.Wait() + _ = p.Close() + }() + + time.Sleep(1 * time.Second) + pid := loginCmd.Process.Pid + t.Logf("terminating PID %d", pid) + err = syscall.Kill(pid, syscall.SIGTERM) + assert.NilError(t, err) + + output, _ := io.ReadAll(p) + assert.Check(t, strings.Contains(string(output), "Username:"), string(output)) +} diff --git a/e2e/registry/main_test.go b/e2e/registry/main_test.go new file mode 100644 index 0000000000..9174715188 --- /dev/null +++ b/e2e/registry/main_test.go @@ -0,0 +1,17 @@ +package registry + +import ( + "fmt" + "os" + "testing" + + "github.com/docker/cli/internal/test/environment" +) + +func TestMain(m *testing.M) { + if err := environment.Setup(); err != nil { + fmt.Println(err.Error()) + os.Exit(3) + } + os.Exit(m.Run()) +}