mac_addr_test.go 2.6 KB

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