[test-improver] Improve tests for sys/container package#4081
Merged
Conversation
- Add TestExtractContainerIDFromCgroup to directly exercise the previously uncovered unexported function (0% → 71.4% coverage) - Expand TestExtractContainerIDFromContent with 7 additional edge-case sub-tests: docker/containerd at end of path, IDs of exactly 11 chars (boundary below minimum), multi-line content matched on the second line, and a docker-in-service-name (no slash-segment ID) case - Add TestDetectContainerID_ReturnedIDIsValidOrEmpty to assert the 12-char minimum invariant on any non-empty container ID returned - Add TestDetectContainerID_EnvVarReturnsEmptyID to assert that env-var detection yields an empty container ID when no file-based indicator is present Overall package coverage: 61.4% → 72.7% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Improves test coverage and edge-case handling for container detection logic in internal/sys, focusing on container ID extraction from cgroup content and the DetectContainerID() contract.
Changes:
- Added additional table-driven cases for
extractContainerIDFromContent(boundary lengths, path endings, multi-line inputs, non-segment “docker” occurrences). - Added tests that exercise cgroup-based ID extraction and assert basic invariants on IDs returned from
DetectContainerID. - Added a test intended to validate the env-var-only detection path returns an empty container ID.
Show a summary per file
| File | Description |
|---|---|
internal/sys/container_test.go |
Adds new subtests and new test functions to increase coverage and validate invariants for container ID detection/extraction. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (3)
internal/sys/container_test.go:400
TestDetectContainerID_EnvVarReturnsEmptyIDcan become flaky because it decides whether the env-var path is the “sole detection method” by only reading/proc/1/cgroup(and it ignores read errors).DetectContainerID()also checks/proc/self/cgroup, so on systems where/proc/1/cgroupdoesn’t show container indicators (or can’t be read) but/proc/self/cgroupdoes, this test may assert an empty ID even thoughDetectContainerID()correctly returns a non-empty ID from the cgroup path. Consider checking both cgroup paths with error handling, andt.Skipwhen you can’t reliably force the env-var-only path.
cgroupData, _ := os.ReadFile("/proc/1/cgroup")
cgroupHasIndicator := containsAny(string(cgroupData), []string{"docker", "containerd", "kubepods", "lxc"})
// Only assert the empty ID if the env-var path is the sole detection method.
if !dockerEnvExists && !cgroupHasIndicator {
_, containerID := DetectContainerID()
internal/sys/container_test.go:392
- The comment says “Override file-based detection by using the env var path”, but
DetectContainerID()checks/.dockerenvand cgroups beforeRUNNING_IN_CONTAINER. This is misleading for future readers of the test; please reword to reflect that the env-var branch is only reached when file-based detection does not trigger.
// Override file-based detection by using the env var path.
// We can only guarantee an empty ID when /.dockerenv does not exist and
// no cgroup indicator is present.
_, dockerEnvErr := os.Stat("/.dockerenv")
internal/sys/container_test.go:397
- The container indicator list is duplicated here as a hard-coded slice. Since the test is in the same package, it can reuse the
containerIndicatorsvar fromcontainer.goto stay in sync if the indicators change.
cgroupHasIndicator := containsAny(string(cgroupData), []string{"docker", "containerd", "kubepods", "lxc"})
- Files reviewed: 1/1 changed files
- Comments generated: 1
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Test Improvements:
internal/sys/container_test.goFile Analyzed
internal/sys/container_test.gointernal/sysImprovements Made
1. Increased Coverage
TestExtractContainerIDFromCgroup— directly exercises the unexportedextractContainerIDFromCgroup()function which had 0% coverage. The test calls the function and verifies any returned ID is at least 12 characters (the documented minimum), tolerating both container and non-container environments.TestExtractContainerIDFromContent:docker at end of path with no following segment—"0::/docker"(no ID segment)containerd at end of path with no following segment—"0::/containerd"docker with ID exactly 11 chars (below minimum)— boundary case just under the 12-char thresholdcontainerd with ID exactly 11 chars (below minimum)— same boundary for containerdmatch on second line only— multi-line cgroup content where only the second line contains a matchfirst line no match, second line containerd with valid ID— similar multi-line scenariodocker appears in non-segment context within a non-matching path—"0::/system.slice/docker.service"(docker in service name, not a path segment with a following ID)2. New Invariant Tests for
DetectContainerIDTestDetectContainerID_ReturnedIDIsValidOrEmpty— asserts the 12-character minimum invariant on any non-empty container ID returned byDetectContainerID, making the contract explicit.TestDetectContainerID_EnvVarReturnsEmptyID— verifies that container detection via theRUNNING_IN_CONTAINERenv-var path (the only detection method that has no ID extraction) returns an empty container ID when no file-based indicator is present.3. Coverage Improvement
extractContainerIDFromCgroupextractContainerIDFromContentscanner.Err()dead-code branch, unreachable withstrings.NewReader)DetectContainerID/.dockerenvbranch requires root to create)Test Execution
All
internal/systests pass:Note: A pre-existing unrelated failure exists in
internal/config(TestFetchAndFixSchema_NetworkError) that is not caused by these changes.Why These Changes?
extractContainerIDFromCgroupwas the only unexported function in the package with zero test coverage — a clear gap since it is called from theDetectContainerIDproduction path. The boundary-value edge cases added toTestExtractContainerIDFromContent(11-char IDs, trailingdocker/containerdpath segments, multi-line content) make the tests more robust against regressions in the minimum-length check and multi-line scanning logic.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests
Warning
The following domain was blocked by the firewall during workflow execution:
invalidhostthatdoesnotexist12345.comTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.