docker_cli_wait_test.go 3.4 KB

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