github.com/michael-k/docker@v1.7.0-rc2/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/go-check/check"
    10  )
    11  
    12  // non-blocking wait with 0 exit code
    13  func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
    14  
    15  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true")
    16  	out, _, err := runCommandWithOutput(runCmd)
    17  	if err != nil {
    18  		c.Fatal(out, err)
    19  	}
    20  	containerID := strings.TrimSpace(out)
    21  
    22  	status := "true"
    23  	for i := 0; status != "false"; i++ {
    24  		status, err = inspectField(containerID, "State.Running")
    25  		c.Assert(err, check.IsNil)
    26  
    27  		time.Sleep(time.Second)
    28  		if i >= 60 {
    29  			c.Fatal("Container should have stopped by now")
    30  		}
    31  	}
    32  
    33  	runCmd = exec.Command(dockerBinary, "wait", containerID)
    34  	out, _, err = runCommandWithOutput(runCmd)
    35  
    36  	if err != nil || strings.TrimSpace(out) != "0" {
    37  		c.Fatal("failed to set up container", out, err)
    38  	}
    39  
    40  }
    41  
    42  // blocking wait with 0 exit code
    43  func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
    44  	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do sleep 0.01; done")
    45  	containerID := strings.TrimSpace(out)
    46  
    47  	if err := waitRun(containerID); err != nil {
    48  		c.Fatal(err)
    49  	}
    50  
    51  	chWait := make(chan string)
    52  	go func() {
    53  		out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
    54  		chWait <- out
    55  	}()
    56  
    57  	time.Sleep(100 * time.Millisecond)
    58  	dockerCmd(c, "stop", containerID)
    59  
    60  	select {
    61  	case status := <-chWait:
    62  		if strings.TrimSpace(status) != "0" {
    63  			c.Fatalf("expected exit 0, got %s", status)
    64  		}
    65  	case <-time.After(2 * time.Second):
    66  		c.Fatal("timeout waiting for `docker wait` to exit")
    67  	}
    68  
    69  }
    70  
    71  // non-blocking wait with random exit code
    72  func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
    73  
    74  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99")
    75  	out, _, err := runCommandWithOutput(runCmd)
    76  	if err != nil {
    77  		c.Fatal(out, err)
    78  	}
    79  	containerID := strings.TrimSpace(out)
    80  
    81  	status := "true"
    82  	for i := 0; status != "false"; i++ {
    83  		status, err = inspectField(containerID, "State.Running")
    84  		c.Assert(err, check.IsNil)
    85  
    86  		time.Sleep(time.Second)
    87  		if i >= 60 {
    88  			c.Fatal("Container should have stopped by now")
    89  		}
    90  	}
    91  
    92  	runCmd = exec.Command(dockerBinary, "wait", containerID)
    93  	out, _, err = runCommandWithOutput(runCmd)
    94  
    95  	if err != nil || strings.TrimSpace(out) != "99" {
    96  		c.Fatal("failed to set up container", out, err)
    97  	}
    98  
    99  }
   100  
   101  // blocking wait with random exit code
   102  func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
   103  	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do sleep 0.01; done")
   104  	containerID := strings.TrimSpace(out)
   105  	if err := waitRun(containerID); err != nil {
   106  		c.Fatal(err)
   107  	}
   108  	if err := waitRun(containerID); err != nil {
   109  		c.Fatal(err)
   110  	}
   111  
   112  	chWait := make(chan error)
   113  	waitCmd := exec.Command(dockerBinary, "wait", containerID)
   114  	waitCmdOut := bytes.NewBuffer(nil)
   115  	waitCmd.Stdout = waitCmdOut
   116  	if err := waitCmd.Start(); err != nil {
   117  		c.Fatal(err)
   118  	}
   119  
   120  	go func() {
   121  		chWait <- waitCmd.Wait()
   122  	}()
   123  
   124  	dockerCmd(c, "stop", containerID)
   125  
   126  	select {
   127  	case err := <-chWait:
   128  		if err != nil {
   129  			c.Fatal(err)
   130  		}
   131  		status, err := waitCmdOut.ReadString('\n')
   132  		if err != nil {
   133  			c.Fatal(err)
   134  		}
   135  		if strings.TrimSpace(status) != "99" {
   136  			c.Fatalf("expected exit 99, got %s", status)
   137  		}
   138  	case <-time.After(2 * time.Second):
   139  		waitCmd.Process.Kill()
   140  		c.Fatal("timeout waiting for `docker wait` to exit")
   141  	}
   142  }