Browse code

Fix TestAttachClosedOnContainerStop racey test

This test is failing once in a while on the CI, because the docker
attach command might be called after the container ends.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/09/29 23:57:58
Showing 1 changed files
... ...
@@ -13,34 +13,35 @@ import (
13 13
 	"github.com/kr/pty"
14 14
 )
15 15
 
16
-// #9860
16
+// #9860 Make sure attach ends when container ends (with no errors)
17 17
 func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
18 18
 
19
-	out, _ := dockerCmd(c, "run", "-dti", "busybox", "sleep", "2")
19
+	out, _ := dockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`)
20 20
 
21 21
 	id := strings.TrimSpace(out)
22 22
 	c.Assert(waitRun(id), check.IsNil)
23 23
 
24
+	_, tty, err := pty.Open()
25
+	c.Assert(err, check.IsNil)
26
+
27
+	attachCmd := exec.Command(dockerBinary, "attach", id)
28
+	attachCmd.Stdin = tty
29
+	attachCmd.Stdout = tty
30
+	attachCmd.Stderr = tty
31
+	err = attachCmd.Start()
32
+	c.Assert(err, check.IsNil)
33
+
24 34
 	errChan := make(chan error)
25 35
 	go func() {
26 36
 		defer close(errChan)
27
-
28
-		_, tty, err := pty.Open()
29
-		if err != nil {
30
-			errChan <- err
31
-			return
32
-		}
33
-		attachCmd := exec.Command(dockerBinary, "attach", id)
34
-		attachCmd.Stdin = tty
35
-		attachCmd.Stdout = tty
36
-		attachCmd.Stderr = tty
37
-
38
-		if err := attachCmd.Run(); err != nil {
39
-			errChan <- err
40
-			return
41
-		}
37
+		// Container is wating for us to signal it to stop
38
+		dockerCmd(c, "stop", id)
39
+		// And wait for the attach command to end
40
+		errChan <- attachCmd.Wait()
42 41
 	}()
43 42
 
43
+	// Wait for the docker to end (should be done by the
44
+	// stop command in the go routine)
44 45
 	dockerCmd(c, "wait", id)
45 46
 
46 47
 	select {