Merge pull request #299 from dnephin/less-verbose-fake-cli

Less verbose fake cli
This commit is contained in:
Vincent Demeester 2017-07-12 09:56:43 +02:00 committed by GitHub
commit fa7687a362
54 changed files with 242 additions and 335 deletions

View File

@ -1,7 +1,6 @@
package checkpoint package checkpoint
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -39,7 +38,7 @@ func TestCheckpointCreateErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
checkpointCreateFunc: tc.checkpointCreateFunc, checkpointCreateFunc: tc.checkpointCreateFunc,
}, &bytes.Buffer{}) })
cmd := newCreateCommand(cli) cmd := newCreateCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -50,7 +49,6 @@ func TestCheckpointCreateErrors(t *testing.T) {
func TestCheckpointCreateWithOptions(t *testing.T) { func TestCheckpointCreateWithOptions(t *testing.T) {
var containerID, checkpointID, checkpointDir string var containerID, checkpointID, checkpointDir string
var exit bool var exit bool
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error { checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error {
containerID = container containerID = container
@ -59,7 +57,7 @@ func TestCheckpointCreateWithOptions(t *testing.T) {
exit = options.Exit exit = options.Exit
return nil return nil
}, },
}, buf) })
cmd := newCreateCommand(cli) cmd := newCreateCommand(cli)
checkpoint := "checkpoint-bar" checkpoint := "checkpoint-bar"
cmd.SetArgs([]string{"container-foo", checkpoint}) cmd.SetArgs([]string{"container-foo", checkpoint})
@ -70,5 +68,5 @@ func TestCheckpointCreateWithOptions(t *testing.T) {
assert.Equal(t, checkpoint, checkpointID) assert.Equal(t, checkpoint, checkpointID)
assert.Equal(t, "/dir/foo", checkpointDir) assert.Equal(t, "/dir/foo", checkpointDir)
assert.Equal(t, false, exit) assert.Equal(t, false, exit)
assert.Equal(t, checkpoint, strings.TrimSpace(buf.String())) assert.Equal(t, checkpoint, strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -37,7 +37,7 @@ func TestCheckpointListErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
checkpointListFunc: tc.checkpointListFunc, checkpointListFunc: tc.checkpointListFunc,
}, &bytes.Buffer{}) }, &bytes.Buffer{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
@ -50,7 +50,7 @@ func TestCheckpointListErrors(t *testing.T) {
func TestCheckpointListWithOptions(t *testing.T) { func TestCheckpointListWithOptions(t *testing.T) {
var containerID, checkpointDir string var containerID, checkpointDir string
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
checkpointListFunc: func(container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) { checkpointListFunc: func(container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) {
containerID = container containerID = container
checkpointDir = options.CheckpointDir checkpointDir = options.CheckpointDir

View File

@ -36,7 +36,7 @@ func TestCheckpointRemoveErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
checkpointDeleteFunc: tc.checkpointDeleteFunc, checkpointDeleteFunc: tc.checkpointDeleteFunc,
}, &bytes.Buffer{}) }, &bytes.Buffer{})
cmd := newRemoveCommand(cli) cmd := newRemoveCommand(cli)
@ -48,7 +48,7 @@ func TestCheckpointRemoveErrors(t *testing.T) {
func TestCheckpointRemoveWithOptions(t *testing.T) { func TestCheckpointRemoveWithOptions(t *testing.T) {
var containerID, checkpointID, checkpointDir string var containerID, checkpointID, checkpointDir string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
checkpointDeleteFunc: func(container string, options types.CheckpointDeleteOptions) error { checkpointDeleteFunc: func(container string, options types.CheckpointDeleteOptions) error {
containerID = container containerID = container
checkpointID = options.CheckpointID checkpointID = options.CheckpointID

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -41,11 +40,10 @@ func TestConfigCreateErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newConfigCreateCommand( cmd := newConfigCreateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
configCreateFunc: tc.configCreateFunc, configCreateFunc: tc.configCreateFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -55,7 +53,6 @@ func TestConfigCreateErrors(t *testing.T) {
func TestConfigCreateWithName(t *testing.T) { func TestConfigCreateWithName(t *testing.T) {
name := "foo" name := "foo"
buf := new(bytes.Buffer)
var actual []byte var actual []byte
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
@ -69,14 +66,14 @@ func TestConfigCreateWithName(t *testing.T) {
ID: "ID-" + spec.Name, ID: "ID-" + spec.Name,
}, nil }, nil
}, },
}, buf) })
cmd := newConfigCreateCommand(cli) cmd := newConfigCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)}) cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
expected := golden.Get(t, actual, configDataFile) expected := golden.Get(t, actual, configDataFile)
assert.Equal(t, string(expected), string(actual)) assert.Equal(t, string(expected), string(actual))
assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }
func TestConfigCreateWithLabels(t *testing.T) { func TestConfigCreateWithLabels(t *testing.T) {
@ -86,7 +83,6 @@ func TestConfigCreateWithLabels(t *testing.T) {
} }
name := "foo" name := "foo"
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
@ -101,12 +97,12 @@ func TestConfigCreateWithLabels(t *testing.T) {
ID: "ID-" + spec.Name, ID: "ID-" + spec.Name,
}, nil }, nil
}, },
}, buf) })
cmd := newConfigCreateCommand(cli) cmd := newConfigCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)}) cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)})
cmd.Flags().Set("label", "lbl1=Label-foo") cmd.Flags().Set("label", "lbl1=Label-foo")
cmd.Flags().Set("label", "lbl2=Label-bar") cmd.Flags().Set("label", "lbl2=Label-bar")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -55,7 +55,7 @@ func TestConfigInspectErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newConfigInspectCommand( cmd := newConfigInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
configInspectFunc: tc.configInspectFunc, configInspectFunc: tc.configInspectFunc,
}, buf), }, buf),
) )
@ -97,7 +97,7 @@ func TestConfigInspectWithoutFormat(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newConfigInspectCommand( cmd := newConfigInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
configInspectFunc: tc.configInspectFunc, configInspectFunc: tc.configInspectFunc,
}, buf), }, buf),
) )
@ -137,7 +137,7 @@ func TestConfigInspectWithFormat(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newConfigInspectCommand( cmd := newConfigInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
configInspectFunc: tc.configInspectFunc, configInspectFunc: tc.configInspectFunc,
}, buf), }, buf),
) )
@ -174,7 +174,7 @@ func TestConfigInspectPretty(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newConfigInspectCommand( cmd := newConfigInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
configInspectFunc: tc.configInspectFunc, configInspectFunc: tc.configInspectFunc,
}, buf)) }, buf))
cmd.SetArgs([]string{"configID"}) cmd.SetArgs([]string{"configID"})

View File

