Update some tests to remove unnecessary buffers.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-07-05 14:43:39 -04:00
parent 3da0cbfdd1
commit 42a3800783
27 changed files with 135 additions and 203 deletions

View File

@ -1,7 +1,6 @@
package checkpoint package checkpoint
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -37,9 +36,9 @@ func TestCheckpointCreateErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCliWithOutput(&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,8 +49,7 @@ 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.NewFakeCliWithOutput(&fakeClient{
checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error { checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error {
containerID = container containerID = container
checkpointID = options.CheckpointID checkpointID = options.CheckpointID
@ -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

@ -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.NewFakeCliWithOutput(&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,9 +53,8 @@ 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.NewFakeCliWithOutput(&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 {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
@ -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,8 +83,7 @@ func TestConfigCreateWithLabels(t *testing.T) {
} }
name := "foo" name := "foo"
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&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 {
return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.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

@ -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.NewFakeCliWithOutput(&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,8 +47,7 @@ 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.NewFakeCliWithOutput(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), *Config(ConfigID("ID-foo"),
@ -67,18 +64,17 @@ func TestConfigList(t *testing.T) {
), ),
}, nil }, nil
}, },
}, buf) })
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.NewFakeCliWithOutput(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), ConfigName("foo")), *Config(ConfigID("ID-foo"), ConfigName("foo")),
@ -87,18 +83,17 @@ func TestConfigListWithQuietOption(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
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.NewFakeCliWithOutput(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), ConfigName("foo")), *Config(ConfigID("ID-foo"), ConfigName("foo")),
@ -107,20 +102,19 @@ 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.NewFakeCliWithOutput(&fakeClient{
configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) {
return []swarm.Config{ return []swarm.Config{
*Config(ConfigID("ID-foo"), ConfigName("foo")), *Config(ConfigID("ID-foo"), ConfigName("foo")),
@ -129,18 +123,17 @@ 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.NewFakeCliWithOutput(&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])
assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0])
@ -159,12 +152,12 @@ func TestConfigListWithFilter(t *testing.T) {
), ),
}, nil }, nil
}, },
}, buf) })
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

@ -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.NewFakeCliWithOutput(&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

@ -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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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

@ -45,7 +45,7 @@ func TestNewImportCommandErrors(t *testing.T) {
} }
func TestNewImportCommandInvalidFile(t *testing.T) { func TestNewImportCommandInvalidFile(t *testing.T) {
cmd := NewImportCommand(test.NewFakeCliWithOutput(&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")

View File

@ -36,7 +36,7 @@ func TestNewImagesCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewImagesCommand(test.NewFakeCliWithOutput(&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)

View File

@ -43,7 +43,7 @@ func TestNewLoadCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cli := test.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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()

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.NewFakeCliWithOutput(&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"))
@ -44,9 +43,9 @@ func TestNewRemoveCommandErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewRemoveCommand(test.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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)

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.NewFakeCliWithOutput(&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,15 +25,14 @@ func TestCliNewTagCommandErrors(t *testing.T) {
} }
func TestCliNewTagCommand(t *testing.T) { func TestCliNewTagCommand(t *testing.T) {
buf := new(bytes.Buffer)
cmd := NewTagCommand( cmd := NewTagCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
imageTagFunc: func(image string, ref string) error { imageTagFunc: func(image string, ref string) error {
assert.Equal(t, "image1", image) assert.Equal(t, "image1", image)
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.NewFakeCliWithOutput(&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,9 +51,8 @@ func TestNodeDemoteErrors(t *testing.T) {
} }
func TestNodeDemoteNoChange(t *testing.T) { func TestNodeDemoteNoChange(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newDemoteCommand( cmd := newDemoteCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) { nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(), []byte{}, nil return *Node(), []byte{}, nil
}, },
@ -65,15 +62,14 @@ 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.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) { nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(Manager()), []byte{}, nil return *Node(Manager()), []byte{}, nil
}, },
@ -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

@ -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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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)

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.NewFakeCliWithOutput(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

@ -48,9 +48,9 @@ func TestListErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := newListCommand(test.NewFakeCliWithOutput(&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 {

View File

@ -1,7 +1,6 @@
package stack package stack
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
"time" "time"
@ -43,9 +42,9 @@ func TestStackPsErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := newPsCommand(test.NewFakeCliWithOutput(&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)
@ -67,42 +66,39 @@ func TestStackPsEmptyStack(t *testing.T) {
} }
func TestStackPsWithQuietOption(t *testing.T) { func TestStackPsWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 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) })
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.NewFakeCliWithOutput(&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) })
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.NewFakeCliWithOutput(&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(
TaskNodeID("id-node-foo"), TaskNodeID("id-node-foo"),
@ -111,54 +107,51 @@ 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) })
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.NewFakeCliWithOutput(&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) })
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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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(
TaskID("id-foo"), TaskID("id-foo"),
@ -172,11 +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) })
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.NewFakeCliWithOutput(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())
@ -117,7 +116,7 @@ func TestRemoveContinueAfterError(t *testing.T) {
return nil return nil
}, },
} }
cmd := newRemoveCommand(test.NewFakeCliWithOutput(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"
@ -95,62 +94,58 @@ func TestStackServicesEmptyServiceList(t *testing.T) {
} }
func TestStackServicesWithQuietOption(t *testing.T) { func TestStackServicesWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 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) })
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.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(ServiceName("service-name-foo")), *Service(ServiceName("service-name-foo")),
}, nil }, nil
}, },
}, buf) })
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.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(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.NewFakeCliWithOutput(&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(
ServiceName("name-foo"), ServiceName("name-foo"),
@ -165,11 +160,11 @@ func TestStackServicesWithoutFormat(t *testing.T) {
}), }),
)}, nil )}, nil
}, },
}, buf) })
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

@ -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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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,8 +78,7 @@ 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.NewFakeCliWithOutput(&fakeClient{
infoFunc: func() (types.Info, error) { infoFunc: func() (types.Info, error) {
return types.Info{ return types.Info{
Swarm: swarm.Info{ Swarm: swarm.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.NewFakeCliWithOutput(&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.NewFakeCliWithOutput(&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

@ -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.NewFakeCliWithOutput(&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 {