docker_cli_wait_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "time"
  6. "github.com/go-check/check"
  7. )
  8. // non-blocking wait with 0 exit code
  9. func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true")
  11. out, _, err := runCommandWithOutput(runCmd)
  12. if err != nil {
  13. c.Fatal(out, err)
  14. }
  15. containerID := strings.TrimSpace(out)
  16. status := "true"
  17. for i := 0; status != "false"; i++ {
  18. runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
  19. status, _, err = runCommandWithOutput(runCmd)
  20. if err != nil {
  21. c.Fatal(status, err)
  22. }
  23. status = strings.TrimSpace(status)
  24. time.Sleep(time.Second)
  25. if i >= 60 {
  26. c.Fatal("Container should have stopped by now")
  27. }
  28. }
  29. runCmd = exec.Command(dockerBinary, "wait", containerID)
  30. out, _, err = runCommandWithOutput(runCmd)
  31. if err != nil || strings.TrimSpace(out) != "0" {
  32. c.Fatal("failed to set up container", out, err)
  33. }
  34. }
  35. // blocking wait with 0 exit code
  36. func (s *DockerSuite) TestWaitBlockedExitZero(c *check.C) {
  37. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
  38. out, _, err := runCommandWithOutput(runCmd)
  39. if err != nil {
  40. c.Fatal(out, err)
  41. }
  42. containerID := strings.TrimSpace(out)
  43. runCmd = exec.Command(dockerBinary, "wait", containerID)
  44. out, _, err = runCommandWithOutput(runCmd)
  45. if err != nil || strings.TrimSpace(out) != "0" {
  46. c.Fatal("failed to set up container", out, err)
  47. }
  48. }
  49. // non-blocking wait with random exit code
  50. func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
  51. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99")
  52. out, _, err := runCommandWithOutput(runCmd)
  53. if err != nil {
  54. c.Fatal(out, err)
  55. }
  56. containerID := strings.TrimSpace(out)
  57. status := "true"
  58. for i := 0; status != "false"; i++ {
  59. runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
  60. status, _, err = runCommandWithOutput(runCmd)
  61. if err != nil {
  62. c.Fatal(status, err)
  63. }
  64. status = strings.TrimSpace(status)
  65. time.Sleep(time.Second)
  66. if i >= 60 {
  67. c.Fatal("Container should have stopped by now")
  68. }
  69. }
  70. runCmd = exec.Command(dockerBinary, "wait", containerID)
  71. out, _, err = runCommandWithOutput(runCmd)
  72. if err != nil || strings.TrimSpace(out) != "99" {
  73. c.Fatal("failed to set up container", out, err)
  74. }
  75. }
  76. // blocking wait with random exit code
  77. func (s *DockerSuite) TestWaitBlockedExitRandom(c *check.C) {
  78. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99")
  79. out, _, err := runCommandWithOutput(runCmd)
  80. if err != nil {
  81. c.Fatal(out, err)
  82. }
  83. containerID := strings.TrimSpace(out)
  84. runCmd = exec.Command(dockerBinary, "wait", containerID)
  85. out, _, err = runCommandWithOutput(runCmd)
  86. if err != nil || strings.TrimSpace(out) != "99" {
  87. c.Fatal("failed to set up container", out, err)
  88. }
  89. }