2016-04-18 09:48:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2016-04-18 09:48:13 +00:00
|
|
|
"time"
|
2016-07-15 18:21:19 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-03-23 17:35:22 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2016-04-18 09:48:13 +00:00
|
|
|
)
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func waitForHealthStatus(c *testing.T, name string, prev string, expected string) {
|
2016-04-18 09:48:13 +00:00
|
|
|
prev = prev + "\n"
|
|
|
|
expected = expected + "\n"
|
|
|
|
for {
|
|
|
|
out, _ := dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
|
|
|
|
if out == expected {
|
|
|
|
return
|
|
|
|
}
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, prev)
|
2016-04-18 09:48:13 +00:00
|
|
|
if out != prev {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func getHealth(c *testing.T, name string) *types.Health {
|
2016-04-18 09:48:13 +00:00
|
|
|
out, _ := dockerCmd(c, "inspect", "--format={{json .State.Health}}", name)
|
|
|
|
var health types.Health
|
|
|
|
err := json.Unmarshal([]byte(out), &health)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, err, nil)
|
2016-04-18 09:48:13 +00:00
|
|
|
return &health
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestHealth(c *testing.T) {
|
2016-04-18 09:48:13 +00:00
|
|
|
testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
|
|
|
|
|
2017-09-12 12:53:20 +00:00
|
|
|
existingContainers := ExistingContainerIDs(c)
|
2017-09-08 15:16:15 +00:00
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
imageName := "testhealth"
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
|
2016-04-18 09:48:13 +00:00
|
|
|
RUN echo OK > /status
|
|
|
|
CMD ["/bin/sleep", "120"]
|
|
|
|
STOPSIGNAL SIGKILL
|
|
|
|
HEALTHCHECK --interval=1s --timeout=30s \
|
2017-01-16 10:30:14 +00:00
|
|
|
CMD cat /status`))
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
// No health status before starting
|
|
|
|
name := "test_health"
|
2017-09-12 12:53:20 +00:00
|
|
|
cid, _ := dockerCmd(c, "create", "--name", name, imageName)
|
|
|
|
out, _ := dockerCmd(c, "ps", "-a", "--format={{.ID}} {{.Status}}")
|
2017-09-08 15:16:15 +00:00
|
|
|
out = RemoveOutputForExistingElements(out, existingContainers)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, cid[:12]+" Created\n")
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
// Inspect the options
|
|
|
|
out, _ = dockerCmd(c, "inspect",
|
2017-03-23 17:35:22 +00:00
|
|
|
"--format=timeout={{.Config.Healthcheck.Timeout}} interval={{.Config.Healthcheck.Interval}} retries={{.Config.Healthcheck.Retries}} test={{.Config.Healthcheck.Test}}", name)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, "timeout=30s interval=1s retries=0 test=[CMD-SHELL cat /status]\n")
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
// Start
|
|
|
|
dockerCmd(c, "start", name)
|
|
|
|
waitForHealthStatus(c, name, "starting", "healthy")
|
|
|
|
|
|
|
|
// Make it fail
|
|
|
|
dockerCmd(c, "exec", name, "rm", "/status")
|
|
|
|
waitForHealthStatus(c, name, "healthy", "unhealthy")
|
|
|
|
|
|
|
|
// Inspect the status
|
|
|
|
out, _ = dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, "unhealthy\n")
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
// Make it healthy again
|
|
|
|
dockerCmd(c, "exec", name, "touch", "/status")
|
|
|
|
waitForHealthStatus(c, name, "unhealthy", "healthy")
|
|
|
|
|
|
|
|
// Remove container
|
|
|
|
dockerCmd(c, "rm", "-f", name)
|
|
|
|
|
|
|
|
// Disable the check from the CLI
|
2018-07-09 17:40:34 +00:00
|
|
|
dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
|
2016-04-18 09:48:13 +00:00
|
|
|
out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, "[NONE]\n")
|
2016-04-18 09:48:13 +00:00
|
|
|
dockerCmd(c, "rm", "noh")
|
|
|
|
|
|
|
|
// Disable the check with a new build
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "no_healthcheck", build.WithDockerfile(`FROM testhealth
|
2017-01-16 10:30:14 +00:00
|
|
|
HEALTHCHECK NONE`))
|
2016-04-18 09:48:13 +00:00
|
|
|
|
2018-05-23 20:44:15 +00:00
|
|
|
out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "no_healthcheck")
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, "[NONE]\n")
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
// Enable the checks from the CLI
|
|
|
|
_, _ = dockerCmd(c, "run", "-d", "--name=fatal_healthcheck",
|
2017-01-17 07:55:45 +00:00
|
|
|
"--health-interval=1s",
|
2016-04-18 09:48:13 +00:00
|
|
|
"--health-retries=3",
|
|
|
|
"--health-cmd=cat /status",
|
|
|
|
"no_healthcheck")
|
|
|
|
waitForHealthStatus(c, "fatal_healthcheck", "starting", "healthy")
|
|
|
|
health := getHealth(c, "fatal_healthcheck")
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, health.Status, "healthy")
|
|
|
|
assert.Equal(c, health.FailingStreak, 0)
|
2016-04-18 09:48:13 +00:00
|
|
|
last := health.Log[len(health.Log)-1]
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, last.ExitCode, 0)
|
|
|
|
assert.Equal(c, last.Output, "OK\n")
|
2016-04-18 09:48:13 +00:00
|
|
|
|
2016-06-16 12:07:06 +00:00
|
|
|
// Fail the check
|
2016-04-18 09:48:13 +00:00
|
|
|
dockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status")
|
2016-06-16 12:07:06 +00:00
|
|
|
waitForHealthStatus(c, "fatal_healthcheck", "healthy", "unhealthy")
|
2016-04-18 09:48:13 +00:00
|
|
|
|
|
|
|
failsStr, _ := dockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck")
|
|
|
|
fails, err := strconv.Atoi(strings.TrimSpace(failsStr))
|
2019-09-09 21:05:57 +00:00
|
|
|
assert.Assert(c, err == nil)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, fails >= 3, true)
|
2016-04-18 09:48:13 +00:00
|
|
|
dockerCmd(c, "rm", "-f", "fatal_healthcheck")
|
|
|
|
|
|
|
|
// Check timeout
|
|
|
|
// Note: if the interval is too small, it seems that Docker spends all its time running health
|
|
|
|
// checks and never gets around to killing it.
|
|
|
|
_, _ = dockerCmd(c, "run", "-d", "--name=test",
|
2017-01-17 07:55:45 +00:00
|
|
|
"--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1s", imageName)
|
2016-04-18 09:48:13 +00:00
|
|
|
waitForHealthStatus(c, "test", "starting", "unhealthy")
|
|
|
|
health = getHealth(c, "test")
|
|
|
|
last = health.Log[len(health.Log)-1]
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, health.Status, "unhealthy")
|
|
|
|
assert.Equal(c, last.ExitCode, -1)
|
|
|
|
assert.Equal(c, last.Output, "Health check exceeded timeout (1s)")
|
2016-04-18 09:48:13 +00:00
|
|
|
dockerCmd(c, "rm", "-f", "test")
|
2016-08-31 16:36:10 +00:00
|
|
|
|
|
|
|
// Check JSON-format
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
|
2016-08-31 16:36:10 +00:00
|
|
|
RUN echo OK > /status
|
|
|
|
CMD ["/bin/sleep", "120"]
|
|
|
|
STOPSIGNAL SIGKILL
|
|
|
|
HEALTHCHECK --interval=1s --timeout=30s \
|
2017-01-16 10:30:14 +00:00
|
|
|
CMD ["cat", "/my status"]`))
|
2016-08-31 16:36:10 +00:00
|
|
|
out, _ = dockerCmd(c, "inspect",
|
|
|
|
"--format={{.Config.Healthcheck.Test}}", imageName)
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, out, "[CMD cat /my status]\n")
|
2016-08-31 16:36:10 +00:00
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
2017-05-17 12:30:51 +00:00
|
|
|
|
2017-10-04 16:38:09 +00:00
|
|
|
// GitHub #33021
|
2019-09-09 21:05:55 +00:00
|
|
|
func (s *DockerSuite) TestUnsetEnvVarHealthCheck(c *testing.T) {
|
2017-05-17 12:30:51 +00:00
|
|
|
testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
|
|
|
|
|
|
|
|
imageName := "testhealth"
|
|
|
|
buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
|
|
|
|
HEALTHCHECK --interval=1s --timeout=5s --retries=5 CMD /bin/sh -c "sleep 1"
|
|
|
|
ENTRYPOINT /bin/sh -c "sleep 600"`))
|
|
|
|
|
|
|
|
name := "env_test_health"
|
|
|
|
// No health status before starting
|
|
|
|
dockerCmd(c, "run", "-d", "--name", name, "-e", "FOO", imageName)
|
|
|
|
defer func() {
|
|
|
|
dockerCmd(c, "rm", "-f", name)
|
|
|
|
dockerCmd(c, "rmi", imageName)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start
|
|
|
|
dockerCmd(c, "start", name)
|
|
|
|
waitForHealthStatus(c, name, "starting", "healthy")
|
|
|
|
|
|
|
|
}
|