Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions cmd/nerdctl/container/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,6 @@ func createOptions(cmd *cobra.Command) (types.ContainerCreateOptions, error) {
if err != nil {
return opt, err
}
opt.HealthStartInterval, err = cmd.Flags().GetDuration("health-start-interval")
if err != nil {
return opt, err
}
opt.NoHealthcheck, err = cmd.Flags().GetBool("no-healthcheck")
if err != nil {
return opt, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import (
"github.com/containerd/nerdctl/mod/tigron/tig"

"github.com/containerd/nerdctl/v2/pkg/healthcheck"
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
)

func TestContainerHealthCheckBasic(t *testing.T) {

testCase := nerdtest.Setup()

// Docker CLI does not provide a standalone healthcheck command.
Expand Down Expand Up @@ -602,3 +604,320 @@ func TestContainerHealthCheckAdvance(t *testing.T) {

testCase.Run(t)
}

func TestHealthCheck_SystemdIntegration_Basic(t *testing.T) {
testCase := nerdtest.Setup()
testCase.Require = require.Not(nerdtest.Docker)
// Skip systemd tests in rootless environment to bypass dbus permission issues
if rootlessutil.IsRootless() {
t.Skip("systemd healthcheck tests are skipped in rootless environment")
}

testCase.SubTests = []*test.Case{
{
Description: "Basic healthy container with systemd-triggered healthcheck",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(),
"--health-cmd", "echo healthy",
"--health-interval", "2s",
testutil.CommonImage, "sleep", "30")
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
// Ensure proper cleanup of systemd units
helpers.Anyhow("stop", data.Identifier())
helpers.Anyhow("rm", "-f", data.Identifier())
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Output: expect.All(func(stdout string, t tig.T) {
var h *healthcheck.Health

// Poll up to 5 times for health status
maxAttempts := 5
var finalStatus string

for i := 0; i < maxAttempts; i++ {
inspect := nerdtest.InspectContainer(helpers, data.Identifier())
h = inspect.State.Health

assert.Assert(t, h != nil, "expected health state to be present")
finalStatus = h.Status

// If healthy, break and pass the test
if finalStatus == "healthy" {
t.Log(fmt.Sprintf("Container became healthy on attempt %d/%d", i+1, maxAttempts))
break
}

// If unhealthy, fail immediately
if finalStatus == "unhealthy" {
assert.Assert(t, false, fmt.Sprintf("Container became unhealthy on attempt %d/%d, status: %s", i+1, maxAttempts, finalStatus))
return
}

// If not the last attempt, wait before retrying
if i < maxAttempts-1 {
t.Log(fmt.Sprintf("Attempt %d/%d: status is '%s', waiting 1 second before retry", i+1, maxAttempts, finalStatus))
time.Sleep(1 * time.Second)
}
}

if finalStatus != "healthy" {
assert.Assert(t, false, fmt.Sprintf("Container did not become healthy after %d attempts, final status: %s", maxAttempts, finalStatus))
return
}

assert.Assert(t, len(h.Log) > 0, "expected at least one health check log entry")
}),
}
},
},
{
Description: "Kill stops healthcheck execution and cleans up systemd timer",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(),
"--health-cmd", "echo healthy",
"--health-interval", "1s",
testutil.CommonImage, "sleep", "30")
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
helpers.Ensure("kill", data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
// Container is already killed, just remove it
helpers.Anyhow("rm", "-f", data.Identifier())
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, t tig.T) {
// Get container info for verification
inspect := nerdtest.InspectContainer(helpers, data.Identifier())
containerID := inspect.ID
h := inspect.State.Health

// Verify health state and logs exist
assert.Assert(t, h != nil, "expected health state to be present")
assert.Assert(t, len(h.Log) > 0, "expected at least one health check log entry")

// Get container FinishedAt timestamp
containerEnd, err := time.Parse(time.RFC3339Nano, inspect.State.FinishedAt)
assert.NilError(t, err, "parsing container FinishedAt")

// Assert all healthcheck log start times are before container finished
for _, entry := range h.Log {
assert.Assert(t, entry.Start.Before(containerEnd), "healthcheck ran after container was killed")
}

// Ensure systemd timers are removed
result := helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, _ tig.T) {
assert.Assert(t, !strings.Contains(stdout, containerID),
"expected nerdctl healthcheck timer for container ID %s to be removed after container stop", containerID)
},
})
},
}
},
},
{
Description: "Remove cleans up systemd timer",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(),
"--health-cmd", "echo healthy",
"--health-interval", "1s",
testutil.CommonImage, "sleep", "30")
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
helpers.Ensure("rm", "-f", data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
// Container is already removed, no cleanup needed
helpers.Anyhow("rm", "-f", data.Identifier())
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, t tig.T) {
inspect := nerdtest.InspectContainer(helpers, data.Identifier())
containerID := inspect.ID

// Check systemd timers to ensure cleanup
result := helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, _ tig.T) {
// Verify systemd timer has been cleaned up by checking systemctl output
// We check that no timer contains our test identifier
assert.Assert(t, !strings.Contains(stdout, containerID),
"expected nerdctl healthcheck timer for container ID %s to be removed after container removal", containerID)
},
})
},
}
},
},
{
Description: "Stop cleans up systemd timer",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(),
"--health-cmd", "echo healthy",
"--health-interval", "1s",
testutil.CommonImage, "sleep", "30")
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
helpers.Ensure("stop", data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
// Container is already stopped, just remove it
helpers.Anyhow("rm", "-f", data.Identifier())
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, t tig.T) {
// Get container info for verification
inspect := nerdtest.InspectContainer(helpers, data.Identifier())
containerID := inspect.ID

// Ensure systemd timers are removed
result := helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, _ tig.T) {
assert.Assert(t, !strings.Contains(stdout, containerID),
"expected nerdctl healthcheck timer for container ID %s to be removed after container stop", containerID)
},
})
},
}
},
},
}
testCase.Run(t)
}

