From b68bf3afe44309e52f1bf993488d6b038586947e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 13 Oct 2024 18:02:35 +0200 Subject: [PATCH] completion: add test for NetworkNames Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 302d73f990f7ddba8ac5c61d84cd000dcf6c802c) Signed-off-by: Sebastiaan van Stijn --- cli/command/completion/functions_test.go | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/cli/command/completion/functions_test.go b/cli/command/completion/functions_test.go index dde30d2f66..fdf347450f 100644 --- a/cli/command/completion/functions_test.go +++ b/cli/command/completion/functions_test.go @@ -10,6 +10,7 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/image" + "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" "github.com/google/go-cmp/cmp/cmpopts" "github.com/spf13/cobra" @@ -31,6 +32,7 @@ type fakeClient struct { client.Client containerListFunc func(options container.ListOptions) ([]types.Container, error) imageListFunc func(options image.ListOptions) ([]image.Summary, error) + networkListFunc func(ctx context.Context, options network.ListOptions) ([]network.Summary, error) } func (c *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]types.Container, error) { @@ -47,6 +49,13 @@ func (c *fakeClient) ImageList(_ context.Context, options image.ListOptions) ([] return []image.Summary{}, nil } +func (c *fakeClient) NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error) { + if c.networkListFunc != nil { + return c.networkListFunc(ctx, options) + } + return []network.Inspect{}, nil +} + func TestCompleteContainerNames(t *testing.T) { tests := []struct { doc string @@ -228,6 +237,52 @@ func TestCompleteImageNames(t *testing.T) { } } +func TestCompleteNetworkNames(t *testing.T) { + tests := []struct { + doc string + networks []network.Summary + expOut []string + expDirective cobra.ShellCompDirective + }{ + { + doc: "no results", + expDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + doc: "with results", + networks: []network.Summary{ + {ID: "nw-c", Name: "network-c"}, + {ID: "nw-b", Name: "network-b"}, + {ID: "nw-a", Name: "network-a"}, + }, + expOut: []string{"network-c", "network-b", "network-a"}, + expDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + doc: "with error", + expDirective: cobra.ShellCompDirectiveError, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.doc, func(t *testing.T) { + comp := NetworkNames(fakeCLI{&fakeClient{ + networkListFunc: func(ctx context.Context, options network.ListOptions) ([]network.Summary, error) { + if tc.expDirective == cobra.ShellCompDirectiveError { + return nil, errors.New("some error occurred") + } + return tc.networks, nil + }, + }}) + + volumes, directives := comp(&cobra.Command{}, nil, "") + assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective)) + assert.Check(t, is.DeepEqual(volumes, tc.expOut)) + }) + } +} + func TestCompleteNoComplete(t *testing.T) { values, directives := NoComplete(nil, nil, "") assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))