From 69b142b52a44e0182f86d5e5d212a980e5192215 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 5 Jul 2017 14:19:52 -0400 Subject: [PATCH] Update FakeCli to remove duplication in tests. Use byte buffers by default, since that is what is done most of the time. Signed-off-by: Daniel Nephin --- cli/command/checkpoint/create_test.go | 4 +-- cli/command/checkpoint/list_test.go | 4 +-- cli/command/checkpoint/remove_test.go | 4 +-- cli/command/config/create_test.go | 6 ++--- cli/command/config/inspect_test.go | 8 +++--- cli/command/config/ls_test.go | 12 ++++----- cli/command/config/remove_test.go | 6 ++--- cli/command/container/attach_test.go | 2 +- cli/command/container/exec_test.go | 2 +- cli/command/image/build_test.go | 2 +- cli/command/image/history_test.go | 4 +-- cli/command/image/import_test.go | 6 ++--- cli/command/image/inspect_test.go | 4 +-- cli/command/image/list_test.go | 6 ++--- cli/command/image/load_test.go | 6 ++--- cli/command/image/prune_test.go | 4 +-- cli/command/image/pull_test.go | 4 +-- cli/command/image/push_test.go | 4 +-- cli/command/image/remove_test.go | 6 ++--- cli/command/image/save_test.go | 4 +-- cli/command/image/tag_test.go | 4 +-- cli/command/node/demote_test.go | 6 ++--- cli/command/node/inspect_test.go | 4 +-- cli/command/node/list_test.go | 12 ++++----- cli/command/node/promote_test.go | 6 ++--- cli/command/node/ps_test.go | 4 +-- cli/command/node/remove_test.go | 4 +-- cli/command/node/update_test.go | 4 +-- cli/command/registry_test.go | 2 +- cli/command/secret/create_test.go | 6 ++--- cli/command/secret/inspect_test.go | 8 +++--- cli/command/secret/ls_test.go | 12 ++++----- cli/command/secret/remove_test.go | 6 ++--- cli/command/service/ps_test.go | 2 +- cli/command/stack/deploy_test.go | 2 +- cli/command/stack/list_test.go | 8 +++--- cli/command/stack/ps_test.go | 23 ++++++++---------- cli/command/stack/remove_test.go | 13 ++++------ cli/command/stack/services_test.go | 19 ++++++--------- cli/command/swarm/init_test.go | 4 +-- cli/command/swarm/join_test.go | 4 +-- cli/command/swarm/join_token_test.go | 4 +-- cli/command/swarm/leave_test.go | 4 +-- cli/command/swarm/unlock_key_test.go | 4 +-- cli/command/swarm/unlock_test.go | 4 +-- cli/command/swarm/update_test.go | 4 +-- cli/command/task/print_test.go | 12 ++++----- cli/command/volume/create_test.go | 6 ++--- cli/command/volume/inspect_test.go | 6 ++--- cli/command/volume/list_test.go | 8 +++--- cli/command/volume/prune_test.go | 8 +++--- cli/command/volume/remove_test.go | 4 +-- cli/internal/test/cli.go | 35 ++++++++++++++++++++++----- 53 files changed, 182 insertions(+), 168 deletions(-) diff --git a/cli/command/checkpoint/create_test.go b/cli/command/checkpoint/create_test.go index dfbf6317a1..fd1dec423a 100644 --- a/cli/command/checkpoint/create_test.go +++ b/cli/command/checkpoint/create_test.go @@ -37,7 +37,7 @@ func TestCheckpointCreateErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointCreateFunc: tc.checkpointCreateFunc, }, &bytes.Buffer{}) cmd := newCreateCommand(cli) @@ -51,7 +51,7 @@ func TestCheckpointCreateWithOptions(t *testing.T) { var containerID, checkpointID, checkpointDir string var exit bool buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error { containerID = container checkpointID = options.CheckpointID diff --git a/cli/command/checkpoint/list_test.go b/cli/command/checkpoint/list_test.go index 7d4f2e1b73..1c13f27755 100644 --- a/cli/command/checkpoint/list_test.go +++ b/cli/command/checkpoint/list_test.go @@ -37,7 +37,7 @@ func TestCheckpointListErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointListFunc: tc.checkpointListFunc, }, &bytes.Buffer{}) cmd := newListCommand(cli) @@ -50,7 +50,7 @@ func TestCheckpointListErrors(t *testing.T) { func TestCheckpointListWithOptions(t *testing.T) { var containerID, checkpointDir string buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointListFunc: func(container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) { containerID = container checkpointDir = options.CheckpointDir diff --git a/cli/command/checkpoint/remove_test.go b/cli/command/checkpoint/remove_test.go index 2ca8e32d66..ecc66d8be5 100644 --- a/cli/command/checkpoint/remove_test.go +++ b/cli/command/checkpoint/remove_test.go @@ -36,7 +36,7 @@ func TestCheckpointRemoveErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointDeleteFunc: tc.checkpointDeleteFunc, }, &bytes.Buffer{}) cmd := newRemoveCommand(cli) @@ -48,7 +48,7 @@ func TestCheckpointRemoveErrors(t *testing.T) { func TestCheckpointRemoveWithOptions(t *testing.T) { var containerID, checkpointID, checkpointDir string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointDeleteFunc: func(container string, options types.CheckpointDeleteOptions) error { containerID = container checkpointID = options.CheckpointID diff --git a/cli/command/config/create_test.go b/cli/command/config/create_test.go index 25b133836f..6a71bee955 100644 --- a/cli/command/config/create_test.go +++ b/cli/command/config/create_test.go @@ -43,7 +43,7 @@ func TestConfigCreateErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigCreateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configCreateFunc: tc.configCreateFunc, }, buf), ) @@ -57,7 +57,7 @@ func TestConfigCreateWithName(t *testing.T) { name := "foo" buf := new(bytes.Buffer) var actual []byte - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { if spec.Name != name { return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) @@ -87,7 +87,7 @@ func TestConfigCreateWithLabels(t *testing.T) { name := "foo" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { if spec.Name != name { return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) diff --git a/cli/command/config/inspect_test.go b/cli/command/config/inspect_test.go index 69d36cb7ad..e1af0b6b09 100644 --- a/cli/command/config/inspect_test.go +++ b/cli/command/config/inspect_test.go @@ -55,7 +55,7 @@ func TestConfigInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf), ) @@ -97,7 +97,7 @@ func TestConfigInspectWithoutFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf), ) @@ -137,7 +137,7 @@ func TestConfigInspectWithFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf), ) @@ -174,7 +174,7 @@ func TestConfigInspectPretty(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf)) cmd.SetArgs([]string{"configID"}) diff --git a/cli/command/config/ls_test.go b/cli/command/config/ls_test.go index b43c764a4d..465cf12e91 100644 --- a/cli/command/config/ls_test.go +++ b/cli/command/config/ls_test.go @@ -38,7 +38,7 @@ func TestConfigListErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigListCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configListFunc: tc.configListFunc, }, buf), ) @@ -50,7 +50,7 @@ func TestConfigListErrors(t *testing.T) { func TestConfigList(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), @@ -79,7 +79,7 @@ func TestConfigList(t *testing.T) { func TestConfigListWithQuietOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), ConfigName("foo")), @@ -100,7 +100,7 @@ func TestConfigListWithQuietOption(t *testing.T) { func TestConfigListWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), ConfigName("foo")), @@ -122,7 +122,7 @@ func TestConfigListWithConfigFormat(t *testing.T) { func TestConfigListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), ConfigName("foo")), @@ -142,7 +142,7 @@ func TestConfigListWithFormat(t *testing.T) { func TestConfigListWithFilter(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { assert.Equal(t, "foo", options.Filters.Get("name")[0]) assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) diff --git a/cli/command/config/remove_test.go b/cli/command/config/remove_test.go index c183a93c8b..85b188c41d 100644 --- a/cli/command/config/remove_test.go +++ b/cli/command/config/remove_test.go @@ -33,7 +33,7 @@ func TestConfigRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configRemoveFunc: tc.configRemoveFunc, }, buf), ) @@ -47,7 +47,7 @@ func TestConfigRemoveWithName(t *testing.T) { names := []string{"foo", "bar"} buf := new(bytes.Buffer) var removedConfigs []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configRemoveFunc: func(name string) error { removedConfigs = append(removedConfigs, name) return nil @@ -65,7 +65,7 @@ func TestConfigRemoveContinueAfterError(t *testing.T) { buf := new(bytes.Buffer) var removedConfigs []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configRemoveFunc: func(name string) error { removedConfigs = append(removedConfigs, name) if name == "foo" { diff --git a/cli/command/container/attach_test.go b/cli/command/container/attach_test.go index 14b8137dae..a8f089706e 100644 --- a/cli/command/container/attach_test.go +++ b/cli/command/container/attach_test.go @@ -69,7 +69,7 @@ func TestNewAttachCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf)) + cmd := NewAttachCommand(test.NewFakeCliWithOutput(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) diff --git a/cli/command/container/exec_test.go b/cli/command/container/exec_test.go index b2df1c2b0c..783a17a606 100644 --- a/cli/command/container/exec_test.go +++ b/cli/command/container/exec_test.go @@ -140,7 +140,7 @@ func TestNewExecCommandErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) conf := configfile.ConfigFile{} - cli := test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf) cli.SetConfigfile(&conf) cmd := NewExecCommand(cli) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/image/build_test.go b/cli/command/image/build_test.go index bf77292a85..3e2507166f 100644 --- a/cli/command/image/build_test.go +++ b/cli/command/image/build_test.go @@ -38,7 +38,7 @@ func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) { return types.ImageBuildResponse{Body: ioutil.NopCloser(body)}, nil } - cli := test.NewFakeCli(&fakeClient{imageBuildFunc: fakeImageBuild}, ioutil.Discard) + cli := test.NewFakeCliWithOutput(&fakeClient{imageBuildFunc: fakeImageBuild}, ioutil.Discard) dockerfile := bytes.NewBufferString(` FROM alpine:3.6 COPY foo / diff --git a/cli/command/image/history_test.go b/cli/command/image/history_test.go index 8582c09b62..0f3a605e9f 100644 --- a/cli/command/image/history_test.go +++ b/cli/command/image/history_test.go @@ -39,7 +39,7 @@ func TestNewHistoryCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) + cmd := NewHistoryCommand(test.NewFakeCliWithOutput(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -91,7 +91,7 @@ func TestNewHistoryCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) + cmd := NewHistoryCommand(test.NewFakeCliWithOutput(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() diff --git a/cli/command/image/import_test.go b/cli/command/image/import_test.go index 134722f0f1..7e07310098 100644 --- a/cli/command/image/import_test.go +++ b/cli/command/image/import_test.go @@ -37,7 +37,7 @@ func TestNewImportCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) + cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -45,7 +45,7 @@ func TestNewImportCommandErrors(t *testing.T) { } func TestNewImportCommandInvalidFile(t *testing.T) { - cmd := NewImportCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) + cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"testdata/import-command-success.unexistent-file"}) testutil.ErrorContains(t, cmd.Execute(), "testdata/import-command-success.unexistent-file") @@ -92,7 +92,7 @@ func TestNewImportCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) + cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/image/inspect_test.go b/cli/command/image/inspect_test.go index b3a2c2f5f5..2f336bdbad 100644 --- a/cli/command/image/inspect_test.go +++ b/cli/command/image/inspect_test.go @@ -27,7 +27,7 @@ func TestNewInspectCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := newInspectCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -79,7 +79,7 @@ func TestNewInspectCommandSuccess(t *testing.T) { for _, tc := range testCases { imageInspectInvocationCount = 0 buf := new(bytes.Buffer) - cmd := newInspectCommand(test.NewFakeCli(&fakeClient{imageInspectFunc: tc.imageInspectFunc}, buf)) + cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{imageInspectFunc: tc.imageInspectFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() diff --git a/cli/command/image/list_test.go b/cli/command/image/list_test.go index 070b19efb1..76c8731d91 100644 --- a/cli/command/image/list_test.go +++ b/cli/command/image/list_test.go @@ -36,7 +36,7 @@ func TestNewImagesCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewImagesCommand(test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}, new(bytes.Buffer))) + cmd := NewImagesCommand(test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, new(bytes.Buffer))) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -81,7 +81,7 @@ func TestNewImagesCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, buf) cli.SetConfigfile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat}) cmd := NewImagesCommand(cli) cmd.SetOutput(ioutil.Discard) @@ -95,7 +95,7 @@ func TestNewImagesCommandSuccess(t *testing.T) { } func TestNewListCommandAlias(t *testing.T) { - cmd := newListCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) assert.True(t, cmd.HasAlias("images")) assert.True(t, cmd.HasAlias("list")) assert.False(t, cmd.HasAlias("other")) diff --git a/cli/command/image/load_test.go b/cli/command/image/load_test.go index ebe1a0c7e3..d0f8d81c65 100644 --- a/cli/command/image/load_test.go +++ b/cli/command/image/load_test.go @@ -43,7 +43,7 @@ func TestNewLoadCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, new(bytes.Buffer)) + cli := test.NewFakeCliWithOutput(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, new(bytes.Buffer)) cli.In().SetIsTerminal(tc.isTerminalIn) cmd := NewLoadCommand(cli) cmd.SetOutput(ioutil.Discard) @@ -54,7 +54,7 @@ func TestNewLoadCommandErrors(t *testing.T) { func TestNewLoadCommandInvalidInput(t *testing.T) { expectedError := "open *" - cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) + cmd := NewLoadCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"--input", "*"}) err := cmd.Execute() @@ -93,7 +93,7 @@ func TestNewLoadCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, buf)) + cmd := NewLoadCommand(test.NewFakeCliWithOutput(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() diff --git a/cli/command/image/prune_test.go b/cli/command/image/prune_test.go index c17a75d224..be25826df0 100644 --- a/cli/command/image/prune_test.go +++ b/cli/command/image/prune_test.go @@ -38,7 +38,7 @@ func TestNewPruneCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ + cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{ imagesPruneFunc: tc.imagesPruneFunc, }, buf)) cmd.SetOutput(ioutil.Discard) @@ -86,7 +86,7 @@ func TestNewPruneCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ + cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{ imagesPruneFunc: tc.imagesPruneFunc, }, buf)) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/image/pull_test.go b/cli/command/image/pull_test.go index 690e6222eb..26d0b74460 100644 --- a/cli/command/image/pull_test.go +++ b/cli/command/image/pull_test.go @@ -42,7 +42,7 @@ func TestNewPullCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) cli.SetConfigfile(configfile.New("filename")) cmd := NewPullCommand(cli) cmd.SetOutput(ioutil.Discard) @@ -67,7 +67,7 @@ func TestNewPullCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) cli.SetConfigfile(configfile.New("filename")) cmd := NewPullCommand(cli) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/image/push_test.go b/cli/command/image/push_test.go index f2c35dee21..2e0c58e642 100644 --- a/cli/command/image/push_test.go +++ b/cli/command/image/push_test.go @@ -48,7 +48,7 @@ func TestNewPushCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{imagePushFunc: tc.imagePushFunc}, buf) cli.SetConfigfile(configfile.New("filename")) cmd := NewPushCommand(cli) cmd.SetOutput(ioutil.Discard) @@ -69,7 +69,7 @@ func TestNewPushCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil }, diff --git a/cli/command/image/remove_test.go b/cli/command/image/remove_test.go index 9b02b24a90..195e56ade6 100644 --- a/cli/command/image/remove_test.go +++ b/cli/command/image/remove_test.go @@ -15,7 +15,7 @@ import ( ) func TestNewRemoveCommandAlias(t *testing.T) { - cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) assert.True(t, cmd.HasAlias("rmi")) assert.True(t, cmd.HasAlias("remove")) assert.False(t, cmd.HasAlias("other")) @@ -44,7 +44,7 @@ func TestNewRemoveCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{ + cmd := NewRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{ imageRemoveFunc: tc.imageRemoveFunc, }, new(bytes.Buffer))) cmd.SetOutput(ioutil.Discard) @@ -99,7 +99,7 @@ func TestNewRemoveCommandSuccess(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) errBuf := new(bytes.Buffer) - fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}, buf) + fakeCli := test.NewFakeCliWithOutput(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}, buf) fakeCli.SetErr(errBuf) cmd := NewRemoveCommand(fakeCli) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/image/save_test.go b/cli/command/image/save_test.go index d137dc92f9..042c90b768 100644 --- a/cli/command/image/save_test.go +++ b/cli/command/image/save_test.go @@ -45,7 +45,7 @@ func TestNewSaveCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc}, new(bytes.Buffer)) + cli := test.NewFakeCliWithOutput(&fakeClient{imageSaveFunc: tc.imageSaveFunc}, new(bytes.Buffer)) cli.Out().SetIsTerminal(tc.isTerminal) cmd := NewSaveCommand(cli) cmd.SetOutput(ioutil.Discard) @@ -85,7 +85,7 @@ func TestNewSaveCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{ + cmd := NewSaveCommand(test.NewFakeCliWithOutput(&fakeClient{ imageSaveFunc: func(images []string) (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil }, diff --git a/cli/command/image/tag_test.go b/cli/command/image/tag_test.go index 8cf0c534b8..f9748746f4 100644 --- a/cli/command/image/tag_test.go +++ b/cli/command/image/tag_test.go @@ -19,7 +19,7 @@ func TestCliNewTagCommandErrors(t *testing.T) { expectedError := "\"tag\" requires exactly 2 argument(s)." buf := new(bytes.Buffer) for _, args := range testCases { - cmd := NewTagCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := NewTagCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) cmd.SetArgs(args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), expectedError) @@ -29,7 +29,7 @@ func TestCliNewTagCommandErrors(t *testing.T) { func TestCliNewTagCommand(t *testing.T) { buf := new(bytes.Buffer) cmd := NewTagCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ imageTagFunc: func(image string, ref string) error { assert.Equal(t, "image1", image) assert.Equal(t, "image2", ref) diff --git a/cli/command/node/demote_test.go b/cli/command/node/demote_test.go index 7126dddd13..df4eb62410 100644 --- a/cli/command/node/demote_test.go +++ b/cli/command/node/demote_test.go @@ -42,7 +42,7 @@ func TestNodeDemoteErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newDemoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, }, buf)) @@ -55,7 +55,7 @@ func TestNodeDemoteErrors(t *testing.T) { func TestNodeDemoteNoChange(t *testing.T) { buf := new(bytes.Buffer) cmd := newDemoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(), []byte{}, nil }, @@ -73,7 +73,7 @@ func TestNodeDemoteNoChange(t *testing.T) { func TestNodeDemoteMultipleNode(t *testing.T) { buf := new(bytes.Buffer) cmd := newDemoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(Manager()), []byte{}, nil }, diff --git a/cli/command/node/inspect_test.go b/cli/command/node/inspect_test.go index 2a21e12a28..ca88371f8a 100644 --- a/cli/command/node/inspect_test.go +++ b/cli/command/node/inspect_test.go @@ -69,7 +69,7 @@ func TestNodeInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, infoFunc: tc.infoFunc, }, buf)) @@ -111,7 +111,7 @@ func TestNodeInspectPretty(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, }, buf)) cmd.SetArgs([]string{"nodeID"}) diff --git a/cli/command/node/list_test.go b/cli/command/node/list_test.go index c4538ba141..dd906d7144 100644 --- a/cli/command/node/list_test.go +++ b/cli/command/node/list_test.go @@ -43,7 +43,7 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: tc.nodeListFunc, infoFunc: tc.infoFunc, }, buf) @@ -56,7 +56,7 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) { func TestNodeList(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -82,7 +82,7 @@ func TestNodeList(t *testing.T) { func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(), @@ -99,7 +99,7 @@ func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { // Test case for #24090 func TestNodeListContainsHostname(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) @@ -108,7 +108,7 @@ func TestNodeListContainsHostname(t *testing.T) { func TestNodeListDefaultFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -136,7 +136,7 @@ func TestNodeListDefaultFormat(t *testing.T) { func TestNodeListFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), diff --git a/cli/command/node/promote_test.go b/cli/command/node/promote_test.go index 495bdfe60d..95e067eb15 100644 --- a/cli/command/node/promote_test.go +++ b/cli/command/node/promote_test.go @@ -42,7 +42,7 @@ func TestNodePromoteErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, }, buf)) @@ -55,7 +55,7 @@ func TestNodePromoteErrors(t *testing.T) { func TestNodePromoteNoChange(t *testing.T) { buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(Manager()), []byte{}, nil }, @@ -73,7 +73,7 @@ func TestNodePromoteNoChange(t *testing.T) { func TestNodePromoteMultipleNode(t *testing.T) { buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(), []byte{}, nil }, diff --git a/cli/command/node/ps_test.go b/cli/command/node/ps_test.go index d25a55f0cf..d3ef6f30a2 100644 --- a/cli/command/node/ps_test.go +++ b/cli/command/node/ps_test.go @@ -52,7 +52,7 @@ func TestNodePsErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newPsCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, nodeInspectFunc: tc.nodeInspectFunc, taskInspectFunc: tc.taskInspectFunc, @@ -116,7 +116,7 @@ func TestNodePs(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newPsCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, nodeInspectFunc: tc.nodeInspectFunc, taskInspectFunc: tc.taskInspectFunc, diff --git a/cli/command/node/remove_test.go b/cli/command/node/remove_test.go index 7123288f47..81daa2f025 100644 --- a/cli/command/node/remove_test.go +++ b/cli/command/node/remove_test.go @@ -31,7 +31,7 @@ func TestNodeRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeRemoveFunc: tc.nodeRemoveFunc, }, buf)) cmd.SetArgs(tc.args) @@ -42,7 +42,7 @@ func TestNodeRemoveErrors(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) { buf := new(bytes.Buffer) - cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) cmd.SetArgs([]string{"nodeID1", "nodeID2"}) assert.NoError(t, cmd.Execute()) } diff --git a/cli/command/node/update_test.go b/cli/command/node/update_test.go index 0085934b9c..5e5a764d9e 100644 --- a/cli/command/node/update_test.go +++ b/cli/command/node/update_test.go @@ -59,7 +59,7 @@ func TestNodeUpdateErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, }, buf)) @@ -160,7 +160,7 @@ func TestNodeUpdate(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, }, buf)) diff --git a/cli/command/registry_test.go b/cli/command/registry_test.go index f6adeb49f4..75003a68b6 100644 --- a/cli/command/registry_test.go +++ b/cli/command/registry_test.go @@ -64,7 +64,7 @@ func TestElectAuthServer(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc}, buf) + cli := test.NewFakeCliWithOutput(&fakeClient{infoFunc: tc.infoFunc}, buf) errBuf := new(bytes.Buffer) cli.SetErr(errBuf) server := ElectAuthServer(context.Background(), cli) diff --git a/cli/command/secret/create_test.go b/cli/command/secret/create_test.go index 197483cf70..1df9c7108f 100644 --- a/cli/command/secret/create_test.go +++ b/cli/command/secret/create_test.go @@ -43,7 +43,7 @@ func TestSecretCreateErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretCreateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretCreateFunc: tc.secretCreateFunc, }, buf), ) @@ -57,7 +57,7 @@ func TestSecretCreateWithName(t *testing.T) { name := "foo" buf := new(bytes.Buffer) var actual []byte - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { if spec.Name != name { return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) @@ -87,7 +87,7 @@ func TestSecretCreateWithLabels(t *testing.T) { name := "foo" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { if spec.Name != name { return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) diff --git a/cli/command/secret/inspect_test.go b/cli/command/secret/inspect_test.go index 07a3d7a313..093624ddfe 100644 --- a/cli/command/secret/inspect_test.go +++ b/cli/command/secret/inspect_test.go @@ -55,7 +55,7 @@ func TestSecretInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf), ) @@ -97,7 +97,7 @@ func TestSecretInspectWithoutFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf), ) @@ -137,7 +137,7 @@ func TestSecretInspectWithFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf), ) @@ -173,7 +173,7 @@ func TestSecretInspectPretty(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf)) cmd.SetArgs([]string{"secretID"}) diff --git a/cli/command/secret/ls_test.go b/cli/command/secret/ls_test.go index 5d2103cb2a..a07f9c8cbb 100644 --- a/cli/command/secret/ls_test.go +++ b/cli/command/secret/ls_test.go @@ -38,7 +38,7 @@ func TestSecretListErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretListCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: tc.secretListFunc, }, buf), ) @@ -50,7 +50,7 @@ func TestSecretListErrors(t *testing.T) { func TestSecretList(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), @@ -79,7 +79,7 @@ func TestSecretList(t *testing.T) { func TestSecretListWithQuietOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -100,7 +100,7 @@ func TestSecretListWithQuietOption(t *testing.T) { func TestSecretListWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -122,7 +122,7 @@ func TestSecretListWithConfigFormat(t *testing.T) { func TestSecretListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -142,7 +142,7 @@ func TestSecretListWithFormat(t *testing.T) { func TestSecretListWithFilter(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo") assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) diff --git a/cli/command/secret/remove_test.go b/cli/command/secret/remove_test.go index 08443ec9ae..6e5ab5fb6c 100644 --- a/cli/command/secret/remove_test.go +++ b/cli/command/secret/remove_test.go @@ -33,7 +33,7 @@ func TestSecretRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretRemoveFunc: tc.secretRemoveFunc, }, buf), ) @@ -47,7 +47,7 @@ func TestSecretRemoveWithName(t *testing.T) { names := []string{"foo", "bar"} buf := new(bytes.Buffer) var removedSecrets []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretRemoveFunc: func(name string) error { removedSecrets = append(removedSecrets, name) return nil @@ -65,7 +65,7 @@ func TestSecretRemoveContinueAfterError(t *testing.T) { buf := new(bytes.Buffer) var removedSecrets []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretRemoveFunc: func(name string) error { removedSecrets = append(removedSecrets, name) if name == "foo" { diff --git a/cli/command/service/ps_test.go b/cli/command/service/ps_test.go index a53748d6d3..0e1bcb438b 100644 --- a/cli/command/service/ps_test.go +++ b/cli/command/service/ps_test.go @@ -107,7 +107,7 @@ func TestRunPSWarnsOnNotFound(t *testing.T) { } out := new(bytes.Buffer) - cli := test.NewFakeCli(client, out) + cli := test.NewFakeCliWithOutput(client, out) options := psOptions{ services: []string{"foo", "bar"}, filter: opts.NewFilterOpt(), diff --git a/cli/command/stack/deploy_test.go b/cli/command/stack/deploy_test.go index ffdd759699..6ad8a9bdac 100644 --- a/cli/command/stack/deploy_test.go +++ b/cli/command/stack/deploy_test.go @@ -18,7 +18,7 @@ func TestPruneServices(t *testing.T) { "keep": {}, } client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}} - dockerCli := test.NewFakeCli(client, &bytes.Buffer{}) + dockerCli := test.NewFakeCliWithOutput(client, &bytes.Buffer{}) dockerCli.SetErr(&bytes.Buffer{}) pruneServices(ctx, dockerCli, namespace, services) diff --git a/cli/command/stack/list_test.go b/cli/command/stack/list_test.go index 4f258977ed..e072e41325 100644 --- a/cli/command/stack/list_test.go +++ b/cli/command/stack/list_test.go @@ -48,7 +48,7 @@ func TestListErrors(t *testing.T) { } for _, tc := range testCases { - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: tc.serviceListFunc, }, &bytes.Buffer{})) cmd.SetArgs(tc.args) @@ -62,7 +62,7 @@ func TestListErrors(t *testing.T) { func TestListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service( @@ -81,7 +81,7 @@ func TestListWithFormat(t *testing.T) { func TestListWithoutFormat(t *testing.T) { buf := new(bytes.Buffer) - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service( @@ -99,7 +99,7 @@ func TestListWithoutFormat(t *testing.T) { func TestListOrder(t *testing.T) { buf := new(bytes.Buffer) - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service( diff --git a/cli/command/stack/ps_test.go b/cli/command/stack/ps_test.go index 7dc766609c..b67adb06be 100644 --- a/cli/command/stack/ps_test.go +++ b/cli/command/stack/ps_test.go @@ -43,7 +43,7 @@ func TestStackPsErrors(t *testing.T) { } for _, tc := range testCases { - cmd := newPsCommand(test.NewFakeCli(&fakeClient{ + cmd := newPsCommand(test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: tc.taskListFunc, }, &bytes.Buffer{})) cmd.SetArgs(tc.args) @@ -53,25 +53,22 @@ func TestStackPsErrors(t *testing.T) { } func TestStackPsEmptyStack(t *testing.T) { - out := new(bytes.Buffer) - stderr := new(bytes.Buffer) fakeCli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{}, nil }, - }, out) - fakeCli.SetErr(stderr) + }) cmd := newPsCommand(fakeCli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "", out.String()) - assert.Equal(t, "Nothing found in stack: foo\n", stderr.String()) + assert.Equal(t, "", fakeCli.OutBuffer().String()) + assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().String()) } func TestStackPsWithQuietOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskID("id-foo"))}, nil }, @@ -89,7 +86,7 @@ func TestStackPsWithQuietOption(t *testing.T) { func TestStackPsWithNoTruncOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil }, @@ -107,7 +104,7 @@ func TestStackPsWithNoTruncOption(t *testing.T) { func TestStackPsWithNoResolveOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task( TaskNodeID("id-node-foo"), @@ -130,7 +127,7 @@ func TestStackPsWithNoResolveOption(t *testing.T) { func TestStackPsWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, @@ -147,7 +144,7 @@ func TestStackPsWithFormat(t *testing.T) { func TestStackPsWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, @@ -165,7 +162,7 @@ func TestStackPsWithConfigFormat(t *testing.T) { func TestStackPsWithoutFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task( TaskID("id-foo"), diff --git a/cli/command/stack/remove_test.go b/cli/command/stack/remove_test.go index a924407af0..0bbeb2da15 100644 --- a/cli/command/stack/remove_test.go +++ b/cli/command/stack/remove_test.go @@ -46,7 +46,7 @@ func TestRemoveStack(t *testing.T) { secrets: allSecrets, configs: allConfigs, } - cmd := newRemoveCommand(test.NewFakeCli(cli, &bytes.Buffer{})) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(cli, &bytes.Buffer{})) cmd.SetArgs([]string{"foo", "bar"}) assert.NoError(t, cmd.Execute()) @@ -57,8 +57,6 @@ func TestRemoveStack(t *testing.T) { } func TestRemoveStackSkipEmpty(t *testing.T) { - out := new(bytes.Buffer) - stderr := new(bytes.Buffer) allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")} allServiceIDs := buildObjectIDs(allServices) @@ -77,14 +75,13 @@ func TestRemoveStackSkipEmpty(t *testing.T) { secrets: allSecrets, configs: allConfigs, } - fakeCli := test.NewFakeCli(fakeClient, out) - fakeCli.SetErr(stderr) + fakeCli := test.NewFakeCli(fakeClient) cmd := newRemoveCommand(fakeCli) cmd.SetArgs([]string{"foo", "bar"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "", out.String()) - assert.Contains(t, stderr.String(), "Nothing found in stack: foo\n") + assert.Equal(t, "", fakeCli.OutBuffer().String()) + assert.Contains(t, fakeCli.ErrBuffer().String(), "Nothing found in stack: foo\n") assert.Equal(t, allServiceIDs, fakeClient.removedServices) assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks) assert.Equal(t, allSecretIDs, fakeClient.removedSecrets) @@ -120,7 +117,7 @@ func TestRemoveContinueAfterError(t *testing.T) { return nil }, } - cmd := newRemoveCommand(test.NewFakeCli(cli, &bytes.Buffer{})) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(cli, &bytes.Buffer{})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"foo", "bar"}) diff --git a/cli/command/stack/services_test.go b/cli/command/stack/services_test.go index 88408b4cd3..0225822fdb 100644 --- a/cli/command/stack/services_test.go +++ b/cli/command/stack/services_test.go @@ -66,7 +66,7 @@ func TestStackServicesErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: tc.serviceListFunc, nodeListFunc: tc.nodeListFunc, taskListFunc: tc.taskListFunc, @@ -83,24 +83,21 @@ func TestStackServicesErrors(t *testing.T) { } func TestStackServicesEmptyServiceList(t *testing.T) { - out := new(bytes.Buffer) - stderr := new(bytes.Buffer) fakeCli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{}, nil }, - }, out) - fakeCli.SetErr(stderr) + }) cmd := newServicesCommand(fakeCli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "", out.String()) - assert.Equal(t, "Nothing found in stack: foo\n", stderr.String()) + assert.Equal(t, "", fakeCli.OutBuffer().String()) + assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().String()) } func TestStackServicesWithQuietOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{*Service(ServiceID("id-foo"))}, nil }, @@ -117,7 +114,7 @@ func TestStackServicesWithQuietOption(t *testing.T) { func TestStackServicesWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service(ServiceName("service-name-foo")), @@ -136,7 +133,7 @@ func TestStackServicesWithFormat(t *testing.T) { func TestStackServicesWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service(ServiceName("service-name-foo")), @@ -156,7 +153,7 @@ func TestStackServicesWithConfigFormat(t *testing.T) { func TestStackServicesWithoutFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{*Service( ServiceName("name-foo"), diff --git a/cli/command/swarm/init_test.go b/cli/command/swarm/init_test.go index 0fcdbb786d..f6969d1b0e 100644 --- a/cli/command/swarm/init_test.go +++ b/cli/command/swarm/init_test.go @@ -67,7 +67,7 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInitCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInitFunc: tc.swarmInitFunc, swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, @@ -114,7 +114,7 @@ func TestSwarmInit(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInitCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInitFunc: tc.swarmInitFunc, swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, diff --git a/cli/command/swarm/join_test.go b/cli/command/swarm/join_test.go index d4f1098ff9..331f2c1753 100644 --- a/cli/command/swarm/join_test.go +++ b/cli/command/swarm/join_test.go @@ -51,7 +51,7 @@ func TestSwarmJoinErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmJoinFunc: tc.swarmJoinFunc, infoFunc: tc.infoFunc, }, buf)) @@ -93,7 +93,7 @@ func TestSwarmJoin(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, }, buf)) cmd.SetArgs([]string{"remote"}) diff --git a/cli/command/swarm/join_token_test.go b/cli/command/swarm/join_token_test.go index 5c095b4156..f8619a4bd6 100644 --- a/cli/command/swarm/join_token_test.go +++ b/cli/command/swarm/join_token_test.go @@ -92,7 +92,7 @@ func TestSwarmJoinTokenErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinTokenCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, infoFunc: tc.infoFunc, @@ -200,7 +200,7 @@ func TestSwarmJoinToken(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinTokenCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, infoFunc: tc.infoFunc, nodeInspectFunc: tc.nodeInspectFunc, diff --git a/cli/command/swarm/leave_test.go b/cli/command/swarm/leave_test.go index dd7597efb5..07c1945296 100644 --- a/cli/command/swarm/leave_test.go +++ b/cli/command/swarm/leave_test.go @@ -35,7 +35,7 @@ func TestSwarmLeaveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newLeaveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmLeaveFunc: tc.swarmLeaveFunc, }, buf)) cmd.SetArgs(tc.args) @@ -47,7 +47,7 @@ func TestSwarmLeaveErrors(t *testing.T) { func TestSwarmLeave(t *testing.T) { buf := new(bytes.Buffer) cmd := newLeaveCommand( - test.NewFakeCli(&fakeClient{}, buf)) + test.NewFakeCliWithOutput(&fakeClient{}, buf)) assert.NoError(t, cmd.Execute()) assert.Equal(t, "Node left the swarm.", strings.TrimSpace(buf.String())) } diff --git a/cli/command/swarm/unlock_key_test.go b/cli/command/swarm/unlock_key_test.go index 6b0246b199..f2f60706ff 100644 --- a/cli/command/swarm/unlock_key_test.go +++ b/cli/command/swarm/unlock_key_test.go @@ -85,7 +85,7 @@ func TestSwarmUnlockKeyErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUnlockKeyCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, @@ -160,7 +160,7 @@ func TestSwarmUnlockKey(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUnlockKeyCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, diff --git a/cli/command/swarm/unlock_test.go b/cli/command/swarm/unlock_test.go index 3dae5239f5..63b47e472a 100644 --- a/cli/command/swarm/unlock_test.go +++ b/cli/command/swarm/unlock_test.go @@ -68,7 +68,7 @@ func TestSwarmUnlockErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUnlockCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, swarmUnlockFunc: tc.swarmUnlockFunc, }, buf)) @@ -81,7 +81,7 @@ func TestSwarmUnlockErrors(t *testing.T) { func TestSwarmUnlock(t *testing.T) { input := "unlockKey" buf := new(bytes.Buffer) - dockerCli := test.NewFakeCli(&fakeClient{ + dockerCli := test.NewFakeCliWithOutput(&fakeClient{ infoFunc: func() (types.Info, error) { return types.Info{ Swarm: swarm.Info{ diff --git a/cli/command/swarm/update_test.go b/cli/command/swarm/update_test.go index a6372cfee9..926916f01f 100644 --- a/cli/command/swarm/update_test.go +++ b/cli/command/swarm/update_test.go @@ -70,7 +70,7 @@ func TestSwarmUpdateErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, @@ -166,7 +166,7 @@ func TestSwarmUpdate(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, diff --git a/cli/command/task/print_test.go b/cli/command/task/print_test.go index 93171016f2..174ad72cd7 100644 --- a/cli/command/task/print_test.go +++ b/cli/command/task/print_test.go @@ -24,7 +24,7 @@ func TestTaskPrintWithQuietOption(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskID("id-foo")), } @@ -41,7 +41,7 @@ func TestTaskPrintWithNoTruncOption(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")), } @@ -58,7 +58,7 @@ func TestTaskPrintWithGlobalService(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)), } @@ -75,7 +75,7 @@ func TestTaskPrintWithReplicatedService(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskServiceID("service-id-foo"), TaskSlot(1)), } @@ -99,7 +99,7 @@ func TestTaskPrintWithIndentation(t *testing.T) { return *Node(NodeName("node-name-bar")), nil, nil }, } - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task( TaskID("id-foo"), @@ -138,7 +138,7 @@ func TestTaskPrintWithResolution(t *testing.T) { return *Node(NodeName("node-name-bar")), nil, nil }, } - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskServiceID("service-id-foo"), TaskSlot(1)), } diff --git a/cli/command/volume/create_test.go b/cli/command/volume/create_test.go index 74c81aba1f..ed85cd34c6 100644 --- a/cli/command/volume/create_test.go +++ b/cli/command/volume/create_test.go @@ -43,7 +43,7 @@ func TestVolumeCreateErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newCreateCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeCreateFunc: tc.volumeCreateFunc, }, buf), ) @@ -59,7 +59,7 @@ func TestVolumeCreateErrors(t *testing.T) { func TestVolumeCreateWithName(t *testing.T) { name := "foo" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) { if body.Name != name { return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name) @@ -97,7 +97,7 @@ func TestVolumeCreateWithFlags(t *testing.T) { name := "banana" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) { if body.Name != "" { return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name) diff --git a/cli/command/volume/inspect_test.go b/cli/command/volume/inspect_test.go index 3c16f125ab..8f452d7dee 100644 --- a/cli/command/volume/inspect_test.go +++ b/cli/command/volume/inspect_test.go @@ -56,7 +56,7 @@ func TestVolumeInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeInspectFunc: tc.volumeInspectFunc, }, buf), ) @@ -98,7 +98,7 @@ func TestVolumeInspectWithoutFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeInspectFunc: tc.volumeInspectFunc, }, buf), ) @@ -138,7 +138,7 @@ func TestVolumeInspectWithFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeInspectFunc: tc.volumeInspectFunc, }, buf), ) diff --git a/cli/command/volume/list_test.go b/cli/command/volume/list_test.go index 2dc11a8b8e..1338915dc4 100644 --- a/cli/command/volume/list_test.go +++ b/cli/command/volume/list_test.go @@ -39,7 +39,7 @@ func TestVolumeListErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newListCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: tc.volumeListFunc, }, buf), ) @@ -54,7 +54,7 @@ func TestVolumeListErrors(t *testing.T) { func TestVolumeListWithoutFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { return volumetypes.VolumesListOKBody{ Volumes: []*types.Volume{ @@ -77,7 +77,7 @@ func TestVolumeListWithoutFormat(t *testing.T) { func TestVolumeListWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { return volumetypes.VolumesListOKBody{ Volumes: []*types.Volume{ @@ -102,7 +102,7 @@ func TestVolumeListWithConfigFormat(t *testing.T) { func TestVolumeListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { return volumetypes.VolumesListOKBody{ Volumes: []*types.Volume{ diff --git a/cli/command/volume/prune_test.go b/cli/command/volume/prune_test.go index 33a8d5dc19..cf26d283f4 100644 --- a/cli/command/volume/prune_test.go +++ b/cli/command/volume/prune_test.go @@ -41,7 +41,7 @@ func TestVolumePruneErrors(t *testing.T) { } for _, tc := range testCases { cmd := NewPruneCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: tc.volumePruneFunc, }, ioutil.Discard), ) @@ -70,7 +70,7 @@ func TestVolumePruneForce(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := NewPruneCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: tc.volumePruneFunc, }, buf), ) @@ -88,7 +88,7 @@ func TestVolumePrunePromptYes(t *testing.T) { } for _, input := range []string{"y", "Y"} { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: simplePruneFunc, }, buf) @@ -110,7 +110,7 @@ func TestVolumePrunePromptNo(t *testing.T) { } for _, input := range []string{"n", "N", "no", "anything", "really"} { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: simplePruneFunc, }, buf) diff --git a/cli/command/volume/remove_test.go b/cli/command/volume/remove_test.go index 8916700cc6..3d01905681 100644 --- a/cli/command/volume/remove_test.go +++ b/cli/command/volume/remove_test.go @@ -31,7 +31,7 @@ func TestVolumeRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeRemoveFunc: tc.volumeRemoveFunc, }, buf)) cmd.SetArgs(tc.args) @@ -42,7 +42,7 @@ func TestVolumeRemoveErrors(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) { buf := new(bytes.Buffer) - cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) cmd.SetArgs([]string{"volume1", "volume2"}) assert.NoError(t, cmd.Execute()) } diff --git a/cli/internal/test/cli.go b/cli/internal/test/cli.go index 378ee9cdc2..de765742ba 100644 --- a/cli/internal/test/cli.go +++ b/cli/internal/test/cli.go @@ -1,6 +1,7 @@ package test import ( + "bytes" "io" "io/ioutil" "strings" @@ -16,17 +17,29 @@ type FakeCli struct { client client.APIClient configfile *configfile.ConfigFile out *command.OutStream - err io.Writer + outBuffer *bytes.Buffer + err *bytes.Buffer in *command.InStream server command.ServerInfo } -// NewFakeCli returns a Cli backed by the fakeCli -func NewFakeCli(client client.APIClient, out io.Writer) *FakeCli { +// NewFakeCliWithOutput returns a Cli backed by the fakeCli +// Deprecated: Use NewFakeCli +func NewFakeCliWithOutput(client client.APIClient, out io.Writer) *FakeCli { + cli := NewFakeCli(client) + cli.out = command.NewOutStream(out) + return cli +} + +// NewFakeCli returns a fake for the command.Cli interface +func NewFakeCli(client client.APIClient) *FakeCli { + outBuffer := new(bytes.Buffer) + errBuffer := new(bytes.Buffer) return &FakeCli{ client: client, - out: command.NewOutStream(out), - err: ioutil.Discard, + out: command.NewOutStream(outBuffer), + outBuffer: outBuffer, + err: errBuffer, in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))), configfile: configfile.New("configfile"), } @@ -38,7 +51,7 @@ func (c *FakeCli) SetIn(in *command.InStream) { } // SetErr sets the stderr stream for the cli to the specified io.Writer -func (c *FakeCli) SetErr(err io.Writer) { +func (c *FakeCli) SetErr(err *bytes.Buffer) { c.err = err } @@ -76,3 +89,13 @@ func (c *FakeCli) ConfigFile() *configfile.ConfigFile { func (c *FakeCli) ServerInfo() command.ServerInfo { return c.server } + +// OutBuffer returns the stdout buffer +func (c *FakeCli) OutBuffer() *bytes.Buffer { + return c.outBuffer +} + +// ErrBuffer Buffer returns the stderr buffer +func (c *FakeCli) ErrBuffer() *bytes.Buffer { + return c.err +}