func TestHealthCheck_SystemdIntegration_Advanced(t *testing.T) {

testCase := nerdtest.Setup()
testCase.Require = require.Not(nerdtest.Docker)
// Skip systemd tests in rootless environment to bypass dbus permission issues
if rootlessutil.IsRootless() {
t.Skip("systemd healthcheck tests are skipped in rootless environment")
}

testCase.SubTests = []*test.Case{
{
// Tests that CreateTimer() successfully creates systemd timer units and
// RemoveTransientHealthCheckFiles() properly cleans up units when container stops.
Description: "Systemd timer unit creation and cleanup",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(),
"--health-cmd", "echo healthy",
"--health-interval", "1s",
testutil.CommonImage, "sleep", "30")
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("inspect", data.Identifier())
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0,
Output: expect.All(func(stdout string, t tig.T) {
// Get container ID and check systemd timer
containerInspect := nerdtest.InspectContainer(helpers, data.Identifier())
containerID := containerInspect.ID

// Check systemd timer
result := helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, _ tig.T) {
// Verify that a timer exists for this specific container
assert.Assert(t, strings.Contains(stdout, containerID),
"expected to find nerdctl healthcheck timer containing container ID: %s", containerID)
},
})
// Stop container and verify cleanup
helpers.Ensure("stop", data.Identifier())

// Check that timer is gone
result = helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, _ tig.T) {
assert.Assert(t, !strings.Contains(stdout, containerID),
"expected nerdctl healthcheck timer for container ID %s to be removed after container stop", containerID)
},
})
}),
}
},
},
{
Description: "Container restart recreates systemd timer",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Ensure("run", "-d", "--name", data.Identifier(),
"--health-cmd", "echo restart-test",
"--health-interval", "2s",
testutil.CommonImage, "sleep", "60")
nerdtest.EnsureContainerStarted(helpers, data.Identifier())
},
Cleanup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
// Get container ID for verification
containerInspect := nerdtest.InspectContainer(helpers, data.Identifier())
containerID := containerInspect.ID

