github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/network/inspect_test.go (about) 1 package network // import "github.com/Prakhar-Agarwal-byte/moby/integration/network" 2 3 import ( 4 "testing" 5 6 "github.com/Prakhar-Agarwal-byte/moby/api/types" 7 "github.com/Prakhar-Agarwal-byte/moby/integration/internal/network" 8 "github.com/Prakhar-Agarwal-byte/moby/integration/internal/swarm" 9 "github.com/Prakhar-Agarwal-byte/moby/testutil" 10 "gotest.tools/v3/assert" 11 "gotest.tools/v3/poll" 12 "gotest.tools/v3/skip" 13 ) 14 15 func TestInspectNetwork(t *testing.T) { 16 skip.If(t, testEnv.DaemonInfo.OSType == "windows", "FIXME") 17 skip.If(t, testEnv.IsRootless, "rootless mode doesn't support Swarm-mode") 18 ctx := setupTest(t) 19 20 d := swarm.NewSwarm(ctx, t, testEnv) 21 defer d.Stop(t) 22 c := d.NewClientT(t) 23 defer c.Close() 24 25 networkName := "Overlay" + t.Name() 26 overlayID := network.CreateNoError(ctx, t, c, networkName, 27 network.WithDriver("overlay"), 28 ) 29 30 var instances uint64 = 2 31 serviceName := "TestService" + t.Name() 32 33 serviceID := swarm.CreateService(ctx, t, d, 34 swarm.ServiceWithReplicas(instances), 35 swarm.ServiceWithName(serviceName), 36 swarm.ServiceWithNetwork(networkName), 37 ) 38 39 poll.WaitOn(t, swarm.RunningTasksCount(ctx, c, serviceID, instances), swarm.ServicePoll) 40 41 tests := []struct { 42 name string 43 network string 44 opts types.NetworkInspectOptions 45 }{ 46 { 47 name: "full network id", 48 network: overlayID, 49 opts: types.NetworkInspectOptions{ 50 Verbose: true, 51 }, 52 }, 53 { 54 name: "partial network id", 55 network: overlayID[0:11], 56 opts: types.NetworkInspectOptions{ 57 Verbose: true, 58 }, 59 }, 60 { 61 name: "network name", 62 network: networkName, 63 opts: types.NetworkInspectOptions{ 64 Verbose: true, 65 }, 66 }, 67 { 68 name: "network name and swarm scope", 69 network: networkName, 70 opts: types.NetworkInspectOptions{ 71 Verbose: true, 72 Scope: "swarm", 73 }, 74 }, 75 } 76 for _, tc := range tests { 77 tc := tc 78 t.Run(tc.name, func(t *testing.T) { 79 ctx := testutil.StartSpan(ctx, t) 80 nw, err := c.NetworkInspect(ctx, tc.network, tc.opts) 81 assert.NilError(t, err) 82 83 if service, ok := nw.Services[serviceName]; ok { 84 assert.Equal(t, len(service.Tasks), int(instances)) 85 } 86 87 assert.Assert(t, nw.IPAM.Config != nil) 88 89 for _, cfg := range nw.IPAM.Config { 90 assert.Assert(t, cfg.Gateway != "") 91 assert.Assert(t, cfg.Subnet != "") 92 } 93 }) 94 } 95 96 // TODO find out why removing networks is needed; other tests fail if the network is not removed, even though they run on a new daemon. 97 err := c.ServiceRemove(ctx, serviceID) 98 assert.NilError(t, err) 99 poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll) 100 err = c.NetworkRemove(ctx, overlayID) 101 assert.NilError(t, err) 102 poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll) 103 }