docker_cli_service_health_test.go 5.1 KB

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