Przeglądaj źródła

[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 <tophj@linux.vnet.ibm.com>
Christopher Jones 8 lat temu
rodzic
commit
ff42a1ab53

+ 10 - 2
integration-cli/docker_cli_kill_test.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
 	"strings"
 	"strings"
+	"time"
 
 
 	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/go-check/check"
 	"github.com/go-check/check"
@@ -15,6 +16,7 @@ func (s *DockerSuite) TestKillContainer(c *check.C) {
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
 
 
 	dockerCmd(c, "kill", cleanedContainerID)
 	dockerCmd(c, "kill", cleanedContainerID)
+	c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
 
 
 	out, _ = dockerCmd(c, "ps", "-q")
 	out, _ = dockerCmd(c, "ps", "-q")
 	c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
 	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)
 	cleanedContainerID := strings.TrimSpace(out)
 
 
 	dockerCmd(c, "stop", cleanedContainerID)
 	dockerCmd(c, "stop", cleanedContainerID)
+	c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
 
 
 	_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
 	_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
 	c.Assert(err, check.Not(check.IsNil), check.Commentf("Container %s is not running", 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)
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
 
 
 	dockerCmd(c, "kill", cleanedContainerID)
 	dockerCmd(c, "kill", cleanedContainerID)
+	c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
 
 
 	out, _ = dockerCmd(c, "ps", "-q")
 	out, _ = dockerCmd(c, "ps", "-q")
 	c.Assert(out, checker.Not(checker.Contains), cleanedContainerID, check.Commentf("killed container is still running"))
 	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)
 	c.Assert(waitRun(cid), check.IsNil)
 
 
 	dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
 	dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
+	time.Sleep(250 * time.Millisecond)
 
 
 	running := inspectField(c, cid, "State.Running")
 	running := inspectField(c, cid, "State.Running")
 
 
@@ -67,8 +72,10 @@ func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPo
 	cid := strings.TrimSpace(out)
 	cid := strings.TrimSpace(out)
 	c.Assert(waitRun(cid), check.IsNil)
 	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)
 	dockerCmd(c, "kill", "-s", "TERM", cid)
+	c.Assert(waitExited(cid, 10*time.Second), check.IsNil)
 
 
 	out, _ = dockerCmd(c, "ps", "-q")
 	out, _ = dockerCmd(c, "ps", "-q")
 	c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running"))
 	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)
 	cid := strings.TrimSpace(out)
 	c.Assert(waitRun(cid), check.IsNil)
 	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
 	// It will kill the process, but not disable the restart policy
 	dockerCmd(c, "kill", "-s", "TERM", cid)
 	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
 	// Restart policy should still be in place, so it should be still running
 	c.Assert(waitRun(cid), check.IsNil)
 	c.Assert(waitRun(cid), check.IsNil)

+ 5 - 0
integration-cli/docker_utils.go

@@ -1401,6 +1401,11 @@ func waitForContainer(contID string, args ...string) error {
 	return waitRun(contID)
 	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.
 // waitRun will wait for the specified container to be running, maximum 5 seconds.
 func waitRun(contID string) error {
 func waitRun(contID string) error {
 	return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)
 	return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)