Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -9,6 +9,7 @@ import ( |
| 9 | 9 |
"time" |
| 10 | 10 |
|
| 11 | 11 |
"github.com/docker/docker/api/types" |
| 12 |
+ "github.com/docker/docker/integration-cli/cli" |
|
| 12 | 13 |
"github.com/docker/docker/integration-cli/cli/build" |
| 13 | 14 |
"gotest.tools/v3/assert" |
| 14 | 15 |
) |
| ... | ... |
@@ -29,7 +30,7 @@ func waitForHealthStatus(c *testing.T, name string, prev string, expected string |
| 29 | 29 |
prev = prev + "\n" |
| 30 | 30 |
expected = expected + "\n" |
| 31 | 31 |
for {
|
| 32 |
- out, _ := dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
|
|
| 32 |
+ out := cli.DockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name).Stdout()
|
|
| 33 | 33 |
if out == expected {
|
| 34 | 34 |
return |
| 35 | 35 |
} |
| ... | ... |
@@ -42,7 +43,7 @@ func waitForHealthStatus(c *testing.T, name string, prev string, expected string |
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 | 44 |
func getHealth(c *testing.T, name string) *types.Health {
|
| 45 |
- out, _ := dockerCmd(c, "inspect", "--format={{json .State.Health}}", name)
|
|
| 45 |
+ out := cli.DockerCmd(c, "inspect", "--format={{json .State.Health}}", name).Stdout()
|
|
| 46 | 46 |
var health types.Health |
| 47 | 47 |
err := json.Unmarshal([]byte(out), &health) |
| 48 | 48 |
assert.Equal(c, err, nil) |
| ... | ... |
@@ -64,54 +65,54 @@ func (s *DockerCLIHealthSuite) TestHealth(c *testing.T) {
|
| 64 | 64 |
|
| 65 | 65 |
// No health status before starting |
| 66 | 66 |
name := "test_health" |
| 67 |
- cid, _ := dockerCmd(c, "create", "--name", name, imageName) |
|
| 68 |
- out, _ := dockerCmd(c, "ps", "-a", "--format={{.ID}} {{.Status}}")
|
|
| 67 |
+ cid := cli.DockerCmd(c, "create", "--name", name, imageName).Stdout() |
|
| 68 |
+ out := cli.DockerCmd(c, "ps", "-a", "--format={{.ID}} {{.Status}}").Stdout()
|
|
| 69 | 69 |
out = RemoveOutputForExistingElements(out, existingContainers) |
| 70 | 70 |
assert.Equal(c, out, cid[:12]+" Created\n") |
| 71 | 71 |
|
| 72 | 72 |
// Inspect the options |
| 73 |
- out, _ = dockerCmd(c, "inspect", |
|
| 74 |
- "--format=timeout={{.Config.Healthcheck.Timeout}} interval={{.Config.Healthcheck.Interval}} retries={{.Config.Healthcheck.Retries}} test={{.Config.Healthcheck.Test}}", name)
|
|
| 73 |
+ out = cli.DockerCmd(c, "inspect", "--format=timeout={{.Config.Healthcheck.Timeout}} interval={{.Config.Healthcheck.Interval}} retries={{.Config.Healthcheck.Retries}} test={{.Config.Healthcheck.Test}}", name).Stdout()
|
|
| 75 | 74 |
assert.Equal(c, out, "timeout=30s interval=1s retries=0 test=[CMD-SHELL cat /status]\n") |
| 76 | 75 |
|
| 77 | 76 |
// Start |
| 78 |
- dockerCmd(c, "start", name) |
|
| 77 |
+ cli.DockerCmd(c, "start", name) |
|
| 79 | 78 |
waitForHealthStatus(c, name, "starting", "healthy") |
| 80 | 79 |
|
| 81 | 80 |
// Make it fail |
| 82 |
- dockerCmd(c, "exec", name, "rm", "/status") |
|
| 81 |
+ cli.DockerCmd(c, "exec", name, "rm", "/status") |
|
| 83 | 82 |
waitForHealthStatus(c, name, "healthy", "unhealthy") |
| 84 | 83 |
|
| 85 | 84 |
// Inspect the status |
| 86 |
- out, _ = dockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name)
|
|
| 85 |
+ out = cli.DockerCmd(c, "inspect", "--format={{.State.Health.Status}}", name).Stdout()
|
|
| 87 | 86 |
assert.Equal(c, out, "unhealthy\n") |
| 88 | 87 |
|
| 89 | 88 |
// Make it healthy again |
| 90 |
- dockerCmd(c, "exec", name, "touch", "/status") |
|
| 89 |
+ cli.DockerCmd(c, "exec", name, "touch", "/status") |
|
| 91 | 90 |
waitForHealthStatus(c, name, "unhealthy", "healthy") |
| 92 | 91 |
|
| 93 | 92 |
// Remove container |
| 94 |
- dockerCmd(c, "rm", "-f", name) |
|
| 93 |
+ cli.DockerCmd(c, "rm", "-f", name) |
|
| 95 | 94 |
|
| 96 | 95 |
// Disable the check from the CLI |
| 97 |
- dockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName) |
|
| 98 |
- out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh")
|
|
| 96 |
+ cli.DockerCmd(c, "create", "--name=noh", "--no-healthcheck", imageName) |
|
| 97 |
+ out = cli.DockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "noh").Stdout()
|
|
| 99 | 98 |
assert.Equal(c, out, "[NONE]\n") |
| 100 |
- dockerCmd(c, "rm", "noh") |
|
| 99 |
+ cli.DockerCmd(c, "rm", "noh") |
|
| 101 | 100 |
|
| 102 | 101 |
// Disable the check with a new build |
| 103 | 102 |
buildImageSuccessfully(c, "no_healthcheck", build.WithDockerfile(`FROM testhealth |
| 104 | 103 |
HEALTHCHECK NONE`)) |
| 105 | 104 |
|
| 106 |
- out, _ = dockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "no_healthcheck")
|
|
| 105 |
+ out = cli.DockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", "no_healthcheck").Stdout()
|
|
| 107 | 106 |
assert.Equal(c, out, "[NONE]\n") |
| 108 | 107 |
|
| 109 | 108 |
// Enable the checks from the CLI |
| 110 |
- _, _ = dockerCmd(c, "run", "-d", "--name=fatal_healthcheck", |
|
| 109 |
+ cli.DockerCmd(c, "run", "-d", "--name=fatal_healthcheck", |
|
| 111 | 110 |
"--health-interval=1s", |
| 112 | 111 |
"--health-retries=3", |
| 113 | 112 |
"--health-cmd=cat /status", |
| 114 |
- "no_healthcheck") |
|
| 113 |
+ "no_healthcheck", |
|
| 114 |
+ ) |
|
| 115 | 115 |
waitForHealthStatus(c, "fatal_healthcheck", "starting", "healthy") |
| 116 | 116 |
health := getHealth(c, "fatal_healthcheck") |
| 117 | 117 |
assert.Equal(c, health.Status, "healthy") |
| ... | ... |
@@ -121,27 +122,26 @@ func (s *DockerCLIHealthSuite) TestHealth(c *testing.T) {
|
| 121 | 121 |
assert.Equal(c, last.Output, "OK\n") |
| 122 | 122 |
|
| 123 | 123 |
// Fail the check |
| 124 |
- dockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status") |
|
| 124 |
+ cli.DockerCmd(c, "exec", "fatal_healthcheck", "rm", "/status") |
|
| 125 | 125 |
waitForHealthStatus(c, "fatal_healthcheck", "healthy", "unhealthy") |
| 126 | 126 |
|
| 127 |
- failsStr, _ := dockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck")
|
|
| 127 |
+ failsStr := cli.DockerCmd(c, "inspect", "--format={{.State.Health.FailingStreak}}", "fatal_healthcheck").Combined()
|
|
| 128 | 128 |
fails, err := strconv.Atoi(strings.TrimSpace(failsStr)) |
| 129 |
- assert.Assert(c, err == nil) |
|
| 129 |
+ assert.Check(c, err) |
|
| 130 | 130 |
assert.Equal(c, fails >= 3, true) |
| 131 |
- dockerCmd(c, "rm", "-f", "fatal_healthcheck") |
|
| 131 |
+ cli.DockerCmd(c, "rm", "-f", "fatal_healthcheck") |
|
| 132 | 132 |
|
| 133 | 133 |
// Check timeout |
| 134 | 134 |
// Note: if the interval is too small, it seems that Docker spends all its time running health |
| 135 | 135 |
// checks and never gets around to killing it. |
| 136 |
- _, _ = dockerCmd(c, "run", "-d", "--name=test", |
|
| 137 |
- "--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1s", imageName) |
|
| 136 |
+ cli.DockerCmd(c, "run", "-d", "--name=test", "--health-interval=1s", "--health-cmd=sleep 5m", "--health-timeout=1s", imageName) |
|
| 138 | 137 |
waitForHealthStatus(c, "test", "starting", "unhealthy") |
| 139 | 138 |
health = getHealth(c, "test") |
| 140 | 139 |
last = health.Log[len(health.Log)-1] |
| 141 | 140 |
assert.Equal(c, health.Status, "unhealthy") |
| 142 | 141 |
assert.Equal(c, last.ExitCode, -1) |
| 143 | 142 |
assert.Equal(c, last.Output, "Health check exceeded timeout (1s)") |
| 144 |
- dockerCmd(c, "rm", "-f", "test") |
|
| 143 |
+ cli.DockerCmd(c, "rm", "-f", "test") |
|
| 145 | 144 |
|
| 146 | 145 |
// Check JSON-format |
| 147 | 146 |
buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox |
| ... | ... |
@@ -150,8 +150,7 @@ func (s *DockerCLIHealthSuite) TestHealth(c *testing.T) {
|
| 150 | 150 |
STOPSIGNAL SIGKILL |
| 151 | 151 |
HEALTHCHECK --interval=1s --timeout=30s \ |
| 152 | 152 |
CMD ["cat", "/my status"]`)) |
| 153 |
- out, _ = dockerCmd(c, "inspect", |
|
| 154 |
- "--format={{.Config.Healthcheck.Test}}", imageName)
|
|
| 153 |
+ out = cli.DockerCmd(c, "inspect", "--format={{.Config.Healthcheck.Test}}", imageName).Stdout()
|
|
| 155 | 154 |
assert.Equal(c, out, "[CMD cat /my status]\n") |
| 156 | 155 |
} |
| 157 | 156 |
|
| ... | ... |
@@ -166,13 +165,13 @@ ENTRYPOINT /bin/sh -c "sleep 600"`)) |
| 166 | 166 |
|
| 167 | 167 |
name := "env_test_health" |
| 168 | 168 |
// No health status before starting |
| 169 |
- dockerCmd(c, "run", "-d", "--name", name, "-e", "FOO", imageName) |
|
| 169 |
+ cli.DockerCmd(c, "run", "-d", "--name", name, "-e", "FOO", imageName) |
|
| 170 | 170 |
defer func() {
|
| 171 |
- dockerCmd(c, "rm", "-f", name) |
|
| 172 |
- dockerCmd(c, "rmi", imageName) |
|
| 171 |
+ cli.DockerCmd(c, "rm", "-f", name) |
|
| 172 |
+ cli.DockerCmd(c, "rmi", imageName) |
|
| 173 | 173 |
}() |
| 174 | 174 |
|
| 175 | 175 |
// Start |
| 176 |
- dockerCmd(c, "start", name) |
|
| 176 |
+ cli.DockerCmd(c, "start", name) |
|
| 177 | 177 |
waitForHealthStatus(c, name, "starting", "healthy") |
| 178 | 178 |
} |