123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package main
- import (
- "bytes"
- "os/exec"
- "strings"
- "time"
- "github.com/go-check/check"
- )
- // non-blocking wait with 0 exit code
- func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
- runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true")
- out, _, err := runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- containerID := strings.TrimSpace(out)
- status := "true"
- for i := 0; status != "false"; i++ {
- status, err = inspectField(containerID, "State.Running")
- c.Assert(err, check.IsNil)
- time.Sleep(time.Second)
- if i >= 60 {
- c.Fatal("Container should have stopped by now")
- }
- }
- runCmd = exec.Command(dockerBinary, "wait", containerID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil || strings.TrimSpace(out) != "0" {
- c.Fatal("failed to set up container", out, err)
- }
- }
- // blocking wait with 0 exit code
- func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
- out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 0' TERM; while true; do sleep 0.01; done")
- containerID := strings.TrimSpace(out)
- if err := waitRun(containerID); err != nil {
- c.Fatal(err)
- }
- chWait := make(chan string)
- go func() {
- out, _, _ := runCommandWithOutput(exec.Command(dockerBinary, "wait", containerID))
- chWait <- out
- }()
- time.Sleep(100 * time.Millisecond)
- dockerCmd(c, "stop", containerID)
- select {
- case status := <-chWait:
- if strings.TrimSpace(status) != "0" {
- c.Fatalf("expected exit 0, got %s", status)
- }
- case <-time.After(2 * time.Second):
- c.Fatal("timeout waiting for `docker wait` to exit")
- }
- }
- // non-blocking wait with random exit code
- func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
- runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99")
- out, _, err := runCommandWithOutput(runCmd)
- if err != nil {
- c.Fatal(out, err)
- }
- containerID := strings.TrimSpace(out)
- status := "true"
- for i := 0; status != "false"; i++ {
- status, err = inspectField(containerID, "State.Running")
- c.Assert(err, check.IsNil)
- time.Sleep(time.Second)
- if i >= 60 {
- c.Fatal("Container should have stopped by now")
- }
- }
- runCmd = exec.Command(dockerBinary, "wait", containerID)
- out, _, err = runCommandWithOutput(runCmd)
- if err != nil || strings.TrimSpace(out) != "99" {
- c.Fatal("failed to set up container", out, err)
- }
- }
- // blocking wait with random exit code
- func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
- out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "trap 'exit 99' TERM; while true; do sleep 0.01; done")
- containerID := strings.TrimSpace(out)
- if err := waitRun(containerID); err != nil {
- c.Fatal(err)
- }
- if err := waitRun(containerID); err != nil {
- c.Fatal(err)
- }
- chWait := make(chan error)
- waitCmd := exec.Command(dockerBinary, "wait", containerID)
- waitCmdOut := bytes.NewBuffer(nil)
- waitCmd.Stdout = waitCmdOut
- if err := waitCmd.Start(); err != nil {
- c.Fatal(err)
- }
- go func() {
- chWait <- waitCmd.Wait()
- }()
- dockerCmd(c, "stop", containerID)
- select {
- case err := <-chWait:
- if err != nil {
- c.Fatal(err)
- }
- status, err := waitCmdOut.ReadString('\n')
- if err != nil {
- c.Fatal(err)
- }
- if strings.TrimSpace(status) != "99" {
- c.Fatalf("expected exit 99, got %s", status)
- }
- case <-time.After(2 * time.Second):
- waitCmd.Process.Kill()
- c.Fatal("timeout waiting for `docker wait` to exit")
- }
- }
|