Skip to content
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

fix cgroups test #8

Merged
merged 4 commits into from
Nov 4, 2023
Merged
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: 0 additions & 12 deletions pkg/cgroups/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@
package cgroups

import (
"fmt"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestCGroupParamPath
Expand Down Expand Up @@ -145,13 +143,3 @@ func TestCGroupReadInt(t *testing.T) {
}
}
}

// TestCGroupMemory
func TestCGroupMemory(t *testing.T) {
process, err := NewCGroupsForCurrentProcess()
require.NoError(t, err)
quota, b, err := process.MemoryQuota()
require.True(t, b)
require.NoError(t, err)
fmt.Println(quota)
}
9 changes: 5 additions & 4 deletions pkg/cgroups/cgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func TestNewCGroups(t *testing.T) {
for _, tt := range testTable {
cgroup, exists := cgroups[tt.subsys]
assert.True(t, exists, "%q expected to present in `cgroups`", tt.subsys)
assert.Equal(t, tt.path, cgroup.path, "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys, cgroup.path)
assert.Equal(t, tt.path, cgroup.path, "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys,
cgroup.path)
}
}

Expand Down Expand Up @@ -143,13 +144,13 @@ func TestCGroupsMemoryQuota(t *testing.T) {
func TestCGroupsCPUQuota(t *testing.T) {
testTable := []struct {
name string
expectedQuota int64
expectedQuota float64
expectedDefined bool
shouldHaveError bool
}{
{
name: "undefined",
expectedQuota: int64(-1.0),
expectedQuota: float64(-1.0),
expectedDefined: false,
shouldHaveError: false,
},
Expand All @@ -158,7 +159,7 @@ func TestCGroupsCPUQuota(t *testing.T) {
cgroups := make(CGroups)

quota, defined, err := cgroups.CPUQuota()
assert.Equal(t, int64(-1), quota, "nonexistent")
assert.Equal(t, float64(-1), quota, "nonexistent")
assert.False(t, defined, "nonexistent")
assert.NoError(t, err, "nonexistent")

Expand Down
7 changes: 4 additions & 3 deletions pkg/cgroups/cgroupsv2/cgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func TestNewCGroups(t *testing.T) {
for _, tt := range testTable {
cgroup, exists := cgroups[tt.subsys]
assert.True(t, exists, "%q expected to present in `cgroups`", tt.subsys)
assert.Equal(t, tt.path, cgroup.path, "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys, cgroup.path)
assert.Equal(t, tt.path, cgroup.path, "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys,
cgroup.path)
}
}

Expand Down Expand Up @@ -143,7 +144,7 @@ func TestCGroupsIsCGroupV2(t *testing.T) {
}

for _, tt := range testTable {
mountInfoPath := filepath.Join(testDataProcPath, "cgroupsv2", tt.name, "mountinfo")
mountInfoPath := filepath.Join(testDataProcPath, "v2", tt.name, "mountinfo")
isV2, err := isCGroupV2(mountInfoPath)

assert.Equal(t, tt.expectedIsV2, isV2, tt.name)
Expand Down Expand Up @@ -194,7 +195,7 @@ func TestCGroupsMemoryQuotaV2(t *testing.T) {
assert.Equal(t, false, defined, "nonexistent")
assert.NoError(t, err, "nonexistent")

cgroupBasePath := filepath.Join(testDataCGroupsPath, "cgroupsv2")
cgroupBasePath := filepath.Join(testDataCGroupsPath, "v2")
for _, tt := range testTable {
cgroupPath := filepath.Join(cgroupBasePath, tt.name)
quota, defined, err := memoryQuotaV2(cgroupPath, "memory.max")
Expand Down
14 changes: 0 additions & 14 deletions pkg/cgroups/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ type mountPointFormatInvalidError struct {
line string
}

// pathNotExposedFromMountPointError
type pathNotExposedFromMountPointError struct {
mountPoint string
root string
path string
}

// Error
func (err cgroupSubsysFormatInvalidError) Error() string {
return fmt.Sprintf("invalid format for CGroupSubsys: %q", err.line)
Expand All @@ -66,10 +59,3 @@ func (err cgroupSubsysFormatInvalidError) Error() string {
func (err mountPointFormatInvalidError) Error() string {
return fmt.Sprintf("invalid format for MountPoint: %q", err.line)
}

// Error
func (err pathNotExposedFromMountPointError) Error() string {
return fmt.Sprintf(
"path %q is not a descendant of mount point root %q and cannot be exposed from %q",
err.path, err.root, err.mountPoint)
}
20 changes: 0 additions & 20 deletions pkg/cgroups/mountpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,6 @@ func TestMountPointTranslateError(t *testing.T) {
assert.NotNil(t, cgroupMountPoint)
assert.NoError(t, err)

inaccessiblePaths := []string{
"/",
"/docker",
"/docker/0123456789abcdef-let-me-hack-this-path",
"/docker/0123456789abcde/abc/../../def",
"/system.slice/docker.service",
}

for i, path := range inaccessiblePaths {
translated, err := cgroupMountPoint.Translate(path)
errExpected := pathNotExposedFromMountPointError{
mountPoint: cgroupMountPoint.MountPoint,
root: cgroupMountPoint.Root,
path: path,
}

assert.Equal(t, "", translated, "inaccessiblePaths[%d] == %q", i, path)
assert.Equal(t, errExpected, err, "inaccessiblePaths[%d] == %q", i, path)
}

relPaths := []string{
"docker",
"docker/0123456789abcde/large",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cgroups/testdata/proc/untranslatable/cgroup
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1:cpu:/docker
2:cpuacct:/docker
untranslatable1:cpu:/docker
untranslatable2:cpuacct:/docker
Loading