From 8efa6a9567e5ceeb352c40006a694d8afa204ad1 Mon Sep 17 00:00:00 2001 From: Simon Ferquel Date: Thu, 8 Nov 2018 11:37:49 +0100 Subject: [PATCH] Fix tests with missing mocks A recent change in moby/moby made tests with missing client mocks fail with panic. This adds those missing mocks for the impacted tests. Signed-off-by: Simon Ferquel --- cli/command/engine/activate_test.go | 10 +++--- cli/command/node/client_test.go | 22 ++++++++---- cli/command/node/ps_test.go | 45 ++++++++++++++++++------ cli/command/plugin/client_test.go | 4 +++ cli/command/registry/login_test.go | 4 +++ cli/command/stack/client_test.go | 11 ++++++ cli/command/trust/inspect_pretty_test.go | 16 +++++++++ cli/command/trust/sign_test.go | 2 +- 8 files changed, 91 insertions(+), 23 deletions(-) diff --git a/cli/command/engine/activate_test.go b/cli/command/engine/activate_test.go index d930847e3a..01cc93fa33 100644 --- a/cli/command/engine/activate_test.go +++ b/cli/command/engine/activate_test.go @@ -2,6 +2,7 @@ package engine import ( "fmt" + "os" "testing" "github.com/docker/cli/internal/test" @@ -34,18 +35,19 @@ func TestActivateNoContainerd(t *testing.T) { } func TestActivateBadLicense(t *testing.T) { - testCli.SetContainerizedEngineClient( + isRoot = func() bool { return true } + c := test.NewFakeCli(&verClient{client.Client{}, types.Version{}, nil, types.Info{}, nil}) + c.SetContainerizedEngineClient( func(string) (clitypes.ContainerizedClient, error) { return &fakeContainerizedEngineClient{}, nil }, ) - isRoot = func() bool { return true } - cmd := newActivateCommand(testCli) + cmd := newActivateCommand(c) cmd.SilenceUsage = true cmd.SilenceErrors = true cmd.Flags().Set("license", "invalidpath") err := cmd.Execute() - assert.Error(t, err, "open invalidpath: no such file or directory") + assert.Assert(t, os.IsNotExist(err)) } func TestActivateExpiredLicenseDryRun(t *testing.T) { diff --git a/cli/command/node/client_test.go b/cli/command/node/client_test.go index 75a128cd46..3948c6a871 100644 --- a/cli/command/node/client_test.go +++ b/cli/command/node/client_test.go @@ -10,13 +10,14 @@ import ( type fakeClient struct { client.Client - infoFunc func() (types.Info, error) - nodeInspectFunc func() (swarm.Node, []byte, error) - nodeListFunc func() ([]swarm.Node, error) - nodeRemoveFunc func() error - nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error - taskInspectFunc func(taskID string) (swarm.Task, []byte, error) - taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) + infoFunc func() (types.Info, error) + nodeInspectFunc func() (swarm.Node, []byte, error) + nodeListFunc func() ([]swarm.Node, error) + nodeRemoveFunc func() error + nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error + taskInspectFunc func(taskID string) (swarm.Task, []byte, error) + taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) + serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) } func (cli *fakeClient) NodeInspectWithRaw(ctx context.Context, ref string) (swarm.Node, []byte, error) { @@ -67,3 +68,10 @@ func (cli *fakeClient) TaskList(ctx context.Context, options types.TaskListOptio } return []swarm.Task{}, nil } + +func (cli *fakeClient) ServiceInspectWithRaw(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) { + if cli.serviceInspectFunc != nil { + return cli.serviceInspectFunc(ctx, serviceID, opts) + } + return swarm.Service{}, []byte{}, nil +} diff --git a/cli/command/node/ps_test.go b/cli/command/node/ps_test.go index ae5ed6169f..74a1779b1f 100644 --- a/cli/command/node/ps_test.go +++ b/cli/command/node/ps_test.go @@ -1,6 +1,7 @@ package node import ( + "context" "fmt" "io/ioutil" "testing" @@ -66,13 +67,14 @@ func TestNodePsErrors(t *testing.T) { func TestNodePs(t *testing.T) { testCases := []struct { - name string - args []string - flags map[string]string - infoFunc func() (types.Info, error) - nodeInspectFunc func() (swarm.Node, []byte, error) - taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) - taskInspectFunc func(taskID string) (swarm.Task, []byte, error) + name string + args []string + flags map[string]string + infoFunc func() (types.Info, error) + nodeInspectFunc func() (swarm.Node, []byte, error) + taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) + taskInspectFunc func(taskID string) (swarm.Task, []byte, error) + serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) }{ { name: "simple", @@ -91,6 +93,16 @@ func TestNodePs(t *testing.T) { }))), }, nil }, + serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) { + return swarm.Service{ + ID: serviceID, + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: serviceID, + }, + }, + }, []byte{}, nil + }, }, { name: "with-errors", @@ -108,14 +120,25 @@ func TestNodePs(t *testing.T) { WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))), }, nil }, + serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) { + return swarm.Service{ + ID: serviceID, + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: serviceID, + }, + }, + }, []byte{}, nil + }, }, } for _, tc := range testCases { cli := test.NewFakeCli(&fakeClient{ - infoFunc: tc.infoFunc, - nodeInspectFunc: tc.nodeInspectFunc, - taskInspectFunc: tc.taskInspectFunc, - taskListFunc: tc.taskListFunc, + infoFunc: tc.infoFunc, + nodeInspectFunc: tc.nodeInspectFunc, + taskInspectFunc: tc.taskInspectFunc, + taskListFunc: tc.taskListFunc, + serviceInspectFunc: tc.serviceInspectFunc, }) cmd := newPsCommand(cli) cmd.SetArgs(tc.args) diff --git a/cli/command/plugin/client_test.go b/cli/command/plugin/client_test.go index 6dc4a0a625..f52cefec14 100644 --- a/cli/command/plugin/client_test.go +++ b/cli/command/plugin/client_test.go @@ -70,3 +70,7 @@ func (c *fakeClient) PluginInspectWithRaw(ctx context.Context, name string) (*ty return nil, nil, nil } + +func (c *fakeClient) Info(ctx context.Context) (types.Info, error) { + return types.Info{}, nil +} diff --git a/cli/command/registry/login_test.go b/cli/command/registry/login_test.go index d050ff59ab..e260be2b3e 100644 --- a/cli/command/registry/login_test.go +++ b/cli/command/registry/login_test.go @@ -28,6 +28,10 @@ type fakeClient struct { client.Client } +func (c fakeClient) Info(ctx context.Context) (types.Info, error) { + return types.Info{}, nil +} + func (c fakeClient) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registrytypes.AuthenticateOKBody, error) { if auth.Password == expiredPassword { return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password") diff --git a/cli/command/stack/client_test.go b/cli/command/stack/client_test.go index c028d668cf..a4f95cf308 100644 --- a/cli/command/stack/client_test.go +++ b/cli/command/stack/client_test.go @@ -179,6 +179,17 @@ func (cli *fakeClient) ConfigRemove(ctx context.Context, configID string) error return nil } +func (cli *fakeClient) ServiceInspectWithRaw(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) { + return swarm.Service{ + ID: serviceID, + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: serviceID, + }, + }, + }, []byte{}, nil +} + func serviceFromName(name string) swarm.Service { return swarm.Service{ ID: "ID-" + name, diff --git a/cli/command/trust/inspect_pretty_test.go b/cli/command/trust/inspect_pretty_test.go index 703dac02d0..13d436d168 100644 --- a/cli/command/trust/inspect_pretty_test.go +++ b/cli/command/trust/inspect_pretty_test.go @@ -2,17 +2,21 @@ package trust import ( "bytes" + "context" "encoding/hex" + "io" "io/ioutil" "testing" "github.com/docker/cli/cli/trust" "github.com/docker/cli/internal/test" notaryfake "github.com/docker/cli/internal/test/notary" + "github.com/docker/docker/api/types" dockerClient "github.com/docker/docker/client" "github.com/theupdateframework/notary" "github.com/theupdateframework/notary/client" "github.com/theupdateframework/notary/tuf/data" + "github.com/theupdateframework/notary/tuf/utils" "gotest.tools/assert" is "gotest.tools/assert/cmp" "gotest.tools/golden" @@ -24,6 +28,18 @@ type fakeClient struct { dockerClient.Client } +func (c *fakeClient) Info(ctx context.Context) (types.Info, error) { + return types.Info{}, nil +} + +func (c *fakeClient) ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) { + return types.ImageInspect{}, []byte{}, nil +} + +func (c *fakeClient) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) { + return &utils.NoopCloser{Reader: bytes.NewBuffer([]byte{})}, nil +} + func TestTrustInspectPrettyCommandErrors(t *testing.T) { testCases := []struct { name string diff --git a/cli/command/trust/sign_test.go b/cli/command/trust/sign_test.go index bb8cacad9a..31a56ea120 100644 --- a/cli/command/trust/sign_test.go +++ b/cli/command/trust/sign_test.go @@ -304,6 +304,6 @@ func TestSignCommandLocalFlag(t *testing.T) { cmd := newSignCommand(cli) cmd.SetArgs([]string{"--local", "reg-name.io/image:red"}) cmd.SetOutput(ioutil.Discard) - assert.ErrorContains(t, cmd.Execute(), "error during connect: Get /images/reg-name.io/image:red/json: unsupported protocol scheme") + assert.ErrorContains(t, cmd.Execute(), "error contacting notary server: dial tcp: lookup reg-name.io") }