From 492b3c94cb49ab91ab19901072acc5d7326a46e9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 12 Jul 2025 16:40:28 +0200 Subject: [PATCH] integration-cli: fix flaky TestRestartStoppedContainer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 (cherry picked from commit a327a9f34187cfe5373da2212c27dbc1931dd83b) Signed-off-by: Paweł Gronowski --- integration-cli/docker_cli_restart_test.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/integration-cli/docker_cli_restart_test.go b/integration-cli/docker_cli_restart_test.go index bf5c5fbae0..8660a6b266 100644 --- a/integration-cli/docker_cli_restart_test.go +++ b/integration-cli/docker_cli_restart_test.go @@ -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) {