mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-19 01:39:48 +00:00
runtime: modify ValidCgroupPath unit test
Modify ValidCgroupPath unit test. Fixes: #8930 Signed-off-by: yaoyinnan <35447132+yaoyinnan@users.noreply.github.com>
This commit is contained in:
parent
feed5c8ff9
commit
b0b8523cea
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user