diff --git a/cli/command/registry/login_test.go b/cli/command/registry/login_test.go index 9dfb13dfe751..072a72feabf6 100644 --- a/cli/command/registry/login_test.go +++ b/cli/command/registry/login_test.go @@ -1,6 +1,7 @@ package registry import ( + "bytes" "context" "errors" "fmt" @@ -10,6 +11,7 @@ import ( "time" "github.com/creack/pty" + "github.com/docker/cli/cli/config/configfile" configtypes "github.com/docker/cli/cli/config/types" "github.com/docker/cli/cli/streams" "github.com/docker/cli/internal/prompt" @@ -19,7 +21,6 @@ import ( "github.com/moby/moby/client" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" - "gotest.tools/v3/fs" ) const ( @@ -71,8 +72,9 @@ func TestLoginWithCredStoreCreds(t *testing.T) { }, } ctx := context.Background() + tmpDir := t.TempDir() cli := test.NewFakeCli(&fakeClient{}) - cli.ConfigFile().Filename = filepath.Join(t.TempDir(), "config.json") + cli.SetConfigFile(configfile.New(filepath.Join(tmpDir, "config.json"))) for _, tc := range testCases { _, err := loginWithStoredCredentials(ctx, cli, tc.inputAuthConfig) if tc.expectedErrMsg != "" { @@ -91,6 +93,7 @@ func TestRunLogin(t *testing.T) { testCases := []struct { doc string priorCredentials map[string]configtypes.AuthConfig + stdIn string input loginOptions expectedCredentials map[string]configtypes.AuthConfig expectedErr string @@ -286,20 +289,39 @@ func TestRunLogin(t *testing.T) { }, }, }, + { + doc: "password stdin with line-endings", + priorCredentials: map[string]configtypes.AuthConfig{}, + stdIn: "my password\r\n", + input: loginOptions{ + serverAddress: "reg1", + user: "my-username", + passwordStdin: true, + }, + expectedCredentials: map[string]configtypes.AuthConfig{ + "reg1": { + Username: "my-username", + Password: "my password", + ServerAddress: "reg1", + }, + }, + }, } for _, tc := range testCases { t.Run(tc.doc, func(t *testing.T) { - tmpFile := fs.NewFile(t, "test-run-login") - defer tmpFile.Remove() + tmpDir := t.TempDir() + cfg := configfile.New(filepath.Join(tmpDir, "config.json")) cli := test.NewFakeCli(&fakeClient{}) - configfile := cli.ConfigFile() - configfile.Filename = tmpFile.Path() + cli.SetConfigFile(cfg) + if tc.input.passwordStdin { + cli.SetIn(streams.NewIn(io.NopCloser(bytes.NewBufferString(tc.stdIn)))) + } for _, priorCred := range tc.priorCredentials { - assert.NilError(t, configfile.GetCredentialsStore(priorCred.ServerAddress).Store(priorCred)) + assert.NilError(t, cfg.GetCredentialsStore(priorCred.ServerAddress).Store(priorCred)) } - storedCreds, err := configfile.GetAllCredentials() + storedCreds, err := cfg.GetAllCredentials() assert.NilError(t, err) assert.DeepEqual(t, storedCreds, tc.priorCredentials) @@ -310,7 +332,7 @@ func TestRunLogin(t *testing.T) { } assert.NilError(t, loginErr) - outputCreds, err := configfile.GetAllCredentials() + outputCreds, err := cfg.GetAllCredentials() assert.Check(t, err) assert.DeepEqual(t, outputCreds, tc.expectedCredentials) }) @@ -356,11 +378,10 @@ func TestLoginNonInteractive(t *testing.T) { for _, registryAddr := range registries { for _, tc := range testCases { t.Run(tc.doc, func(t *testing.T) { - tmpFile := fs.NewFile(t, "test-run-login") - defer tmpFile.Remove() + tmpDir := t.TempDir() + cfg := configfile.New(filepath.Join(tmpDir, "config.json")) cli := test.NewFakeCli(&fakeClient{}) - cfg := cli.ConfigFile() - cfg.Filename = tmpFile.Path() + cli.SetConfigFile(cfg) options := loginOptions{ serverAddress: registryAddr, } @@ -419,11 +440,10 @@ func TestLoginNonInteractive(t *testing.T) { for _, registryAddr := range registries { for _, tc := range testCases { t.Run(tc.doc, func(t *testing.T) { - tmpFile := fs.NewFile(t, "test-run-login") - defer tmpFile.Remove() + tmpDir := t.TempDir() + cfg := configfile.New(filepath.Join(tmpDir, "config.json")) cli := test.NewFakeCli(&fakeClient{}) - cfg := cli.ConfigFile() - cfg.Filename = tmpFile.Path() + cli.SetConfigFile(cfg) serverAddress := registryAddr if serverAddress == "" { serverAddress = "https://index.docker.io/v1/" @@ -465,17 +485,15 @@ func TestLoginTermination(t *testing.T) { _ = p.Close() }) + tmpDir := t.TempDir() + cfg := configfile.New(filepath.Join(tmpDir, "config.json")) cli := test.NewFakeCli(&fakeClient{}, func(fc *test.FakeCli) { fc.SetOut(streams.NewOut(tty)) fc.SetIn(streams.NewIn(tty)) }) - tmpFile := fs.NewFile(t, "test-login-termination") - defer tmpFile.Remove() - - configFile := cli.ConfigFile() - configFile.Filename = tmpFile.Path() + cli.SetConfigFile(cfg) - ctx, cancel := context.WithCancel(context.Background()) + ctx, cancel := context.WithCancel(t.Context()) t.Cleanup(cancel) runErr := make(chan error)