github.com/rish1988/moby@v25.0.2+incompatible/integration/networking/mac_addr_test.go (about)

     1  package networking
     2  
     3  import (
     4  	"testing"
     5  
     6  	containertypes "github.com/docker/docker/api/types/container"
     7  	"github.com/docker/docker/integration/internal/container"
     8  	"github.com/docker/docker/integration/internal/network"
     9  	"github.com/docker/docker/testutil/daemon"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/skip"
    13  )
    14  
    15  // TestMACAddrOnRestart is a regression test for https://github.com/moby/moby/issues/47146
    16  //   - Start a container, let it use a generated MAC address.
    17  //   - Stop that container.
    18  //   - Start a second container, it'll also use a generated MAC address.
    19  //     (It's likely to recycle the first container's MAC address.)
    20  //   - Restart the first container.
    21  //     (The bug was that it kept its original MAC address, now already in-use.)
    22  //   - Check that the two containers have different MAC addresses.
    23  func TestMACAddrOnRestart(t *testing.T) {
    24  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    25  
    26  	ctx := setupTest(t)
    27  
    28  	d := daemon.New(t)
    29  	d.StartWithBusybox(ctx, t)
    30  	defer d.Stop(t)
    31  
    32  	c := d.NewClientT(t)
    33  	defer c.Close()
    34  
    35  	const netName = "testmacaddrs"
    36  	network.CreateNoError(ctx, t, c, netName,
    37  		network.WithDriver("bridge"),
    38  		network.WithOption("com.docker.network.bridge.name", netName))
    39  	defer network.RemoveNoError(ctx, t, c, netName)
    40  
    41  	const ctr1Name = "ctr1"
    42  	id1 := container.Run(ctx, t, c,
    43  		container.WithName(ctr1Name),
    44  		container.WithImage("busybox:latest"),
    45  		container.WithCmd("top"),
    46  		container.WithNetworkMode(netName))
    47  	defer c.ContainerRemove(ctx, id1, containertypes.RemoveOptions{
    48  		Force: true,
    49  	})
    50  	err := c.ContainerStop(ctx, ctr1Name, containertypes.StopOptions{})
    51  	assert.Assert(t, is.Nil(err))
    52  
    53  	// Start a second container, giving the daemon a chance to recycle the first container's
    54  	// IP and MAC addresses.
    55  	const ctr2Name = "ctr2"
    56  	id2 := container.Run(ctx, t, c,
    57  		container.WithName(ctr2Name),
    58  		container.WithImage("busybox:latest"),
    59  		container.WithCmd("top"),
    60  		container.WithNetworkMode(netName))
    61  	defer c.ContainerRemove(ctx, id2, containertypes.RemoveOptions{
    62  		Force: true,
    63  	})
    64  
    65  	// Restart the first container.
    66  	err = c.ContainerStart(ctx, ctr1Name, containertypes.StartOptions{})
    67  	assert.Assert(t, is.Nil(err))
    68  
    69  	// Check that the containers ended up with different MAC addresses.
    70  
    71  	ctr1Inspect := container.Inspect(ctx, t, c, ctr1Name)
    72  	ctr1MAC := ctr1Inspect.NetworkSettings.Networks[netName].MacAddress
    73  
    74  	ctr2Inspect := container.Inspect(ctx, t, c, ctr2Name)
    75  	ctr2MAC := ctr2Inspect.NetworkSettings.Networks[netName].MacAddress
    76  
    77  	assert.Check(t, ctr1MAC != ctr2MAC,
    78  		"expected containers to have different MAC addresses; got %q for both", ctr1MAC)
    79  }