@ -1,7 +1,6 @@
package config package config
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
"time" "time"
@ -36,11 +35,10 @@ func TestConfigListErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newConfigListCommand( cmd := newConfigListCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
configListFunc: tc.configListFunc, configListFunc: tc.configListFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -49,7 +47,6 @@ func TestConfigListErrors(t *testing.T) {
} }
func TestConfigList(t *testing.T) { func TestConfigList(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
@ -67,18 +64,16 @@ func TestConfigList(t *testing.T) {
), ),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.SetOutput(buf) cmd.SetOutput(cli.OutBuffer())
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "config-list.golden") expected := golden.Get(t, []byte(actual), "config-list.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithQuietOption(t *testing.T) { func TestConfigListWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
@ -88,18 +83,16 @@ func TestConfigListWithQuietOption(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "config-list-with-quiet-option.golden") expected := golden.Get(t, []byte(actual), "config-list-with-quiet-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithConfigFormat(t *testing.T) { func TestConfigListWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
@ -109,19 +102,18 @@ func TestConfigListWithConfigFormat(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
ConfigFormat: "{{ .Name }} {{ .Labels }}", ConfigFormat: "{{ .Name }} {{ .Labels }}",
}) })
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "config-list-with-config-format.golden") expected := golden.Get(t, []byte(actual), "config-list-with-config-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithFormat(t *testing.T) { func TestConfigListWithFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
@ -131,17 +123,16 @@ func TestConfigListWithFormat(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}") cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "config-list-with-format.golden") expected := golden.Get(t, []byte(actual), "config-list-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestConfigListWithFilter(t *testing.T) { func TestConfigListWithFilter(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
assert.Equal(t, "foo", options.Filters.Get("name")[0]) assert.Equal(t, "foo", options.Filters.Get("name")[0])
@ -161,13 +152,12 @@ func TestConfigListWithFilter(t *testing.T) {
), ),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newConfigListCommand(cli) cmd := newConfigListCommand(cli)
cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "name=foo")
cmd.Flags().Set("filter", "label=lbl1=Label-bar") cmd.Flags().Set("filter", "label=lbl1=Label-bar")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "config-list-with-filter.golden") expected := golden.Get(t, []byte(actual), "config-list-with-filter.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -33,7 +33,7 @@ func TestConfigRemoveErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newConfigRemoveCommand( cmd := newConfigRemoveCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
configRemoveFunc: tc.configRemoveFunc, configRemoveFunc: tc.configRemoveFunc,
}, buf), }, buf),
) )
@ -47,7 +47,7 @@ func TestConfigRemoveWithName(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
var removedConfigs []string var removedConfigs []string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
configRemoveFunc: func(name string) error { configRemoveFunc: func(name string) error {
removedConfigs = append(removedConfigs, name) removedConfigs = append(removedConfigs, name)
return nil return nil
@ -65,7 +65,7 @@ func TestConfigRemoveContinueAfterError(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
var removedConfigs []string var removedConfigs []string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
configRemoveFunc: func(name string) error { configRemoveFunc: func(name string) error {
removedConfigs = append(removedConfigs, name) removedConfigs = append(removedConfigs, name)
if name == "foo" { if name == "foo" {

View File

@ -1,7 +1,6 @@
package container package container
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -68,8 +67,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}))
cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)

View File

@ -1,15 +1,14 @@
package container package container
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/internal/test" "github.com/docker/cli/cli/internal/test"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/require"
) )
type arguments struct { type arguments struct {
@ -79,9 +78,7 @@ func TestParseExec(t *testing.T) {
for valid, expectedExecConfig := range valids { for valid, expectedExecConfig := range valids {
execConfig, err := parseExec(&valid.options, valid.execCmd) execConfig, err := parseExec(&valid.options, valid.execCmd)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
if !compareExecConfig(expectedExecConfig, execConfig) { if !compareExecConfig(expectedExecConfig, execConfig) {
t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig) t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig)
} }
@ -138,10 +135,7 @@ func TestNewExecCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc})
conf := configfile.ConfigFile{}
cli := test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf)
cli.SetConfigfile(&conf)
cmd := NewExecCommand(cli) cmd := NewExecCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)

View File

@ -38,7 +38,7 @@ func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) {
return types.ImageBuildResponse{Body: ioutil.NopCloser(body)}, nil return types.ImageBuildResponse{Body: ioutil.NopCloser(body)}, nil
} }
cli := test.NewFakeCli(&fakeClient{imageBuildFunc: fakeImageBuild}, ioutil.Discard) cli := test.NewFakeCli(&fakeClient{imageBuildFunc: fakeImageBuild})
dockerfile := bytes.NewBufferString(` dockerfile := bytes.NewBufferString(`
FROM alpine:3.6 FROM alpine:3.6
COPY foo / COPY foo /

View File

@ -1,7 +1,6 @@
package image package image
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"regexp" "regexp"
@ -38,8 +37,7 @@ func TestNewHistoryCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}))
cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -90,13 +88,13 @@ func TestNewHistoryCommandSuccess(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc})
cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) cmd := NewHistoryCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
err := cmd.Execute() err := cmd.Execute()
assert.NoError(t, err) assert.NoError(t, err)
actual := buf.String() actual := cli.OutBuffer().String()
if tc.outputRegex == "" { if tc.outputRegex == "" {
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("history-command-success.%s.golden", tc.name))[:]) expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("history-command-success.%s.golden", tc.name))[:])
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)

View File

@ -37,7 +37,7 @@ func TestNewImportCommandErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) 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.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -45,7 +45,7 @@ func TestNewImportCommandErrors(t *testing.T) {
} }
func TestNewImportCommandInvalidFile(t *testing.T) { func TestNewImportCommandInvalidFile(t *testing.T) {
cmd := NewImportCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) cmd := NewImportCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs([]string{"testdata/import-command-success.unexistent-file"}) cmd.SetArgs([]string{"testdata/import-command-success.unexistent-file"})
testutil.ErrorContains(t, cmd.Execute(), "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 { for _, tc := range testCases {
buf := new(bytes.Buffer) 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.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())

View File

@ -27,7 +27,7 @@ func TestNewInspectCommandErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInspectCommand(test.NewFakeCli(&fakeClient{}, buf)) cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -79,7 +79,7 @@ func TestNewInspectCommandSuccess(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
imageInspectInvocationCount = 0 imageInspectInvocationCount = 0
buf := new(bytes.Buffer) 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.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
err := cmd.Execute() err := cmd.Execute()

View File

@ -36,7 +36,7 @@ func TestNewImagesCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewImagesCommand(test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}, new(bytes.Buffer))) cmd := NewImagesCommand(test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -81,8 +81,8 @@ func TestNewImagesCommandSuccess(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) 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}) cli.SetConfigFile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat})
cmd := NewImagesCommand(cli) cmd := NewImagesCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
@ -95,7 +95,7 @@ func TestNewImagesCommandSuccess(t *testing.T) {
} }
func TestNewListCommandAlias(t *testing.T) { func TestNewListCommandAlias(t *testing.T) {
cmd := newListCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) cmd := newListCommand(test.NewFakeCli(&fakeClient{}))
assert.True(t, cmd.HasAlias("images")) assert.True(t, cmd.HasAlias("images"))
assert.True(t, cmd.HasAlias("list")) assert.True(t, cmd.HasAlias("list"))
assert.False(t, cmd.HasAlias("other")) assert.False(t, cmd.HasAlias("other"))

View File

@ -43,7 +43,7 @@ func TestNewLoadCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, new(bytes.Buffer)) cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc})
cli.In().SetIsTerminal(tc.isTerminalIn) cli.In().SetIsTerminal(tc.isTerminalIn)
cmd := NewLoadCommand(cli) cmd := NewLoadCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -54,7 +54,7 @@ func TestNewLoadCommandErrors(t *testing.T) {
func TestNewLoadCommandInvalidInput(t *testing.T) { func TestNewLoadCommandInvalidInput(t *testing.T) {
expectedError := "open *" expectedError := "open *"
cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs([]string{"--input", "*"}) cmd.SetArgs([]string{"--input", "*"})
err := cmd.Execute() err := cmd.Execute()
@ -93,7 +93,7 @@ func TestNewLoadCommandSuccess(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) 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.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
err := cmd.Execute() err := cmd.Execute()

View File

@ -38,7 +38,7 @@ func TestNewPruneCommandErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{
imagesPruneFunc: tc.imagesPruneFunc, imagesPruneFunc: tc.imagesPruneFunc,
}, buf)) }, buf))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -86,7 +86,7 @@ func TestNewPruneCommandSuccess(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{
imagesPruneFunc: tc.imagesPruneFunc, imagesPruneFunc: tc.imagesPruneFunc,
}, buf)) }, buf))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)

