From f1c111b17618ae144af4e5b889cf2be56d8386e9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 21 Jun 2022 16:21:37 +0200 Subject: [PATCH] fix flaky TestRunContainerWithRmFlag tests (take 2) This is a new attempt on making these tests less flaky. The previous attempt in commit 585c147b7a78c02f04773ab611bb41268ed1aa3b assumed that the test was failing if the test-daemon still had unrelated containers present from other tests, but it appears that the actual reason for the tests to be flaky may be that the `--rm` option was moved to the daemon side and an asynchronous operation. As a result, the container may not yet be removed once the `docker run` completes, which happens frequently on Windows (likely be- cause removing containers is somewhat slower on Windows). This patch adds a retry-loop (using `poll.WaitOn()`) to wait for the container to be removed. make DOCKER_GRAPHDRIVER=vfs TEST_FILTER='TestRunContainerWithRmFlag' test-integration INFO: Testing against a local daemon === RUN TestDockerSuite === RUN TestDockerSuite/TestRunContainerWithRmFlagCannotStartContainer === RUN TestDockerSuite/TestRunContainerWithRmFlagExitCodeNotEqualToZero --- PASS: TestDockerSuite (1.00s) --- PASS: TestDockerSuite/TestRunContainerWithRmFlagCannotStartContainer (0.50s) --- PASS: TestDockerSuite/TestRunContainerWithRmFlagExitCodeNotEqualToZero (0.49s) Signed-off-by: Sebastiaan van Stijn --- integration-cli/docker_cli_run_test.go | 27 ++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 18397e8ed4..ff7db05b3c 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -34,6 +34,7 @@ import ( "github.com/moby/sys/mountinfo" "gotest.tools/v3/assert" "gotest.tools/v3/icmd" + "gotest.tools/v3/poll" "gotest.tools/v3/skip" ) @@ -2750,11 +2751,7 @@ func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *testin ExitCode: 1, }) - cli.Docker(cli.Args("container", "inspect", name)).Assert(c, icmd.Expected{ - ExitCode: 1, - Out: "[]\n", - Err: "o such container", // (N|n)o such container - }) + poll.WaitOn(c, containerRemoved(name)) } func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *testing.T) { @@ -2763,11 +2760,21 @@ func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *testing. ExitCode: 127, }) - cli.Docker(cli.Args("container", "inspect", name)).Assert(c, icmd.Expected{ - ExitCode: 1, - Out: "[]\n", - Err: "o such container", // (N|n)o such container - }) + poll.WaitOn(c, containerRemoved(name)) +} + +func containerRemoved(name string) poll.Check { + return func(l poll.LogT) poll.Result { + err := cli.Docker(cli.Args("container", "inspect", "--format='{{.ID}}'", name)).Compare(icmd.Expected{ + ExitCode: 1, + Out: "", + Err: "o such container", // (N|n)o such container + }) + if err != nil { + return poll.Continue("waiting for container '%s' to be removed", name) + } + return poll.Success() + } } func (s *DockerSuite) TestRunPIDHostWithChildIsKillable(c *testing.T) {