docker_cli_health_test.go 5.6 KB

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