From b0b8523ceaf5407523b0c5de1d6e5ec4a8c35c51 Mon Sep 17 00:00:00 2001 From: yaoyinnan <35447132+yaoyinnan@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:37:17 +0800 Subject: [PATCH] runtime: modify ValidCgroupPath unit test Modify ValidCgroupPath unit test. Fixes: #8930 Signed-off-by: yaoyinnan <35447132+yaoyinnan@users.noreply.github.com> --- .../pkg/resourcecontrol/utils_linux_test.go | 80 +++---------------- 1 file changed, 12 insertions(+), 68 deletions(-) diff --git a/src/runtime/pkg/resourcecontrol/utils_linux_test.go b/src/runtime/pkg/resourcecontrol/utils_linux_test.go index 804f6253a8..6ed70fbcb6 100644 --- a/src/runtime/pkg/resourcecontrol/utils_linux_test.go +++ b/src/runtime/pkg/resourcecontrol/utils_linux_test.go @@ -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 { @@ -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 @@ -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) {