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>
This commit is contained in:
Simon Ferquel 2018-11-08 11:37:49 +01:00
parent 561474d770
commit 8efa6a9567
8 changed files with 91 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package engine
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
@ -34,18 +35,19 @@ func TestActivateNoContainerd(t *testing.T) {
} }
func TestActivateBadLicense(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) { func(string) (clitypes.ContainerizedClient, error) {
return &fakeContainerizedEngineClient{}, nil return &fakeContainerizedEngineClient{}, nil
}, },
) )
isRoot = func() bool { return true } cmd := newActivateCommand(c)
cmd := newActivateCommand(testCli)
cmd.SilenceUsage = true cmd.SilenceUsage = true
cmd.SilenceErrors = true cmd.SilenceErrors = true
cmd.Flags().Set("license", "invalidpath") cmd.Flags().Set("license", "invalidpath")
err := cmd.Execute() 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) { func TestActivateExpiredLicenseDryRun(t *testing.T) {

View File

@ -10,13 +10,14 @@ import (
type fakeClient struct { type fakeClient struct {
client.Client client.Client
infoFunc func() (types.Info, error) infoFunc func() (types.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error) nodeInspectFunc func() (swarm.Node, []byte, error)
nodeListFunc func() ([]swarm.Node, error) nodeListFunc func() ([]swarm.Node, error)
nodeRemoveFunc func() error nodeRemoveFunc func() error
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
taskInspectFunc func(taskID string) (swarm.Task, []byte, error) taskInspectFunc func(taskID string) (swarm.Task, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, 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) { 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 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
}

View File

@ -1,6 +1,7 @@
package node package node
import ( import (
"context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -66,13 +67,14 @@ func TestNodePsErrors(t *testing.T) {
func TestNodePs(t *testing.T) { func TestNodePs(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
args []string args []string
flags map[string]string flags map[string]string
infoFunc func() (types.Info, error) infoFunc func() (types.Info, error)
nodeInspectFunc func() (swarm.Node, []byte, error) nodeInspectFunc func() (swarm.Node, []byte, error)
taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error) taskListFunc func(options types.TaskListOptions) ([]swarm.Task, error)
taskInspectFunc func(taskID string) (swarm.Task, []byte, 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", name: "simple",
@ -91,6 +93,16 @@ func TestNodePs(t *testing.T) {
}))), }))),
}, nil }, 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", name: "with-errors",
@ -108,14 +120,25 @@ func TestNodePs(t *testing.T) {
WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))), WithStatus(Timestamp(time.Now().Add(-4*time.Hour)), StatusErr("a task error"))),
}, nil }, 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 { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
taskInspectFunc: tc.taskInspectFunc, taskInspectFunc: tc.taskInspectFunc,
taskListFunc: tc.taskListFunc, taskListFunc: tc.taskListFunc,
serviceInspectFunc: tc.serviceInspectFunc,
}) })
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)

View File

@ -70,3 +70,7 @@ func (c *fakeClient) PluginInspectWithRaw(ctx context.Context, name string) (*ty
return nil, nil, nil return nil, nil, nil
} }
func (c *fakeClient) Info(ctx context.Context) (types.Info, error) {
return types.Info{}, nil
}

View File

@ -28,6 +28,10 @@ type fakeClient struct {
client.Client 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) { func (c fakeClient) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registrytypes.AuthenticateOKBody, error) {
if auth.Password == expiredPassword { if auth.Password == expiredPassword {
return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password") return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password")

View File

@ -179,6 +179,17 @@ func (cli *fakeClient) ConfigRemove(ctx context.Context, configID string) error
return nil 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 { func serviceFromName(name string) swarm.Service {
return swarm.Service{ return swarm.Service{
ID: "ID-" + name, ID: "ID-" + name,

View File

@ -2,17 +2,21 @@ package trust
import ( import (
"bytes" "bytes"
"context"
"encoding/hex" "encoding/hex"
"io"
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/docker/cli/cli/trust" "github.com/docker/cli/cli/trust"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
notaryfake "github.com/docker/cli/internal/test/notary" notaryfake "github.com/docker/cli/internal/test/notary"
"github.com/docker/docker/api/types"
dockerClient "github.com/docker/docker/client" dockerClient "github.com/docker/docker/client"
"github.com/theupdateframework/notary" "github.com/theupdateframework/notary"
"github.com/theupdateframework/notary/client" "github.com/theupdateframework/notary/client"
"github.com/theupdateframework/notary/tuf/data" "github.com/theupdateframework/notary/tuf/data"
"github.com/theupdateframework/notary/tuf/utils"
"gotest.tools/assert" "gotest.tools/assert"
is "gotest.tools/assert/cmp" is "gotest.tools/assert/cmp"
"gotest.tools/golden" "gotest.tools/golden"
@ -24,6 +28,18 @@ type fakeClient struct {
dockerClient.Client 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) { func TestTrustInspectPrettyCommandErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string

View File

@ -304,6 +304,6 @@ func TestSignCommandLocalFlag(t *testing.T) {
cmd := newSignCommand(cli) cmd := newSignCommand(cli)
cmd.SetArgs([]string{"--local", "reg-name.io/image:red"}) cmd.SetArgs([]string{"--local", "reg-name.io/image:red"})
cmd.SetOutput(ioutil.Discard) 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")
} }