docker_cli_wait_test.go 3.3 KB

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