2016-07-12 13:56:01 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
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"
|
2016-07-12 13:56:01 -04:00
|
|
|
"github.com/go-check/check"
|
2018-04-18 10:45:55 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/icmd"
|
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
|
|
|
|
func (s *DockerSwarmSuite) TestServiceHealthRun(c *check.C) {
|
|
|
|
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
|
|
|
|
HEALTHCHECK --interval=1s --timeout=1s --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")
|
2016-07-12 13:56:01 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
tasks = d.GetServiceTasks(c, id)
|
2016-07-12 13:56:01 -04:00
|
|
|
return tasks, nil
|
|
|
|
}, checker.HasLen, 1)
|
|
|
|
|
|
|
|
task := tasks[0]
|
|
|
|
|
|
|
|
// wait for task to start
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2016-07-12 13:56:01 -04:00
|
|
|
return task.Status.State, nil
|
2016-09-29 10:44:00 -04:00
|
|
|
}, checker.Equals, swarm.TaskStateRunning)
|
2016-07-12 13:56:01 -04:00
|
|
|
containerID := task.Status.ContainerStatus.ContainerID
|
|
|
|
|
|
|
|
// wait for container to be healthy
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
out, _ := d.Cmd("inspect", "--format={{.State.Health.Status}}", containerID)
|
|
|
|
return strings.TrimSpace(out), nil
|
|
|
|
}, checker.Equals, "healthy")
|
|
|
|
|
|
|
|
// make it fail
|
|
|
|
d.Cmd("exec", containerID, "rm", "/status")
|
|
|
|
// wait for container to be unhealthy
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
out, _ := d.Cmd("inspect", "--format={{.State.Health.Status}}", containerID)
|
|
|
|
return strings.TrimSpace(out), nil
|
|
|
|
}, checker.Equals, "unhealthy")
|
|
|
|
|
|
|
|
// Task should be terminated
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2016-07-12 13:56:01 -04:00
|
|
|
return task.Status.State, nil
|
|
|
|
}, checker.Equals, swarm.TaskStateFailed)
|
|
|
|
|
|
|
|
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
|
|
|
|
func (s *DockerSwarmSuite) TestServiceHealthStart(c *check.C) {
|
|
|
|
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")
|
2016-07-12 13:56:01 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(out))
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
var tasks []swarm.Task
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
tasks = d.GetServiceTasks(c, id)
|
2016-07-12 13:56:01 -04:00
|
|
|
return tasks, nil
|
|
|
|
}, checker.HasLen, 1)
|
|
|
|
|
|
|
|
task := tasks[0]
|
|
|
|
|
|
|
|
// wait for task to start
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2016-07-12 13:56:01 -04:00
|
|
|
return task.Status.State, nil
|
|
|
|
}, checker.Equals, swarm.TaskStateStarting)
|
|
|
|
|
|
|
|
containerID := task.Status.ContainerStatus.ContainerID
|
|
|
|
|
|
|
|
// wait for health check to work
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
out, _ := d.Cmd("inspect", "--format={{.State.Health.FailingStreak}}", containerID)
|
|
|
|
failingStreak, _ := strconv.Atoi(strings.TrimSpace(out))
|
|
|
|
return failingStreak, nil
|
|
|
|
}, checker.GreaterThan, 0)
|
|
|
|
|
|
|
|
// task should be blocked at starting status
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2016-07-12 13:56:01 -04:00
|
|
|
c.Assert(task.Status.State, check.Equals, swarm.TaskStateStarting)
|
|
|
|
|
|
|
|
// make it healthy
|
|
|
|
d.Cmd("exec", containerID, "touch", "/status")
|
|
|
|
|
|
|
|
// Task should be at running status
|
|
|
|
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 04:17:53 -05:00
|
|
|
task = d.GetTask(c, task.ID)
|
2016-07-12 13:56:01 -04:00
|
|
|
return task.Status.State, nil
|
|
|
|
}, checker.Equals, swarm.TaskStateRunning)
|
|
|
|
}
|