From e03af58525abd63baa23f322f3cb775294127324 Mon Sep 17 00:00:00 2001 From: Harald Albers Date: Tue, 22 Oct 2024 07:15:57 +0000 Subject: [PATCH] Parametrized existing completion tests Signed-off-by: Harald Albers --- cli/command/system/completion_test.go | 158 ++++++++++++-------------- 1 file changed, 72 insertions(+), 86 deletions(-) diff --git a/cli/command/system/completion_test.go b/cli/command/system/completion_test.go index 8da28f97cb3a..73af79221ba6 100644 --- a/cli/command/system/completion_test.go +++ b/cli/command/system/completion_test.go @@ -3,6 +3,7 @@ package system import ( "context" "errors" + "fmt" "testing" "github.com/docker/docker/api/types/system" @@ -16,98 +17,83 @@ import ( "gotest.tools/v3/assert" ) -// Successful completion lists all container names, prefixed with "container=". -// Filtering the completions by the current word is delegated to the completion script. -func TestCompleteEventFilterContainer(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { - return []container.Summary{ - *builders.Container("c1"), - *builders.Container("c2"), - }, nil +func TestCompleteEventFilter(t *testing.T) { + tests := []struct { + client *fakeClient + toComplete string + expected []string + }{ + { + client: &fakeClient{ + containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { + return []container.Summary{ + *builders.Container("c1"), + *builders.Container("c2"), + }, nil + }, + }, + toComplete: "container=", + expected: []string{"container=c1", "container=c2"}, }, - }) - - completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=") - - assert.DeepEqual(t, completions, []string{"container=c1", "container=c2"}) - assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) -} - -// In case of API errors, no completions are returned. -func TestCompleteEventFilterContainerAPIError(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { - return nil, errors.New("API error") + { + client: &fakeClient{ + containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { + return nil, errors.New("API error") + }, + }, + toComplete: "container=", + expected: []string{}, }, - }) - - completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=") - - assert.DeepEqual(t, completions, []string{}) - assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) -} - -// Successful completion lists the name and id of the current Docker daemon, prefixed with "daemon=". -// Filtering the completions by the current word is delegated to the completion script. -func TestCompleteEventFilterDaemon(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - infoFunc: func(ctx context.Context) (system.Info, error) { - return system.Info{ - ID: "daemon-id", - Name: "daemon-name", - }, nil + { + client: &fakeClient{ + infoFunc: func(ctx context.Context) (system.Info, error) { + return system.Info{ + ID: "daemon-id", + Name: "daemon-name", + }, nil + }, + }, + toComplete: "daemon=", + expected: []string{"daemon=daemon-name", "daemon=daemon-id"}, }, - }) - - completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "daemon=") - - assert.DeepEqual(t, completions, []string{"daemon=daemon-name", "daemon=daemon-id"}) - assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) -} - -// In case of API errors, no completions are returned. -func TestCompleteEventFilterDaemonAPIError(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - infoFunc: func(ctx context.Context) (system.Info, error) { - return system.Info{}, errors.New("API error") + { + client: &fakeClient{ + infoFunc: func(ctx context.Context) (system.Info, error) { + return system.Info{}, errors.New("API error") + }, + }, + toComplete: "daemon=", + expected: []string{}, }, - }) - - completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "daemon=") - - assert.DeepEqual(t, completions, []string{}) - assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) -} - -// Successful completion lists all network names, prefixed with "network=". -// Filtering the completions by the current word is delegated to the completion script. -func TestCompleteEventFilterNetwork(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) { - return []network.Summary{ - *builders.NetworkResource(builders.NetworkResourceName("nw1")), - *builders.NetworkResource(builders.NetworkResourceName("nw2")), - }, nil + { + client: &fakeClient{ + networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) { + return []network.Summary{ + *builders.NetworkResource(builders.NetworkResourceName("nw1")), + *builders.NetworkResource(builders.NetworkResourceName("nw2")), + }, nil + }, + }, + toComplete: "network=", + expected: []string{"network=nw1", "network=nw2"}, }, - }) - - completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=") - - assert.DeepEqual(t, completions, []string{"network=nw1", "network=nw2"}) - assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) -} - -// In case of API errors, no completions are returned. -func TestCompleteEventFilterNetworkAPIError(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) { - return nil, errors.New("API error") + { + client: &fakeClient{ + networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) { + return nil, errors.New("API error") + }, + }, + toComplete: "network=", + expected: []string{}, }, - }) + } + + for _, tc := range tests { + cli := test.NewFakeCli(tc.client) - completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=") + completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, tc.toComplete) - assert.DeepEqual(t, completions, []string{}) - assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) + assert.DeepEqual(t, completions, tc.expected) + assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete)) + } }