99 "github.com/docker/docker/api/types/container"
1010 "github.com/docker/docker/api/types/filters"
1111 "github.com/docker/docker/api/types/image"
12+ "github.com/docker/docker/api/types/network"
1213 "github.com/docker/docker/client"
1314 "github.com/google/go-cmp/cmp/cmpopts"
1415 "github.com/spf13/cobra"
@@ -30,6 +31,7 @@ type fakeClient struct {
3031 client.Client
3132 containerListFunc func (options container.ListOptions ) ([]container.Summary , error )
3233 imageListFunc func (options image.ListOptions ) ([]image.Summary , error )
34+ networkListFunc func (ctx context.Context , options network.ListOptions ) ([]network.Summary , error )
3335}
3436
3537func (c * fakeClient ) ContainerList (_ context.Context , options container.ListOptions ) ([]container.Summary , error ) {
@@ -46,6 +48,13 @@ func (c *fakeClient) ImageList(_ context.Context, options image.ListOptions) ([]
4648 return []image.Summary {}, nil
4749}
4850
51+ func (c * fakeClient ) NetworkList (ctx context.Context , options network.ListOptions ) ([]network.Summary , error ) {
52+ if c .networkListFunc != nil {
53+ return c .networkListFunc (ctx , options )
54+ }
55+ return []network.Inspect {}, nil
56+ }
57+
4958func TestCompleteContainerNames (t * testing.T ) {
5059 tests := []struct {
5160 doc string
@@ -227,6 +236,52 @@ func TestCompleteImageNames(t *testing.T) {
227236 }
228237}
229238
239+ func TestCompleteNetworkNames (t * testing.T ) {
240+ tests := []struct {
241+ doc string
242+ networks []network.Summary
243+ expOut []string
244+ expDirective cobra.ShellCompDirective
245+ }{
246+ {
247+ doc : "no results" ,
248+ expDirective : cobra .ShellCompDirectiveNoFileComp ,
249+ },
250+ {
251+ doc : "with results" ,
252+ networks : []network.Summary {
253+ {ID : "nw-c" , Name : "network-c" },
254+ {ID : "nw-b" , Name : "network-b" },
255+ {ID : "nw-a" , Name : "network-a" },
256+ },
257+ expOut : []string {"network-c" , "network-b" , "network-a" },
258+ expDirective : cobra .ShellCompDirectiveNoFileComp ,
259+ },
260+ {
261+ doc : "with error" ,
262+ expDirective : cobra .ShellCompDirectiveError ,
263+ },
264+ }
265+
266+ for _ , tc := range tests {
267+ tc := tc
268+ t .Run (tc .doc , func (t * testing.T ) {
269+ comp := NetworkNames (fakeCLI {& fakeClient {
270+ networkListFunc : func (ctx context.Context , options network.ListOptions ) ([]network.Summary , error ) {
271+ if tc .expDirective == cobra .ShellCompDirectiveError {
272+ return nil , errors .New ("some error occurred" )
273+ }
274+ return tc .networks , nil
275+ },
276+ }})
277+
278+ volumes , directives := comp (& cobra.Command {}, nil , "" )
279+ assert .Check (t , is .Equal (directives & tc .expDirective , tc .expDirective ))
280+ assert .Check (t , is .DeepEqual (volumes , tc .expOut ))
281+ })
282+ }
283+ }
284+
230285func TestCompleteNoComplete (t * testing.T ) {
231286 values , directives := NoComplete (nil , nil , "" )
232287 assert .Check (t , is .Equal (directives , cobra .ShellCompDirectiveNoFileComp ))
0 commit comments