Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions internal/sys/container_detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ func TestDetectContainerIDWithPaths_MultipleIndicatorLines(t *testing.T) {
// TestExtractContainerIDFromCgroupFiles_ReturnsFirstFound tests that
// extractContainerIDFromCgroupFiles returns the ID from the first file that yields one.
func TestExtractContainerIDFromCgroupFiles_ReturnsFirstFound(t *testing.T) {
t.Parallel()

dir := t.TempDir()

first := writeTempFile(t, dir, "cgroup1", "0::/docker/aabbccdd1234\n")
Expand All @@ -229,6 +231,8 @@ func TestExtractContainerIDFromCgroupFiles_ReturnsFirstFound(t *testing.T) {
// TestExtractContainerIDFromCgroupFiles_SkipsEmptyFile tests that a file with
// no container ID is skipped and the next file is tried.
func TestExtractContainerIDFromCgroupFiles_SkipsEmptyFile(t *testing.T) {
t.Parallel()

dir := t.TempDir()

noMatch := writeTempFile(t, dir, "cgroup1", "0::/user.slice\n")
Expand All @@ -241,6 +245,8 @@ func TestExtractContainerIDFromCgroupFiles_SkipsEmptyFile(t *testing.T) {
// TestExtractContainerIDFromCgroupFiles_AllEmpty tests that "" is returned when
// no files yield a container ID.
func TestExtractContainerIDFromCgroupFiles_AllEmpty(t *testing.T) {
t.Parallel()

dir := t.TempDir()

f1 := writeTempFile(t, dir, "cgroup1", "0::/\n")
Expand All @@ -252,19 +258,22 @@ func TestExtractContainerIDFromCgroupFiles_AllEmpty(t *testing.T) {

// TestExtractContainerIDFromCgroupFiles_NilPaths tests the nil-slice edge case.
func TestExtractContainerIDFromCgroupFiles_NilPaths(t *testing.T) {
t.Parallel()
id := extractContainerIDFromCgroupFiles(nil)
assert.Empty(t, id)
}

// TestExtractContainerIDFromCgroupFiles_EmptyPaths tests the empty-slice edge case.
func TestExtractContainerIDFromCgroupFiles_EmptyPaths(t *testing.T) {
t.Parallel()
id := extractContainerIDFromCgroupFiles([]string{})
assert.Empty(t, id)
}

// TestExtractContainerIDFromCgroupFiles_NonexistentPaths tests that missing files
// are silently skipped and "" is returned when none exist.
func TestExtractContainerIDFromCgroupFiles_NonexistentPaths(t *testing.T) {
t.Parallel()
id := extractContainerIDFromCgroupFiles([]string{"/nonexistent/a", "/nonexistent/b"})
assert.Empty(t, id)
}
14 changes: 14 additions & 0 deletions internal/sys/container_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sys

import (
"bufio"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -273,6 +274,8 @@ func TestDetectContainerID_ConsistentWithIsRunningInContainer(t *testing.T) {
}

func TestExtractContainerIDFromContent(t *testing.T) {
t.Parallel()

tests := []struct {
name string
content string
Expand Down Expand Up @@ -348,10 +351,18 @@ func TestExtractContainerIDFromContent(t *testing.T) {
content: "0::/system.slice/docker.service",
expected: "",
},
{
// A line that exceeds bufio.MaxScanTokenSize (64 KiB) causes the scanner
// to fail with bufio.ErrTooLong. The function logs the error and returns "".
name: "single line exceeding scanner buffer limit returns empty string",
content: strings.Repeat("x", bufio.MaxScanTokenSize+1),
expected: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := extractContainerIDFromContent(tt.content)
assert.Equal(t, tt.expected, result)
})
Expand Down Expand Up @@ -423,6 +434,8 @@ func containsAny(s string, substrings []string) bool {
}

func TestContainsAny_Helper(t *testing.T) {
t.Parallel()

// Test the helper function itself
tests := []struct {
name string
Expand Down Expand Up @@ -482,6 +495,7 @@ func TestContainsAny_Helper(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := containsAny(tt.input, tt.substrings)
assert.Equal(t, tt.want, result)
})
Expand Down
Loading