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 48ccdd46ae
to make the test less flaky.

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

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit a327a9f341)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Sebastiaan van Stijn
2025-07-12 16:40:28 +02:00
committed by Paweł Gronowski
parent 40ba2f33d1
commit 492b3c94cb

View File

@@ -29,20 +29,22 @@ func (s *DockerCLIRestartSuite) OnTimeout(t *testing.T) {
}
func (s *DockerCLIRestartSuite) TestRestartStoppedContainer(c *testing.T) {
cli.DockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
cID := getIDByName(c, "test")
cID := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && exit 0").Stdout()
cID = strings.TrimSpace(cID)
out := cli.DockerCmd(c, "logs", cID).Combined()
assert.Equal(c, out, "foobar\n")
getLogs := func(t *testing.T) (interface{}, string) {
out := cli.DockerCmd(t, "logs", cID).Combined()
return out, ""
}
// Wait 10 seconds for the 'echo' to appear in the logs
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\n")), poll.WithTimeout(10*time.Second))
// Make sure the container has stopped before we restart it.
cli.WaitExited(c, cID, 20*time.Second)
cli.DockerCmd(c, "restart", cID)
// Wait until the container has stopped
err := waitInspect(cID, "{{.State.Running}}", "false", 20*time.Second)
assert.NilError(c, err)
out = cli.DockerCmd(c, "logs", cID).Combined()
assert.Equal(c, out, "foobar\nfoobar\n")
poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\nfoobar\n")), poll.WithTimeout(10*time.Second))
}
func (s *DockerCLIRestartSuite) TestRestartRunningContainer(c *testing.T) {