// Step 1: Verify timer exists initially
result := helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, t tig.T) {
assert.Assert(t, strings.Contains(stdout, containerID),
"expected timer for container %s to exist initially", containerID)
},
})

// Step 2: Stop container
helpers.Ensure("stop", data.Identifier())

// Step 3: Verify timer is removed after stop
result = helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
result.Run(&test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, t tig.T) {
assert.Assert(t, !strings.Contains(stdout, containerID),
"expected timer for container %s to be removed after stop", containerID)
},
})

// Step 4: Restart container
helpers.Ensure("start", data.Identifier())
nerdtest.EnsureContainerStarted(helpers, data.Identifier())

// Step 5: Verify timer is recreated after restart - this is our final verification
return helpers.Custom("systemctl", "list-timers", "--all", "--no-pager")
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: expect.ExitCodeNoCheck,
Output: func(stdout string, t tig.T) {
containerInspect := nerdtest.InspectContainer(helpers, data.Identifier())
containerID := containerInspect.ID
assert.Assert(t, strings.Contains(stdout, containerID),
"expected timer for container %s to be recreated after restart", containerID)
},
}
},
},
}
testCase.Run(t)
}
10 changes: 9 additions & 1 deletion cmd/nerdctl/container/container_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/containerd/nerdctl/v2/pkg/containerutil"
"github.com/containerd/nerdctl/v2/pkg/defaults"
"github.com/containerd/nerdctl/v2/pkg/errutil"
"github.com/containerd/nerdctl/v2/pkg/healthcheck"
"github.com/containerd/nerdctl/v2/pkg/labels"
"github.com/containerd/nerdctl/v2/pkg/logging"
"github.com/containerd/nerdctl/v2/pkg/netutil"
Expand Down Expand Up @@ -240,7 +241,6 @@ func setCreateFlags(cmd *cobra.Command) {
cmd.Flags().Duration("health-timeout", 0, "Maximum time to allow one check to run (default: 30s)")
cmd.Flags().Int("health-retries", 0, "Consecutive failures needed to report unhealthy (default: 3)")
cmd.Flags().Duration("health-start-period", 0, "Start period for the container to initialize before starting health-retries countdown")
cmd.Flags().Duration("health-start-interval", 0, "Time between running the checks during the start period")
cmd.Flags().Bool("no-healthcheck", false, "Disable any container-specified HEALTHCHECK")

// #region env flags
Expand Down Expand Up @@ -445,6 +445,14 @@ func runAction(cmd *cobra.Command, args []string) error {
return err
}

// Setup container healthchecks.
if err := healthcheck.CreateTimer(ctx, c); err != nil {
return fmt.Errorf("failed to create healthcheck timer: %w", err)
}
if err := healthcheck.StartTimer(ctx, c); err != nil {
return fmt.Errorf("failed to start healthcheck timer: %w", err)
}

if createOpt.Detach {
fmt.Fprintln(createOpt.Stdout, id)
return nil
Expand Down
6 changes: 6 additions & 0 deletions cmd/nerdctl/container/container_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,9 @@ func TestRunDomainname(t *testing.T) {
}

func TestRunHealthcheckFlags(t *testing.T) {
if rootlessutil.IsRootless() {
t.Skip("healthcheck tests are skipped in rootless environment")
}
testCase := nerdtest.Setup()

testCases := []struct {
Expand Down Expand Up @@ -990,6 +993,9 @@ func TestRunHealthcheckFlags(t *testing.T) {
}

func TestRunHealthcheckFromImage(t *testing.T) {
if rootlessutil.IsRootless() {
t.Skip("healthcheck tests are skipped in rootless environment")
}
nerdtest.Setup()

dockerfile := fmt.Sprintf(`FROM %s
Expand Down
Loading
Loading