DockerCLI/cli/command/volume/list_test.go

126 lines
3.8 KiB
Go
Raw Normal View History

package volume
import (
"bytes"
"io/ioutil"
"testing"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/internal/test"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
volumetypes "github.com/docker/docker/api/types/volume"
"github.com/pkg/errors"
// 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/stretchr/testify/assert"
)
func TestVolumeListErrors(t *testing.T) {
testCases := []struct {
args []string
flags map[string]string
volumeListFunc func(filter filters.Args) (volumetypes.VolumesListOKBody, error)
expectedError string
}{
{
args: []string{"foo"},
expectedError: "accepts no argument",
},
{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{}, errors.Errorf("error listing volumes")
},
expectedError: "error listing volumes",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newListCommand(
test.NewFakeCli(&fakeClient{
volumeListFunc: tc.volumeListFunc,
}, buf),
)
cmd.SetArgs(tc.args)
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
cmd.SetOutput(ioutil.Discard)
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
func TestVolumeListWithoutFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{
Volume(),
Volume(VolumeName("foo"), VolumeDriver("bar")),
Volume(VolumeName("baz"), VolumeLabels(map[string]string{
"foo": "bar",
})),
},
}, nil
},
}, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
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))
}
func TestVolumeListWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{
Volume(),
Volume(VolumeName("foo"), VolumeDriver("bar")),
Volume(VolumeName("baz"), VolumeLabels(map[string]string{
"foo": "bar",
})),
},
}, 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))
}
func TestVolumeListWithFormat(t *testing.T) {
buf := new(bytes.Buffer)
cli := test.NewFakeCli(&fakeClient{
volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) {
return volumetypes.VolumesListOKBody{
Volumes: []*types.Volume{
Volume(),
Volume(VolumeName("foo"), VolumeDriver("bar")),
Volume(VolumeName("baz"), VolumeLabels(map[string]string{
"foo": "bar",
})),
},
}, nil
},
}, buf)
cli.SetConfigfile(&configfile.ConfigFile{})
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))
}