Skip to content

Commit 8efa6a9

Browse files
committed
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 <simon.ferquel@docker.com>
1 parent 561474d commit 8efa6a9

8 files changed

Lines changed: 91 additions & 23 deletions

File tree

cli/command/engine/activate_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package engine
22

33
import (
44
"fmt"
5+
"os"
56
"testing"
67

78
"github.com/docker/cli/internal/test"
@@ -34,18 +35,19 @@ func TestActivateNoContainerd(t *testing.T) {
3435
}
3536

3637
func TestActivateBadLicense(t *testing.T) {
37-
testCli.SetContainerizedEngineClient(
38+
isRoot = func() bool { return true }
39+
c := test.NewFakeCli(&verClient{client.Client{}, types.Version{}, nil, types.Info{}, nil})
40+
c.SetContainerizedEngineClient(
3841
func(string) (clitypes.ContainerizedClient, error) {
3942
return &fakeContainerizedEngineClient{}, nil
4043
},
4144
)
42-
isRoot = func() bool { return true }
43-
cmd := newActivateCommand(testCli)
45+
cmd := newActivateCommand(c)
4446
cmd.SilenceUsage = true
4547
cmd.SilenceErrors = true
4648
cmd.Flags().Set("license", "invalidpath")
4749
err := cmd.Execute()
48-
assert.Error(t, err, "open invalidpath: no such file or directory")
50+
assert.Assert(t, os.IsNotExist(err))
4951
}
5052

5153
func TestActivateExpiredLicenseDryRun(t *testing.T) {

cli/command/node/client_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010

1111
type fakeClient struct {
1212
client.Client
13-
infoFunc func() (types.Info, error)
14-
nodeInspectFunc func() (swarm.Node, []byte, error)
15-
nodeListFunc func() ([]swarm.Node, error)
16-
nodeRemoveFunc func() error
17-
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
18-
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
19-
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
13+
infoFunc func() (types.Info, error)
14+
nodeInspectFunc func() (swarm.Node, []byte, error)
15+
nodeListFunc func() ([]swarm.Node, error)
16+
nodeRemoveFunc func() error
17+
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
18+
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
19+
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
20+
serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
2021
}
2122

2223
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
6768
}
6869
return []swarm.Task{}, nil
6970
}
71+
72+
func (cli *fakeClient) ServiceInspectWithRaw(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
73+
if cli.serviceInspectFunc != nil {
74+
return cli.serviceInspectFunc(ctx, serviceID, opts)
75+
}
76+
return swarm.Service{}, []byte{}, nil
77+
}

cli/command/node/ps_test.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package node
22

33
import (
4+
"context"
45
"fmt"
56
"io/ioutil"
67
"testing"
@@ -66,13 +67,14 @@ func TestNodePsErrors(t *testing.T) {
6667

6768
func TestNodePs(t *testing.T) {
6869
testCases := []struct {
69-
name string
70-
args []string
71-
flags map[string]string
72-
infoFunc func() (types.Info, error)
73-
nodeInspectFunc func() (swarm.Node, []byte, error)
74-
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
75-
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
70+
name string
71+
args []string
72+
flags map[string]string
73+
infoFunc func() (types.Info, error)
74+
nodeInspectFunc func() (swarm.Node, []byte, error)
75+
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
76+
taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
77+
serviceInspectFunc func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error)
7678
}{
7779
{
7880
name: "simple",
@@ -91,6 +93,16 @@ func TestNodePs(t *testing.T) {
9193
}))),
9294
}, nil
9395
},
96+
serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
97+
return swarm.Service{
98+
ID: serviceID,
99+
Spec: swarm.ServiceSpec{
100+
Annotations: swarm.Annotations{
101+
Name: serviceID,
102+
},
103+
},
104+
}, []byte{}, nil
105+
},
94106
},
95107
{
96108
name: "with-errors",
@@ -108,14 +120,25 @@ func TestNodePs(t *testing.T) {
108120
WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))),
109121
}, nil
110122
},
123+
serviceInspectFunc: func(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
124+
return swarm.Service{
125+
ID: serviceID,
126+
Spec: swarm.ServiceSpec{
127+
Annotations: swarm.Annotations{
128+
Name: serviceID,
129+
},
130+
},
131+
}, []byte{}, nil
132+
},
111133
},
112134
}
113135
for _, tc := range testCases {
114136
cli := test.NewFakeCli(&fakeClient{
115-
infoFunc: tc.infoFunc,
116-
nodeInspectFunc: tc.nodeInspectFunc,
117-
taskInspectFunc: tc.taskInspectFunc,
118-
taskListFunc: tc.taskListFunc,
137+
infoFunc: tc.infoFunc,
138+
nodeInspectFunc: tc.nodeInspectFunc,
139+
taskInspectFunc: tc.taskInspectFunc,
140+
taskListFunc: tc.taskListFunc,
141+
serviceInspectFunc: tc.serviceInspectFunc,
119142
})
120143
cmd := newPsCommand(cli)
121144
cmd.SetArgs(tc.args)

cli/command/plugin/client_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ func (c *fakeClient) PluginInspectWithRaw(ctx context.Context, name string) (*ty
7070

7171
return nil, nil, nil
7272
}
73+
74+
func (c *fakeClient) Info(ctx context.Context) (types.Info, error) {
75+
return types.Info{}, nil
76+
}

cli/command/registry/login_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type fakeClient struct {
2828
client.Client
2929
}
3030

31+
func (c fakeClient) Info(ctx context.Context) (types.Info, error) {
32+
return types.Info{}, nil
33+
}
34+
3135
func (c fakeClient) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registrytypes.AuthenticateOKBody, error) {
3236
if auth.Password == expiredPassword {
3337
return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password")

cli/command/stack/client_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ func (cli *fakeClient) ConfigRemove(ctx context.Context, configID string) error
179179
return nil
180180
}
181181

182+
func (cli *fakeClient) ServiceInspectWithRaw(ctx context.Context, serviceID string, opts types.ServiceInspectOptions) (swarm.Service, []byte, error) {
183+
return swarm.Service{
184+
ID: serviceID,
185+
Spec: swarm.ServiceSpec{
186+
Annotations: swarm.Annotations{
187+
Name: serviceID,
188+
},
189+
},
190+
}, []byte{}, nil
191+
}
192+
182193
func serviceFromName(name string) swarm.Service {
183194
return swarm.Service{
184195
ID: "ID-" + name,

cli/command/trust/inspect_pretty_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package trust
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/hex"
7+
"io"
68
"io/ioutil"
79
"testing"
810

911
"github.com/docker/cli/cli/trust"
1012
"github.com/docker/cli/internal/test"
1113
notaryfake "github.com/docker/cli/internal/test/notary"
14+
"github.com/docker/docker/api/types"
1215
dockerClient "github.com/docker/docker/client"
1316
"github.com/theupdateframework/notary"
1417
"github.com/theupdateframework/notary/client"
1518
"github.com/theupdateframework/notary/tuf/data"
19+
"github.com/theupdateframework/notary/tuf/utils"
1620
"gotest.tools/assert"
1721
is "gotest.tools/assert/cmp"
1822
"gotest.tools/golden"
@@ -24,6 +28,18 @@ type fakeClient struct {
2428
dockerClient.Client
2529
}
2630

31+
func (c *fakeClient) Info(ctx context.Context) (types.Info, error) {
32+
return types.Info{}, nil
33+
}
34+
35+
func (c *fakeClient) ImageInspectWithRaw(ctx context.Context, imageID string) (types.ImageInspect, []byte, error) {
36+
return types.ImageInspect{}, []byte{}, nil
37+
}
38+
39+
func (c *fakeClient) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
40+
return &utils.NoopCloser{Reader: bytes.NewBuffer([]byte{})}, nil
41+
}
42+
2743
func TestTrustInspectPrettyCommandErrors(t *testing.T) {
2844
testCases := []struct {
2945
name string

cli/command/trust/sign_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,6 @@ func TestSignCommandLocalFlag(t *testing.T) {
304304
cmd := newSignCommand(cli)
305305
cmd.SetArgs([]string{"--local", "reg-name.io/image:red"})
306306
cmd.SetOutput(ioutil.Discard)
307-
assert.ErrorContains(t, cmd.Execute(), "error during connect: Get /images/reg-name.io/image:red/json: unsupported protocol scheme")
307+
assert.ErrorContains(t, cmd.Execute(), "error contacting notary server: dial tcp: lookup reg-name.io")
308308

309309
}

0 commit comments

Comments
 (0)