Skip to content

Commit

Permalink
Add tests for events --filter container=
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 4f7abb8 commit 3eaad53
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 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 @@ -19,6 +19,7 @@ type fakeClient struct {
eventsFn func(context.Context, events.ListOptions) (<-chan events.Message, <-chan error)
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)
}

func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
Expand Down Expand Up @@ -46,3 +47,10 @@ func (cli *fakeClient) NetworksPrune(ctx context.Context, pruneFilter filters.Ar
}
return network.PruneReport{}, nil
}

func (cli *fakeClient) ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) {
if cli.containerListFunc != nil {
return cli.containerListFunc(ctx, options)
}
return []container.Summary{}, nil
}
45 changes: 45 additions & 0 deletions cli/command/system/completion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package system

import (
"context"
"errors"
"testing"

"github.com/docker/cli/internal/test"
"github.com/docker/cli/internal/test/builders"
"github.com/docker/docker/api/types/container"
"github.com/spf13/cobra"
"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("foo"),
*builders.Container("bar"),
}, nil
},
})

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

assert.DeepEqual(t, completions, []string{"container=foo", "container=bar"})
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")
},
})

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

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

0 comments on commit 3eaad53

Please sign in to comment.