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
12 changes: 5 additions & 7 deletions pkg/host/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,17 @@ func containsContainerReference(cgroupFile string) (bool, error) {
// Supports cgroup v1 and v2. Reading "/proc/1/cpuset" would only work for cgroups v1
// mountInfo is the path: "/proc/self/mountinfo"
func containerIDFromMountInfo(mountInfo string) (string, error) {
var errs error
mInfoFile, err := os.Open(mountInfo)
if err != nil {
return "", fmt.Errorf("container ID not found in %s: %w", mountInfo, err)
}
defer func(f *os.File) {
closeErr := f.Close()
if closeErr != nil {
errs = errors.Join(err, closeErr)
err = errors.Join(err, closeErr)
}
}(mInfoFile)

if err != nil {
return "", errors.Join(errs, err)
}

fileScanner := bufio.NewScanner(mInfoFile)
fileScanner.Split(bufio.ScanLines)

Expand All @@ -298,7 +296,7 @@ func containerIDFromMountInfo(mountInfo string) (string, error) {
}
}

return "", errors.Join(errs, fmt.Errorf("container ID not found in %s", mountInfo))
return "", fmt.Errorf("container ID not found in %s", mountInfo)
}

// containerIDFromPatterns checks a word against multiple regex patterns to extract the container ID.
Expand Down
8 changes: 8 additions & 0 deletions pkg/host/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ func TestInfo_ParseOsReleaseFile(t *testing.T) {
}
}

func TestInfo_containerIDFromMountInfo(t *testing.T) {
t.Run("Test 1: non-existent mountinfo file returns error without panic", func(t *testing.T) {
_, err := containerIDFromMountInfo("/non/existent/mountinfo/path")
require.Error(t, err)
assert.Contains(t, err.Error(), "container ID not found in")
})
}

func TestInfo_containerIDFromECS(t *testing.T) {
ctx := context.Background()

Expand Down
Loading