Add TestGatewayErrorOnNetDisconnect

Signed-off-by: Rob Murray <rob.murray@docker.com>
This commit is contained in:
Rob Murray
2025-11-25 18:11:54 +00:00
parent 8f2aa3e0f5
commit 163cc95aea

View File

@@ -2090,3 +2090,52 @@ func TestSetIPWithNoConfiguredSubnet(t *testing.T) {
assert.Check(t, is.Contains(res.Stdout.String(), ip6))
}
}
// Regression test for https://github.com/moby/moby/issues/51578
func TestGatewayErrorOnNetDisconnect(t *testing.T) {
ctx := setupTest(t)
d := daemon.New(t)
d.StartWithBusybox(ctx, t)
defer d.Stop(t)
c := d.NewClientT(t)
network.CreateNoError(ctx, t, c, "n1")
defer network.RemoveNoError(ctx, t, c, "n1")
network.CreateNoError(ctx, t, c, "n2")
defer network.RemoveNoError(ctx, t, c, "n2")
// Run a container attached to both networks, with n1 providing the default gateway
// and n2's interface named "eth2".
ctrID := container.Run(ctx, t, c,
container.WithEndpointSettings("n1", &networktypes.EndpointSettings{GwPriority: 1}),
container.WithEndpointSettings("n2", &networktypes.EndpointSettings{DriverOpts: map[string]string{
netlabel.Ifname: "eth2",
}}),
container.WithCapability("NET_ADMIN"),
)
defer container.Remove(ctx, t, c, ctrID, client.ContainerRemoveOptions{Force: true})
// Break n2 so it can't be used as a gateway (there will be no route).
execRes := container.ExecT(ctx, t, c, ctrID, []string{"ip", "link", "set", "eth2", "down"})
assert.Assert(t, is.Equal(execRes.ExitCode, 0))
// Disconnect n1, n2 will be selected as the gateway and its config will fail.
// The error is only logged and the disconnect proceeds.
_, err := c.NetworkDisconnect(ctx, "n1", client.NetworkDisconnectOptions{Container: ctrID})
assert.Check(t, err)
// Check n1 can be reconnected.
_, err = c.NetworkConnect(ctx, "n1", client.NetworkConnectOptions{Container: ctrID})
assert.Check(t, err)
// Check the container can be restarted.
timeout := 0
_, err = c.ContainerRestart(ctx, ctrID, client.ContainerRestartOptions{Timeout: &timeout})
assert.Check(t, err)
// Both networks should be attached.
ctrInsp := container.Inspect(ctx, t, c, ctrID)
assert.Check(t, is.Len(ctrInsp.NetworkSettings.Networks, 2))
assert.Check(t, is.Contains(ctrInsp.NetworkSettings.Networks, "n1"))
assert.Check(t, is.Contains(ctrInsp.NetworkSettings.Networks, "n2"))
}