2016-07-12 13:56:01 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2016-07-12 13:56:01 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-07-12 13:56:01 -04:00
|
|
|
"github.com/docker/docker/daemon/cluster/executor/container"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2018-04-18 10:45:55 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2016-07-12 13:56:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// start a service, and then make its task unhealthy during running
|
|
|
|
// finally, unhealthy task should be detected and killed
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestServiceHealthRun(c *testing.T) {
|
2016-07-12 13:56:01 -04:00
|
|
|
testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
|
|
|
|
|
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
// build image with health-check
|
|
|
|
imageName := "testhealth"
|
2018-04-18 10:45:55 -04:00
|
|
|
result := cli.BuildCmd(c, imageName, cli.Daemon(d),
|
|
|
|
build.WithDockerfile(`FROM busybox
|
2016-07-12 13:56:01 -04:00
|
|
|
RUN touch /status
|
2019-07-17 21:27:28 -04:00
|
|
|
HEALTHCHECK --interval=1s --timeout=5s --retries=1\
|
2018-04-18 10:45:55 -04:00
|
|
|
CMD cat /status`),
|
|
|
|
)
|
|
|
|
result.Assert(c, icmd.Success)
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
serviceName := "healthServiceRun"
|
2017-05-18 15:23:28 -04:00
|
|
|
out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, out)
|
2016-07-12 13:56:01 -04:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-12-09 04:17:53 -05:00
|
|
|
tasks = d.GetServiceTasks(c, id)
|
2019-09-09 17:09:27 -04:00
|
|
|
return tasks, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.HasLen(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
task := tasks[0]
|
|
|
|
|
|
|
|
// wait for task to start
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return task.Status.State, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.Equals(swarm.TaskStateRunning)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
|
2016-07-12 13:56:01 -04:00
|
|
|
containerID := task.Status.ContainerStatus.ContainerID
|
|
|
|
|
|
|
|
// wait for container to be healthy
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-07-12 13:56:01 -04:00
|
|
|
out, _ := d.Cmd("inspect", "--format={{.State.Health.Status}}", containerID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return strings.TrimSpace(out), ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.Equals("healthy")), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
// make it fail
|
|
|
|
d.Cmd("exec", containerID, "rm", "/status")
|
|
|
|
// wait for container to be unhealthy
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-07-12 13:56:01 -04:00
|
|
|
out, _ := d.Cmd("inspect", "--format={{.State.Health.Status}}", containerID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return strings.TrimSpace(out), ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.Equals("unhealthy")), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
// Task should be terminated
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return task.Status.State, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.Equals(swarm.TaskStateFailed)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
if !strings.Contains(task.Status.Err, container.ErrContainerUnhealthy.Error()) {
|
|
|
|
c.Fatal("unhealthy task exits because of other error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start a service whose task is unhealthy at beginning
|
|
|
|
// its tasks should be blocked in starting stage, until health check is passed
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSwarmSuite) TestServiceHealthStart(c *testing.T) {
|
2016-07-12 13:56:01 -04:00
|
|
|
testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
|
|
|
|
|
|
|
|
d := s.AddDaemon(c, true, true)
|
|
|
|
|
|
|
|
// service started from this image won't pass health check
|
|
|
|
imageName := "testhealth"
|
2018-04-18 10:45:55 -04:00
|
|
|
result := cli.BuildCmd(c, imageName, cli.Daemon(d),
|
|
|
|
build.WithDockerfile(`FROM busybox
|
2016-07-12 13:56:01 -04:00
|
|
|
HEALTHCHECK --interval=1s --timeout=1s --retries=1024\
|
2018-04-18 10:45:55 -04:00
|
|
|
CMD cat /status`),
|
|
|
|
)
|
|
|
|
result.Assert(c, icmd.Success)
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
serviceName := "healthServiceStart"
|
2017-05-18 15:23:28 -04:00
|
|
|
out, err := d.Cmd("service", "create", "--no-resolve-image", "--detach=true", "--name", serviceName, imageName, "top")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, out)
|
2016-07-12 13:56:01 -04:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-12-09 04:17:53 -05:00
|
|
|
tasks = d.GetServiceTasks(c, id)
|
2019-09-09 17:09:27 -04:00
|
|
|
return tasks, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.HasLen(1)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
task := tasks[0]
|
|
|
|
|
|
|
|
// wait for task to start
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return task.Status.State, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.Equals(swarm.TaskStateStarting)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
containerID := task.Status.ContainerStatus.ContainerID
|
|
|
|
|
|
|
|
// wait for health check to work
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-07-12 13:56:01 -04:00
|
|
|
out, _ := d.Cmd("inspect", "--format={{.State.Health.FailingStreak}}", containerID)
|
|
|
|
failingStreak, _ := strconv.Atoi(strings.TrimSpace(out))
|
2019-09-09 17:09:27 -04:00
|
|
|
return failingStreak, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.GreaterThan(0)), poll.WithTimeout(defaultReconciliationTimeout))
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
// task should be blocked at starting status
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2019-09-09 17:05:56 -04:00
|
|
|
assert.Equal(c, task.Status.State, swarm.TaskStateStarting)
|
2016-07-12 13:56:01 -04:00
|
|
|
|
|
|
|
// make it healthy
|
|
|
|
d.Cmd("exec", containerID, "touch", "/status")
|
|
|
|
|
|
|
|
// Task should be at running status
|
2019-08-26 11:51:40 -04:00
|
|
|
poll.WaitOn(c, pollCheck(c, func(c *testing.T) (interface{}, string) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2019-09-09 17:09:27 -04:00
|
|
|
return task.Status.State, ""
|
2019-08-26 11:51:40 -04:00
|
|
|
}, checker.Equals(swarm.TaskStateRunning)), poll.WithTimeout(defaultReconciliationTimeout))
|
|
|
|
|
2016-07-12 13:56:01 -04:00
|
|
|
}
|