Skip to content

Commit e1c472a

Browse files
committed
completion: add test for VolumeNames
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 302d73f commit e1c472a

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

cli/command/completion/functions_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/docker/docker/api/types/filters"
1111
"github.com/docker/docker/api/types/image"
1212
"github.com/docker/docker/api/types/network"
13+
"github.com/docker/docker/api/types/volume"
1314
"github.com/docker/docker/client"
1415
"github.com/google/go-cmp/cmp/cmpopts"
1516
"github.com/spf13/cobra"
@@ -32,6 +33,7 @@ type fakeClient struct {
3233
containerListFunc func(options container.ListOptions) ([]container.Summary, error)
3334
imageListFunc func(options image.ListOptions) ([]image.Summary, error)
3435
networkListFunc func(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
36+
volumeListFunc func(filter filters.Args) (volume.ListResponse, error)
3537
}
3638

3739
func (c *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]container.Summary, error) {
@@ -55,6 +57,13 @@ func (c *fakeClient) NetworkList(ctx context.Context, options network.ListOption
5557
return []network.Inspect{}, nil
5658
}
5759

60+
func (c *fakeClient) VolumeList(_ context.Context, options volume.ListOptions) (volume.ListResponse, error) {
61+
if c.volumeListFunc != nil {
62+
return c.volumeListFunc(options.Filters)
63+
}
64+
return volume.ListResponse{}, nil
65+
}
66+
5867
func TestCompleteContainerNames(t *testing.T) {
5968
tests := []struct {
6069
doc string
@@ -293,3 +302,49 @@ func TestCompletePlatforms(t *testing.T) {
293302
assert.Check(t, is.Equal(directives&cobra.ShellCompDirectiveNoFileComp, cobra.ShellCompDirectiveNoFileComp), "Should not perform file completion")
294303
assert.Check(t, is.DeepEqual(values, commonPlatforms))
295304
}
305+
306+
func TestCompleteVolumeNames(t *testing.T) {
307+
tests := []struct {
308+
doc string
309+
volumes []*volume.Volume
310+
expOut []string
311+
expDirective cobra.ShellCompDirective
312+
}{
313+
{
314+
doc: "no results",
315+
expDirective: cobra.ShellCompDirectiveNoFileComp,
316+
},
317+
{
318+
doc: "with results",
319+
volumes: []*volume.Volume{
320+
{Name: "volume-c"},
321+
{Name: "volume-b"},
322+
{Name: "volume-a"},
323+
},
324+
expOut: []string{"volume-c", "volume-b", "volume-a"},
325+
expDirective: cobra.ShellCompDirectiveNoFileComp,
326+
},
327+
{
328+
doc: "with error",
329+
expDirective: cobra.ShellCompDirectiveError,
330+
},
331+
}
332+
333+
for _, tc := range tests {
334+
tc := tc
335+
t.Run(tc.doc, func(t *testing.T) {
336+
comp := VolumeNames(fakeCLI{&fakeClient{
337+
volumeListFunc: func(filter filters.Args) (volume.ListResponse, error) {
338+
if tc.expDirective == cobra.ShellCompDirectiveError {
339+
return volume.ListResponse{}, errors.New("some error occurred")
340+
}
341+
return volume.ListResponse{Volumes: tc.volumes}, nil
342+
},
343+
}})
344+
345+
volumes, directives := comp(&cobra.Command{}, nil, "")
346+
assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective))
347+
assert.Check(t, is.DeepEqual(volumes, tc.expOut))
348+
})
349+
}
350+
}

0 commit comments

Comments
 (0)