From 946b75e2c960af696eff77c0f0da8392555c0027 Mon Sep 17 00:00:00 2001 From: Ma Shimiao Date: Fri, 3 Jun 2016 10:36:23 +0800 Subject: [PATCH] runtimetest: add validation of cgroups path Signed-off-by: Ma Shimiao --- cmd/runtimetest/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/cmd/runtimetest/main.go b/cmd/runtimetest/main.go index be5885775..f9b0b64a1 100644 --- a/cmd/runtimetest/main.go +++ b/cmd/runtimetest/main.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "bufio" "encoding/json" "fmt" "io" @@ -244,6 +245,45 @@ func validateROPaths(spec *rspec.Spec) error { return nil } +func getThisCgroupPath(subsystem string) (string, error) { + f, err := os.Open("/proc/self/cgroup") + if err != nil { + return "", err + } + defer f.Close() + + s := bufio.NewScanner(f) + for s.Scan() { + if err := s.Err(); err != nil { + return "", err + } + + text := s.Text() + parts := strings.Split(text, ":") + + for _, subs := range strings.Split(parts[1], ",") { + if subs == subsystem { + return parts[2], nil + } + } + } + return "", fmt.Errorf("cgroup %v not found", subsystem) +} + +func validateCgroupsPath(spec *rspec.Spec) error { + fmt.Println("validating cgroupsPath") + expectedPath := spec.Linux.CgroupsPath + actualPath, err := getThisCgroupPath("devices") + if err != nil { + return err + } + + if expectedPath != nil && *expectedPath != actualPath { + return fmt.Errorf("Cgroup path expected: %v, actual: %v", *expectedPath, actualPath) + } + return nil +} + func main() { spec, err := loadSpecConfig() if err != nil { @@ -254,6 +294,7 @@ func main() { validateRootFS, validateProcess, validateCapabilities, + validateCgroupsPath, validateHostname, validateRlimits, validateSysctls,