Skip to content

Commit 302d73f

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

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
@@ -9,6 +9,7 @@ import (
99
"github.com/docker/docker/api/types/container"
1010
"github.com/docker/docker/api/types/filters"
1111
"github.com/docker/docker/api/types/image"
12+
"github.com/docker/docker/api/types/network"
1213
"github.com/docker/docker/client"
1314
"github.com/google/go-cmp/cmp/cmpopts"
1415
"github.com/spf13/cobra"
@@ -30,6 +31,7 @@ type fakeClient struct {
3031
client.Client
3132
containerListFunc func(options container.ListOptions) ([]container.Summary, error)
3233
imageListFunc func(options image.ListOptions) ([]image.Summary, error)
34+
networkListFunc func(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
3335
}
3436

3537
func (c *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]container.Summary, error) {
@@ -46,6 +48,13 @@ func (c *fakeClient) ImageList(_ context.Context, options image.ListOptions) ([]
4648
return []image.Summary{}, nil
4749
}
4850

51+
func (c *fakeClient) NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
52+
if c.networkListFunc != nil {
53+
return c.networkListFunc(ctx, options)
54+
}
55+
return []network.Inspect{}, nil
56+
}
57+
4958
func TestCompleteContainerNames(t *testing.T) {
5059
tests := []struct {
5160
doc string
@@ -227,6 +236,52 @@ func TestCompleteImageNames(t *testing.T) {
227236
}
228237
}
229238

239+
func TestCompleteNetworkNames(t *testing.T) {
240+
tests := []struct {
241+
doc string
242+
networks []network.Summary
243+
expOut []string
244+
expDirective cobra.ShellCompDirective
245+
}{
246+
{
247+
doc: "no results",
248+
expDirective: cobra.ShellCompDirectiveNoFileComp,
249+
},
250+
{
251+
doc: "with results",
252+
networks: []network.Summary{
253+
{ID: "nw-c", Name: "network-c"},
254+
{ID: "nw-b", Name: "network-b"},
255+
{ID: "nw-a", Name: "network-a"},
256+
},
257+
expOut: []string{"network-c", "network-b", "network-a"},
258+
expDirective: cobra.ShellCompDirectiveNoFileComp,
259+
},
260+
{
261+
doc: "with error",
262+
expDirective: cobra.ShellCompDirectiveError,
263+
},
264+
}
265+
266+
for _, tc := range tests {
267+
tc := tc
268+
t.Run(tc.doc, func(t *testing.T) {
269+
comp := NetworkNames(fakeCLI{&fakeClient{
270+
networkListFunc: func(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
271+
if tc.expDirective == cobra.ShellCompDirectiveError {
272+
return nil, errors.New("some error occurred")
273+
}
274+
return tc.networks, nil
275+
},
276+
}})
277+
278+
volumes, directives := comp(&cobra.Command{}, nil, "")
279+
assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective))
280+
assert.Check(t, is.DeepEqual(volumes, tc.expOut))
281+
})
282+
}
283+
}
284+
230285
func TestCompleteNoComplete(t *testing.T) {
231286
values, directives := NoComplete(nil, nil, "")
232287
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))

0 commit comments

Comments
 (0)