View File

@ -1,12 +1,10 @@
package image package image
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/internal/test" "github.com/docker/cli/cli/internal/test"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/docker/docker/pkg/testutil/golden"
@ -41,9 +39,7 @@ func TestNewPullCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{})
cli := test.NewFakeCli(&fakeClient{}, buf)
cli.SetConfigfile(configfile.New("filename"))
cmd := NewPullCommand(cli) cmd := NewPullCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
@ -66,15 +62,13 @@ func TestNewPullCommandSuccess(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{})
cli := test.NewFakeCli(&fakeClient{}, buf)
cli.SetConfigfile(configfile.New("filename"))
cmd := NewPullCommand(cli) cmd := NewPullCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
err := cmd.Execute() err := cmd.Execute()
assert.NoError(t, err) assert.NoError(t, err)
actual := buf.String() actual := cli.OutBuffer().String()
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("pull-command-success.%s.golden", tc.name))[:]) expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("pull-command-success.%s.golden", tc.name))[:])
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)
} }

View File

@ -1,13 +1,11 @@
package image package image
import ( import (
"bytes"
"io" "io"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/internal/test" "github.com/docker/cli/cli/internal/test"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
@ -47,9 +45,7 @@ func TestNewPushCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc})
cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc}, buf)
cli.SetConfigfile(configfile.New("filename"))
cmd := NewPushCommand(cli) cmd := NewPushCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
@ -68,13 +64,11 @@ func TestNewPushCommandSuccess(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader("")), nil return ioutil.NopCloser(strings.NewReader("")), nil
}, },
}, buf) })
cli.SetConfigfile(configfile.New("filename"))
cmd := NewPushCommand(cli) cmd := NewPushCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)

View File

@ -1,7 +1,6 @@
package image package image
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -15,7 +14,7 @@ import (
) )
func TestNewRemoveCommandAlias(t *testing.T) { func TestNewRemoveCommandAlias(t *testing.T) {
cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}))
assert.True(t, cmd.HasAlias("rmi")) assert.True(t, cmd.HasAlias("rmi"))
assert.True(t, cmd.HasAlias("remove")) assert.True(t, cmd.HasAlias("remove"))
assert.False(t, cmd.HasAlias("other")) assert.False(t, cmd.HasAlias("other"))
@ -46,7 +45,7 @@ func TestNewRemoveCommandErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{ cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{
imageRemoveFunc: tc.imageRemoveFunc, imageRemoveFunc: tc.imageRemoveFunc,
}, new(bytes.Buffer))) }))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -97,18 +96,15 @@ func TestNewRemoveCommandSuccess(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc})
errBuf := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}, buf)
fakeCli.SetErr(errBuf)
cmd := NewRemoveCommand(fakeCli) cmd := NewRemoveCommand(fakeCli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
if tc.expectedErrMsg != "" { if tc.expectedErrMsg != "" {
assert.Equal(t, tc.expectedErrMsg, errBuf.String()) assert.Equal(t, tc.expectedErrMsg, fakeCli.ErrBuffer().String())
} }
actual := buf.String() actual := fakeCli.OutBuffer().String()
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("remove-command-success.%s.golden", tc.name))[:]) expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("remove-command-success.%s.golden", tc.name))[:])
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)
} }

View File

@ -45,7 +45,7 @@ func TestNewSaveCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc}, new(bytes.Buffer)) cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})
cli.Out().SetIsTerminal(tc.isTerminal) cli.Out().SetIsTerminal(tc.isTerminal)
cmd := NewSaveCommand(cli) cmd := NewSaveCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -85,7 +85,7 @@ func TestNewSaveCommandSuccess(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{ cmd := NewSaveCommand(test.NewFakeCliWithOutput(&fakeClient{
imageSaveFunc: func(images []string) (io.ReadCloser, error) { imageSaveFunc: func(images []string) (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader("")), nil return ioutil.NopCloser(strings.NewReader("")), nil
}, },

View File

@ -1,7 +1,6 @@
package image package image
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -17,9 +16,8 @@ func TestCliNewTagCommandErrors(t *testing.T) {
{"image1", "image2", "image3"}, {"image1", "image2", "image3"},
} }
expectedError := "\"tag\" requires exactly 2 argument(s)." expectedError := "\"tag\" requires exactly 2 argument(s)."
buf := new(bytes.Buffer)
for _, args := range testCases { for _, args := range testCases {
cmd := NewTagCommand(test.NewFakeCli(&fakeClient{}, buf)) cmd := NewTagCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetArgs(args) cmd.SetArgs(args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), expectedError) testutil.ErrorContains(t, cmd.Execute(), expectedError)
@ -27,7 +25,6 @@ func TestCliNewTagCommandErrors(t *testing.T) {
} }
func TestCliNewTagCommand(t *testing.T) { func TestCliNewTagCommand(t *testing.T) {
buf := new(bytes.Buffer)
cmd := NewTagCommand( cmd := NewTagCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
imageTagFunc: func(image string, ref string) error { imageTagFunc: func(image string, ref string) error {
@ -35,7 +32,7 @@ func TestCliNewTagCommand(t *testing.T) {
assert.Equal(t, "image2", ref) assert.Equal(t, "image2", ref)
return nil return nil
}, },
}, buf)) }))
cmd.SetArgs([]string{"image1", "image2"}) cmd.SetArgs([]string{"image1", "image2"})
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())

View File

@ -1,7 +1,6 @@
package network package network
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -130,11 +129,10 @@ func TestNetworkCreateErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newCreateCommand( cmd := newCreateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
networkCreateFunc: tc.networkCreateFunc, networkCreateFunc: tc.networkCreateFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -155,7 +153,6 @@ func TestNetworkCreateWithFlags(t *testing.T) {
map[string]string{}, map[string]string{},
}, },
} }
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
networkCreateFunc: func(ctx context.Context, name string, createBody types.NetworkCreate) (types.NetworkCreateResponse, error) { networkCreateFunc: func(ctx context.Context, name string, createBody types.NetworkCreate) (types.NetworkCreateResponse, error) {
assert.Equal(t, expectedDriver, createBody.Driver, "not expected driver error") assert.Equal(t, expectedDriver, createBody.Driver, "not expected driver error")
@ -164,7 +161,7 @@ func TestNetworkCreateWithFlags(t *testing.T) {
ID: name, ID: name,
}, nil }, nil
}, },
}, buf) })
args := []string{"banana"} args := []string{"banana"}
cmd := newCreateCommand(cli) cmd := newCreateCommand(cli)
@ -174,5 +171,5 @@ func TestNetworkCreateWithFlags(t *testing.T) {
cmd.Flags().Set("gateway", "192.168.4.1/24") cmd.Flags().Set("gateway", "192.168.4.1/24")
cmd.Flags().Set("subnet", "192.168.4.0/24") cmd.Flags().Set("subnet", "192.168.4.0/24")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "banana", strings.TrimSpace(buf.String())) assert.Equal(t, "banana", strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -1,7 +1,6 @@
package node package node
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -40,12 +39,11 @@ func TestNodeDemoteErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newDemoteCommand( cmd := newDemoteCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc, nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -53,7 +51,6 @@ func TestNodeDemoteErrors(t *testing.T) {
} }
func TestNodeDemoteNoChange(t *testing.T) { func TestNodeDemoteNoChange(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newDemoteCommand( cmd := newDemoteCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) { nodeInspectFunc: func() (swarm.Node, []byte, error) {
@ -65,13 +62,12 @@ func TestNodeDemoteNoChange(t *testing.T) {
} }
return nil return nil
}, },
}, buf)) }))
cmd.SetArgs([]string{"nodeID"}) cmd.SetArgs([]string{"nodeID"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
} }
func TestNodeDemoteMultipleNode(t *testing.T) { func TestNodeDemoteMultipleNode(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newDemoteCommand( cmd := newDemoteCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) { nodeInspectFunc: func() (swarm.Node, []byte, error) {
@ -83,7 +79,7 @@ func TestNodeDemoteMultipleNode(t *testing.T) {
} }
return nil return nil
}, },
}, buf)) }))
cmd.SetArgs([]string{"nodeID1", "nodeID2"}) cmd.SetArgs([]string{"nodeID1", "nodeID2"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
} }

