Created
January 3, 2017 09:24
-
-
Save vdemeester/4321846a47e513faf1047a46cfa92bda to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" | |
_, _, err := d.BuildImageWithOut(imageName, | |
`FROM busybox | |
HEALTHCHECK --interval=1s --timeout=1s --retries=1024\ | |
CMD cat /status`, | |
true) | |
c.Check(err, check.IsNil) | |
serviceName := "healthServiceStart" | |
out, err := d.Cmd("service", "create", "--name", serviceName, imageName, "top") | |
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) { | |
tasks = d.GetServiceTasks(c, id) | |
return tasks, nil | |
}, checker.HasLen, 1) | |
task := tasks[0] | |
// wait for task to start | |
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) { | |
task = d.GetTask(c, task.ID) | |
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 | |
task = d.GetTask(c, task.ID) | |
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) { | |
task = d.GetTask(c, task.ID) | |
return task.Status.State, nil | |
}, checker.Equals, swarm.TaskStateRunning) | |
} | |
// 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) TestServiceHealthUpdate(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" | |
_, _, err := d.BuildImageWithOut(imageName, | |
`FROM busybox | |
HEALTHCHECK --interval=1s --timeout=1s --retries=1024\ | |
CMD cat /status`, | |
true) | |
c.Check(err, check.IsNil) | |
serviceName := "healthServiceStart" | |
out, err := d.Cmd("service", "create", "--name", serviceName, imageName, "top") | |
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) { | |
tasks = d.GetServiceTasks(c, id) | |
return tasks, nil | |
}, checker.HasLen, 1) | |
task := tasks[0] | |
// wait for task to start | |
waitAndAssert(c, defaultReconciliationTimeout, func(c *check.C) (interface{}, check.CommentInterface) { | |
task = d.GetTask(c, task.ID) | |
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 | |
task = d.GetTask(c, task.ID) | |
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) { | |
task = d.GetTask(c, task.ID) | |
return task.Status.State, nil | |
}, checker.Equals, swarm.TaskStateRunning) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment