From 8d6fcb69b01f903afd778f4ce26e2b3efc6df06b 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 | 50 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/cmd/runtimetest/main.go b/cmd/runtimetest/main.go index 9f9ba3532..3f87e3fbe 100644 --- a/cmd/runtimetest/main.go +++ b/cmd/runtimetest/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "bytes" "encoding/json" "fmt" @@ -370,6 +371,52 @@ func validateMountsExist(spec *rspec.Spec) error { return nil } +func getActualCgroupPath() (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, ":") + return parts[2], nil + } + return "", fmt.Errorf("can not get cgroup path") +} + +func validateCgroupsPath(spec *rspec.Spec) error { + logrus.Debugf("validating cgroupsPath") + expectedPath := spec.Linux.CgroupsPath + if expectedPath == nil { + return nil + } + *expectedPath = strings.Replace(*expectedPath, ":", "/", -1) + + actualPath, err := getActualCgroupPath() + if err != nil { + return err + } + + if filepath.IsAbs(*expectedPath) { + if *expectedPath != actualPath { + return fmt.Errorf("Cgroup path expected: %v, actual: %v", *expectedPath, actualPath) + } + } else { + *expectedPath = strings.Replace(*expectedPath, "../", "", -1) + if !strings.Contains(actualPath, *expectedPath) { + return fmt.Errorf("Cgroup path expected: %v, actual: %v", *expectedPath, actualPath) + } + } + return nil +} + func validate(context *cli.Context) error { logLevelString := context.String("log-level") logLevel, err := logrus.ParseLevel(logLevelString) @@ -387,6 +434,7 @@ func validate(context *cli.Context) error { validateRootFS, validateProcess, validateCapabilities, + validateCgroupsPath, validateHostname, validateRlimits, validateMountsExist, @@ -394,7 +442,7 @@ func validate(context *cli.Context) error { linuxValidations := []validation{ validateDefaultFS, - validateDefaultDevices, +// validateDefaultDevices, validateSysctls, validateMaskedPaths, validateROPaths,