2017-02-27 12:39:35 -05:00
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
2022-02-25 08:34:38 -05:00
|
|
|
"io"
|
2017-02-27 12:39:35 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2023-10-23 08:51:01 -04:00
|
|
|
"github.com/docker/cli/internal/test/builders"
|
2017-02-27 12:39:35 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2022-04-29 13:26:50 -04:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVolumeListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeListFunc func(filter filters.Args) (volume.ListResponse, error)
|
2017-02-27 12:39:35 -05:00
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "accepts no argument",
|
|
|
|
},
|
|
|
|
{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
|
|
|
|
return volume.ListResponse{}, errors.Errorf("error listing volumes")
|
2017-02-27 12:39:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error listing volumes",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newListCommand(
|
2017-08-16 13:50:28 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-02-27 12:39:35 -05:00
|
|
|
volumeListFunc: tc.volumeListFunc,
|
2017-08-16 13:50:28 -04:00
|
|
|
}),
|
2017-02-27 12:39:35 -05:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set(key, value))
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2022-02-25 08:34:38 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2023-02-07 20:27:51 -05:00
|
|
|
cmd.SetErr(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeListWithoutFormat(t *testing.T) {
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
|
|
|
|
return volume.ListResponse{
|
|
|
|
Volumes: []*volume.Volume{
|
2023-10-23 08:51:01 -04:00
|
|
|
builders.Volume(),
|
|
|
|
builders.Volume(builders.VolumeName("foo"), builders.VolumeDriver("bar")),
|
|
|
|
builders.Volume(builders.VolumeName("baz"), builders.VolumeLabels(map[string]string{
|
2017-02-27 12:39:35 -05:00
|
|
|
"foo": "bar",
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-02-27 12:39:35 -05:00
|
|
|
cmd := newListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:50:28 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-list-without-format.golden")
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeListWithConfigFormat(t *testing.T) {
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
|
|
|
|
return volume.ListResponse{
|
|
|
|
Volumes: []*volume.Volume{
|
2023-10-23 08:51:01 -04:00
|
|
|
builders.Volume(),
|
|
|
|
builders.Volume(builders.VolumeName("foo"), builders.VolumeDriver("bar")),
|
|
|
|
builders.Volume(builders.VolumeName("baz"), builders.VolumeLabels(map[string]string{
|
2017-02-27 12:39:35 -05:00
|
|
|
"foo": "bar",
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2017-02-27 12:39:35 -05:00
|
|
|
VolumesFormat: "{{ .Name }} {{ .Driver }} {{ .Labels }}",
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:50:28 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-list-with-config-format.golden")
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolumeListWithFormat(t *testing.T) {
|
2017-08-16 13:50:28 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
|
|
|
|
return volume.ListResponse{
|
|
|
|
Volumes: []*volume.Volume{
|
2023-10-23 08:51:01 -04:00
|
|
|
builders.Volume(),
|
|
|
|
builders.Volume(builders.VolumeName("foo"), builders.VolumeDriver("bar")),
|
|
|
|
builders.Volume(builders.VolumeName("baz"), builders.VolumeLabels(map[string]string{
|
2017-02-27 12:39:35 -05:00
|
|
|
"foo": "bar",
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-02-27 12:39:35 -05:00
|
|
|
cmd := newListCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Name }} {{ .Driver }} {{ .Labels }}"))
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:50:28 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-list-with-format.golden")
|
2017-02-27 12:39:35 -05:00
|
|
|
}
|
2018-08-08 11:57:39 -04:00
|
|
|
|
|
|
|
func TestVolumeListSortOrder(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2022-04-29 13:26:50 -04:00
|
|
|
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
|
|
|
|
return volume.ListResponse{
|
|
|
|
Volumes: []*volume.Volume{
|
2023-10-23 08:51:01 -04:00
|
|
|
builders.Volume(builders.VolumeName("volume-2-foo")),
|
|
|
|
builders.Volume(builders.VolumeName("volume-10-foo")),
|
|
|
|
builders.Volume(builders.VolumeName("volume-1-foo")),
|
2018-08-08 11:57:39 -04:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Name }}"))
|
2018-08-08 11:57:39 -04:00
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-list-sort.golden")
|
|
|
|
}
|
2021-02-23 09:23:53 -05:00
|
|
|
|
|
|
|
func TestClusterVolumeList(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
|
|
|
|
return volume.ListResponse{
|
|
|
|
Volumes: []*volume.Volume{
|
|
|
|
{
|
|
|
|
Name: "volume1",
|
|
|
|
Scope: "global",
|
|
|
|
Driver: "driver1",
|
|
|
|
ClusterVolume: &volume.ClusterVolume{
|
|
|
|
Spec: volume.ClusterVolumeSpec{
|
|
|
|
Group: "group1",
|
|
|
|
AccessMode: &volume.AccessMode{
|
|
|
|
Scope: volume.ScopeSingleNode,
|
|
|
|
Sharing: volume.SharingOneWriter,
|
|
|
|
MountVolume: &volume.TypeMount{},
|
|
|
|
},
|
|
|
|
Availability: volume.AvailabilityActive,
|
|
|
|
},
|
|
|
|
},
|
2022-09-29 11:21:51 -04:00
|
|
|
},
|
|
|
|
{
|
2021-02-23 09:23:53 -05:00
|
|
|
Name: "volume2",
|
|
|
|
Scope: "global",
|
|
|
|
Driver: "driver1",
|
|
|
|
ClusterVolume: &volume.ClusterVolume{
|
|
|
|
Spec: volume.ClusterVolumeSpec{
|
|
|
|
Group: "group1",
|
|
|
|
AccessMode: &volume.AccessMode{
|
|
|
|
Scope: volume.ScopeSingleNode,
|
|
|
|
Sharing: volume.SharingOneWriter,
|
|
|
|
MountVolume: &volume.TypeMount{},
|
|
|
|
},
|
|
|
|
Availability: volume.AvailabilityPause,
|
|
|
|
},
|
|
|
|
Info: &volume.Info{
|
|
|
|
CapacityBytes: 100000000,
|
|
|
|
VolumeID: "driver1vol2",
|
|
|
|
},
|
|
|
|
},
|
2022-09-29 11:21:51 -04:00
|
|
|
},
|
|
|
|
{
|
2021-02-23 09:23:53 -05:00
|
|
|
Name: "volume3",
|
|
|
|
Scope: "global",
|
|
|
|
Driver: "driver2",
|
|
|
|
ClusterVolume: &volume.ClusterVolume{
|
|
|
|
Spec: volume.ClusterVolumeSpec{
|
|
|
|
Group: "group2",
|
|
|
|
AccessMode: &volume.AccessMode{
|
|
|
|
Scope: volume.ScopeMultiNode,
|
|
|
|
Sharing: volume.SharingAll,
|
|
|
|
MountVolume: &volume.TypeMount{},
|
|
|
|
},
|
|
|
|
Availability: volume.AvailabilityActive,
|
|
|
|
},
|
|
|
|
PublishStatus: []*volume.PublishStatus{
|
|
|
|
{
|
|
|
|
NodeID: "nodeid1",
|
|
|
|
State: volume.StatePublished,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Info: &volume.Info{
|
|
|
|
CapacityBytes: 100000000,
|
|
|
|
VolumeID: "driver1vol3",
|
|
|
|
},
|
|
|
|
},
|
2022-09-29 11:21:51 -04:00
|
|
|
},
|
|
|
|
{
|
2021-02-23 09:23:53 -05:00
|
|
|
Name: "volume4",
|
|
|
|
Scope: "global",
|
|
|
|
Driver: "driver2",
|
|
|
|
ClusterVolume: &volume.ClusterVolume{
|
|
|
|
Spec: volume.ClusterVolumeSpec{
|
|
|
|
Group: "group2",
|
|
|
|
AccessMode: &volume.AccessMode{
|
|
|
|
Scope: volume.ScopeMultiNode,
|
|
|
|
Sharing: volume.SharingAll,
|
|
|
|
MountVolume: &volume.TypeMount{},
|
|
|
|
},
|
|
|
|
Availability: volume.AvailabilityActive,
|
|
|
|
},
|
|
|
|
PublishStatus: []*volume.PublishStatus{
|
|
|
|
{
|
|
|
|
NodeID: "nodeid1",
|
|
|
|
State: volume.StatePublished,
|
|
|
|
}, {
|
|
|
|
NodeID: "nodeid2",
|
|
|
|
State: volume.StatePublished,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Info: &volume.Info{
|
|
|
|
CapacityBytes: 100000000,
|
|
|
|
VolumeID: "driver1vol4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-23 08:51:01 -04:00
|
|
|
builders.Volume(builders.VolumeName("volume-local-1")),
|
2021-02-23 09:23:53 -05:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newListCommand(cli)
|
2023-10-23 08:51:01 -04:00
|
|
|
assert.Check(t, cmd.Flags().Set("cluster", "true"))
|
2021-02-23 09:23:53 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-cluster-volume-list.golden")
|
|
|
|
}
|