Skip to content
Open
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,204 @@ func TestContainerHealthCheckAdvance(t *testing.T) {

testCase.Run(t)
}

func TestHealthCheck_SystemdIntegration_Basic(t *testing.T) {
testCase := nerdtest.Setup()
testCase.Require = require.Not(nerdtest.Docker)

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this portion needs a retry to for container to be in a healthy state.
Initially it should be starting and we can retry, if unhealthy we break with error, if healthy success.
And we retry a few times.

inspect := nerdtest.InspectContainer(helpers, data.Identifier())
h := inspect.State.Health
assert.Assert(t, h != nil, "expected health state to be present")
assert.Equal(t, h.Status, "healthy")
assert.Assert(t, len(h.Log) > 0, "expected at least one health check log entry")
}),
}
},
},
{
Description: "Kill stops healthcheck execution",
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()) // Kill the container
},
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: 0,
Output: expect.All(func(stdout string, t tig.T) {
inspect := nerdtest.InspectContainer(helpers, data.Identifier())
h := inspect.State.Health
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check we remove the systemd timer on a kill signal?
Integ test should try to see the intended steady state condition in case of an event.

for _, entry := range h.Log {
assert.NilError(t, err, "parsing healthcheck Start time")
assert.Assert(t, entry.Start.Before(containerEnd), "healthcheck ran after container was killed")
}
}),
}
},
},
}
testCase.Run(t)
}

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

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())
// Wait longer for systemd timer creation and first healthcheck execution
time.Sleep(3 * time.Second)
},
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())
time.Sleep(500 * time.Millisecond) // Allow cleanup to complete

// 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())
time.Sleep(3 * time.Second) // Wait for initial timer creation
},
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())
time.Sleep(1 * time.Second) // Allow cleanup

// 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())
time.Sleep(3 * time.Second) // Wait for timer recreation

// 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
6 changes: 1 addition & 5 deletions cmd/nerdctl/helpers/flagutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func ValidateHealthcheckFlags(options types.ContainerCreateOptions) error {
options.HealthInterval != 0 ||
options.HealthTimeout != 0 ||
options.HealthRetries != 0 ||
options.HealthStartPeriod != 0 ||
options.HealthStartInterval != 0
options.HealthStartPeriod != 0

if options.NoHealthcheck {
if options.HealthCmd != "" || healthFlagsSet {
Expand All @@ -74,9 +73,6 @@ func ValidateHealthcheckFlags(options types.ContainerCreateOptions) error {
if options.HealthStartPeriod < 0 {
return fmt.Errorf("--health-start-period cannot be negative")
}
if options.HealthStartInterval < 0 {
return fmt.Errorf("--health-start-interval cannot be negative")
}
return nil
}

Expand Down
Loading
Loading