Browse code

integration-cli: fix flaky TestRestartStoppedContainer

This test was failing frequently on Windows, waiting for the state
of the container to be "running" after restarting, however, this
would race because the command of the container was very short-lived;

=== Failed
=== FAIL: github.com/docker/docker/integration-cli TestDockerCLIRestartSuite/TestRestartStoppedContainer (37.00s)
docker_cli_restart_test.go:42: assertion failed: error is not nil: condition ""true" == "false"" not true in time (20s)

Ironically, that check was added in 48ccdd46aea4bb16925d0a333f792a712f1c11dc
to make the test less flaky.

This patch takes the approach from TestRestartRunningContainer, which had
similar issues on Windows that were addressed in bae22d167cd29016541a6d4f93d38f2608d8e51f

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/07/12 23:40:28
Showing 1 changed files
... ...
@@ -29,20 +29,22 @@ func (s *DockerCLIRestartSuite) OnTimeout(t *testing.T) {
29 29
 }
30 30
 
31 31
 func (s *DockerCLIRestartSuite) TestRestartStoppedContainer(c *testing.T) {
32
-	cli.DockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
33
-	cID := getIDByName(c, "test")
32
+	cID := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && exit 0").Stdout()
33
+	cID = strings.TrimSpace(cID)
34 34
 
35
-	out := cli.DockerCmd(c, "logs", cID).Combined()
36
-	assert.Equal(c, out, "foobar\n")
35
+	getLogs := func(t *testing.T) (interface{}, string) {
36
+		out := cli.DockerCmd(t, "logs", cID).Combined()
37
+		return out, ""
38
+	}
37 39
 
38
-	cli.DockerCmd(c, "restart", cID)
40
+	// Wait 10 seconds for the 'echo' to appear in the logs
41
+	poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\n")), poll.WithTimeout(10*time.Second))
39 42
 
40
-	// Wait until the container has stopped
41
-	err := waitInspect(cID, "{{.State.Running}}", "false", 20*time.Second)
42
-	assert.NilError(c, err)
43
+	// Make sure the container has stopped before we restart it.
44
+	cli.WaitExited(c, cID, 20*time.Second)
45
+	cli.DockerCmd(c, "restart", cID)
43 46
 
44
-	out = cli.DockerCmd(c, "logs", cID).Combined()
45
-	assert.Equal(c, out, "foobar\nfoobar\n")
47
+	poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\nfoobar\n")), poll.WithTimeout(10*time.Second))
46 48
 }
47 49
 
48 50
 func (s *DockerCLIRestartSuite) TestRestartRunningContainer(c *testing.T) {