View File

@ -69,7 +69,7 @@ func TestNodeInspectErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInspectCommand( cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
}, buf)) }, buf))
@ -111,7 +111,7 @@ func TestNodeInspectPretty(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInspectCommand( cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
}, buf)) }, buf))
cmd.SetArgs([]string{"nodeID"}) cmd.SetArgs([]string{"nodeID"})

View File

@ -42,12 +42,10 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
nodeListFunc: tc.nodeListFunc, nodeListFunc: tc.nodeListFunc,
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
assert.EqualError(t, cmd.Execute(), tc.expectedError) assert.EqualError(t, cmd.Execute(), tc.expectedError)
@ -55,7 +53,6 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) {
} }
func TestNodeList(t *testing.T) { func TestNodeList(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
nodeListFunc: func() ([]swarm.Node, error) { nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{ return []swarm.Node{
@ -71,25 +68,25 @@ func TestNodeList(t *testing.T) {
}, },
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Contains(t, buf.String(), `nodeID1 * nodeHostname1 Ready Active Leader`) out := cli.OutBuffer().String()
assert.Contains(t, buf.String(), `nodeID2 nodeHostname2 Ready Active Reachable`) assert.Contains(t, out, `nodeID1 * nodeHostname1 Ready Active Leader`)
assert.Contains(t, buf.String(), `nodeID3 nodeHostname3 Ready Active`) assert.Contains(t, out, `nodeID2 nodeHostname2 Ready Active Reachable`)
assert.Contains(t, out, `nodeID3 nodeHostname3 Ready Active`)
} }
func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
nodeListFunc: func() ([]swarm.Node, error) { nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{ return []swarm.Node{
*Node(), *Node(),
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
@ -99,8 +96,7 @@ func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) {
// Test case for #24090 // Test case for #24090
func TestNodeListContainsHostname(t *testing.T) { func TestNodeListContainsHostname(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{}, buf) cli := test.NewFakeCliWithOutput(&fakeClient{}, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Contains(t, buf.String(), "HOSTNAME") assert.Contains(t, buf.String(), "HOSTNAME")
@ -108,7 +104,7 @@ func TestNodeListContainsHostname(t *testing.T) {
func TestNodeListDefaultFormat(t *testing.T) { func TestNodeListDefaultFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
nodeListFunc: func() ([]swarm.Node, error) { nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{ return []swarm.Node{
*Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())),
@ -124,7 +120,7 @@ func TestNodeListDefaultFormat(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}",
}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
@ -136,7 +132,7 @@ func TestNodeListDefaultFormat(t *testing.T) {
func TestNodeListFormat(t *testing.T) { func TestNodeListFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
nodeListFunc: func() ([]swarm.Node, error) { nodeListFunc: func() ([]swarm.Node, error) {
return []swarm.Node{ return []swarm.Node{
*Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())),
@ -151,7 +147,7 @@ func TestNodeListFormat(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}",
}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)

View File

@ -42,7 +42,7 @@ func TestNodePromoteErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newPromoteCommand( cmd := newPromoteCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc, nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf)) }, buf))
@ -55,7 +55,7 @@ func TestNodePromoteErrors(t *testing.T) {
func TestNodePromoteNoChange(t *testing.T) { func TestNodePromoteNoChange(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newPromoteCommand( cmd := newPromoteCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) { nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(Manager()), []byte{}, nil return *Node(Manager()), []byte{}, nil
}, },
@ -73,7 +73,7 @@ func TestNodePromoteNoChange(t *testing.T) {
func TestNodePromoteMultipleNode(t *testing.T) { func TestNodePromoteMultipleNode(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newPromoteCommand( cmd := newPromoteCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) { nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(), []byte{}, nil return *Node(), []byte{}, nil
}, },

View File

@ -52,7 +52,7 @@ func TestNodePsErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newPsCommand( cmd := newPsCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
taskInspectFunc: tc.taskInspectFunc, taskInspectFunc: tc.taskInspectFunc,
@ -116,7 +116,7 @@ func TestNodePs(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newPsCommand( cmd := newPsCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
taskInspectFunc: tc.taskInspectFunc, taskInspectFunc: tc.taskInspectFunc,

View File

@ -31,7 +31,7 @@ func TestNodeRemoveErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newRemoveCommand( cmd := newRemoveCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
nodeRemoveFunc: tc.nodeRemoveFunc, nodeRemoveFunc: tc.nodeRemoveFunc,
}, buf)) }, buf))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
@ -42,7 +42,7 @@ func TestNodeRemoveErrors(t *testing.T) {
func TestNodeRemoveMultiple(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, buf)) cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf))
cmd.SetArgs([]string{"nodeID1", "nodeID2"}) cmd.SetArgs([]string{"nodeID1", "nodeID2"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
} }

View File

@ -1,7 +1,6 @@
package node package node
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -57,12 +56,11 @@ func TestNodeUpdateErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newUpdateCommand( cmd := newUpdateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc, nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
@ -158,12 +156,11 @@ func TestNodeUpdate(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newUpdateCommand( cmd := newUpdateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc, nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)

View File

@ -1,7 +1,6 @@
package command_test package command_test
import ( import (
"bytes"
"testing" "testing"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -63,13 +62,10 @@ func TestElectAuthServer(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc})
cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc}, buf)
errBuf := new(bytes.Buffer)
cli.SetErr(errBuf)
server := ElectAuthServer(context.Background(), cli) server := ElectAuthServer(context.Background(), cli)
assert.Equal(t, tc.expectedAuthServer, server) assert.Equal(t, tc.expectedAuthServer, server)
actual := errBuf.String() actual := cli.ErrBuffer().String()
if tc.expectedWarning == "" { if tc.expectedWarning == "" {
assert.Empty(t, actual) assert.Empty(t, actual)
} else { } else {

View File

@ -41,11 +41,10 @@ func TestSecretCreateErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newSecretCreateCommand( cmd := newSecretCreateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
secretCreateFunc: tc.secretCreateFunc, secretCreateFunc: tc.secretCreateFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -57,7 +56,7 @@ func TestSecretCreateWithName(t *testing.T) {
name := "foo" name := "foo"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
var actual []byte var actual []byte
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
@ -87,7 +86,7 @@ func TestSecretCreateWithLabels(t *testing.T) {
name := "foo" name := "foo"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)

View File

@ -55,7 +55,7 @@ func TestSecretInspectErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newSecretInspectCommand( cmd := newSecretInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
secretInspectFunc: tc.secretInspectFunc, secretInspectFunc: tc.secretInspectFunc,
}, buf), }, buf),
) )
@ -97,7 +97,7 @@ func TestSecretInspectWithoutFormat(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newSecretInspectCommand( cmd := newSecretInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
secretInspectFunc: tc.secretInspectFunc, secretInspectFunc: tc.secretInspectFunc,
}, buf), }, buf),
) )
@ -137,7 +137,7 @@ func TestSecretInspectWithFormat(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newSecretInspectCommand( cmd := newSecretInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
secretInspectFunc: tc.secretInspectFunc, secretInspectFunc: tc.secretInspectFunc,
}, buf), }, buf),
) )
@ -173,7 +173,7 @@ func TestSecretInspectPretty(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newSecretInspectCommand( cmd := newSecretInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
secretInspectFunc: tc.secretInspectFunc, secretInspectFunc: tc.secretInspectFunc,
}, buf)) }, buf))
cmd.SetArgs([]string{"secretID"}) cmd.SetArgs([]string{"secretID"})

