github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_wait_test.go (about)

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