|
9 | 9 | "testing" |
10 | 10 |
|
11 | 11 | containertypes "github.com/moby/moby/api/types/container" |
| 12 | + networktypes "github.com/moby/moby/api/types/network" |
12 | 13 | "github.com/moby/moby/client" |
13 | 14 | "github.com/moby/moby/v2/daemon/libnetwork/drivers/bridge" |
14 | 15 | "github.com/moby/moby/v2/integration/internal/container" |
@@ -305,6 +306,54 @@ func TestWatchtowerCreate(t *testing.T) { |
305 | 306 | assert.Check(t, is.Equal(netSettings.MacAddress.String(), ctrMAC)) |
306 | 307 | } |
307 | 308 |
|
| 309 | +// TestNetworkConnectWithMACAddress tests that a MAC address can be specified |
| 310 | +// when connecting a container to a network using NetworkConnect. |
| 311 | +// This is a feature request from https://github.com/moby/moby/issues/51796 |
| 312 | +func TestNetworkConnectWithMACAddress(t *testing.T) { |
| 313 | + skip.If(t, testEnv.DaemonInfo.OSType == "windows") |
| 314 | + |
| 315 | + ctx := setupTest(t) |
| 316 | + |
| 317 | + d := daemon.New(t) |
| 318 | + d.StartWithBusybox(ctx, t) |
| 319 | + defer d.Stop(t) |
| 320 | + |
| 321 | + c := d.NewClientT(t) |
| 322 | + defer c.Close() |
| 323 | + |
| 324 | + const netName = "testmacconnect" |
| 325 | + network.CreateNoError(ctx, t, c, netName, |
| 326 | + network.WithDriver("bridge"), |
| 327 | + network.WithOption(bridge.BridgeName, netName)) |
| 328 | + defer network.RemoveNoError(ctx, t, c, netName) |
| 329 | + |
| 330 | + const ctrName = "ctr1" |
| 331 | + id := container.Run(ctx, t, c, |
| 332 | + container.WithName(ctrName), |
| 333 | + container.WithImage("busybox:latest"), |
| 334 | + container.WithCmd("top")) |
| 335 | + defer c.ContainerRemove(ctx, id, client.ContainerRemoveOptions{Force: true}) |
| 336 | + |
| 337 | + const wantMAC = "02:42:ac:11:00:42" |
| 338 | + _, err := c.NetworkConnect(ctx, netName, client.NetworkConnectOptions{ |
| 339 | + Container: ctrName, |
| 340 | + EndpointConfig: &networktypes.EndpointSettings{ |
| 341 | + MacAddress: networktypes.HardwareAddr{0x02, 0x42, 0xac, 0x11, 0x00, 0x42}, |
| 342 | + }, |
| 343 | + }) |
| 344 | + assert.NilError(t, err) |
| 345 | + |
| 346 | + // Verify via container inspect |
| 347 | + inspect := container.Inspect(ctx, t, c, ctrName) |
| 348 | + gotMAC := inspect.NetworkSettings.Networks[netName].MacAddress |
| 349 | + assert.Check(t, is.Equal(wantMAC, gotMAC.String())) |
| 350 | + |
| 351 | + // Verify the MAC address is actually applied to the interface inside the container |
| 352 | + res := container.ExecT(ctx, t, c, id, []string{"cat", "/sys/class/net/eth1/address"}) |
| 353 | + res.AssertSuccess(t) |
| 354 | + assert.Check(t, is.Equal(wantMAC+"\n", res.Stdout())) |
| 355 | +} |
| 356 | + |
308 | 357 | type legacyCreateRequest struct { |
309 | 358 | containertypes.CreateRequest |
310 | 359 | // Mac Address of the container. |
|
0 commit comments