Skip to content

Commit a828440

Browse files
committed
validation: drop hardcoded cgroup paths from delete
Both delete tests build the cgroup directory out of a literal /sys/fs/cgroup/pids. On a unified hierarchy that directory never exists, so delete_only_create_resources fails when it tries to populate it and delete_resources passes without testing anything: the path it expects the runtime to have removed was never there. Ask the Cgroup implementation where the cgroup lives instead, which also picks up the real mount point rather than assuming one. Move a process with cgroup.procs, which both hierarchies provide, rather than with the cgroup v1 only tasks file. Fixes #807 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
1 parent 22809e4 commit a828440

5 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎cgroups/cgroups.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
// Cgroup represents interfaces for cgroup validation
2424
type Cgroup interface {
2525
Version() int
26+
ControllerPath(controller string, cgPath string) string
2627
GetBlockIOData(pid int, cgPath string) (*rspec.LinuxBlockIO, error)
2728
GetCPUData(pid int, cgPath string) (*rspec.LinuxCPU, error)
2829
GetDevicesData(pid int, cgPath string) ([]rspec.LinuxDeviceCgroup, error)

‎cgroups/cgroups_v1.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func (cg *CgroupV1) Version() int {
2222
return 1
2323
}
2424

25+
// ControllerPath returns the directory backing an absolute cgroupsPath
26+
func (cg *CgroupV1) ControllerPath(controller string, cgPath string) string {
27+
return filepath.Join(cg.MountPath, controller, cgPath)
28+
}
29+
2530
// HugePageSizeUnitList is a list of the units used by the linux kernel when
2631
// naming the HugePage control files.
2732
// https://www.kernel.org/doc/Documentation/cgroup-v1/hugetlb.txt

‎cgroups/cgroups_v2.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ func (cg *CgroupV2) Version() int {
3737
return 2
3838
}
3939

40+
// ControllerPath returns the directory backing an absolute cgroupsPath.
41+
// The unified hierarchy keeps every controller in one directory, so the
42+
// controller is ignored.
43+
func (cg *CgroupV2) ControllerPath(controller string, cgPath string) string {
44+
return filepath.Join(unifiedPath, cgPath)
45+
}
46+
4047
func (cg *CgroupV2) dir(pid int, cgPath string) (string, error) {
4148
if filepath.IsAbs(cgPath) {
4249
path := filepath.Join(unifiedPath, cgPath)

‎validation/delete_only_create_resources/delete_only_create_resources.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
tap "github.com/mndrix/tap-go"
1212
"github.com/mrunalp/fileutils"
1313
rspec "github.com/opencontainers/runtime-spec/specs-go"
14+
"github.com/opencontainers/runtime-tools/cgroups"
1415
"github.com/opencontainers/runtime-tools/specerror"
1516
"github.com/opencontainers/runtime-tools/validation/util"
1617
)
@@ -19,9 +20,13 @@ func main() {
1920
t := tap.New()
2021
t.Header(0)
2122

23+
cg, err := cgroups.FindCgroup()
24+
if err != nil {
25+
util.Fatal(err)
26+
}
27+
2228
// Create a cgroup
23-
cgPath := "/sys/fs/cgroup"
24-
testPath := filepath.Join(cgPath, "pids", "cgrouptest")
29+
testPath := cg.ControllerPath("pids", cgroups.AbsCgroupPath)
2530
os.Mkdir(testPath, 0o755)
2631
defer os.RemoveAll(testPath)
2732

@@ -61,7 +66,7 @@ func main() {
6166
util.Fatal(err)
6267
}
6368
// Add the container to the cgroup
64-
err = os.WriteFile(filepath.Join(testPath, "tasks"), []byte(strconv.Itoa(state.Pid)), 0o644)
69+
err = os.WriteFile(filepath.Join(testPath, "cgroup.procs"), []byte(strconv.Itoa(state.Pid)), 0o644)
6570
if err != nil {
6671
util.Fatal(err)
6772
}

‎validation/delete_resources/delete_resources.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func main() {
5050
util.Fatal(err)
5151
}
5252

53+
cg, err := cgroups.FindCgroup()
54+
if err != nil {
55+
util.Fatal(err)
56+
}
57+
5358
err = r.Create()
5459
if err != nil {
5560
util.Fatal(err)
@@ -76,8 +81,7 @@ func main() {
7681
t.Fail(err.Error())
7782
}
7883

79-
path := filepath.Join("/sys/fs/cgroup/pids", cgroups.AbsCgroupPath)
80-
_, err = os.Stat(path)
84+
_, err = os.Stat(cg.ControllerPath("pids", cgroups.AbsCgroupPath))
8185
util.SpecErrorOK(t, os.IsNotExist(err), specerror.NewError(specerror.DeleteResImplement, fmt.Errorf("Deleting a container MUST delete the resources that were created during the `create` step"), rspec.Version), nil)
8286

8387
t.AutoPlan()

0 commit comments

Comments
 (0)