docker_cli_wait_test.go 3.0 KB

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