|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os/exec" |
| 6 | + "strings" |
| 7 | + "syscall" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/creack/pty" |
| 12 | + "gotest.tools/v3/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestOauthLogin(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + loginCmd := exec.Command("docker", "login") |
| 18 | + |
| 19 | + p, err := pty.Start(loginCmd) |
| 20 | + assert.NilError(t, err) |
| 21 | + defer func() { |
| 22 | + _ = loginCmd.Wait() |
| 23 | + _ = p.Close() |
| 24 | + }() |
| 25 | + |
| 26 | + time.Sleep(1 * time.Second) |
| 27 | + pid := loginCmd.Process.Pid |
| 28 | + t.Logf("terminating PID %d", pid) |
| 29 | + err = syscall.Kill(pid, syscall.SIGTERM) |
| 30 | + assert.NilError(t, err) |
| 31 | + |
| 32 | + output, _ := io.ReadAll(p) |
| 33 | + assert.Check(t, strings.Contains(string(output), "USING WEB BASED LOGIN"), string(output)) |
| 34 | +} |
| 35 | + |
| 36 | +func TestLoginWithEscapeHatch(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + loginCmd := exec.Command("docker", "login") |
| 39 | + loginCmd.Env = append(loginCmd.Env, "DOCKER_CLI_DISABLE_OAUTH_LOGIN=1") |
| 40 | + |
| 41 | + p, err := pty.Start(loginCmd) |
| 42 | + assert.NilError(t, err) |
| 43 | + defer func() { |
| 44 | + _ = loginCmd.Wait() |
| 45 | + _ = p.Close() |
| 46 | + }() |
| 47 | + |
| 48 | + time.Sleep(1 * time.Second) |
| 49 | + pid := loginCmd.Process.Pid |
| 50 | + t.Logf("terminating PID %d", pid) |
| 51 | + err = syscall.Kill(pid, syscall.SIGTERM) |
| 52 | + assert.NilError(t, err) |
| 53 | + |
| 54 | + output, _ := io.ReadAll(p) |
| 55 | + assert.Check(t, strings.Contains(string(output), "Username:"), string(output)) |
| 56 | +} |
0 commit comments