docker_cli_health_test.go 5.8 KB

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