integration-cli: DockerCLIHealthSuite: replace dockerCmd and waitRun

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-27 13:45:34 +02:00
parent 707bb9b9ed
commit 58c5986f8b
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -9,6 +9,7 @@ import (
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"gotest.tools/v3/assert"
)
@ -29,7 +30,7 @@ func waitForHealthStatus(c *testing.T, name string, prev string, expected string
prev = prev + "\n"
expected = expected + "\n"
for {
out, _ := dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
out := cli.DockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name).Stdout()
if out == expected {
return
}
@ -42,7 +43,7 @@ func waitForHealthStatus(c *testing.T, name string, prev string, expected string
}
func getHealth(c *testing.T, name string) *types.Health {
out, _ := dockerCmd(c, "inspect", "--format={{json .State.Health}}", name)
out := cli.DockerCmd(c, "inspect", "--format={{json .State.Health}}", name).Stdout()
var health types.Health
err := json.Unmarshal([]byte(out), &health)
assert.Equal(c, err, nil)
@ -64,54 +65,54 @@ func (s *DockerCLIHealthSuite) TestHealth(c *testing.T) {
// No health status before starting
name := "test_health"
cid, _ := dockerCmd(c, "create", "--name", name, imageName)
out, _ := dockerCmd(c, "ps", "-a", "--format={{.ID}} {{.Status}}")
cid := cli.DockerCmd(c, "create", "--name", name, imageName).Stdout()
out := cli.DockerCmd(c, "ps", "-a", "--format={{.ID}} {{.Status}}").Stdout()
out = RemoveOutputForExistingElements(out, existingContainers)
assert.Equal(c, out, cid[:12]+" Created\n")
// Inspect the options
out, _ = dockerCmd(c, "inspect",
"--format=timeout={{.Config.Healthcheck.Timeout}} interval={{.Config.Healthcheck.Interval}} retries={{.Config.Healthcheck.Retries}} test={{.Config.Healthcheck.Test}}", name)
out = cli.DockerCmd(c, "inspect", "--format=timeout={{.Config.Healthcheck.Timeout}} interval={{.Config.Healthcheck.Interval}} retries={{.Config.Healthcheck.Retries}} test={{.Config.Healthcheck.Test}}", name).Stdout()
assert.Equal(c, out, "timeout=30s interval=1s retries=0 test=[CMD-SHELL cat /status]\n")
// Start
dockerCmd(c, "start", name)
cli.DockerCmd(c, "start", name)
waitForHealthStatus(c, name, "starting", "healthy")
// Make it fail
dockerCmd(c, "exec", name, "rm", "/status")
cli.DockerCmd(c, "exec", name, "rm", "/status")
waitForHealthStatus(c, name, "healthy", "unhealthy")
// Inspect the status
out, _ = dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
out = cli.DockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name).Stdout()
assert.Equal(c, out, "unhealthy\n")
// Make it healthy again
dockerCmd(c, "exec", name, "touch", "/status")
cli.DockerCmd(c, "exec", name, "touch", "/status")
waitForHealthStatus(c, name, "unhealthy", "healthy")
// Remove container
dockerCmd(c, "rm", "-f", name)
cli.DockerCmd(c, "rm", "-f", name)
// Disable the check from the CLI
dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
cli.DockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName)
out = cli.DockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh").Stdout()
assert.Equal(c, out, "[NONE]\n")
dockerCmd(c, "rm", "noh")
cli.DockerCmd(c, "rm", "noh")
// Disable the check with a new build
buildImageSuccessfully(c, "no_healthcheck", build.WithDockerfile(`FROM testhealth
HEALTHCHECK NONE`))
out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "no_healthcheck")
out = cli.DockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "no_healthcheck").Stdout()
assert.Equal(c, out, "[NONE]\n")
// Enable the checks from the CLI
_, _ = dockerCmd(c, "run", "-d", "--name=fatal_healthcheck",
cli.DockerCmd(c, "run", "-d", "--name=fatal_healthcheck",
"--health-interval=1s",
"--health-retries=3",
"--health-cmd=cat /status",
"no_healthcheck")
"no_healthcheck",
)
waitForHealthStatus(c, "fatal_healthcheck", "starting", "healthy")
health := getHealth(c, "fatal_healthcheck")
assert.Equal(c, health.Status, "healthy")
@ -121,27 +122,26 @@ func (s *DockerCLIHealthSuite) TestHealth(c *testing.T) {
assert.Equal(c, last.Output, "OK\n")
// Fail the check
dockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status")
cli.DockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status")
waitForHealthStatus(c, "fatal_healthcheck", "healthy", "unhealthy")
failsStr, _ := dockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck")
failsStr := cli.DockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck").Combined()
fails, err := strconv.Atoi(strings.TrimSpace(failsStr))
assert.Assert(c, err == nil)
assert.Check(c, err)
assert.Equal(c, fails >= 3, true)
dockerCmd(c, "rm", "-f", "fatal_healthcheck")
cli.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",
"--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1s", imageName)
cli.DockerCmd(c, "run", "-d", "--name=test", "--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1s", imageName)
waitForHealthStatus(c, "test", "starting", "unhealthy")
health = getHealth(c, "test")
last = health.Log[len(health.Log)-1]
assert.Equal(c, health.Status, "unhealthy")
assert.Equal(c, last.ExitCode, -1)
assert.Equal(c, last.Output, "Health check exceeded timeout (1s)")
dockerCmd(c, "rm", "-f", "test")
cli.DockerCmd(c, "rm", "-f", "test")
// Check JSON-format
buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
@ -150,8 +150,7 @@ func (s *DockerCLIHealthSuite) TestHealth(c *testing.T) {
STOPSIGNAL SIGKILL
HEALTHCHECK --interval=1s --timeout=30s \
CMD ["cat", "/my status"]`))
out, _ = dockerCmd(c, "inspect",
"--format={{.Config.Healthcheck.Test}}", imageName)
out = cli.DockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", imageName).Stdout()
assert.Equal(c, out, "[CMD cat /my status]\n")
}
@ -166,13 +165,13 @@ ENTRYPOINT /bin/sh -c "sleep 600"`))
name := "env_test_health"
// No health status before starting
dockerCmd(c, "run", "-d", "--name", name, "-e", "FOO", imageName)
cli.DockerCmd(c, "run", "-d", "--name", name, "-e", "FOO", imageName)
defer func() {
dockerCmd(c, "rm", "-f", name)
dockerCmd(c, "rmi", imageName)
cli.DockerCmd(c, "rm", "-f", name)
cli.DockerCmd(c, "rmi", imageName)
}()
// Start
dockerCmd(c, "start", name)
cli.DockerCmd(c, "start", name)
waitForHealthStatus(c, name, "starting", "healthy")
}