Browse Source

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 9 years ago
parent
commit
50852f3be3
1 changed files with 18 additions and 17 deletions
  1. 18 17
      integration-cli/docker_cli_attach_unix_test.go

+ 18 - 17
integration-cli/docker_cli_attach_unix_test.go

@@ -13,34 +13,35 @@ import (
 	"github.com/kr/pty"
 	"github.com/kr/pty"
 )
 )
 
 
-// #9860
+// #9860 Make sure attach ends when container ends (with no errors)
 func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
 func (s *DockerSuite) TestAttachClosedOnContainerStop(c *check.C) {
 
 
-	out, _ := dockerCmd(c, "run", "-dti", "busybox", "sleep", "2")
+	out, _ := dockerCmd(c, "run", "-dti", "busybox", "/bin/sh", "-c", `trap 'exit 0' SIGTERM; while true; do sleep 1; done`)
 
 
 	id := strings.TrimSpace(out)
 	id := strings.TrimSpace(out)
 	c.Assert(waitRun(id), check.IsNil)
 	c.Assert(waitRun(id), check.IsNil)
 
 
+	_, tty, err := pty.Open()
+	c.Assert(err, check.IsNil)
+
+	attachCmd := exec.Command(dockerBinary, "attach", id)
+	attachCmd.Stdin = tty
+	attachCmd.Stdout = tty
+	attachCmd.Stderr = tty
+	err = attachCmd.Start()
+	c.Assert(err, check.IsNil)
+
 	errChan := make(chan error)
 	errChan := make(chan error)
 	go func() {
 	go func() {
 		defer close(errChan)
 		defer close(errChan)
-
-		_, tty, err := pty.Open()
-		if err != nil {
-			errChan <- err
-			return
-		}
-		attachCmd := exec.Command(dockerBinary, "attach", id)
-		attachCmd.Stdin = tty
-		attachCmd.Stdout = tty
-		attachCmd.Stderr = tty
-
-		if err := attachCmd.Run(); err != nil {
-			errChan <- err
-			return
-		}
+		// Container is wating for us to signal it to stop
+		dockerCmd(c, "stop", id)
+		// And wait for the attach command to end
+		errChan <- attachCmd.Wait()
 	}()
 	}()
 
 
+	// Wait for the docker to end (should be done by the
+	// stop command in the go routine)
 	dockerCmd(c, "wait", id)
 	dockerCmd(c, "wait", id)
 
 
 	select {
 	select {