docker_cli_service_health_test.go 4.9 KB

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