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
import (
"bytes"
"io/ioutil"
"reflect"
"strings"
@ -57,8 +56,7 @@ func TestVolumeCreateErrors(t *testing.T) {
func TestVolumeCreateWithName(t *testing.T) {
name := "foo"
buf := new(bytes.Buffer)
cli := test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
if body.Name != name {
return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name)
@ -67,7 +65,9 @@ func TestVolumeCreateWithName(t *testing.T) {
Name: body.Name,
}, nil
},
}, buf)
})
buf := cli.OutBuffer()
// Test by flags
cmd := newCreateCommand(cli)
@ -95,8 +95,7 @@ func TestVolumeCreateWithFlags(t *testing.T) {
}
name := "banana"
buf := new(bytes.Buffer)
cli := test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) {
if 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,
}, nil
},
}, buf)
})
cmd := newCreateCommand(cli)
cmd.Flags().Set("driver", "foo")
@ -123,5 +122,5 @@ func TestVolumeCreateWithFlags(t *testing.T) {
cmd.Flags().Set("label", "lbl1=v1")
cmd.Flags().Set("label", "lbl2=v2")
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
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
@ -12,7 +11,7 @@ import (
// Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden"
"github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert"
)
@ -54,11 +53,10 @@ func TestVolumeInspectErrors(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{
test.NewFakeCli(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc,
}, buf),
}),
)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
@ -96,17 +94,13 @@ func TestVolumeInspectWithoutFormat(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc,
}, buf),
)
})
cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("volume-inspect-without-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-inspect-without-format.%s.golden", tc.name))
}
}
@ -136,17 +130,13 @@ func TestVolumeInspectWithFormat(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumeInspectFunc: tc.volumeInspectFunc,
}, buf),
)
})
cmd := newInspectCommand(cli)
cmd.SetArgs(tc.args)
cmd.Flags().Set("format", tc.format)
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("volume-inspect-with-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-inspect-with-format.%s.golden", tc.name))
}
}

View File

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

View File

@ -1,7 +1,6 @@
package volume
import (
"bytes"
"fmt"
"io/ioutil"
"runtime"
@ -13,7 +12,8 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"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/stretchr/testify/assert"
)
@ -41,9 +41,9 @@ func TestVolumePruneErrors(t *testing.T) {
}
for _, tc := range testCases {
cmd := NewPruneCommand(
test.NewFakeCliWithOutput(&fakeClient{
test.NewFakeCli(&fakeClient{
volumePruneFunc: tc.volumePruneFunc,
}, ioutil.Discard),
}),
)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
@ -68,60 +68,45 @@ func TestVolumePruneForce(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := NewPruneCommand(
test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumePruneFunc: tc.volumePruneFunc,
}, buf),
)
})
cmd := NewPruneCommand(cli)
cmd.Flags().Set("force", "true")
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), fmt.Sprintf("volume-prune.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("volume-prune.%s.golden", tc.name))
}
}
func TestVolumePrunePromptYes(t *testing.T) {
if runtime.GOOS == "windows" {
// 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"} {
buf := new(bytes.Buffer)
cli := test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumePruneFunc: simplePruneFunc,
}, buf)
})
cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cmd := NewPruneCommand(
cli,
)
cmd := NewPruneCommand(cli)
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), "volume-prune-yes.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-yes.golden")
}
}
func TestVolumePrunePromptNo(t *testing.T) {
if runtime.GOOS == "windows" {
// 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"} {
buf := new(bytes.Buffer)
cli := test.NewFakeCliWithOutput(&fakeClient{
cli := test.NewFakeCli(&fakeClient{
volumePruneFunc: simplePruneFunc,
}, buf)
})
cli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
cmd := NewPruneCommand(
cli,
)
cmd := NewPruneCommand(cli)
assert.NoError(t, cmd.Execute())
actual := buf.String()
expected := golden.Get(t, []byte(actual), "volume-prune-no.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
golden.Assert(t, cli.OutBuffer().String(), "volume-prune-no.golden")
}
}

View File

@ -1,7 +1,6 @@
package volume
import (
"bytes"
"io/ioutil"
"testing"
@ -29,11 +28,10 @@ func TestVolumeRemoveErrors(t *testing.T) {
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newRemoveCommand(
test.NewFakeCliWithOutput(&fakeClient{
test.NewFakeCli(&fakeClient{
volumeRemoveFunc: tc.volumeRemoveFunc,
}, buf))
}))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
@ -41,8 +39,7 @@ func TestVolumeRemoveErrors(t *testing.T) {
}
func TestNodeRemoveMultiple(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf))
cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetArgs([]string{"volume1", "volume2"})
assert.NoError(t, cmd.Execute())
}

View File

@ -23,14 +23,6 @@ type FakeCli struct {
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
func NewFakeCli(client client.APIClient) *FakeCli {
outBuffer := new(bytes.Buffer)