docker_cli_wait_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. // non-blocking wait with 0 exit code
  9. func TestWaitNonBlockedExitZero(t *testing.T) {
  10. defer deleteAllContainers()
  11. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "true")
  12. out, _, err := runCommandWithOutput(runCmd)
  13. if err != nil {
  14. t.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. t.Fatal(status, err)
  23. }
  24. status = strings.TrimSpace(status)
  25. time.Sleep(time.Second)
  26. if i >= 60 {
  27. t.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. t.Fatal("failed to set up container", out, err)
  34. }
  35. logDone("wait - non-blocking wait with 0 exit code")
  36. }
  37. // blocking wait with 0 exit code
  38. func TestWaitBlockedExitZero(t *testing.T) {
  39. defer deleteAllContainers()
  40. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
  41. out, _, err := runCommandWithOutput(runCmd)
  42. if err != nil {
  43. t.Fatal(out, err)
  44. }
  45. containerID := strings.TrimSpace(out)
  46. runCmd = exec.Command(dockerBinary, "wait", containerID)
  47. out, _, err = runCommandWithOutput(runCmd)
  48. if err != nil || strings.TrimSpace(out) != "0" {
  49. t.Fatal("failed to set up container", out, err)
  50. }
  51. logDone("wait - blocking wait with 0 exit code")
  52. }
  53. // non-blocking wait with random exit code
  54. func TestWaitNonBlockedExitRandom(t *testing.T) {
  55. defer deleteAllContainers()
  56. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "exit 99")
  57. out, _, err := runCommandWithOutput(runCmd)
  58. if err != nil {
  59. t.Fatal(out, err)
  60. }
  61. containerID := strings.TrimSpace(out)
  62. status := "true"
  63. for i := 0; status != "false"; i++ {
  64. runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
  65. status, _, err = runCommandWithOutput(runCmd)
  66. if err != nil {
  67. t.Fatal(status, err)
  68. }
  69. status = strings.TrimSpace(status)
  70. time.Sleep(time.Second)
  71. if i >= 60 {
  72. t.Fatal("Container should have stopped by now")
  73. }
  74. }
  75. runCmd = exec.Command(dockerBinary, "wait", containerID)
  76. out, _, err = runCommandWithOutput(runCmd)
  77. if err != nil || strings.TrimSpace(out) != "99" {
  78. t.Fatal("failed to set up container", out, err)
  79. }
  80. logDone("wait - non-blocking wait with random exit code")
  81. }
  82. // blocking wait with random exit code
  83. func TestWaitBlockedExitRandom(t *testing.T) {
  84. defer deleteAllContainers()
  85. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10; exit 99")
  86. out, _, err := runCommandWithOutput(runCmd)
  87. if err != nil {
  88. t.Fatal(out, err)
  89. }
  90. containerID := strings.TrimSpace(out)
  91. runCmd = exec.Command(dockerBinary, "wait", containerID)
  92. out, _, err = runCommandWithOutput(runCmd)
  93. if err != nil || strings.TrimSpace(out) != "99" {
  94. t.Fatal("failed to set up container", out, err)
  95. }
  96. logDone("wait - blocking wait with random exit code")
  97. }