docker_cli_health_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. _, err := buildImage(imageName,
  37. `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. true)
  44. c.Check(err, check.IsNil)
  45. // No health status before starting
  46. name := "test_health"
  47. dockerCmd(c, "create", "--name", name, imageName)
  48. out, _ := dockerCmd(c, "ps", "-a", "--format={{.Status}}")
  49. c.Check(out, checker.Equals, "Created\n")
  50. // Inspect the options
  51. out, _ = dockerCmd(c, "inspect",
  52. "--format=timeout={{.Config.Healthcheck.Timeout}} "+
  53. "interval={{.Config.Healthcheck.Interval}} "+
  54. "retries={{.Config.Healthcheck.Retries}} "+
  55. "test={{.Config.Healthcheck.Test}}", name)
  56. c.Check(out, checker.Equals, "timeout=30s interval=1s retries=0 test=[CMD-SHELL cat /status]\n")
  57. // Start
  58. dockerCmd(c, "start", name)
  59. waitForHealthStatus(c, name, "starting", "healthy")
  60. // Make it fail
  61. dockerCmd(c, "exec", name, "rm", "/status")
  62. waitForHealthStatus(c, name, "healthy", "unhealthy")
  63. // Inspect the status
  64. out, _ = dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
  65. c.Check(out, checker.Equals, "unhealthy\n")
  66. // Make it healthy again
  67. dockerCmd(c, "exec", name, "touch", "/status")
  68. waitForHealthStatus(c, name, "unhealthy", "healthy")
  69. // Remove container
  70. dockerCmd(c, "rm", "-f", name)
  71. // Disable the check from the CLI
  72. out, _ = dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
  73. out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
  74. c.Check(out, checker.Equals, "[NONE]\n")
  75. dockerCmd(c, "rm", "noh")
  76. // Disable the check with a new build
  77. _, err = buildImage("no_healthcheck",
  78. `FROM testhealth
  79. HEALTHCHECK NONE`, true)
  80. c.Check(err, check.IsNil)
  81. out, _ = dockerCmd(c, "inspect", "--format={{.ContainerConfig.Healthcheck.Test}}", "no_healthcheck")
  82. c.Check(out, checker.Equals, "[NONE]\n")
  83. // Enable the checks from the CLI
  84. _, _ = dockerCmd(c, "run", "-d", "--name=fatal_healthcheck",
  85. "--health-interval=0.5s",
  86. "--health-retries=3",
  87. "--health-cmd=cat /status",
  88. "no_healthcheck")
  89. waitForHealthStatus(c, "fatal_healthcheck", "starting", "healthy")
  90. health := getHealth(c, "fatal_healthcheck")
  91. c.Check(health.Status, checker.Equals, "healthy")
  92. c.Check(health.FailingStreak, checker.Equals, 0)
  93. last := health.Log[len(health.Log)-1]
  94. c.Check(last.ExitCode, checker.Equals, 0)
  95. c.Check(last.Output, checker.Equals, "OK\n")
  96. // Fail the check
  97. dockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status")
  98. waitForHealthStatus(c, "fatal_healthcheck", "healthy", "unhealthy")
  99. failsStr, _ := dockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck")
  100. fails, err := strconv.Atoi(strings.TrimSpace(failsStr))
  101. c.Check(err, check.IsNil)
  102. c.Check(fails >= 3, checker.Equals, true)
  103. dockerCmd(c, "rm", "-f", "fatal_healthcheck")
  104. // Check timeout
  105. // Note: if the interval is too small, it seems that Docker spends all its time running health
  106. // checks and never gets around to killing it.
  107. _, _ = dockerCmd(c, "run", "-d", "--name=test",
  108. "--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1ms", imageName)
  109. waitForHealthStatus(c, "test", "starting", "unhealthy")
  110. health = getHealth(c, "test")
  111. last = health.Log[len(health.Log)-1]
  112. c.Check(health.Status, checker.Equals, "unhealthy")
  113. c.Check(last.ExitCode, checker.Equals, -1)
  114. c.Check(last.Output, checker.Equals, "Health check exceeded timeout (1ms)")
  115. dockerCmd(c, "rm", "-f", "test")
  116. // Check JSON-format
  117. _, err = buildImage(imageName,
  118. `FROM busybox
  119. RUN echo OK > /status
  120. CMD ["/bin/sleep", "120"]
  121. STOPSIGNAL SIGKILL
  122. HEALTHCHECK --interval=1s --timeout=30s \
  123. CMD ["cat", "/my status"]`,
  124. true)
  125. c.Check(err, check.IsNil)
  126. out, _ = dockerCmd(c, "inspect",
  127. "--format={{.Config.Healthcheck.Test}}", imageName)
  128. c.Check(out, checker.Equals, "[CMD cat /my status]\n")
  129. }