View File

@ -38,7 +38,7 @@ func TestSecretListErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newSecretListCommand( cmd := newSecretListCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: tc.secretListFunc, secretListFunc: tc.secretListFunc,
}, buf), }, buf),
) )
@ -50,7 +50,7 @@ func TestSecretListErrors(t *testing.T) {
func TestSecretList(t *testing.T) { func TestSecretList(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), *Secret(SecretID("ID-foo"),
@ -68,7 +68,6 @@ func TestSecretList(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.SetOutput(buf) cmd.SetOutput(buf)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
@ -79,7 +78,7 @@ func TestSecretList(t *testing.T) {
func TestSecretListWithQuietOption(t *testing.T) { func TestSecretListWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
@ -89,7 +88,6 @@ func TestSecretListWithQuietOption(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
@ -100,7 +98,7 @@ func TestSecretListWithQuietOption(t *testing.T) {
func TestSecretListWithConfigFormat(t *testing.T) { func TestSecretListWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
@ -110,7 +108,7 @@ func TestSecretListWithConfigFormat(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
SecretFormat: "{{ .Name }} {{ .Labels }}", SecretFormat: "{{ .Name }} {{ .Labels }}",
}) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
@ -122,7 +120,7 @@ func TestSecretListWithConfigFormat(t *testing.T) {
func TestSecretListWithFormat(t *testing.T) { func TestSecretListWithFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
@ -142,7 +140,7 @@ func TestSecretListWithFormat(t *testing.T) {
func TestSecretListWithFilter(t *testing.T) { func TestSecretListWithFilter(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo") assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo")
assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0])
@ -162,7 +160,6 @@ func TestSecretListWithFilter(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "name=foo")
cmd.Flags().Set("filter", "label=lbl1=Label-bar") cmd.Flags().Set("filter", "label=lbl1=Label-bar")

View File

@ -33,7 +33,7 @@ func TestSecretRemoveErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newSecretRemoveCommand( cmd := newSecretRemoveCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
secretRemoveFunc: tc.secretRemoveFunc, secretRemoveFunc: tc.secretRemoveFunc,
}, buf), }, buf),
) )
@ -47,7 +47,7 @@ func TestSecretRemoveWithName(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
var removedSecrets []string var removedSecrets []string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(name string) error {
removedSecrets = append(removedSecrets, name) removedSecrets = append(removedSecrets, name)
return nil return nil
@ -65,7 +65,7 @@ func TestSecretRemoveContinueAfterError(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
var removedSecrets []string var removedSecrets []string
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(name string) error {
removedSecrets = append(removedSecrets, name) removedSecrets = append(removedSecrets, name)
if name == "foo" { if name == "foo" {

View File

@ -107,7 +107,7 @@ func TestRunPSWarnsOnNotFound(t *testing.T) {
} }
out := new(bytes.Buffer) out := new(bytes.Buffer)
cli := test.NewFakeCli(client, out) cli := test.NewFakeCliWithOutput(client, out)
options := psOptions{ options := psOptions{
services: []string{"foo", "bar"}, services: []string{"foo", "bar"},
filter: opts.NewFilterOpt(), filter: opts.NewFilterOpt(),

View File

@ -1,7 +1,6 @@
package stack package stack
import ( import (
"bytes"
"testing" "testing"
"github.com/docker/cli/cli/compose/convert" "github.com/docker/cli/cli/compose/convert"
@ -18,10 +17,8 @@ func TestPruneServices(t *testing.T) {
"keep": {}, "keep": {},
} }
client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}} client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}}
dockerCli := test.NewFakeCli(client, &bytes.Buffer{}) dockerCli := test.NewFakeCli(client)
dockerCli.SetErr(&bytes.Buffer{})
pruneServices(ctx, dockerCli, namespace, services) pruneServices(ctx, dockerCli, namespace, services)
assert.Equal(t, buildObjectIDs([]string{objectName("foo", "remove")}), client.removedServices) assert.Equal(t, buildObjectIDs([]string{objectName("foo", "remove")}), client.removedServices)
} }

View File

@ -50,7 +50,7 @@ func TestListErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
cmd := newListCommand(test.NewFakeCli(&fakeClient{ cmd := newListCommand(test.NewFakeCli(&fakeClient{
serviceListFunc: tc.serviceListFunc, serviceListFunc: tc.serviceListFunc,
}, &bytes.Buffer{})) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -62,7 +62,7 @@ func TestListErrors(t *testing.T) {
func TestListWithFormat(t *testing.T) { func TestListWithFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newListCommand(test.NewFakeCli(&fakeClient{ cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{ return []swarm.Service{
*Service( *Service(
@ -81,7 +81,7 @@ func TestListWithFormat(t *testing.T) {
func TestListWithoutFormat(t *testing.T) { func TestListWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newListCommand(test.NewFakeCli(&fakeClient{ cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{ return []swarm.Service{
*Service( *Service(
@ -99,7 +99,7 @@ func TestListWithoutFormat(t *testing.T) {
func TestListOrder(t *testing.T) { func TestListOrder(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newListCommand(test.NewFakeCli(&fakeClient{ cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{ return []swarm.Service{
*Service( *Service(

View File

@ -1,7 +1,6 @@
package stack package stack
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
"time" "time"
@ -45,7 +44,7 @@ func TestStackPsErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
cmd := newPsCommand(test.NewFakeCli(&fakeClient{ cmd := newPsCommand(test.NewFakeCli(&fakeClient{
taskListFunc: tc.taskListFunc, taskListFunc: tc.taskListFunc,
}, &bytes.Buffer{})) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -53,60 +52,52 @@ func TestStackPsErrors(t *testing.T) {
} }
func TestStackPsEmptyStack(t *testing.T) { func TestStackPsEmptyStack(t *testing.T) {
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{ fakeCli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, nil return []swarm.Task{}, nil
}, },
}, out) })
fakeCli.SetErr(stderr)
cmd := newPsCommand(fakeCli) cmd := newPsCommand(fakeCli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "", out.String()) assert.Equal(t, "", fakeCli.OutBuffer().String())
assert.Equal(t, "Nothing found in stack: foo\n", stderr.String()) assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().String())
} }
func TestStackPsWithQuietOption(t *testing.T) { func TestStackPsWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskID("id-foo"))}, nil return []swarm.Task{*Task(TaskID("id-foo"))}, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-ps-with-quiet-option.golden") expected := golden.Get(t, []byte(actual), "stack-ps-with-quiet-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackPsWithNoTruncOption(t *testing.T) { func TestStackPsWithNoTruncOption(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
cmd.Flags().Set("no-trunc", "true") cmd.Flags().Set("no-trunc", "true")
cmd.Flags().Set("format", "{{ .ID }}") cmd.Flags().Set("format", "{{ .ID }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-ps-with-no-trunc-option.golden") expected := golden.Get(t, []byte(actual), "stack-ps-with-no-trunc-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackPsWithNoResolveOption(t *testing.T) { func TestStackPsWithNoResolveOption(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task( return []swarm.Task{*Task(
@ -116,55 +107,50 @@ func TestStackPsWithNoResolveOption(t *testing.T) {
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
return *Node(NodeName("node-name-bar")), nil, nil return *Node(NodeName("node-name-bar")), nil, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
cmd.Flags().Set("no-resolve", "true") cmd.Flags().Set("no-resolve", "true")
cmd.Flags().Set("format", "{{ .Node }}") cmd.Flags().Set("format", "{{ .Node }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-ps-with-no-resolve-option.golden") expected := golden.Get(t, []byte(actual), "stack-ps-with-no-resolve-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackPsWithFormat(t *testing.T) { func TestStackPsWithFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
cmd.Flags().Set("format", "{{ .Name }}") cmd.Flags().Set("format", "{{ .Name }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-ps-with-format.golden") expected := golden.Get(t, []byte(actual), "stack-ps-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackPsWithConfigFormat(t *testing.T) { func TestStackPsWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
TasksFormat: "{{ .Name }}", TasksFormat: "{{ .Name }}",
}) })
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-ps-with-config-format.golden") expected := golden.Get(t, []byte(actual), "stack-ps-with-config-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackPsWithoutFormat(t *testing.T) { func TestStackPsWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{*Task( return []swarm.Task{*Task(
@ -179,12 +165,11 @@ func TestStackPsWithoutFormat(t *testing.T) {
nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) {
return *Node(NodeName("node-name-bar")), nil, nil return *Node(NodeName("node-name-bar")), nil, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newPsCommand(cli) cmd := newPsCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-ps-without-format.golden") expected := golden.Get(t, []byte(actual), "stack-ps-without-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -1,7 +1,6 @@
package stack package stack
import ( import (
"bytes"
"errors" "errors"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -46,7 +45,7 @@ func TestRemoveStack(t *testing.T) {
secrets: allSecrets, secrets: allSecrets,
configs: allConfigs, configs: allConfigs,
} }
cmd := newRemoveCommand(test.NewFakeCli(cli, &bytes.Buffer{})) cmd := newRemoveCommand(test.NewFakeCli(cli))
cmd.SetArgs([]string{"foo", "bar"}) cmd.SetArgs([]string{"foo", "bar"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
@ -57,8 +56,6 @@ func TestRemoveStack(t *testing.T) {
} }
func TestRemoveStackSkipEmpty(t *testing.T) { func TestRemoveStackSkipEmpty(t *testing.T) {
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")} allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")}
allServiceIDs := buildObjectIDs(allServices) allServiceIDs := buildObjectIDs(allServices)
@ -77,14 +74,13 @@ func TestRemoveStackSkipEmpty(t *testing.T) {
secrets: allSecrets, secrets: allSecrets,
configs: allConfigs, configs: allConfigs,
} }
fakeCli := test.NewFakeCli(fakeClient, out) fakeCli := test.NewFakeCli(fakeClient)
fakeCli.SetErr(stderr)
cmd := newRemoveCommand(fakeCli) cmd := newRemoveCommand(fakeCli)
cmd.SetArgs([]string{"foo", "bar"}) cmd.SetArgs([]string{"foo", "bar"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "", out.String()) assert.Equal(t, "", fakeCli.OutBuffer().String())
assert.Contains(t, stderr.String(), "Nothing found in stack: foo\n") assert.Contains(t, fakeCli.ErrBuffer().String(), "Nothing found in stack: foo\n")
assert.Equal(t, allServiceIDs, fakeClient.removedServices) assert.Equal(t, allServiceIDs, fakeClient.removedServices)
assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks) assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks)
assert.Equal(t, allSecretIDs, fakeClient.removedSecrets) assert.Equal(t, allSecretIDs, fakeClient.removedSecrets)
@ -120,7 +116,7 @@ func TestRemoveContinueAfterError(t *testing.T) {
return nil return nil
}, },
} }
cmd := newRemoveCommand(test.NewFakeCli(cli, &bytes.Buffer{})) cmd := newRemoveCommand(test.NewFakeCli(cli))
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
cmd.SetArgs([]string{"foo", "bar"}) cmd.SetArgs([]string{"foo", "bar"})

View File

@ -1,7 +1,6 @@
package stack package stack
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -70,8 +69,7 @@ func TestStackServicesErrors(t *testing.T) {
serviceListFunc: tc.serviceListFunc, serviceListFunc: tc.serviceListFunc,
nodeListFunc: tc.nodeListFunc, nodeListFunc: tc.nodeListFunc,
taskListFunc: tc.taskListFunc, taskListFunc: tc.taskListFunc,
}, &bytes.Buffer{}) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newServicesCommand(cli) cmd := newServicesCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -83,79 +81,70 @@ func TestStackServicesErrors(t *testing.T) {
} }
func TestStackServicesEmptyServiceList(t *testing.T) { func TestStackServicesEmptyServiceList(t *testing.T) {
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{ fakeCli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, nil return []swarm.Service{}, nil
}, },
}, out) })
fakeCli.SetErr(stderr)
cmd := newServicesCommand(fakeCli) cmd := newServicesCommand(fakeCli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "", out.String()) assert.Equal(t, "", fakeCli.OutBuffer().String())
assert.Equal(t, "Nothing found in stack: foo\n", stderr.String()) assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().String())
} }
func TestStackServicesWithQuietOption(t *testing.T) { func TestStackServicesWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{*Service(ServiceID("id-foo"))}, nil return []swarm.Service{*Service(ServiceID("id-foo"))}, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newServicesCommand(cli) cmd := newServicesCommand(cli)
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-services-with-quiet-option.golden") expected := golden.Get(t, []byte(actual), "stack-services-with-quiet-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackServicesWithFormat(t *testing.T) { func TestStackServicesWithFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{ return []swarm.Service{
*Service(ServiceName("service-name-foo")), *Service(ServiceName("service-name-foo")),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newServicesCommand(cli) cmd := newServicesCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
cmd.Flags().Set("format", "{{ .Name }}") cmd.Flags().Set("format", "{{ .Name }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-services-with-format.golden") expected := golden.Get(t, []byte(actual), "stack-services-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackServicesWithConfigFormat(t *testing.T) { func TestStackServicesWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{ return []swarm.Service{
*Service(ServiceName("service-name-foo")), *Service(ServiceName("service-name-foo")),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
ServicesFormat: "{{ .Name }}", ServicesFormat: "{{ .Name }}",
}) })
cmd := newServicesCommand(cli) cmd := newServicesCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-services-with-config-format.golden") expected := golden.Get(t, []byte(actual), "stack-services-with-config-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestStackServicesWithoutFormat(t *testing.T) { func TestStackServicesWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{*Service( return []swarm.Service{*Service(
@ -171,12 +160,11 @@ func TestStackServicesWithoutFormat(t *testing.T) {
}), }),
)}, nil )}, nil
}, },
}, buf) })
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newServicesCommand(cli) cmd := newServicesCommand(cli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), "stack-services-without-format.golden") expected := golden.Get(t, []byte(actual), "stack-services-without-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -67,7 +67,7 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInitCommand( cmd := newInitCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
swarmInitFunc: tc.swarmInitFunc, swarmInitFunc: tc.swarmInitFunc,
swarmInspectFunc: tc.swarmInspectFunc, swarmInspectFunc: tc.swarmInspectFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
@ -114,7 +114,7 @@ func TestSwarmInit(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInitCommand( cmd := newInitCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
swarmInitFunc: tc.swarmInitFunc, swarmInitFunc: tc.swarmInitFunc,
swarmInspectFunc: tc.swarmInspectFunc, swarmInspectFunc: tc.swarmInspectFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,

View File

@ -51,7 +51,7 @@ func TestSwarmJoinErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newJoinCommand( cmd := newJoinCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
swarmJoinFunc: tc.swarmJoinFunc, swarmJoinFunc: tc.swarmJoinFunc,
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
}, buf)) }, buf))
@ -93,7 +93,7 @@ func TestSwarmJoin(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newJoinCommand( cmd := newJoinCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
}, buf)) }, buf))
cmd.SetArgs([]string{"remote"}) cmd.SetArgs([]string{"remote"})

View File

@ -92,7 +92,7 @@ func TestSwarmJoinTokenErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newJoinTokenCommand( cmd := newJoinTokenCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc, swarmInspectFunc: tc.swarmInspectFunc,
swarmUpdateFunc: tc.swarmUpdateFunc, swarmUpdateFunc: tc.swarmUpdateFunc,
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
@ -200,7 +200,7 @@ func TestSwarmJoinToken(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newJoinTokenCommand( cmd := newJoinTokenCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc, swarmInspectFunc: tc.swarmInspectFunc,
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
nodeInspectFunc: tc.nodeInspectFunc, nodeInspectFunc: tc.nodeInspectFunc,

View File

@ -1,7 +1,6 @@
package swarm package swarm
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -33,11 +32,10 @@ func TestSwarmLeaveErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newLeaveCommand( cmd := newLeaveCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
swarmLeaveFunc: tc.swarmLeaveFunc, swarmLeaveFunc: tc.swarmLeaveFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -45,9 +43,8 @@ func TestSwarmLeaveErrors(t *testing.T) {
} }
func TestSwarmLeave(t *testing.T) { func TestSwarmLeave(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{})
cmd := newLeaveCommand( cmd := newLeaveCommand(cli)
test.NewFakeCli(&fakeClient{}, buf))
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "Node left the swarm.", strings.TrimSpace(buf.String())) assert.Equal(t, "Node left the swarm.", strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -1,7 +1,6 @@
package swarm package swarm
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -83,13 +82,12 @@ func TestSwarmUnlockKeyErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newUnlockKeyCommand( cmd := newUnlockKeyCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc, swarmInspectFunc: tc.swarmInspectFunc,
swarmUpdateFunc: tc.swarmUpdateFunc, swarmUpdateFunc: tc.swarmUpdateFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
@ -158,19 +156,18 @@ func TestSwarmUnlockKey(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newUnlockKeyCommand( swarmInspectFunc: tc.swarmInspectFunc,
test.NewFakeCli(&fakeClient{ swarmUpdateFunc: tc.swarmUpdateFunc,
swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
swarmUpdateFunc: tc.swarmUpdateFunc, })
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, cmd := newUnlockKeyCommand(cli)
}, buf))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
} }
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("unlockkeys-%s.golden", tc.name)) expected := golden.Get(t, []byte(actual), fmt.Sprintf("unlockkeys-%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -1,7 +1,6 @@
package swarm package swarm
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -66,12 +65,11 @@ func TestSwarmUnlockErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newUnlockCommand( cmd := newUnlockCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
infoFunc: tc.infoFunc, infoFunc: tc.infoFunc,
swarmUnlockFunc: tc.swarmUnlockFunc, swarmUnlockFunc: tc.swarmUnlockFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -80,7 +78,6 @@ func TestSwarmUnlockErrors(t *testing.T) {
func TestSwarmUnlock(t *testing.T) { func TestSwarmUnlock(t *testing.T) {
input := "unlockKey" input := "unlockKey"
buf := new(bytes.Buffer)
dockerCli := test.NewFakeCli(&fakeClient{ dockerCli := test.NewFakeCli(&fakeClient{
infoFunc: func() (types.Info, error) { infoFunc: func() (types.Info, error) {
return types.Info{ return types.Info{
@ -95,7 +92,7 @@ func TestSwarmUnlock(t *testing.T) {
} }
return nil return nil
}, },
}, buf) })
dockerCli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input)))) dockerCli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cmd := newUnlockCommand(dockerCli) cmd := newUnlockCommand(dockerCli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())

View File

@ -1,7 +1,6 @@
package swarm package swarm
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -68,13 +67,12 @@ func TestSwarmUpdateErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newUpdateCommand( cmd := newUpdateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
swarmInspectFunc: tc.swarmInspectFunc, swarmInspectFunc: tc.swarmInspectFunc,
swarmUpdateFunc: tc.swarmUpdateFunc, swarmUpdateFunc: tc.swarmUpdateFunc,
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
}, buf)) }))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
@ -164,20 +162,19 @@ func TestSwarmUpdate(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newUpdateCommand( swarmInspectFunc: tc.swarmInspectFunc,
test.NewFakeCli(&fakeClient{ swarmUpdateFunc: tc.swarmUpdateFunc,
swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
swarmUpdateFunc: tc.swarmUpdateFunc, })
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, cmd := newUpdateCommand(cli)
}, buf))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
cmd.Flags().Set(key, value) cmd.Flags().Set(key, value)
} }
cmd.SetOutput(buf) cmd.SetOutput(cli.OutBuffer())
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := cli.OutBuffer().String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("update-%s.golden", tc.name)) expected := golden.Get(t, []byte(actual), fmt.Sprintf("update-%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -24,7 +24,7 @@ func TestTaskPrintWithQuietOption(t *testing.T) {
noResolve := true noResolve := true
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
apiClient := &fakeClient{} apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient, buf) cli := test.NewFakeCliWithOutput(apiClient, buf)
tasks := []swarm.Task{ tasks := []swarm.Task{
*Task(TaskID("id-foo")), *Task(TaskID("id-foo")),
} }
@ -41,7 +41,7 @@ func TestTaskPrintWithNoTruncOption(t *testing.T) {
noResolve := true noResolve := true
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
apiClient := &fakeClient{} apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient, buf) cli := test.NewFakeCliWithOutput(apiClient, buf)
tasks := []swarm.Task{ tasks := []swarm.Task{
*Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")), *Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")),
} }
@ -58,7 +58,7 @@ func TestTaskPrintWithGlobalService(t *testing.T) {
noResolve := true noResolve := true
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
apiClient := &fakeClient{} apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient, buf) cli := test.NewFakeCliWithOutput(apiClient, buf)
tasks := []swarm.Task{ tasks := []swarm.Task{
*Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)), *Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)),
} }
@ -75,7 +75,7 @@ func TestTaskPrintWithReplicatedService(t *testing.T) {
noResolve := true noResolve := true
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
apiClient := &fakeClient{} apiClient := &fakeClient{}
cli := test.NewFakeCli(apiClient, buf) cli := test.NewFakeCliWithOutput(apiClient, buf)
tasks := []swarm.Task{ tasks := []swarm.Task{
*Task(TaskServiceID("service-id-foo"), TaskSlot(1)), *Task(TaskServiceID("service-id-foo"), TaskSlot(1)),
} }
@ -99,7 +99,7 @@ func TestTaskPrintWithIndentation(t *testing.T) {
return *Node(NodeName("node-name-bar")), nil, nil return *Node(NodeName("node-name-bar")), nil, nil
}, },
} }
cli := test.NewFakeCli(apiClient, buf) cli := test.NewFakeCliWithOutput(apiClient, buf)
tasks := []swarm.Task{ tasks := []swarm.Task{
*Task( *Task(
TaskID("id-foo"), TaskID("id-foo"),
@ -138,7 +138,7 @@ func TestTaskPrintWithResolution(t *testing.T) {
return *Node(NodeName("node-name-bar")), nil, nil return *Node(NodeName("node-name-bar")), nil, nil
}, },
} }
cli := test.NewFakeCli(apiClient, buf) cli := test.NewFakeCliWithOutput(apiClient, buf)
tasks := []swarm.Task{ tasks := []swarm.Task{
*Task(TaskServiceID("service-id-foo"), TaskSlot(1)), *Task(TaskServiceID("service-id-foo"), TaskSlot(1)),
} }

View File

@ -41,11 +41,10 @@ func TestVolumeCreateErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newCreateCommand( cmd := newCreateCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCli(&fakeClient{
volumeCreateFunc: tc.volumeCreateFunc, volumeCreateFunc: tc.volumeCreateFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -59,7 +58,7 @@ func TestVolumeCreateErrors(t *testing.T) {
func TestVolumeCreateWithName(t *testing.T) { func TestVolumeCreateWithName(t *testing.T) {
name := "foo" name := "foo"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) { volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
if body.Name != name { if body.Name != name {
return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name) return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name)
@ -97,7 +96,7 @@ func TestVolumeCreateWithFlags(t *testing.T) {
name := "banana" name := "banana"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) { volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
if body.Name != "" { if body.Name != "" {
return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name) return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name)

View File

@ -56,7 +56,7 @@ func TestVolumeInspectErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInspectCommand( cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc, volumeInspectFunc: tc.volumeInspectFunc,
}, buf), }, buf),
) )
@ -98,7 +98,7 @@ func TestVolumeInspectWithoutFormat(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInspectCommand( cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc, volumeInspectFunc: tc.volumeInspectFunc,
}, buf), }, buf),
) )
@ -138,7 +138,7 @@ func TestVolumeInspectWithFormat(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newInspectCommand( cmd := newInspectCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc, volumeInspectFunc: tc.volumeInspectFunc,
}, buf), }, buf),
) )

