-
Notifications
You must be signed in to change notification settings - Fork 695
add healthcheck orchestration logic [WIP] #4427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coderbirju
wants to merge
2
commits into
containerd:main
Choose a base branch
from
coderbirju:automate_health_checks_feat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+686
−58
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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) { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we check we remove the systemd timer on a kill signal? |
||
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") | ||
coderbirju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.