mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-21 02:40:57 +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,7 +41,15 @@ func TestIsSystemdCgroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidCgroupPathV1(t *testing.T) {
|
func TestValidCgroupPath(t *testing.T) {
|
||||||
|
// test with cgroup v1
|
||||||
|
runValidCgroupPathTest(t, false)
|
||||||
|
|
||||||
|
// test with cgroup v2
|
||||||
|
runValidCgroupPathTest(t, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runValidCgroupPathTest(t *testing.T, isCgroupV2 bool) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
for _, t := range []struct {
|
for _, t := range []struct {
|
||||||
@ -85,74 +93,7 @@ func TestValidCgroupPathV1(t *testing.T) {
|
|||||||
{"x.slice:kata:55555", true, false},
|
{"x.slice:kata:55555", true, false},
|
||||||
{"system.slice:kata:afhts2e5d4g5s", true, false},
|
{"system.slice:kata:afhts2e5d4g5s", true, false},
|
||||||
} {
|
} {
|
||||||
path, err := ValidCgroupPathV1(t.path, t.systemdCgroup)
|
path, err := ValidCgroupPath(t.path, isCgroupV2, 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 TestValidCgroupPathV2(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 := ValidCgroupPathV2(t.path, t.systemdCgroup)
|
|
||||||
if t.error {
|
if t.error {
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
continue
|
continue
|
||||||
@ -165,14 +106,17 @@ func TestValidCgroupPathV2(t *testing.T) {
|
|||||||
assert.True(strings.HasPrefix(path, cleanPath),
|
assert.True(strings.HasPrefix(path, cleanPath),
|
||||||
"%v should have prefix %v", path, cleanPath)
|
"%v should have prefix %v", path, cleanPath)
|
||||||
} else if t.systemdCgroup {
|
} else if t.systemdCgroup {
|
||||||
|
if isCgroupV2 {
|
||||||
assert.Equal(filepath.Join("/", t.path), path)
|
assert.Equal(filepath.Join("/", t.path), path)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t.path, path)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
assert.True(
|
assert.True(
|
||||||
strings.HasPrefix(path, DefaultResourceControllerID),
|
strings.HasPrefix(path, DefaultResourceControllerID),
|
||||||
"%v should have prefix /%v", path, DefaultResourceControllerID)
|
"%v should have prefix /%v", path, DefaultResourceControllerID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeviceToCgroupDeviceRule(t *testing.T) {
|
func TestDeviceToCgroupDeviceRule(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user