Skip to content

Commit

Permalink
Add tests for events --filter network=
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Albers <[email protected]>
  • Loading branch information
albers committed Oct 19, 2024
1 parent 3eaad53 commit 6baf2a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cli/command/system/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type fakeClient struct {
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
networkPruneFunc func(ctx context.Context, pruneFilter filters.Args) (network.PruneReport, error)
containerListFunc func(context.Context, container.ListOptions) ([]container.Summary, error)
networkListFunc func(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
}

func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
Expand Down Expand Up @@ -54,3 +55,10 @@ func (cli *fakeClient) ContainerList(ctx context.Context, options container.List
}
return []container.Summary{}, nil
}

func (cli *fakeClient) NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
if cli.networkListFunc != nil {
return cli.networkListFunc(ctx, options)
}
return []network.Summary{}, nil
}
40 changes: 37 additions & 3 deletions cli/command/system/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"testing"

"github.com/docker/docker/api/types/network"

"github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders"
"github.com/docker/docker/api/types/container"
Expand All @@ -18,15 +20,15 @@ func TestCompleteEventFilterContainer(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
return []container.Summary{
*builders.Container("foo"),
*builders.Container("bar"),
*builders.Container("c1"),
*builders.Container("c2"),
}, nil
},
})

completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=")

assert.DeepEqual(t, completions, []string{"container=foo", "container=bar"})
assert.DeepEqual(t, completions, []string{"container=c1", "container=c2"})
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
}

Expand All @@ -43,3 +45,35 @@ func TestCompleteEventFilterContainerAPIError(t *testing.T) {
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
},
})

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")
},
})

completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=")

assert.DeepEqual(t, completions, []string{})
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
}

0 comments on commit 6baf2a0

Please sign in to comment.