docker_cli_service_health_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //go:build !windows
  2. package main
  3. import (
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/api/types/swarm"
  8. "github.com/docker/docker/daemon/cluster/executor/container"
  9. "github.com/docker/docker/integration-cli/checker"
  10. "github.com/docker/docker/integration-cli/cli"
  11. "github.com/docker/docker/integration-cli/cli/build"
  12. "gotest.tools/v3/assert"
  13. "gotest.tools/v3/icmd"
  14. "gotest.tools/v3/poll"
  15. )
  16. // start a service, and then make its task unhealthy during running
  17. // finally, unhealthy task should be detected and killed
  18. func (s *DockerSwarmSuite) TestServiceHealthRun(c *testing.T) {
  19. testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
  20. d := s.AddDaemon(c, true, true)
  21. // build image with health-check
  22. imageName := "testhealth"
  23. result := cli.BuildCmd(c, imageName, cli.Daemon(d),
  24. build.WithDockerfile(`FROM busybox
  25. RUN touch /status
  26. HEALTHCHECK --interval=1s --timeout=5s --retries=1\
  27. CMD cat /status`),
  28. )
  29. result.Assert(c, icmd.Success)
  30. serviceName := "healthServiceRun"
  31. out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")
  32. assert.NilError(c, err, out)
  33. id := strings.TrimSpace(out)
  34. var tasks []swarm.Task
  35. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  36. tasks = d.GetServiceTasks(c, id)
  37. return tasks, ""
  38. }, checker.HasLen(1)), poll.WithTimeout(defaultReconciliationTimeout))
  39. task := tasks[0]
  40. // wait for task to start
  41. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  42. task = d.GetTask(c, task.ID)
  43. return task.Status.State, ""
  44. }, checker.Equals(swarm.TaskStateRunning)), poll.WithTimeout(defaultReconciliationTimeout))
  45. containerID := task.Status.ContainerStatus.ContainerID
  46. // wait for container to be healthy
  47. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  48. out, _ := d.Cmd("inspect", "--format={{.State.Health.Status}}", containerID)
  49. return strings.TrimSpace(out), ""
  50. }, checker.Equals("healthy")), poll.WithTimeout(defaultReconciliationTimeout))
  51. // make it fail
  52. d.Cmd("exec", containerID, "rm", "/status")
  53. // wait for container to be unhealthy
  54. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  55. out, _ := d.Cmd("inspect", "--format={{.State.Health.Status}}", containerID)
  56. return strings.TrimSpace(out), ""
  57. }, checker.Equals("unhealthy")), poll.WithTimeout(defaultReconciliationTimeout))
  58. // Task should be terminated
  59. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  60. task = d.GetTask(c, task.ID)
  61. return task.Status.State, ""
  62. }, checker.Equals(swarm.TaskStateFailed)), poll.WithTimeout(defaultReconciliationTimeout))
  63. if !strings.Contains(task.Status.Err, container.ErrContainerUnhealthy.Error()) {
  64. c.Fatal("unhealthy task exits because of other error")
  65. }
  66. }
  67. // start a service whose task is unhealthy at beginning
  68. // its tasks should be blocked in starting stage, until health check is passed
  69. func (s *DockerSwarmSuite) TestServiceHealthStart(c *testing.T) {
  70. testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
  71. d := s.AddDaemon(c, true, true)
  72. // service started from this image won't pass health check
  73. imageName := "testhealth"
  74. result := cli.BuildCmd(c, imageName, cli.Daemon(d),
  75. build.WithDockerfile(`FROM busybox
  76. HEALTHCHECK --interval=1s --timeout=1s --retries=1024\
  77. CMD cat /status`),
  78. )
  79. result.Assert(c, icmd.Success)
  80. serviceName := "healthServiceStart"
  81. out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")
  82. assert.NilError(c, err, out)
  83. id := strings.TrimSpace(out)
  84. var tasks []swarm.Task
  85. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  86. tasks = d.GetServiceTasks(c, id)
  87. return tasks, ""
  88. }, checker.HasLen(1)), poll.WithTimeout(defaultReconciliationTimeout))
  89. task := tasks[0]
  90. // wait for task to start
  91. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  92. task = d.GetTask(c, task.ID)
  93. return task.Status.State, ""
  94. }, checker.Equals(swarm.TaskStateStarting)), poll.WithTimeout(defaultReconciliationTimeout))
  95. containerID := task.Status.ContainerStatus.ContainerID
  96. // wait for health check to work
  97. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  98. out, _ := d.Cmd("inspect", "--format={{.State.Health.FailingStreak}}", containerID)
  99. failingStreak, _ := strconv.Atoi(strings.TrimSpace(out))
  100. return failingStreak, ""
  101. }, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
  102. // task should be blocked at starting status
  103. task = d.GetTask(c, task.ID)
  104. assert.Equal(c, task.Status.State, swarm.TaskStateStarting)
  105. // make it healthy
  106. d.Cmd("exec", containerID, "touch", "/status")
  107. // Task should be at running status
  108. poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
  109. task = d.GetTask(c, task.ID)
  110. return task.Status.State, ""
  111. }, checker.Equals(swarm.TaskStateRunning)), poll.WithTimeout(defaultReconciliationTimeout))
  112. }