2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2017-12-20 11:45:18 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-22 22:34:51 -04:00
|
|
|
"fmt"
|
2017-12-20 11:45:18 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-02-12 18:08:25 -05:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2017-12-20 11:45:18 -05:00
|
|
|
"github.com/docker/docker/client"
|
2018-02-12 18:08:25 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-12-20 11:45:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestHealthCheckWorkdir verifies that health-checks inherit the containers'
|
|
|
|
// working-dir.
|
|
|
|
func TestHealthCheckWorkdir(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2017-12-20 11:45:18 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2017-12-20 11:45:18 -05:00
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, client, container.WithTty(true), container.WithWorkingDir("/foo"), func(c *container.TestContainerConfig) {
|
2018-02-12 18:08:25 -05:00
|
|
|
c.Config.Healthcheck = &containertypes.HealthConfig{
|
|
|
|
Test: []string{"CMD-SHELL", "if [ \"$PWD\" = \"/foo\" ]; then exit 0; else exit 1; fi;"},
|
|
|
|
Interval: 50 * time.Millisecond,
|
|
|
|
Retries: 3,
|
|
|
|
}
|
|
|
|
})
|
2017-12-20 11:45:18 -05:00
|
|
|
|
2018-02-12 18:08:25 -05:00
|
|
|
poll.WaitOn(t, pollForHealthStatus(ctx, client, cID, types.Healthy), poll.WithDelay(100*time.Millisecond))
|
2017-12-20 11:45:18 -05:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:26:14 -04:00
|
|
|
// GitHub #37263
|
|
|
|
// Do not stop healthchecks just because we sent a signal to the container
|
|
|
|
func TestHealthKillContainer(t *testing.T) {
|
2019-07-19 10:09:50 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "Windows only supports SIGKILL and SIGTERM? See https://github.com/moby/moby/issues/39574")
|
2019-07-12 16:26:14 -04:00
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := testEnv.APIClient()
|
|
|
|
|
|
|
|
id := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
|
2021-10-21 15:27:07 -04:00
|
|
|
cmd := `
|
|
|
|
# Set the initial HEALTH value so the healthcheck passes
|
|
|
|
HEALTH="1"
|
|
|
|
echo $HEALTH > /health
|
|
|
|
|
|
|
|
# Any time doHealth is run we flip the value
|
|
|
|
# This lets us use kill signals to determine when healtchecks have run.
|
|
|
|
doHealth() {
|
|
|
|
case "$HEALTH" in
|
|
|
|
"0")
|
|
|
|
HEALTH="1"
|
|
|
|
;;
|
|
|
|
"1")
|
|
|
|
HEALTH="0"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
echo $HEALTH > /health
|
|
|
|
}
|
|
|
|
|
|
|
|
trap 'doHealth' USR1
|
|
|
|
|
|
|
|
while true; do sleep 1; done
|
|
|
|
`
|
|
|
|
c.Config.Cmd = []string{"/bin/sh", "-c", cmd}
|
2019-07-12 16:26:14 -04:00
|
|
|
c.Config.Healthcheck = &containertypes.HealthConfig{
|
2021-10-21 15:27:07 -04:00
|
|
|
Test: []string{"CMD-SHELL", `[ "$(cat /health)" = "1" ]`},
|
2019-07-12 16:26:14 -04:00
|
|
|
Interval: time.Second,
|
|
|
|
Retries: 5,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ctxPoll, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "healthy"), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
err := client.ContainerKill(ctx, id, "SIGUSR1")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2021-10-21 15:27:07 -04:00
|
|
|
ctxPoll, cancel = context.WithTimeout(ctx, 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "unhealthy"), poll.WithDelay(100*time.Millisecond))
|
|
|
|
|
|
|
|
err = client.ContainerKill(ctx, id, "SIGUSR1")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2019-07-12 16:26:14 -04:00
|
|
|
ctxPoll, cancel = context.WithTimeout(ctx, 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
poll.WaitOn(t, pollForHealthStatus(ctxPoll, client, id, "healthy"), poll.WithDelay(100*time.Millisecond))
|
|
|
|
}
|
|
|
|
|
2022-08-22 22:34:51 -04:00
|
|
|
// TestHealthCheckProcessKilled verifies that health-checks exec get killed on time-out.
|
|
|
|
func TestHealthCheckProcessKilled(t *testing.T) {
|
|
|
|
// FIXME: Broken on Windows + containerd combination
|
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
|
|
|
apiClient := testEnv.APIClient()
|
|
|
|
|
|
|
|
cID := container.Run(ctx, t, apiClient, func(c *container.TestContainerConfig) {
|
|
|
|
c.Config.Healthcheck = &containertypes.HealthConfig{
|
|
|
|
Test: []string{"CMD", "sh", "-c", "sleep 60"},
|
|
|
|
Interval: 100 * time.Millisecond,
|
|
|
|
Timeout: 50 * time.Millisecond,
|
|
|
|
Retries: 1,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
poll.WaitOn(t, pollForHealthCheckLog(ctx, apiClient, cID, "Health check exceeded timeout (50ms)"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func pollForHealthCheckLog(ctx context.Context, client client.APIClient, containerID string, expected string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
inspect, err := client.ContainerInspect(ctx, containerID)
|
|
|
|
if err != nil {
|
|
|
|
return poll.Error(err)
|
|
|
|
}
|
|
|
|
healthChecksTotal := len(inspect.State.Health.Log)
|
|
|
|
if healthChecksTotal > 0 {
|
|
|
|
output := inspect.State.Health.Log[healthChecksTotal-1].Output
|
|
|
|
if output == expected {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Error(fmt.Errorf("expected %q, got %q", expected, output))
|
|
|
|
}
|
|
|
|
return poll.Continue("waiting for container healthcheck logs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 11:45:18 -05:00
|
|
|
func pollForHealthStatus(ctx context.Context, client client.APIClient, containerID string, healthStatus string) func(log poll.LogT) poll.Result {
|
|
|
|
return func(log poll.LogT) poll.Result {
|
|
|
|
inspect, err := client.ContainerInspect(ctx, containerID)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return poll.Error(err)
|
|
|
|
case inspect.State.Health.Status == healthStatus:
|
|
|
|
return poll.Success()
|
|
|
|
default:
|
|
|
|
return poll.Continue("waiting for container to become %s", healthStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|