github.com/fntlnz/docker@v1.9.0-rc3/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  		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
    39  		chWait <- out
    40  	}()
    41  
    42  	time.Sleep(100 * time.Millisecond)
    43  	dockerCmd(c, "stop", containerID)
    44  
    45  	select {
    46  	case status := <-chWait:
    47  		c.Assert(strings.TrimSpace(status), checker.Equals, "0", check.Commentf("expected exit 0, got %s", status))
    48  	case <-time.After(2 * time.Second):
    49  		c.Fatal("timeout waiting for `docker wait` to exit")
    50  	}
    51  
    52  }
    53  
    54  // non-blocking wait with random exit code
    55  func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
    56  	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "exit 99")
    57  	containerID := strings.TrimSpace(out)
    58  
    59  	err := waitInspect(containerID, "{{.State.Running}}", "false", 30*time.Second)
    60  	c.Assert(err, checker.IsNil) //Container should have stopped by now
    61  	out, _ = dockerCmd(c, "wait", containerID)
    62  	c.Assert(strings.TrimSpace(out), checker.Equals, "99", check.Commentf("failed to set up container, %v", out))
    63  
    64  }
    65  
    66  // blocking wait with random exit code
    67  func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
    68  	// Cannot run on Windows as trap in Windows busybox does not support trap in this way.
    69  	testRequires(c, DaemonIsLinux)
    70  	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do usleep 10; done")
    71  	containerID := strings.TrimSpace(out)
    72  	c.Assert(waitRun(containerID), checker.IsNil)
    73  
    74  	chWait := make(chan error)
    75  	waitCmd := exec.Command(dockerBinary, "wait", containerID)
    76  	waitCmdOut := bytes.NewBuffer(nil)
    77  	waitCmd.Stdout = waitCmdOut
    78  	c.Assert(waitCmd.Start(), checker.IsNil)
    79  	go func() {
    80  		chWait <- waitCmd.Wait()
    81  	}()
    82  
    83  	dockerCmd(c, "stop", containerID)
    84  
    85  	select {
    86  	case err := <-chWait:
    87  		c.Assert(err, checker.IsNil)
    88  		status, err := waitCmdOut.ReadString('\n')
    89  		c.Assert(err, checker.IsNil)
    90  		c.Assert(strings.TrimSpace(status), checker.Equals, "99", check.Commentf("expected exit 99, got %s", status))
    91  	case <-time.After(2 * time.Second):
    92  		waitCmd.Process.Kill()
    93  		c.Fatal("timeout waiting for `docker wait` to exit")
    94  	}
    95  }