docker_cli_wait_test.go 3.1 KB

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