From 0a58c73e0da38e6114c44e0b1f4d0964190f6680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Thu, 6 Mar 2025 15:46:47 +0100 Subject: [PATCH] integration/net: Retry TestAccessPublishedPortFromAnotherNetwork MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow each test case to be retried up to 5 times. Signed-off-by: Paweł Gronowski --- .../networking/port_mapping_linux_test.go | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/integration/networking/port_mapping_linux_test.go b/integration/networking/port_mapping_linux_test.go index 399c22b76a..6675a6aef1 100644 --- a/integration/networking/port_mapping_linux_test.go +++ b/integration/networking/port_mapping_linux_test.go @@ -859,26 +859,43 @@ func TestAccessPublishedPortFromAnotherNetwork(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - serverID := container.Run(ctx, t, c, - container.WithName("server"), - container.WithCmd("nc", "-lp", "5000"), - container.WithExposedPorts("5000/tcp"), - container.WithPortMap(nat.PortMap{"5000/tcp": {{HostPort: "5000"}}}), - container.WithNetworkMode(servnet)) - defer c.ContainerRemove(ctx, serverID, containertypes.RemoveOptions{Force: true}) + // TODO: Figure out why is it flaky and fix the actual issue. + // https://github.com/moby/moby/issues/49358 + retryFlaky(t, 5, func(t *testing.T) is.Comparison { + serverID := container.Run(ctx, t, c, + container.WithName("server"), + container.WithCmd("nc", "-lp", "5000"), + container.WithExposedPorts("5000/tcp"), + container.WithPortMap(nat.PortMap{"5000/tcp": {{HostPort: "5000"}}}), + container.WithNetworkMode(servnet)) + defer c.ContainerRemove(ctx, serverID, containertypes.RemoveOptions{Force: true}) - clientID := container.Run(ctx, t, c, - container.WithName("client"), - container.WithCmd("/bin/sh", "-c", fmt.Sprintf("echo foobar | nc -w1 %s 5000", tc.daddr)), - container.WithNetworkMode(clientnet)) - defer c.ContainerRemove(ctx, clientID, containertypes.RemoveOptions{Force: true}) + clientID := container.Run(ctx, t, c, + container.WithName("client"), + container.WithCmd("/bin/sh", "-c", fmt.Sprintf("echo foobar | nc -w1 %s 5000", tc.daddr)), + container.WithNetworkMode(clientnet)) + defer c.ContainerRemove(ctx, clientID, containertypes.RemoveOptions{Force: true}) - logs := getContainerStdout(t, ctx, c, serverID) - assert.Assert(t, is.Contains(logs, "foobar"), "Payload was not received by the server container") + logs := getContainerStdout(t, ctx, c, serverID) + return is.Contains(logs, "foobar") + }) }) } } +func retryFlaky(t *testing.T, retries int, f func(t *testing.T) is.Comparison) { + for i := 0; i < retries-1; i++ { + comp := f(t) + if comp().Success() { + return + } + t.Log("Retrying...") + time.Sleep(time.Second) + } + + assert.Assert(t, f(t)) +} + // TestDirectRemoteAccessOnExposedPort checks that remote hosts can't directly // reach a container on one of its exposed port. That is, if container has the // IP address 172.17.24.2, and its port 443 is exposed on the host, no remote