docker_cli_wait_test.go 3.2 KB

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