diff --git a/test/cli_run_cgroup_test.go b/test/cli_run_cgroup_test.go index ccd563db3..4a8d0fcea 100644 --- a/test/cli_run_cgroup_test.go +++ b/test/cli_run_cgroup_test.go @@ -3,10 +3,12 @@ package main import ( "os" "os/exec" + "path/filepath" "strings" "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/environment" + "github.com/alibaba/pouch/test/util" "github.com/go-check/check" "github.com/gotestyourself/gotestyourself/icmd" @@ -51,18 +53,14 @@ func testRunWithCgroupParent(c *check.C, cgroupParent, name string) { defer DelContainerForceMultyTime(c, name) res.Assert(c, icmd.Success) - containerID, err := inspectFilter(name, ".ID") - c.Assert(err, check.IsNil) - - // this code slice may not robust, but for this test case is enough. - cgroupParent = strings.TrimPrefix(cgroupParent, "/") + res = command.PouchRun("exec", name, "cat", "/proc/self/cgroup") + res.Assert(c, icmd.Success) + cgroupPaths := util.ParseCgroupFile(res.Stdout()) - if cgroupParent == "" { - cgroupParent = "default" - } + cgroupMount, err := util.FindCgroupMountpoint("memory") + c.Assert(err, check.IsNil) - file := "/sys/fs/cgroup/memory/" + cgroupParent + "/" + - containerID + "/memory.limit_in_bytes" + file := filepath.Join(cgroupMount, cgroupPaths["memory"], "memory.limit_in_bytes") if _, err := os.Stat(file); err != nil { c.Fatalf("container %s cgroup mountpoint not exists", name) } diff --git a/test/util/util.go b/test/util/util.go index d156fa182..7ad4e7ebf 100644 --- a/test/util/util.go +++ b/test/util/util.go @@ -1,7 +1,9 @@ package util import ( + "bufio" "fmt" + "os" "strings" "time" @@ -80,3 +82,48 @@ func StringSliceTrimSpace(input []string) ([]string, error) { return output, nil } + +// ParseCgroupFile parse cgroup path from cgroup file +func ParseCgroupFile(text string) map[string]string { + cgroups := make(map[string]string) + for _, t := range strings.Split(text, "\n") { + parts := strings.SplitN(t, ":", 3) + if len(parts) < 3 { + continue + } + for _, sub := range strings.Split(parts[1], ",") { + cgroups[sub] = parts[2] + } + } + return cgroups +} + +// FindCgroupMountpoint find cgroup mountpoint for a specified subsystem +func FindCgroupMountpoint(subsystem string) (string, error) { + f, err := os.Open("/proc/self/mountinfo") + if err != nil { + return "", err + } + defer f.Close() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + txt := scanner.Text() + fields := strings.Fields(txt) + if len(fields) < 5 { + continue + } + if strings.Contains(txt, "cgroup") { + for _, opt := range strings.Split(fields[len(fields)-1], ",") { + if opt == subsystem { + return fields[4], nil + } + } + } + } + if err := scanner.Err(); err != nil { + return "", err + } + + return "", fmt.Errorf("failed to find %s cgroup mountpoint", subsystem) +}