docker_cli_health_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/go-check/check"
  10. )
  11. func waitForHealthStatus(c *check.C, name string, prev string, expected string) {
  12. prev = prev + "\n"
  13. expected = expected + "\n"
  14. for {
  15. out, _ := dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
  16. if out == expected {
  17. return
  18. }
  19. c.Check(out, checker.Equals, prev)
  20. if out != prev {
  21. return
  22. }
  23. time.Sleep(100 * time.Millisecond)
  24. }
  25. }
  26. func getHealth(c *check.C, name string) *types.Health {
  27. out, _ := dockerCmd(c, "inspect", "--format={{json .State.Health}}", name)
  28. var health types.Health
  29. err := json.Unmarshal([]byte(out), &health)
  30. c.Check(err, checker.Equals, nil)
  31. return &health
  32. }
  33. func (s *DockerSuite) TestHealth(c *check.C) {
  34. testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
  35. imageName := "testhealth"
  36. buildImageSuccessfully(c, imageName, withDockerfile(`FROM busybox
  37. RUN echo OK > /status
  38. CMD ["/bin/sleep", "120"]
  39. STOPSIGNAL SIGKILL
  40. HEALTHCHECK --interval=1s --timeout=30s \
  41. CMD cat /status`))
  42. // No health status before starting
  43. name := "test_health"
  44. dockerCmd(c, "create", "--name", name, imageName)
  45. out, _ := dockerCmd(c, "ps", "-a", "--format={{.Status}}")
  46. c.Check(out, checker.Equals, "Created\n")
  47. // Inspect the options
  48. out, _ = dockerCmd(c, "inspect",
  49. "--format=timeout={{.Config.Healthcheck.Timeout}} "+
  50. "interval={{.Config.Healthcheck.Interval}} "+
  51. "retries={{.Config.Healthcheck.Retries}} "+
  52. "test={{.Config.Healthcheck.Test}}", name)
  53. c.Check(out, checker.Equals, "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. c.Check(out, checker.Equals, "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. out, _ = dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
  70. out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
  71. c.Check(out, checker.Equals, "[NONE]\n")
  72. dockerCmd(c, "rm", "noh")
  73. // Disable the check with a new build
  74. buildImageSuccessfully(c, "no_healthcheck", withDockerfile(`FROM testhealth
  75. HEALTHCHECK NONE`))
  76. out, _ = dockerCmd(c, "inspect", "--format={{.ContainerConfig.Healthcheck.Test}}", "no_healthcheck")
  77. c.Check(out, checker.Equals, "[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. c.Check(health.Status, checker.Equals, "healthy")
  87. c.Check(health.FailingStreak, checker.Equals, 0)
  88. last := health.Log[len(health.Log)-1]
  89. c.Check(last.ExitCode, checker.Equals, 0)
  90. c.Check(last.Output, checker.Equals, "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. c.Check(err, check.IsNil)
  97. c.Check(fails >= 3, checker.Equals, 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. c.Check(health.Status, checker.Equals, "unhealthy")
  108. c.Check(last.ExitCode, checker.Equals, -1)
  109. c.Check(last.Output, checker.Equals, "Health check exceeded timeout (1s)")
  110. dockerCmd(c, "rm", "-f", "test")
  111. // Check JSON-format
  112. buildImageSuccessfully(c, imageName, 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. c.Check(out, checker.Equals, "[CMD cat /my status]\n")
  121. }