github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration/network/inspect_test.go (about)

     1  package network // import "github.com/docker/docker/integration/network"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/integration/internal/network"
     9  	"github.com/docker/docker/integration/internal/swarm"
    10  	"gotest.tools/assert"
    11  	"gotest.tools/poll"
    12  	"gotest.tools/skip"
    13  )
    14  
    15  func TestInspectNetwork(t *testing.T) {
    16  	skip.If(t, testEnv.OSType == "windows", "FIXME")
    17  	defer setupTest(t)()
    18  	d := swarm.NewSwarm(t, testEnv)
    19  	defer d.Stop(t)
    20  	c := d.NewClientT(t)
    21  	defer c.Close()
    22  
    23  	networkName := "Overlay" + t.Name()
    24  	overlayID := network.CreateNoError(t, context.Background(), c, networkName,
    25  		network.WithDriver("overlay"),
    26  		network.WithCheckDuplicate(),
    27  	)
    28  
    29  	var instances uint64 = 2
    30  	serviceName := "TestService" + t.Name()
    31  
    32  	serviceID := swarm.CreateService(t, d,
    33  		swarm.ServiceWithReplicas(instances),
    34  		swarm.ServiceWithName(serviceName),
    35  		swarm.ServiceWithNetwork(networkName),
    36  	)
    37  
    38  	poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, instances), swarm.ServicePoll)
    39  
    40  	tests := []struct {
    41  		name    string
    42  		network string
    43  		opts    types.NetworkInspectOptions
    44  	}{
    45  		{
    46  			name:    "full network id",
    47  			network: overlayID,
    48  			opts: types.NetworkInspectOptions{
    49  				Verbose: true,
    50  			},
    51  		},
    52  		{
    53  			name:    "partial network id",
    54  			network: overlayID[0:11],
    55  			opts: types.NetworkInspectOptions{
    56  				Verbose: true,
    57  			},
    58  		},
    59  		{
    60  			name:    "network name",
    61  			network: networkName,
    62  			opts: types.NetworkInspectOptions{
    63  				Verbose: true,
    64  			},
    65  		},
    66  		{
    67  			name:    "network name and swarm scope",
    68  			network: networkName,
    69  			opts: types.NetworkInspectOptions{
    70  				Verbose: true,
    71  				Scope:   "swarm",
    72  			},
    73  		},
    74  	}
    75  	ctx := context.Background()
    76  	for _, tc := range tests {
    77  		tc := tc
    78  		t.Run(tc.name, func(t *testing.T) {
    79  			nw, err := c.NetworkInspect(ctx, tc.network, tc.opts)
    80  			assert.NilError(t, err)
    81  
    82  			if service, ok := nw.Services[serviceName]; ok {
    83  				assert.Equal(t, len(service.Tasks), int(instances))
    84  			}
    85  
    86  			assert.Assert(t, nw.IPAM.Config != nil)
    87  
    88  			for _, cfg := range nw.IPAM.Config {
    89  				assert.Assert(t, cfg.Gateway != "")
    90  				assert.Assert(t, cfg.Subnet != "")
    91  			}
    92  		})
    93  	}
    94  
    95  	// 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.
    96  	err := c.ServiceRemove(ctx, serviceID)
    97  	assert.NilError(t, err)
    98  	poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll)
    99  	err = c.NetworkRemove(ctx, overlayID)
   100  	assert.NilError(t, err)
   101  	poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll)
   102  }