github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_wait_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"os/exec"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/docker/docker/pkg/integration/checker"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  // non-blocking wait with 0 exit code
    14  func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
    15  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "true")
    16  	containerID := strings.TrimSpace(out)
    17  
    18  	err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
    19  	c.Assert(err, checker.IsNil) //Container should have stopped by now
    20  
    21  	out, _ = dockerCmd(c, "wait", containerID)
    22  	c.Assert(strings.TrimSpace(out), checker.Equals, "0", check.Commentf("failed to set up container, %v", out))
    23  
    24  }
    25  
    26  // blocking wait with 0 exit code
    27  func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
    28  	// Windows busybox does not support trap in this way, not sleep with sub-second
    29  	// granularity. It will always exit 0x40010004.
    30  	testRequires(c, DaemonIsLinux)
    31  	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do usleep 10; done")
    32  	containerID := strings.TrimSpace(out)
    33  
    34  	c.Assert(waitRun(containerID), checker.IsNil)
    35  
    36  	chWait := make(chan string)
    37  	go func() {
    38  		chWait <- ""
    39  		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
    40  		chWait <- out
    41  	}()
    42  
    43  	<-chWait // make sure the goroutine is started
    44  	time.Sleep(100 * time.Millisecond)
    45  	dockerCmd(c, "stop", containerID)
    46  
    47  	select {
    48  	case status := <-chWait:
    49  		c.Assert(strings.TrimSpace(status), checker.Equals, "0", check.Commentf("expected exit 0, got %s", status))
    50  	case <-time.After(2 * time.Second):
    51  		c.Fatal("timeout waiting for `docker wait` to exit")
    52  	}
    53  
    54  }
    55  
    56  // non-blocking wait with random exit code
    57  func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
    58  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99")
    59  	containerID := strings.TrimSpace(out)
    60  
    61  	err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
    62  	c.Assert(err, checker.IsNil) //Container should have stopped by now
    63  	out, _ = dockerCmd(c, "wait", containerID)
    64  	c.Assert(strings.TrimSpace(out), checker.Equals, "99", check.Commentf("failed to set up container, %v", out))
    65  
    66  }
    67  
    68  // blocking wait with random exit code
    69  func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
    70  	// Cannot run on Windows as trap in Windows busybox does not support trap in this way.
    71  	testRequires(c, DaemonIsLinux)
    72  	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do usleep 10; done")
    73  	containerID := strings.TrimSpace(out)
    74  	c.Assert(waitRun(containerID), checker.IsNil)
    75  
    76  	chWait := make(chan error)
    77  	waitCmd := exec.Command(dockerBinary, "wait", containerID)
    78  	waitCmdOut := bytes.NewBuffer(nil)
    79  	waitCmd.Stdout = waitCmdOut
    80  	c.Assert(waitCmd.Start(), checker.IsNil)
    81  	go func() {
    82  		chWait <- waitCmd.Wait()
    83  	}()
    84  
    85  	dockerCmd(c, "stop", containerID)
    86  
    87  	select {
    88  	case err := <-chWait:
    89  		c.Assert(err, checker.IsNil, check.Commentf(waitCmdOut.String()))
    90  		status, err := waitCmdOut.ReadString('\n')
    91  		c.Assert(err, checker.IsNil)
    92  		c.Assert(strings.TrimSpace(status), checker.Equals, "99", check.Commentf("expected exit 99, got %s", status))
    93  	case <-time.After(2 * time.Second):
    94  		waitCmd.Process.Kill()
    95  		c.Fatal("timeout waiting for `docker wait` to exit")
    96  	}
    97  }