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"
|
2019-04-02 11:20:21 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
|
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 {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
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{
|
2017-02-27 12:39:35 -05:00
|
|
|
Volume(),
|
|
|
|
Volume(VolumeName("foo"), VolumeDriver("bar")),
|
|
|
|
Volume(VolumeName("baz"), VolumeLabels(map[string]string{
|
|
|
|
"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{
|
2017-02-27 12:39:35 -05:00
|
|
|
Volume(),
|
|
|
|
Volume(VolumeName("foo"), VolumeDriver("bar")),
|
|
|
|
Volume(VolumeName("baz"), VolumeLabels(map[string]string{
|
|
|
|
"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{
|
2017-02-27 12:39:35 -05:00
|
|
|
Volume(),
|
|
|
|
Volume(VolumeName("foo"), VolumeDriver("bar")),
|
|
|
|
Volume(VolumeName("baz"), VolumeLabels(map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2017-08-16 13:50:28 -04:00
|
|
|
})
|
2017-02-27 12:39:35 -05:00
|
|
|
cmd := newListCommand(cli)
|
|
|
|
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{
|
2018-08-08 11:57:39 -04:00
|
|
|
Volume(VolumeName("volume-2-foo")),
|
|
|
|
Volume(VolumeName("volume-10-foo")),
|
|
|
|
Volume(VolumeName("volume-1-foo")),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.Flags().Set("format", "{{ .Name }}")
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Volume(VolumeName("volume-local-1")),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.Flags().Set("cluster", "true")
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "volume-cluster-volume-list.golden")
|
|
|
|
}
|