docker_cli_wait_test.go 3.6 KB

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