Compare commits

..

2 Commits

Author SHA1 Message Date
Harald Albers e27c083013
Merge 6baf2a0347 into 062eecf14a 2024-10-20 00:56:24 +02:00
Harald Albers 6baf2a0347 Add tests for `events --filter network=`
Signed-off-by: Harald Albers <github@albersweb.de>
2024-10-19 22:48:52 +00:00
2 changed files with 45 additions and 3 deletions

View File

@ -20,6 +20,7 @@ type fakeClient struct {
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error) containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
networkPruneFunc func(ctx context.Context, pruneFilter filters.Args) (network.PruneReport, error) networkPruneFunc func(ctx context.Context, pruneFilter filters.Args) (network.PruneReport, error)
containerListFunc func(context.Context, container.ListOptions) ([]container.Summary, 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) { func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
@ -54,3 +55,10 @@ func (cli *fakeClient) ContainerList(ctx context.Context, options container.List
} }
return []container.Summary{}, nil 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
}

View File

@ -5,6 +5,8 @@ import (
"errors" "errors"
"testing" "testing"
"github.com/docker/docker/api/types/network"
"github.com/docker/cli/internal/test" "github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders" "github.com/docker/cli/internal/test/builders"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
@ -18,15 +20,15 @@ func TestCompleteEventFilterContainer(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) { containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
return []container.Summary{ return []container.Summary{
*builders.Container("foo"), *builders.Container("c1"),
*builders.Container("bar"), *builders.Container("c2"),
}, nil }, nil
}, },
}) })
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=") 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) assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
} }
@ -43,3 +45,35 @@ func TestCompleteEventFilterContainerAPIError(t *testing.T) {
assert.DeepEqual(t, completions, []string{}) assert.DeepEqual(t, completions, []string{})
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp) 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)
}