@@ -19,6 +19,7 @@ package network
1919import (
2020 "encoding/json"
2121 "errors"
22+ "strings"
2223 "testing"
2324
2425 "gotest.tools/v3/assert"
@@ -38,6 +39,128 @@ func TestNetworkInspect(t *testing.T) {
3839 )
3940
4041 testGroup := & test.Group {
42+ {
43+ Description : "non existent network" ,
44+ Command : test .RunCommand ("network" , "inspect" , "nonexistent" ),
45+ // FIXME: where is this error even comin from?
46+ Expected : test .Expects (1 , []error {errors .New ("no network found matching" )}, nil ),
47+ },
48+ {
49+ Description : "invalid name network" ,
50+ Command : test .RunCommand ("network" , "inspect" , "∞" ),
51+ // FIXME: this is not even a valid identifier
52+ Expected : test .Expects (1 , []error {errors .New ("no network found matching" )}, nil ),
53+ },
54+ {
55+ Description : "none" ,
56+ Require : nerdtest .NerdctlNeedsFixing ,
57+ Command : test .RunCommand ("network" , "inspect" , "none" ),
58+ Expected : test .Expects (0 , nil , func (stdout string , info string , t * testing.T ) {
59+ var dc []dockercompat.Network
60+ err := json .Unmarshal ([]byte (stdout ), & dc )
61+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
62+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
63+ assert .Equal (t , dc [0 ].Name , "none" )
64+ }),
65+ },
66+ {
67+ Description : "host" ,
68+ Require : nerdtest .NerdctlNeedsFixing ,
69+ Command : test .RunCommand ("network" , "inspect" , "host" ),
70+ Expected : test .Expects (0 , nil , func (stdout string , info string , t * testing.T ) {
71+ var dc []dockercompat.Network
72+ err := json .Unmarshal ([]byte (stdout ), & dc )
73+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
74+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
75+ assert .Equal (t , dc [0 ].Name , "host" )
76+ }),
77+ },
78+ {
79+ Description : "bridge" ,
80+ Require : test .Not (test .Windows ),
81+ Command : test .RunCommand ("network" , "inspect" , "bridge" ),
82+ Expected : test .Expects (0 , nil , func (stdout string , info string , t * testing.T ) {
83+ var dc []dockercompat.Network
84+ err := json .Unmarshal ([]byte (stdout ), & dc )
85+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
86+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
87+ assert .Equal (t , dc [0 ].Name , "bridge" )
88+ }),
89+ },
90+ {
91+ Description : "custom" ,
92+ Setup : func (data test.Data , helpers test.Helpers ) {
93+ helpers .Ensure ("network" , "create" , "custom" )
94+ },
95+ Cleanup : func (data test.Data , helpers test.Helpers ) {
96+ helpers .Anyhow ("network" , "remove" , "custom" )
97+ },
98+ Command : test .RunCommand ("network" , "inspect" , "custom" ),
99+ Expected : test .Expects (0 , nil , func (stdout string , info string , t * testing.T ) {
100+ var dc []dockercompat.Network
101+ err := json .Unmarshal ([]byte (stdout ), & dc )
102+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
103+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
104+ assert .Equal (t , dc [0 ].Name , "custom" )
105+ }),
106+ },
107+ {
108+ Description : "match exact id" ,
109+ Require : test .Not (test .Windows ),
110+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
111+ id := strings .TrimSpace (helpers .Capture ("network" , "inspect" , "bridge" , "--format" , "{{ .Id }}" ))
112+ return helpers .Command ("network" , "inspect" , id )
113+ },
114+ Expected : test .Expects (0 , nil , func (stdout string , info string , t * testing.T ) {
115+ var dc []dockercompat.Network
116+ err := json .Unmarshal ([]byte (stdout ), & dc )
117+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
118+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
119+ assert .Equal (t , dc [0 ].Name , "bridge" )
120+ }),
121+ },
122+ {
123+ Description : "match part of id" ,
124+ Require : test .Not (test .Windows ),
125+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
126+ id := strings .TrimSpace (helpers .Capture ("network" , "inspect" , "bridge" , "--format" , "{{ .Id }}" ))
127+ return helpers .Command ("network" , "inspect" , id [0 :25 ])
128+ },
129+ Expected : test .Expects (0 , nil , func (stdout string , info string , t * testing.T ) {
130+ var dc []dockercompat.Network
131+ err := json .Unmarshal ([]byte (stdout ), & dc )
132+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
133+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
134+ assert .Equal (t , dc [0 ].Name , "bridge" )
135+ }),
136+ },
137+ {
138+ Description : "using another net short id" ,
139+ Require : test .Not (test .Windows ),
140+ Setup : func (data test.Data , helpers test.Helpers ) {
141+ id := strings .TrimSpace (helpers .Capture ("network" , "inspect" , "bridge" , "--format" , "{{ .Id }}" ))
142+ helpers .Ensure ("network" , "create" , id [0 :12 ])
143+ data .Set ("netname" , id [0 :12 ])
144+ },
145+ Cleanup : func (data test.Data , helpers test.Helpers ) {
146+ id := strings .TrimSpace (helpers .Capture ("network" , "inspect" , "bridge" , "--format" , "{{ .Id }}" ))
147+ helpers .Anyhow ("network" , "remove" , id [0 :12 ])
148+ },
149+ Command : func (data test.Data , helpers test.Helpers ) test.Command {
150+ return helpers .Command ("network" , "inspect" , data .Get ("netname" ))
151+ },
152+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
153+ return & test.Expected {
154+ Output : func (stdout string , info string , t * testing.T ) {
155+ var dc []dockercompat.Network
156+ err := json .Unmarshal ([]byte (stdout ), & dc )
157+ assert .NilError (t , err , "Unable to unmarshal output\n " + info )
158+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
159+ assert .Equal (t , dc [0 ].Name , data .Get ("netname" ))
160+ },
161+ }
162+ },
163+ },
41164 {
42165 Description : "Test network inspect" ,
43166 // IPAMConfig is not implemented on Windows yet
@@ -88,16 +211,16 @@ func TestNetworkInspect(t *testing.T) {
88211 return & test.Expected {
89212 ExitCode : 0 ,
90213 Output : func (stdout string , info string , t * testing.T ) {
91- cmd := helpers .Command (). Clear (). WithBinary ( "nerdctl" ). WithArgs ( "--namespace" , data .Identifier ())
214+ cmd := helpers .CustomCommand ( "nerdctl" , "--namespace" , data .Identifier ())
92215
93216 cmd .Clone ().WithArgs ("network" , "inspect" , data .Identifier ()).Run (& test.Expected {
94217 ExitCode : 1 ,
95- Errors : []error {errors .New ("no such network" )},
218+ Errors : []error {errors .New ("no network found " )},
96219 })
97220
98221 cmd .Clone ().WithArgs ("network" , "remove" , data .Identifier ()).Run (& test.Expected {
99222 ExitCode : 1 ,
100- Errors : []error {errors .New ("no such network" )},
223+ Errors : []error {errors .New ("no network found " )},
101224 })
102225
103226 cmd .Clone ().WithArgs ("network" , "ls" ).Run (& test.Expected {
0 commit comments