Skip to content

Commit

Permalink
runtime: modify ValidCgroupPath unit test
Browse files Browse the repository at this point in the history
Modify ValidCgroupPath unit test.

Fixes: #8930

Signed-off-by: yaoyinnan <35447132+yaoyinnan@users.noreply.github.com>
  • Loading branch information
yaoyinnan committed Jan 31, 2024
1 parent 60dcca9 commit d535f6f
Showing 1 changed file with 12 additions and 68 deletions.
80 changes: 12 additions & 68 deletions src/runtime/pkg/resourcecontrol/utils_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,74 +41,15 @@ func TestIsSystemdCgroup(t *testing.T) {
}
}

func TestValidCgroupPathV1(t *testing.T) {
assert := assert.New(t)

for _, t := range []struct {
path string
systemdCgroup bool
error bool
}{
// empty paths
{"../../../", false, false},
{"../", false, false},
{".", false, false},
{"../../../", false, false},
{"./../", false, false},

// valid no-systemd paths
{"../../../foo", false, false},
{"/../hi", false, false},
{"/../hi/foo", false, false},
{"o / m /../ g", false, false},
{"/overhead/foobar", false, false},
{"/kata/afhts2e5d4g5s", false, false},
{"/kubepods/besteffort/podxxx-afhts2e5d4g5s/kata_afhts2e5d4g5s", false, false},
{"/sys/fs/cgroup/cpu/sandbox/kata_foobar", false, false},
{"kata_overhead/afhts2e5d4g5s", false, false},

// invalid systemd paths
{"o / m /../ g", true, true},
{"slice:kata", true, true},
{"a:b:c:d", true, true},
{":::", true, true},
{"", true, true},
{":", true, true},
{"::", true, true},
{":::", true, true},
{"a:b", true, true},
{"a:b:", true, true},
{":a:b", true, true},
{"@:@:@", true, true},

// valid systemd paths
{"x.slice:kata:55555", true, false},
{"system.slice:kata:afhts2e5d4g5s", true, false},
} {
path, err := ValidCgroupPathV1(t.path, t.systemdCgroup)
if t.error {
assert.Error(err)
continue
} else {
assert.NoError(err)
}

if filepath.IsAbs(t.path) {
cleanPath := filepath.Dir(filepath.Clean(t.path))
assert.True(strings.HasPrefix(path, cleanPath),
"%v should have prefix %v", path, cleanPath)
} else if t.systemdCgroup {
assert.Equal(t.path, path)
} else {
assert.True(
strings.HasPrefix(path, DefaultResourceControllerID),
"%v should have prefix /%v", path, DefaultResourceControllerID)
}
}
func TestValidCgroupPath(t *testing.T) {
// test with cgroup v1
runValidCgroupPathTest(t, false)

// test with cgroup v2
runValidCgroupPathTest(t, true)
}

func TestValidCgroupPathV2(t *testing.T) {
func runValidCgroupPathTest(t *testing.T, isCgroupV2 bool) {
assert := assert.New(t)

for _, t := range []struct {
Expand Down Expand Up @@ -152,7 +93,7 @@ func TestValidCgroupPathV2(t *testing.T) {
{"x.slice:kata:55555", true, false},
{"system.slice:kata:afhts2e5d4g5s", true, false},
} {
path, err := ValidCgroupPathV2(t.path, t.systemdCgroup)
path, err := ValidCgroupPath(t.path, isCgroupV2, t.systemdCgroup)
if t.error {
assert.Error(err)
continue
Expand All @@ -165,14 +106,17 @@ func TestValidCgroupPathV2(t *testing.T) {
assert.True(strings.HasPrefix(path, cleanPath),
"%v should have prefix %v", path, cleanPath)
} else if t.systemdCgroup {
assert.Equal(filepath.Join("/", t.path), path)
if isCgroupV2 {
assert.Equal(filepath.Join("/", t.path), path)
} else {
assert.Equal(t.path, path)
}
} else {
assert.True(
strings.HasPrefix(path, DefaultResourceControllerID),
"%v should have prefix /%v", path, DefaultResourceControllerID)
}
}

}

func TestDeviceToCgroupDeviceRule(t *testing.T) {
Expand Down

0 comments on commit d535f6f

Please sign in to comment.