|
1 | 1 | package completion |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "errors" |
4 | 6 | "sort" |
5 | 7 | "testing" |
6 | 8 |
|
| 9 | + "github.com/docker/docker/api/types/container" |
| 10 | + "github.com/docker/docker/api/types/filters" |
| 11 | + "github.com/docker/docker/client" |
| 12 | + "github.com/google/go-cmp/cmp/cmpopts" |
7 | 13 | "github.com/spf13/cobra" |
8 | 14 | "gotest.tools/v3/assert" |
9 | 15 | is "gotest.tools/v3/assert/cmp" |
10 | 16 | "gotest.tools/v3/env" |
11 | 17 | ) |
12 | 18 |
|
| 19 | +type fakeCLI struct { |
| 20 | + *fakeClient |
| 21 | +} |
| 22 | + |
| 23 | +// Client implements [APIClientProvider]. |
| 24 | +func (c fakeCLI) Client() client.APIClient { |
| 25 | + return c.fakeClient |
| 26 | +} |
| 27 | + |
| 28 | +type fakeClient struct { |
| 29 | + client.Client |
| 30 | + containerListFunc func(options container.ListOptions) ([]container.Summary, error) |
| 31 | +} |
| 32 | + |
| 33 | +func (c *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]container.Summary, error) { |
| 34 | + if c.containerListFunc != nil { |
| 35 | + return c.containerListFunc(options) |
| 36 | + } |
| 37 | + return []container.Summary{}, nil |
| 38 | +} |
| 39 | + |
| 40 | +func TestCompleteContainerNames(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + doc string |
| 43 | + showAll, showIDs bool |
| 44 | + filters []func(container.Summary) bool |
| 45 | + containers []container.Summary |
| 46 | + expOut []string |
| 47 | + expOpts container.ListOptions |
| 48 | + expDirective cobra.ShellCompDirective |
| 49 | + }{ |
| 50 | + { |
| 51 | + doc: "no results", |
| 52 | + expDirective: cobra.ShellCompDirectiveNoFileComp, |
| 53 | + }, |
| 54 | + { |
| 55 | + doc: "all containers", |
| 56 | + showAll: true, |
| 57 | + containers: []container.Summary{ |
| 58 | + {ID: "id-c", State: "running", Names: []string{"/container-c", "/container-c/link-b"}}, |
| 59 | + {ID: "id-b", State: "created", Names: []string{"/container-b"}}, |
| 60 | + {ID: "id-a", State: "exited", Names: []string{"/container-a"}}, |
| 61 | + }, |
| 62 | + expOut: []string{"container-c", "container-c/link-b", "container-b", "container-a"}, |
| 63 | + expOpts: container.ListOptions{All: true}, |
| 64 | + expDirective: cobra.ShellCompDirectiveNoFileComp, |
| 65 | + }, |
| 66 | + { |
| 67 | + doc: "all containers with ids", |
| 68 | + showAll: true, |
| 69 | + showIDs: true, |
| 70 | + containers: []container.Summary{ |
| 71 | + {ID: "id-c", State: "running", Names: []string{"/container-c", "/container-c/link-b"}}, |
| 72 | + {ID: "id-b", State: "created", Names: []string{"/container-b"}}, |
| 73 | + {ID: "id-a", State: "exited", Names: []string{"/container-a"}}, |
| 74 | + }, |
| 75 | + expOut: []string{"id-c", "container-c", "container-c/link-b", "id-b", "container-b", "id-a", "container-a"}, |
| 76 | + expOpts: container.ListOptions{All: true}, |
| 77 | + expDirective: cobra.ShellCompDirectiveNoFileComp, |
| 78 | + }, |
| 79 | + { |
| 80 | + doc: "only running containers", |
| 81 | + showAll: false, |
| 82 | + containers: []container.Summary{ |
| 83 | + {ID: "id-c", State: "running", Names: []string{"/container-c", "/container-c/link-b"}}, |
| 84 | + }, |
| 85 | + expOut: []string{"container-c", "container-c/link-b"}, |
| 86 | + expDirective: cobra.ShellCompDirectiveNoFileComp, |
| 87 | + }, |
| 88 | + { |
| 89 | + doc: "with filter", |
| 90 | + showAll: true, |
| 91 | + filters: []func(container.Summary) bool{ |
| 92 | + func(container container.Summary) bool { return container.State == "created" }, |
| 93 | + }, |
| 94 | + containers: []container.Summary{ |
| 95 | + {ID: "id-c", State: "running", Names: []string{"/container-c", "/container-c/link-b"}}, |
| 96 | + {ID: "id-b", State: "created", Names: []string{"/container-b"}}, |
| 97 | + {ID: "id-a", State: "exited", Names: []string{"/container-a"}}, |
| 98 | + }, |
| 99 | + expOut: []string{"container-b"}, |
| 100 | + expOpts: container.ListOptions{All: true}, |
| 101 | + expDirective: cobra.ShellCompDirectiveNoFileComp, |
| 102 | + }, |
| 103 | + { |
| 104 | + doc: "multiple filters", |
| 105 | + showAll: true, |
| 106 | + filters: []func(container.Summary) bool{ |
| 107 | + func(container container.Summary) bool { return container.ID == "id-a" }, |
| 108 | + func(container container.Summary) bool { return container.State == "created" }, |
| 109 | + }, |
| 110 | + containers: []container.Summary{ |
| 111 | + {ID: "id-c", State: "running", Names: []string{"/container-c", "/container-c/link-b"}}, |
| 112 | + {ID: "id-b", State: "created", Names: []string{"/container-b"}}, |
| 113 | + {ID: "id-a", State: "created", Names: []string{"/container-a"}}, |
| 114 | + }, |
| 115 | + expOut: []string{"container-a"}, |
| 116 | + expOpts: container.ListOptions{All: true}, |
| 117 | + expDirective: cobra.ShellCompDirectiveNoFileComp, |
| 118 | + }, |
| 119 | + { |
| 120 | + doc: "with error", |
| 121 | + expDirective: cobra.ShellCompDirectiveError, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + for _, tc := range tests { |
| 126 | + tc := tc |
| 127 | + t.Run(tc.doc, func(t *testing.T) { |
| 128 | + if tc.showIDs { |
| 129 | + t.Setenv("DOCKER_COMPLETION_SHOW_CONTAINER_IDS", "yes") |
| 130 | + } |
| 131 | + comp := ContainerNames(fakeCLI{&fakeClient{ |
| 132 | + containerListFunc: func(opts container.ListOptions) ([]container.Summary, error) { |
| 133 | + assert.Check(t, is.DeepEqual(opts, tc.expOpts, cmpopts.IgnoreUnexported(container.ListOptions{}, filters.Args{}))) |
| 134 | + if tc.expDirective == cobra.ShellCompDirectiveError { |
| 135 | + return nil, errors.New("some error occurred") |
| 136 | + } |
| 137 | + return tc.containers, nil |
| 138 | + }, |
| 139 | + }}, tc.showAll, tc.filters...) |
| 140 | + |
| 141 | + containers, directives := comp(&cobra.Command{}, nil, "") |
| 142 | + assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective)) |
| 143 | + assert.Check(t, is.DeepEqual(containers, tc.expOut)) |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
| 147 | + |
13 | 148 | func TestCompleteEnvVarNames(t *testing.T) { |
14 | 149 | env.PatchAll(t, map[string]string{ |
15 | 150 | "ENV_A": "hello-a", |
|
0 commit comments