Skip to content

Commit b650f81

Browse files
committed
Add tests for completions that call the API
Signed-off-by: Harald Albers <github@albersweb.de>
1 parent 4b9db5b commit b650f81

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

cli/command/system/client_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ package system
33
import (
44
"context"
55

6+
"github.com/docker/docker/api/types/swarm"
7+
"github.com/docker/docker/api/types/volume"
8+
9+
"github.com/docker/docker/api/types/image"
10+
11+
"github.com/docker/docker/api/types/system"
12+
613
"github.com/docker/docker/api/types"
714
"github.com/docker/docker/api/types/container"
815
"github.com/docker/docker/api/types/events"
@@ -19,6 +26,12 @@ type fakeClient struct {
1926
eventsFn func(context.Context, events.ListOptions) (<-chan events.Message, <-chan error)
2027
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (container.PruneReport, error)
2128
networkPruneFunc func(ctx context.Context, pruneFilter filters.Args) (network.PruneReport, error)
29+
containerListFunc func(context.Context, container.ListOptions) ([]container.Summary, error)
30+
infoFunc func(ctx context.Context) (system.Info, error)
31+
networkListFunc func(ctx context.Context, options network.ListOptions) ([]network.Summary, error)
32+
imageListFunc func(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
33+
nodeListFunc func(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
34+
volumeListFunc func(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error)
2235
}
2336

2437
func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) {
@@ -46,3 +59,45 @@ func (cli *fakeClient) NetworksPrune(ctx context.Context, pruneFilter filters.Ar
4659
}
4760
return network.PruneReport{}, nil
4861
}
62+
63+
func (cli *fakeClient) ContainerList(ctx context.Context, options container.ListOptions) ([]container.Summary, error) {
64+
if cli.containerListFunc != nil {
65+
return cli.containerListFunc(ctx, options)
66+
}
67+
return []container.Summary{}, nil
68+
}
69+
70+
func (cli *fakeClient) ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error) {
71+
if cli.imageListFunc != nil {
72+
return cli.imageListFunc(ctx, options)
73+
}
74+
return []image.Summary{}, nil
75+
}
76+
77+
func (cli *fakeClient) Info(ctx context.Context) (system.Info, error) {
78+
if cli.infoFunc != nil {
79+
return cli.infoFunc(ctx)
80+
}
81+
return system.Info{}, nil
82+
}
83+
84+
func (cli *fakeClient) NetworkList(ctx context.Context, options network.ListOptions) ([]network.Summary, error) {
85+
if cli.networkListFunc != nil {
86+
return cli.networkListFunc(ctx, options)
87+
}
88+
return []network.Summary{}, nil
89+
}
90+
91+
func (cli *fakeClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
92+
if cli.nodeListFunc != nil {
93+
return cli.nodeListFunc(ctx, options)
94+
}
95+
return []swarm.Node{}, nil
96+
}
97+
98+
func (cli *fakeClient) VolumeList(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error) {
99+
if cli.volumeListFunc != nil {
100+
return cli.volumeListFunc(ctx, options)
101+
}
102+
return volume.ListResponse{}, nil
103+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package system
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"testing"
8+
9+
"github.com/docker/cli/internal/test"
10+
"github.com/docker/cli/internal/test/builders"
11+
"github.com/docker/docker/api/types"
12+
"github.com/docker/docker/api/types/container"
13+
"github.com/docker/docker/api/types/image"
14+
"github.com/docker/docker/api/types/network"
15+
"github.com/docker/docker/api/types/swarm"
16+
"github.com/docker/docker/api/types/system"
17+
"github.com/docker/docker/api/types/volume"
18+
"github.com/spf13/cobra"
19+
"gotest.tools/v3/assert"
20+
)
21+
22+
func TestCompleteEventFilter(t *testing.T) {
23+
tests := []struct {
24+
client *fakeClient
25+
toComplete string
26+
expected []string
27+
}{
28+
{
29+
client: &fakeClient{
30+
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
31+
return []container.Summary{
32+
*builders.Container("c1"),
33+
*builders.Container("c2"),
34+
}, nil
35+
},
36+
},
37+
toComplete: "container=",
38+
expected: []string{"container=c1", "container=c2"},
39+
},
40+
{
41+
client: &fakeClient{
42+
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
43+
return nil, errors.New("API error")
44+
},
45+
},
46+
toComplete: "container=",
47+
expected: []string{},
48+
},
49+
{
50+
client: &fakeClient{
51+
infoFunc: func(ctx context.Context) (system.Info, error) {
52+
return system.Info{
53+
ID: "daemon-id",
54+
Name: "daemon-name",
55+
}, nil
56+
},
57+
},
58+
toComplete: "daemon=",
59+
expected: []string{"daemon=daemon-name", "daemon=daemon-id"},
60+
},
61+
{
62+
client: &fakeClient{
63+
infoFunc: func(ctx context.Context) (system.Info, error) {
64+
return system.Info{}, errors.New("API error")
65+
},
66+
},
67+
toComplete: "daemon=",
68+
expected: []string{},
69+
},
70+
{
71+
client: &fakeClient{
72+
imageListFunc: func(_ context.Context, _ image.ListOptions) ([]image.Summary, error) {
73+
return []image.Summary{
74+
{RepoTags: []string{"img:1"}},
75+
{RepoTags: []string{"img:2"}},
76+
}, nil
77+
},
78+
},
79+
toComplete: "image=",
80+
expected: []string{"image=img:1", "image=img:2"},
81+
},
82+
{
83+
client: &fakeClient{
84+
imageListFunc: func(_ context.Context, _ image.ListOptions) ([]image.Summary, error) {
85+
return []image.Summary{}, errors.New("API error")
86+
},
87+
},
88+
toComplete: "image=",
89+
expected: []string{},
90+
},
91+
{
92+
client: &fakeClient{
93+
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
94+
return []network.Summary{
95+
*builders.NetworkResource(builders.NetworkResourceName("nw1")),
96+
*builders.NetworkResource(builders.NetworkResourceName("nw2")),
97+
}, nil
98+
},
99+
},
100+
toComplete: "network=",
101+
expected: []string{"network=nw1", "network=nw2"},
102+
},
103+
{
104+
client: &fakeClient{
105+
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
106+
return nil, errors.New("API error")
107+
},
108+
},
109+
toComplete: "network=",
110+
expected: []string{},
111+
},
112+
{
113+
client: &fakeClient{
114+
nodeListFunc: func(_ context.Context, _ types.NodeListOptions) ([]swarm.Node, error) {
115+
return []swarm.Node{
116+
*builders.Node(builders.Hostname("n1")),
117+
}, nil
118+
},
119+
},
120+
toComplete: "node=",
121+
expected: []string{"node=n1"},
122+
},
123+
{
124+
client: &fakeClient{
125+
nodeListFunc: func(_ context.Context, _ types.NodeListOptions) ([]swarm.Node, error) {
126+
return []swarm.Node{}, errors.New("API error")
127+
},
128+
},
129+
toComplete: "node=",
130+
expected: []string{},
131+
},
132+
{
133+
client: &fakeClient{
134+
volumeListFunc: func(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error) {
135+
return volume.ListResponse{
136+
Volumes: []*volume.Volume{
137+
builders.Volume(builders.VolumeName("v1")),
138+
builders.Volume(builders.VolumeName("v2")),
139+
},
140+
}, nil
141+
},
142+
},
143+
toComplete: "volume=",
144+
expected: []string{"volume=v1", "volume=v2"},
145+
},
146+
{
147+
client: &fakeClient{
148+
volumeListFunc: func(ctx context.Context, options volume.ListOptions) (volume.ListResponse, error) {
149+
return volume.ListResponse{}, errors.New("API error")
150+
},
151+
},
152+
toComplete: "volume=",
153+
expected: []string{},
154+
},
155+
}
156+
157+
for _, tc := range tests {
158+
cli := test.NewFakeCli(tc.client)
159+
160+
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, tc.toComplete)
161+
162+
assert.DeepEqual(t, completions, tc.expected)
163+
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete))
164+
}
165+
}

0 commit comments

Comments
 (0)