From 4d951615bab833e45705102da2e9b51670924d2f Mon Sep 17 00:00:00 2001 From: Christopher Jones Date: Wed, 9 Nov 2016 16:43:08 -0600 Subject: [PATCH] [integration-cli] fix race condition in kill tests Fixes a race condition in the kill tests where the container would be killed but inspected before it's state changed. Fixes this by waiting for a state change instead. Signed-off-by: Christopher Jones Upstream-commit: ff42a1ab5348944c6d675575b00959b87b2c4032 Component: engine --- .../engine/integration-cli/docker_cli_kill_test.go | 12 ++++++++++-- components/engine/integration-cli/docker_utils.go | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/components/engine/integration-cli/docker_cli_kill_test.go b/components/engine/integration-cli/docker_cli_kill_test.go index da09433f31..623df00418 100644 --- a/components/engine/integration-cli/docker_cli_kill_test.go +++ b/components/engine/integration-cli/docker_cli_kill_test.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "strings" + "time" "github.com/docker/docker/pkg/integration/checker" "github.com/go-check/check" @@ -15,6 +16,7 @@ func (s *DockerSuite) TestKillContainer(c *check.C) { c.Assert(waitRun(cleanedContainerID), check.IsNil) dockerCmd(c, "kill", cleanedContainerID) + c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil) out, _ = dockerCmd(c, "ps", "-q") c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running")) @@ -26,6 +28,7 @@ func (s *DockerSuite) TestKillOffStoppedContainer(c *check.C) { cleanedContainerID := strings.TrimSpace(out) dockerCmd(c, "stop", cleanedContainerID) + c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil) _, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID) c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", cleanedContainerID)) @@ -39,6 +42,7 @@ func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) { c.Assert(waitRun(cleanedContainerID), check.IsNil) dockerCmd(c, "kill", cleanedContainerID) + c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil) out, _ = dockerCmd(c, "ps", "-q") c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running")) @@ -54,6 +58,7 @@ func (s *DockerSuite) TestKillWithSignal(c *check.C) { c.Assert(waitRun(cid), check.IsNil) dockerCmd(c, "kill", "-s", "SIGWINCH", cid) + time.Sleep(250 * time.Millisecond) running := inspectField(c, cid, "State.Running") @@ -67,8 +72,10 @@ func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPo cid := strings.TrimSpace(out) c.Assert(waitRun(cid), check.IsNil) - // Let's docker send a CONT signal to the container + // Let docker send a TERM signal to the container + // It will kill the process and disable the restart policy dockerCmd(c, "kill", "-s", "TERM", cid) + c.Assert(waitExited(cid, 10*time.Second), check.IsNil) out, _ = dockerCmd(c, "ps", "-q") c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running")) @@ -81,9 +88,10 @@ func (s *DockerSuite) TestKillWithStopSignalWithDifferentSignalShouldKeepRestart cid := strings.TrimSpace(out) c.Assert(waitRun(cid), check.IsNil) - // Let's docker send a TERM signal to the container + // Let docker send a TERM signal to the container // It will kill the process, but not disable the restart policy dockerCmd(c, "kill", "-s", "TERM", cid) + c.Assert(waitRestart(cid, 10*time.Second), check.IsNil) // Restart policy should still be in place, so it should be still running c.Assert(waitRun(cid), check.IsNil) diff --git a/components/engine/integration-cli/docker_utils.go b/components/engine/integration-cli/docker_utils.go index 4e1c2bca17..a85ac90d74 100644 --- a/components/engine/integration-cli/docker_utils.go +++ b/components/engine/integration-cli/docker_utils.go @@ -1401,6 +1401,11 @@ func waitForContainer(contID string, args ...string) error { return waitRun(contID) } +// waitRestart will wait for the specified container to restart once +func waitRestart(contID string, duration time.Duration) error { + return waitInspect(contID, "{{.RestartCount}}", "1", duration) +} + // waitRun will wait for the specified container to be running, maximum 5 seconds. func waitRun(contID string) error { return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)