diff --git a/cli/command/registry_test.go b/cli/command/registry_test.go index b332bf0c6e50..a6ee43c6ece7 100644 --- a/cli/command/registry_test.go +++ b/cli/command/registry_test.go @@ -5,13 +5,13 @@ import ( "testing" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" . "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/client" - "github.com/stretchr/testify/require" ) type fakeClient struct { @@ -29,28 +29,33 @@ func (cli *fakeClient) Info(_ context.Context) (types.Info, error) { func TestElectAuthServer(t *testing.T) { testCases := []struct { expectedAuthServer string + expectedWarning string infoFunc func() (types.Info, error) }{ { expectedAuthServer: "https://index.docker.io/v1/", + expectedWarning: "", infoFunc: func() (types.Info, error) { return types.Info{IndexServerAddress: "https://index.docker.io/v1/"}, nil }, }, { expectedAuthServer: "https://index.docker.io/v1/", + expectedWarning: "Empty registry endpoint from daemon", infoFunc: func() (types.Info, error) { return types.Info{IndexServerAddress: ""}, nil }, }, { expectedAuthServer: "https://foo.bar", + expectedWarning: "", infoFunc: func() (types.Info, error) { return types.Info{IndexServerAddress: "https://foo.bar"}, nil }, }, { expectedAuthServer: "https://index.docker.io/v1/", + expectedWarning: "failed to get default registry endpoint from daemon", infoFunc: func() (types.Info, error) { return types.Info{}, errors.Errorf("error getting info") }, @@ -59,7 +64,16 @@ func TestElectAuthServer(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc}, buf) + outBuf := new(bytes.Buffer) + cli.SetOut(outBuf) server := ElectAuthServer(context.Background(), cli) - require.Equal(t, tc.expectedAuthServer, server) + assert.Equal(t, tc.expectedAuthServer, server) + actual := outBuf.String() + if tc.expectedWarning == "" { + assert.Empty(t, actual) + } else { + assert.Contains(t, actual, tc.expectedWarning) + } + } } diff --git a/cli/internal/test/cli.go b/cli/internal/test/cli.go index f0f75f7bd551..49e2bbf2865c 100644 --- a/cli/internal/test/cli.go +++ b/cli/internal/test/cli.go @@ -42,6 +42,11 @@ func (c *FakeCli) SetErr(err io.Writer) { c.err = err } +// SetOut sets the Out stream for the cli to the specified io.Writer +func (c *FakeCli) SetOut(out io.Writer) { + c.out = command.NewOutStream(out) +} + // SetConfigfile sets the "fake" config file func (c *FakeCli) SetConfigfile(configfile *configfile.ConfigFile) { c.configfile = configfile