docker_cli_health_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "encoding/json"
  4. "github.com/docker/docker/pkg/integration/checker"
  5. "github.com/docker/engine-api/types"
  6. "github.com/go-check/check"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func waitForStatus(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.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 waitForHealthStatus(c *check.C, name string, prev string, expected string) {
  27. prev = prev + "\n"
  28. expected = expected + "\n"
  29. for {
  30. out, _ := dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
  31. if out == expected {
  32. return
  33. }
  34. c.Check(out, checker.Equals, prev)
  35. if out != prev {
  36. return
  37. }
  38. time.Sleep(100 * time.Millisecond)
  39. }
  40. }
  41. func getHealth(c *check.C, name string) *types.Health {
  42. out, _ := dockerCmd(c, "inspect", "--format={{json .State.Health}}", name)
  43. var health types.Health
  44. err := json.Unmarshal([]byte(out), &health)
  45. c.Check(err, checker.Equals, nil)
  46. return &health
  47. }
  48. func (s *DockerSuite) TestHealth(c *check.C) {
  49. testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
  50. imageName := "testhealth"
  51. _, err := buildImage(imageName,
  52. `FROM busybox
  53. RUN echo OK > /status
  54. CMD ["/bin/sleep", "120"]
  55. STOPSIGNAL SIGKILL
  56. HEALTHCHECK --interval=1s --timeout=30s \
  57. CMD cat /status`,
  58. true)
  59. c.Check(err, check.IsNil)
  60. // No health status before starting
  61. name := "test_health"
  62. dockerCmd(c, "create", "--name", name, imageName)
  63. out, _ := dockerCmd(c, "ps", "-a", "--format={{.Status}}")
  64. c.Check(out, checker.Equals, "Created\n")
  65. // Inspect the options
  66. out, _ = dockerCmd(c, "inspect",
  67. "--format='timeout={{.Config.Healthcheck.Timeout}} "+
  68. "interval={{.Config.Healthcheck.Interval}} "+
  69. "retries={{.Config.Healthcheck.Retries}} "+
  70. "test={{.Config.Healthcheck.Test}}'", name)
  71. c.Check(out, checker.Equals, "timeout=30s interval=1s retries=0 test=[CMD-SHELL cat /status]\n")
  72. // Start
  73. dockerCmd(c, "start", name)
  74. waitForHealthStatus(c, name, "starting", "healthy")
  75. // Make it fail
  76. dockerCmd(c, "exec", name, "rm", "/status")
  77. waitForHealthStatus(c, name, "healthy", "unhealthy")
  78. // Inspect the status
  79. out, _ = dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
  80. c.Check(out, checker.Equals, "unhealthy\n")
  81. // Make it healthy again
  82. dockerCmd(c, "exec", name, "touch", "/status")
  83. waitForHealthStatus(c, name, "unhealthy", "healthy")
  84. // Remove container
  85. dockerCmd(c, "rm", "-f", name)
  86. // Disable the check from the CLI
  87. out, _ = dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
  88. out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
  89. c.Check(out, checker.Equals, "[NONE]\n")
  90. dockerCmd(c, "rm", "noh")
  91. // Disable the check with a new build
  92. _, err = buildImage("no_healthcheck",
  93. `FROM testhealth
  94. HEALTHCHECK NONE`, true)
  95. c.Check(err, check.IsNil)
  96. out, _ = dockerCmd(c, "inspect", "--format={{.ContainerConfig.Healthcheck.Test}}", "no_healthcheck")
  97. c.Check(out, checker.Equals, "[NONE]\n")
  98. // Enable the checks from the CLI
  99. _, _ = dockerCmd(c, "run", "-d", "--name=fatal_healthcheck",
  100. "--health-interval=0.5s",
  101. "--health-retries=3",
  102. "--health-cmd=cat /status",
  103. "no_healthcheck")
  104. waitForHealthStatus(c, "fatal_healthcheck", "starting", "healthy")
  105. health := getHealth(c, "fatal_healthcheck")
  106. c.Check(health.Status, checker.Equals, "healthy")
  107. c.Check(health.FailingStreak, checker.Equals, 0)
  108. last := health.Log[len(health.Log)-1]
  109. c.Check(last.ExitCode, checker.Equals, 0)
  110. c.Check(last.Output, checker.Equals, "OK\n")
  111. // Fail the check
  112. dockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status")
  113. waitForHealthStatus(c, "fatal_healthcheck", "healthy", "unhealthy")
  114. failsStr, _ := dockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck")
  115. fails, err := strconv.Atoi(strings.TrimSpace(failsStr))
  116. c.Check(err, check.IsNil)
  117. c.Check(fails >= 3, checker.Equals, true)
  118. dockerCmd(c, "rm", "-f", "fatal_healthcheck")
  119. // Check timeout
  120. // Note: if the interval is too small, it seems that Docker spends all its time running health
  121. // checks and never gets around to killing it.
  122. _, _ = dockerCmd(c, "run", "-d", "--name=test",
  123. "--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1ms", imageName)
  124. waitForHealthStatus(c, "test", "starting", "unhealthy")
  125. health = getHealth(c, "test")
  126. last = health.Log[len(health.Log)-1]
  127. c.Check(health.Status, checker.Equals, "unhealthy")
  128. c.Check(last.ExitCode, checker.Equals, -1)
  129. c.Check(last.Output, checker.Equals, "Health check exceeded timeout (1ms)")
  130. dockerCmd(c, "rm", "-f", "test")
  131. }