github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/network/inspect_test.go (about)

     1  package network // import "github.com/demonoid81/moby/integration/network"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/demonoid81/moby/api/types"
     8  	"github.com/demonoid81/moby/integration/internal/network"
     9  	"github.com/demonoid81/moby/integration/internal/swarm"
    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.OSType == "windows", "FIXME")
    17  	skip.If(t, testEnv.IsRootless, "rootless mode doesn't support Swarm-mode")
    18  	defer setupTest(t)()
    19  	d := swarm.NewSwarm(t, testEnv)
    20  	defer d.Stop(t)
    21  	c := d.NewClientT(t)
    22  	defer c.Close()
    23  
    24  	networkName := "Overlay" + t.Name()
    25  	overlayID := network.CreateNoError(context.Background(), t, c, networkName,
    26  		network.WithDriver("overlay"),
    27  		network.WithCheckDuplicate(),
    28  	)
    29  
    30  	var instances uint64 = 2
    31  	serviceName := "TestService" + t.Name()
    32  
    33  	serviceID := swarm.CreateService(t, d,
    34  		swarm.ServiceWithReplicas(instances),
    35  		swarm.ServiceWithName(serviceName),
    36  		swarm.ServiceWithNetwork(networkName),
    37  	)
    38  
    39  	poll.WaitOn(t, swarm.RunningTasksCount(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  	ctx := context.Background()
    77  	for _, tc := range tests {
    78  		tc := tc
    79  		t.Run(tc.name, func(t *testing.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  }