Skip to content

Commit

Permalink
Parametrized existing completion tests
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Albers <[email protected]>
  • Loading branch information
albers committed Oct 22, 2024
1 parent 3517b1b commit e03af58
Showing 1 changed file with 72 additions and 86 deletions.
158 changes: 72 additions & 86 deletions cli/command/system/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package system
import (
"context"
"errors"
"fmt"
"testing"

"github.com/docker/docker/api/types/system"
Expand All @@ -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))
}
}

0 comments on commit e03af58

Please sign in to comment.