Browse code

[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 authored on 2016/11/10 07:43:08
Showing 2 changed files
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"net/http"
6 6
 	"strings"
7
+	"time"
7 8
 
8 9
 	"github.com/docker/docker/pkg/integration/checker"
9 10
 	"github.com/go-check/check"
... ...
@@ -15,6 +16,7 @@ func (s *DockerSuite) TestKillContainer(c *check.C) {
15 15
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
16 16
 
17 17
 	dockerCmd(c, "kill", cleanedContainerID)
18
+	c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
18 19
 
19 20
 	out, _ = dockerCmd(c, "ps", "-q")
20 21
 	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) {
26 26
 	cleanedContainerID := strings.TrimSpace(out)
27 27
 
28 28
 	dockerCmd(c, "stop", cleanedContainerID)
29
+	c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
29 30
 
30 31
 	_, _, err := dockerCmdWithError("kill", "-s", "30", cleanedContainerID)
31 32
 	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) {
39 39
 	c.Assert(waitRun(cleanedContainerID), check.IsNil)
40 40
 
41 41
 	dockerCmd(c, "kill", cleanedContainerID)
42
+	c.Assert(waitExited(cleanedContainerID, 10*time.Second), check.IsNil)
42 43
 
43 44
 	out, _ = dockerCmd(c, "ps", "-q")
44 45
 	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) {
54 54
 	c.Assert(waitRun(cid), check.IsNil)
55 55
 
56 56
 	dockerCmd(c, "kill", "-s", "SIGWINCH", cid)
57
+	time.Sleep(250 * time.Millisecond)
57 58
 
58 59
 	running := inspectField(c, cid, "State.Running")
59 60
 
... ...
@@ -67,8 +72,10 @@ func (s *DockerSuite) TestKillWithStopSignalWithSameSignalShouldDisableRestartPo
67 67
 	cid := strings.TrimSpace(out)
68 68
 	c.Assert(waitRun(cid), check.IsNil)
69 69
 
70
-	// Let's docker send a CONT signal to the container
70
+	// Let docker send a TERM signal to the container
71
+	// It will kill the process and disable the restart policy
71 72
 	dockerCmd(c, "kill", "-s", "TERM", cid)
73
+	c.Assert(waitExited(cid, 10*time.Second), check.IsNil)
72 74
 
73 75
 	out, _ = dockerCmd(c, "ps", "-q")
74 76
 	c.Assert(out, checker.Not(checker.Contains), cid, check.Commentf("killed container is still running"))
... ...
@@ -81,9 +88,10 @@ func (s *DockerSuite) TestKillWithStopSignalWithDifferentSignalShouldKeepRestart
81 81
 	cid := strings.TrimSpace(out)
82 82
 	c.Assert(waitRun(cid), check.IsNil)
83 83
 
84
-	// Let's docker send a TERM signal to the container
84
+	// Let docker send a TERM signal to the container
85 85
 	// It will kill the process, but not disable the restart policy
86 86
 	dockerCmd(c, "kill", "-s", "TERM", cid)
87
+	c.Assert(waitRestart(cid, 10*time.Second), check.IsNil)
87 88
 
88 89
 	// Restart policy should still be in place, so it should be still running
89 90
 	c.Assert(waitRun(cid), check.IsNil)
... ...
@@ -1401,6 +1401,11 @@ func waitForContainer(contID string, args ...string) error {
1401 1401
 	return waitRun(contID)
1402 1402
 }
1403 1403
 
1404
+// waitRestart will wait for the specified container to restart once
1405
+func waitRestart(contID string, duration time.Duration) error {
1406
+	return waitInspect(contID, "{{.RestartCount}}", "1", duration)
1407
+}
1408
+
1404 1409
 // waitRun will wait for the specified container to be running, maximum 5 seconds.
1405 1410
 func waitRun(contID string) error {
1406 1411
 	return waitInspect(contID, "{{.State.Running}}", "true", 5*time.Second)