View File

@ -39,7 +39,7 @@ func TestVolumeListErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newListCommand( cmd := newListCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: tc.volumeListFunc, volumeListFunc: tc.volumeListFunc,
}, buf), }, buf),
) )
@ -54,7 +54,7 @@ func TestVolumeListErrors(t *testing.T) {
func TestVolumeListWithoutFormat(t *testing.T) { func TestVolumeListWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{ return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{ Volumes: []*types.Volume{
@ -67,7 +67,6 @@ func TestVolumeListWithoutFormat(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() actual := buf.String()
@ -77,7 +76,7 @@ func TestVolumeListWithoutFormat(t *testing.T) {
func TestVolumeListWithConfigFormat(t *testing.T) { func TestVolumeListWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{ return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{ Volumes: []*types.Volume{
@ -90,7 +89,7 @@ func TestVolumeListWithConfigFormat(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
VolumesFormat: "{{ .Name }} {{ .Driver }} {{ .Labels }}", VolumesFormat: "{{ .Name }} {{ .Driver }} {{ .Labels }}",
}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
@ -102,7 +101,7 @@ func TestVolumeListWithConfigFormat(t *testing.T) {
func TestVolumeListWithFormat(t *testing.T) { func TestVolumeListWithFormat(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{ return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{ Volumes: []*types.Volume{
@ -115,7 +114,6 @@ func TestVolumeListWithFormat(t *testing.T) {
}, nil }, nil
}, },
}, buf) }, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
cmd := newListCommand(cli) cmd := newListCommand(cli)
cmd.Flags().Set("format", "{{ .Name }} {{ .Driver }} {{ .Labels }}") cmd.Flags().Set("format", "{{ .Name }} {{ .Driver }} {{ .Labels }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())

View File

@ -41,7 +41,7 @@ func TestVolumePruneErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewPruneCommand( cmd := NewPruneCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: tc.volumePruneFunc, volumePruneFunc: tc.volumePruneFunc,
}, ioutil.Discard), }, ioutil.Discard),
) )
@ -70,7 +70,7 @@ func TestVolumePruneForce(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := NewPruneCommand( cmd := NewPruneCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: tc.volumePruneFunc, volumePruneFunc: tc.volumePruneFunc,
}, buf), }, buf),
) )
@ -88,7 +88,7 @@ func TestVolumePrunePromptYes(t *testing.T) {
} }
for _, input := range []string{"y", "Y"} { for _, input := range []string{"y", "Y"} {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: simplePruneFunc, volumePruneFunc: simplePruneFunc,
}, buf) }, buf)
@ -110,7 +110,7 @@ func TestVolumePrunePromptNo(t *testing.T) {
} }
for _, input := range []string{"n", "N", "no", "anything", "really"} { for _, input := range []string{"n", "N", "no", "anything", "really"} {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: simplePruneFunc, volumePruneFunc: simplePruneFunc,
}, buf) }, buf)

