docker_cli_health_test.go 5.4 KB

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