Update volume command tests to use the new golden

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-16 13:50:28 -04:00
parent 3d68aa8416
commit 505a0fe45f
6 changed files with 59 additions and 107 deletions

View File

@ -1,7 +1,6 @@
package volume package volume
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"reflect" "reflect"
"strings" "strings"
@ -57,8 +56,7 @@ func TestVolumeCreateErrors(t *testing.T) {
func TestVolumeCreateWithName(t *testing.T) { func TestVolumeCreateWithName(t *testing.T) {
name := "foo" name := "foo"
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)
@ -67,7 +65,9 @@ func TestVolumeCreateWithName(t *testing.T) {
Name: body.Name, Name: body.Name,
}, nil }, nil
}, },
}, buf) })
buf := cli.OutBuffer()
// Test by flags // Test by flags
cmd := newCreateCommand(cli) cmd := newCreateCommand(cli)
@ -95,8 +95,7 @@ func TestVolumeCreateWithFlags(t *testing.T) {
} }
name := "banana" name := "banana"
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)
@ -114,7 +113,7 @@ func TestVolumeCreateWithFlags(t *testing.T) {
Name: name, Name: name,
}, nil }, nil
}, },
}, buf) })
cmd := newCreateCommand(cli) cmd := newCreateCommand(cli)
cmd.Flags().Set("driver", "foo") cmd.Flags().Set("driver", "foo")
@ -123,5 +122,5 @@ func TestVolumeCreateWithFlags(t *testing.T) {
cmd.Flags().Set("label", "lbl1=v1") cmd.Flags().Set("label", "lbl1=v1")
cmd.Flags().Set("label", "lbl2=v2") cmd.Flags().Set("label", "lbl2=v2")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, name, strings.TrimSpace(buf.String())) assert.Equal(t, name, strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -1,7 +1,6 @@
package volume package volume
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -12,7 +11,7 @@ import (
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders" . "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -54,11 +53,10 @@ func TestVolumeInspectErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInspectCommand( cmd := newInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc, volumeInspectFunc: tc.volumeInspectFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -96,17 +94,13 @@ func TestVolumeInspectWithoutFormat(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc, volumeInspectFunc: tc.volumeInspectFunc,
}, buf), })
) cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-inspect-without-format.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("volume-inspect-without-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
@ -136,17 +130,13 @@ func TestVolumeInspectWithFormat(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc, volumeInspectFunc: tc.volumeInspectFunc,
}, buf), })
) cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.Flags().Set("format", tc.format) cmd.Flags().Set("format", tc.format)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-inspect-with-format.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("volume-inspect-with-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }

View File

@ -1,7 +1,6 @@
package volume package volume
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -14,7 +13,7 @@ import (
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders" . "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -37,11 +36,10 @@ func TestVolumeListErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newListCommand( cmd := newListCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
volumeListFunc: tc.volumeListFunc, volumeListFunc: tc.volumeListFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -53,8 +51,7 @@ func TestVolumeListErrors(t *testing.T) {
} }
func TestVolumeListWithoutFormat(t *testing.T) { func TestVolumeListWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{ return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{ Volumes: []*types.Volume{
@ -66,17 +63,14 @@ func TestVolumeListWithoutFormat(t *testing.T) {
}, },
}, nil }, nil
}, },
}, buf) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "volume-list-without-format.golden")
expected := golden.Get(t, []byte(actual), "volume-list-without-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestVolumeListWithConfigFormat(t *testing.T) { func TestVolumeListWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{ return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{ Volumes: []*types.Volume{
@ -88,20 +82,17 @@ func TestVolumeListWithConfigFormat(t *testing.T) {
}, },
}, nil }, nil
}, },
}, buf) })
cli.SetConfigFile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
VolumesFormat: "{{ .Name }} {{ .Driver }} {{ .Labels }}", VolumesFormat: "{{ .Name }} {{ .Driver }} {{ .Labels }}",
}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "volume-list-with-config-format.golden")
expected := golden.Get(t, []byte(actual), "volume-list-with-config-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestVolumeListWithFormat(t *testing.T) { func TestVolumeListWithFormat(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{ return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{ Volumes: []*types.Volume{
@ -113,11 +104,9 @@ func TestVolumeListWithFormat(t *testing.T) {
}, },
}, nil }, nil
}, },
}, buf) })
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())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "volume-list-with-format.golden")
expected := golden.Get(t, []byte(actual), "volume-list-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -1,7 +1,6 @@
package volume package volume
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"runtime" "runtime"
@ -13,7 +12,8 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/gotestyourself/gotestyourself/skip"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -41,9 +41,9 @@ func TestVolumePruneErrors(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
cmd := NewPruneCommand( cmd := NewPruneCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
volumePruneFunc: tc.volumePruneFunc, volumePruneFunc: tc.volumePruneFunc,
}, ioutil.Discard), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -68,60 +68,45 @@ func TestVolumePruneForce(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := NewPruneCommand(
test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: tc.volumePruneFunc, volumePruneFunc: tc.volumePruneFunc,
}, buf), })
) cmd := NewPruneCommand(cli)
cmd.Flags().Set("force", "true") cmd.Flags().Set("force", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-prune.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("volume-prune.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
func TestVolumePrunePromptYes(t *testing.T) { func TestVolumePrunePromptYes(t *testing.T) {
if runtime.GOOS == "windows" {
// FIXME(vdemeester) make it work.. // FIXME(vdemeester) make it work..
t.Skip("skipping this test on Windows") skip.IfCondition(t, runtime.GOOS == "windows", "TODO: fix test on windows")
}
for _, input := range []string{"y", "Y"} { for _, input := range []string{"y", "Y"} {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: simplePruneFunc, volumePruneFunc: simplePruneFunc,
}, buf) })
cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input)))) cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cmd := NewPruneCommand( cmd := NewPruneCommand(cli)
cli,
)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "volume-prune-yes.golden")
expected := golden.Get(t, []byte(actual), "volume-prune-yes.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
func TestVolumePrunePromptNo(t *testing.T) { func TestVolumePrunePromptNo(t *testing.T) {
if runtime.GOOS == "windows" {
// FIXME(vdemeester) make it work.. // FIXME(vdemeester) make it work..
t.Skip("skipping this test on Windows") skip.IfCondition(t, runtime.GOOS == "windows", "TODO: fix test on windows")
}
for _, input := range []string{"n", "N", "no", "anything", "really"} { for _, input := range []string{"n", "N", "no", "anything", "really"} {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
volumePruneFunc: simplePruneFunc, volumePruneFunc: simplePruneFunc,
}, buf) })
cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input)))) cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cmd := NewPruneCommand( cmd := NewPruneCommand(cli)
cli,
)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "volume-prune-no.golden")
expected := golden.Get(t, []byte(actual), "volume-prune-no.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }

View File

@ -1,7 +1,6 @@
package volume package volume
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -29,11 +28,10 @@ func TestVolumeRemoveErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newRemoveCommand( cmd := newRemoveCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
volumeRemoveFunc: tc.volumeRemoveFunc, volumeRemoveFunc: tc.volumeRemoveFunc,
}, 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)
@ -41,8 +39,7 @@ func TestVolumeRemoveErrors(t *testing.T) {
} }
func TestNodeRemoveMultiple(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) {
buf := new(bytes.Buffer) cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}))
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

@ -23,14 +23,6 @@ type FakeCli struct {
server command.ServerInfo server command.ServerInfo
} }
// NewFakeCliWithOutput returns a Cli backed by the fakeCli
// Deprecated: Use NewFakeCli
func NewFakeCliWithOutput(client client.APIClient, out io.Writer) *FakeCli {
cli := NewFakeCli(client)
cli.out = command.NewOutStream(out)
return cli
}
// NewFakeCli returns a fake for the command.Cli interface // NewFakeCli returns a fake for the command.Cli interface
func NewFakeCli(client client.APIClient) *FakeCli { func NewFakeCli(client client.APIClient) *FakeCli {
outBuffer := new(bytes.Buffer) outBuffer := new(bytes.Buffer)