View File

@ -31,7 +31,7 @@ func TestVolumeRemoveErrors(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newRemoveCommand( cmd := newRemoveCommand(
test.NewFakeCli(&fakeClient{ test.NewFakeCliWithOutput(&fakeClient{
volumeRemoveFunc: tc.volumeRemoveFunc, volumeRemoveFunc: tc.volumeRemoveFunc,
}, buf)) }, buf))
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
@ -42,7 +42,7 @@ func TestVolumeRemoveErrors(t *testing.T) {
func TestNodeRemoveMultiple(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, buf)) cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf))
cmd.SetArgs([]string{"volume1", "volume2"}) cmd.SetArgs([]string{"volume1", "volume2"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
} }

View File

@ -1,6 +1,7 @@
package test package test
import ( import (
"bytes"
"io" "io"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -16,17 +17,29 @@ type FakeCli struct {
client client.APIClient client client.APIClient
configfile *configfile.ConfigFile configfile *configfile.ConfigFile
out *command.OutStream out *command.OutStream
err io.Writer outBuffer *bytes.Buffer
err *bytes.Buffer
in *command.InStream in *command.InStream
server command.ServerInfo server command.ServerInfo
} }
// NewFakeCli returns a Cli backed by the fakeCli // NewFakeCliWithOutput returns a Cli backed by the fakeCli
func NewFakeCli(client client.APIClient, out io.Writer) *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{ return &FakeCli{
client: client, client: client,
out: command.NewOutStream(out), out: command.NewOutStream(outBuffer),
err: ioutil.Discard, outBuffer: outBuffer,
err: errBuffer,
in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))), in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))),
configfile: configfile.New("configfile"), configfile: configfile.New("configfile"),
} }
@ -38,12 +51,12 @@ func (c *FakeCli) SetIn(in *command.InStream) {
} }
// SetErr sets the stderr stream for the cli to the specified io.Writer // 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 c.err = err
} }
// SetConfigfile sets the "fake" config file // SetConfigFile sets the "fake" config file
func (c *FakeCli) SetConfigfile(configfile *configfile.ConfigFile) { func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) {
c.configfile = configfile c.configfile = configfile
} }
@ -76,3 +89,13 @@ func (c *FakeCli) ConfigFile() *configfile.ConfigFile {
func (c *FakeCli) ServerInfo() command.ServerInfo { func (c *FakeCli) ServerInfo() command.ServerInfo {
return c.server 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
}