@@ -28,6 +28,7 @@ import (
2828 "github.com/containerd/nerdctl/mod/tigron/expect"
2929 "github.com/containerd/nerdctl/mod/tigron/require"
3030 "github.com/containerd/nerdctl/mod/tigron/test"
31+ "github.com/containerd/nerdctl/mod/tigron/tig"
3132
3233 "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3334 "github.com/containerd/nerdctl/v2/pkg/testutil"
@@ -323,33 +324,55 @@ func TestNetworkInspect(t *testing.T) {
323324 {
324325 Description : "Display containers belonging to multiple networks in the output of nerdctl network inspect" ,
325326 Setup : func (data test.Data , helpers test.Helpers ) {
326- helpers .Ensure ("network" , "create" , data .Identifier ("nginx- network-1" ))
327- helpers .Ensure ("network" , "create" , data .Identifier ("nginx- network-2" ))
327+ helpers .Ensure ("network" , "create" , data .Identifier ("network-1" ))
328+ helpers .Ensure ("network" , "create" , data .Identifier ("network-2" ))
328329
329- helpers .Ensure ("run" , "-d" , "--name" , data .Identifier (), "--network" , data .Identifier ("nginx- network-1" ), "--network" , data .Identifier ("nginx- network-2" ), testutil .NginxAlpineImage )
330+ containerID := helpers .Capture ("run" , "-d" , "--name" , data .Identifier (), "--network" , data .Identifier ("network-1" ), "--network" , data .Identifier ("network-2" ), testutil .CommonImage , "sleep" , nerdtest . Infinity )
330331
331- data .Labels ().Set ("containerID" , strings .Trim (helpers . Capture ( "inspect" , data . Identifier (), "--format" , "{{.Id}}" ) , "\n " ))
332+ data .Labels ().Set ("containerID" , strings .Trim (containerID , "\n " ))
332333 },
333334 Cleanup : func (data test.Data , helpers test.Helpers ) {
334335 helpers .Anyhow ("rm" , "-f" , data .Identifier ())
335- helpers .Anyhow ("network" , "remove" , data .Identifier ("nginx- network-1" ))
336- helpers .Anyhow ("network" , "remove" , data .Identifier ("nginx- network-2" ))
336+ helpers .Anyhow ("network" , "remove" , data .Identifier ("network-1" ))
337+ helpers .Anyhow ("network" , "remove" , data .Identifier ("network-2" ))
337338 },
338339 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
339- return helpers .Command ("network" , "inspect" , data .Identifier ("nginx- network-1" ))
340+ return helpers .Command ("network" , "inspect" , data .Identifier ("network-1" ))
340341 },
341342 Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
342343 return & test.Expected {
343- Output : func (stdout string , info string , t * testing.T ) {
344- var dc []dockercompat.Network
345- err := json .Unmarshal ([]byte (stdout ), & dc )
346-
347- assert .NilError (t , err , "Unable to unmarshal output\n " + info )
344+ Output : expect .JSON ([]dockercompat.Network {}, func (dc []dockercompat.Network , info string , t tig.T ) {
348345 assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
349- assert .Equal (t , dc [0 ].Name , data .Identifier ("nginx- network-1" ))
346+ assert .Equal (t , dc [0 ].Name , data .Identifier ("network-1" ))
350347 assert .Equal (t , 1 , len (dc [0 ].Containers ), "Expected a single container as per configuration, but got multiple." )
351348 assert .Equal (t , data .Identifier (), dc [0 ].Containers [data .Labels ().Get ("containerID" )].Name )
352- },
349+ }),
350+ }
351+ },
352+ },
353+ {
354+ Description : "Display only containers attached to the specific network" ,
355+ Setup : func (data test.Data , helpers test.Helpers ) {
356+ helpers .Ensure ("network" , "create" , data .Identifier ("some-network" ))
357+ helpers .Ensure ("network" , "create" , data .Identifier ("some-network-as-well" ))
358+
359+ helpers .Ensure ("run" , "-d" , "--name" , data .Identifier (), "--network" , data .Identifier ("some-network-as-well" ), testutil .CommonImage , "sleep" , nerdtest .Infinity )
360+ },
361+ Cleanup : func (data test.Data , helpers test.Helpers ) {
362+ helpers .Anyhow ("rm" , "-f" , data .Identifier ())
363+ helpers .Anyhow ("network" , "remove" , data .Identifier ("some-network" ))
364+ helpers .Anyhow ("network" , "remove" , data .Identifier ("some-network-as-well" ))
365+ },
366+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
367+ return helpers .Command ("network" , "inspect" , data .Identifier ("some-network" ))
368+ },
369+ Expected : func (data test.Data , helpers test.Helpers ) * test.Expected {
370+ return & test.Expected {
371+ Output : expect .JSON ([]dockercompat.Network {}, func (dc []dockercompat.Network , info string , t tig.T ) {
372+ assert .Equal (t , 1 , len (dc ), "Unexpectedly got multiple results\n " + info )
373+ assert .Equal (t , dc [0 ].Name , data .Identifier ("some-network" ))
374+ assert .Equal (t , 0 , len (dc [0 ].Containers ), "Expected no containers as per configuration, but got multiple." )
375+ }),
353376 }
354377 },
355378 },
0 commit comments