github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/service/network_test.go (about)

     1  package service // import "github.com/docker/docker/integration/service"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/api/types/network"
     9  	"github.com/docker/docker/integration/internal/container"
    10  	net "github.com/docker/docker/integration/internal/network"
    11  	"github.com/docker/docker/integration/internal/swarm"
    12  	"gotest.tools/v3/assert"
    13  	is "gotest.tools/v3/assert/cmp"
    14  	"gotest.tools/v3/skip"
    15  )
    16  
    17  func TestDockerNetworkConnectAlias(t *testing.T) {
    18  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    19  	defer setupTest(t)()
    20  	d := swarm.NewSwarm(t, testEnv)
    21  	defer d.Stop(t)
    22  	client := d.NewClientT(t)
    23  	defer client.Close()
    24  	ctx := context.Background()
    25  
    26  	name := t.Name() + "test-alias"
    27  	net.CreateNoError(ctx, t, client, name,
    28  		net.WithDriver("overlay"),
    29  		net.WithAttachable(),
    30  	)
    31  
    32  	cID1 := container.Create(ctx, t, client, func(c *container.TestContainerConfig) {
    33  		c.NetworkingConfig = &network.NetworkingConfig{
    34  			EndpointsConfig: map[string]*network.EndpointSettings{
    35  				name: {},
    36  			},
    37  		}
    38  	})
    39  
    40  	err := client.NetworkConnect(ctx, name, cID1, &network.EndpointSettings{
    41  		Aliases: []string{
    42  			"aaa",
    43  		},
    44  	})
    45  	assert.NilError(t, err)
    46  
    47  	err = client.ContainerStart(ctx, cID1, types.ContainerStartOptions{})
    48  	assert.NilError(t, err)
    49  
    50  	ng1, err := client.ContainerInspect(ctx, cID1)
    51  	assert.NilError(t, err)
    52  	assert.Check(t, is.Equal(len(ng1.NetworkSettings.Networks[name].Aliases), 2))
    53  	assert.Check(t, is.Equal(ng1.NetworkSettings.Networks[name].Aliases[0], "aaa"))
    54  
    55  	cID2 := container.Create(ctx, t, client, func(c *container.TestContainerConfig) {
    56  		c.NetworkingConfig = &network.NetworkingConfig{
    57  			EndpointsConfig: map[string]*network.EndpointSettings{
    58  				name: {},
    59  			},
    60  		}
    61  	})
    62  
    63  	err = client.NetworkConnect(ctx, name, cID2, &network.EndpointSettings{
    64  		Aliases: []string{
    65  			"bbb",
    66  		},
    67  	})
    68  	assert.NilError(t, err)
    69  
    70  	err = client.ContainerStart(ctx, cID2, types.ContainerStartOptions{})
    71  	assert.NilError(t, err)
    72  
    73  	ng2, err := client.ContainerInspect(ctx, cID2)
    74  	assert.NilError(t, err)
    75  	assert.Check(t, is.Equal(len(ng2.NetworkSettings.Networks[name].Aliases), 2))
    76  	assert.Check(t, is.Equal(ng2.NetworkSettings.Networks[name].Aliases[0], "bbb"))
    77  }
    78  
    79  func TestDockerNetworkReConnect(t *testing.T) {
    80  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    81  	defer setupTest(t)()
    82  	d := swarm.NewSwarm(t, testEnv)
    83  	defer d.Stop(t)
    84  	client := d.NewClientT(t)
    85  	defer client.Close()
    86  	ctx := context.Background()
    87  
    88  	name := t.Name() + "dummyNet"
    89  	net.CreateNoError(ctx, t, client, name,
    90  		net.WithDriver("overlay"),
    91  		net.WithAttachable(),
    92  	)
    93  
    94  	c1 := container.Create(ctx, t, client, func(c *container.TestContainerConfig) {
    95  		c.NetworkingConfig = &network.NetworkingConfig{
    96  			EndpointsConfig: map[string]*network.EndpointSettings{
    97  				name: {},
    98  			},
    99  		}
   100  	})
   101  
   102  	err := client.NetworkConnect(ctx, name, c1, &network.EndpointSettings{})
   103  	assert.NilError(t, err)
   104  
   105  	err = client.ContainerStart(ctx, c1, types.ContainerStartOptions{})
   106  	assert.NilError(t, err)
   107  
   108  	n1, err := client.ContainerInspect(ctx, c1)
   109  	assert.NilError(t, err)
   110  
   111  	err = client.NetworkConnect(ctx, name, c1, &network.EndpointSettings{})
   112  	assert.ErrorContains(t, err, "is already attached to network")
   113  
   114  	n2, err := client.ContainerInspect(ctx, c1)
   115  	assert.NilError(t, err)
   116  	assert.Check(t, is.DeepEqual(n1, n2))
   117  }