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 (
"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) {

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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")

View File

@ -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,

View File

@ -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

View File

@ -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")
}