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