docker_cli_health_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/integration-cli/checker"
  9. "github.com/docker/docker/integration-cli/cli/build"
  10. "github.com/go-check/check"
  11. )
  12. func waitForHealthStatus(c *check.C, name string, prev string, expected string) {
  13. prev = prev + "\n"
  14. expected = expected + "\n"
  15. for {
  16. out, _ := dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
  17. if out == expected {
  18. return
  19. }
  20. c.Check(out, checker.Equals, prev)
  21. if out != prev {
  22. return
  23. }
  24. time.Sleep(100 * time.Millisecond)
  25. }
  26. }
  27. func getHealth(c *check.C, name string) *types.Health {
  28. out, _ := dockerCmd(c, "inspect", "--format={{json .State.Health}}", name)
  29. var health types.Health
  30. err := json.Unmarshal([]byte(out), &health)
  31. c.Check(err, checker.Equals, nil)
  32. return &health
  33. }
  34. func (s *DockerSuite) TestHealth(c *check.C) {
  35. testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
  36. imageName := "testhealth"
  37. buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
  38. RUN echo OK > /status
  39. CMD ["/bin/sleep", "120"]
  40. STOPSIGNAL SIGKILL
  41. HEALTHCHECK --interval=1s --timeout=30s \
  42. CMD cat /status`))
  43. // No health status before starting
  44. name := "test_health"
  45. dockerCmd(c, "create", "--name", name, imageName)
  46. out, _ := dockerCmd(c, "ps", "-a", "--format={{.Status}}")
  47. c.Check(out, checker.Equals, "Created\n")
  48. // Inspect the options
  49. out, _ = dockerCmd(c, "inspect",
  50. "--format=timeout={{.Config.Healthcheck.Timeout}} interval={{.Config.Healthcheck.Interval}} retries={{.Config.Healthcheck.Retries}} test={{.Config.Healthcheck.Test}}", name)
  51. c.Check(out, checker.Equals, "timeout=30s interval=1s retries=0 test=[CMD-SHELL cat /status]\n")
  52. // Start
  53. dockerCmd(c, "start", name)
  54. waitForHealthStatus(c, name, "starting", "healthy")
  55. // Make it fail
  56. dockerCmd(c, "exec", name, "rm", "/status")
  57. waitForHealthStatus(c, name, "healthy", "unhealthy")
  58. // Inspect the status
  59. out, _ = dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
  60. c.Check(out, checker.Equals, "unhealthy\n")
  61. // Make it healthy again
  62. dockerCmd(c, "exec", name, "touch", "/status")
  63. waitForHealthStatus(c, name, "unhealthy", "healthy")
  64. // Remove container
  65. dockerCmd(c, "rm", "-f", name)
  66. // Disable the check from the CLI
  67. out, _ = dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
  68. out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
  69. c.Check(out, checker.Equals, "[NONE]\n")
  70. dockerCmd(c, "rm", "noh")
  71. // Disable the check with a new build
  72. buildImageSuccessfully(c, "no_healthcheck", build.WithDockerfile(`FROM testhealth
  73. HEALTHCHECK NONE`))
  74. out, _ = dockerCmd(c, "inspect", "--format={{.ContainerConfig.Healthcheck.Test}}", "no_healthcheck")
  75. c.Check(out, checker.Equals, "[NONE]\n")
  76. // Enable the checks from the CLI
  77. _, _ = dockerCmd(c, "run", "-d", "--name=fatal_healthcheck",
  78. "--health-interval=1s",
  79. "--health-retries=3",
  80. "--health-cmd=cat /status",
  81. "no_healthcheck")
  82. waitForHealthStatus(c, "fatal_healthcheck", "starting", "healthy")
  83. health := getHealth(c, "fatal_healthcheck")
  84. c.Check(health.Status, checker.Equals, "healthy")
  85. c.Check(health.FailingStreak, checker.Equals, 0)
  86. last := health.Log[len(health.Log)-1]
  87. c.Check(last.ExitCode, checker.Equals, 0)
  88. c.Check(last.Output, checker.Equals, "OK\n")
  89. // Fail the check
  90. dockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status")
  91. waitForHealthStatus(c, "fatal_healthcheck", "healthy", "unhealthy")
  92. failsStr, _ := dockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck")
  93. fails, err := strconv.Atoi(strings.TrimSpace(failsStr))
  94. c.Check(err, check.IsNil)
  95. c.Check(fails >= 3, checker.Equals, true)
  96. dockerCmd(c, "rm", "-f", "fatal_healthcheck")
  97. // Check timeout
  98. // Note: if the interval is too small, it seems that Docker spends all its time running health
  99. // checks and never gets around to killing it.
  100. _, _ = dockerCmd(c, "run", "-d", "--name=test",
  101. "--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1s", imageName)
  102. waitForHealthStatus(c, "test", "starting", "unhealthy")
  103. health = getHealth(c, "test")
  104. last = health.Log[len(health.Log)-1]
  105. c.Check(health.Status, checker.Equals, "unhealthy")
  106. c.Check(last.ExitCode, checker.Equals, -1)
  107. c.Check(last.Output, checker.Equals, "Health check exceeded timeout (1s)")
  108. dockerCmd(c, "rm", "-f", "test")
  109. // Check JSON-format
  110. buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
  111. RUN echo OK > /status
  112. CMD ["/bin/sleep", "120"]
  113. STOPSIGNAL SIGKILL
  114. HEALTHCHECK --interval=1s --timeout=30s \
  115. CMD ["cat", "/my status"]`))
  116. out, _ = dockerCmd(c, "inspect",
  117. "--format={{.Config.Healthcheck.Test}}", imageName)
  118. c.Check(out, checker.Equals, "[CMD cat /my status]\n")
  119. }
  120. // Github #33021
  121. func (s *DockerSuite) TestUnsetEnvVarHealthCheck(c *check.C) {
  122. testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
  123. imageName := "testhealth"
  124. buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
  125. HEALTHCHECK --interval=1s --timeout=5s --retries=5 CMD /bin/sh -c "sleep 1"
  126. ENTRYPOINT /bin/sh -c "sleep 600"`))
  127. name := "env_test_health"
  128. // No health status before starting
  129. dockerCmd(c, "run", "-d", "--name", name, "-e", "FOO", imageName)
  130. defer func() {
  131. dockerCmd(c, "rm", "-f", name)
  132. dockerCmd(c, "rmi", imageName)
  133. }()
  134. // Start
  135. dockerCmd(c, "start", name)
  136. waitForHealthStatus(c, name, "starting", "healthy")
  137. }