diff --git a/cmd/runtimetest/main.go b/cmd/runtimetest/main.go index 9f9ba3532..51e6cd1e4 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,62 @@ 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() + + cgroupPath := "" + s := bufio.NewScanner(f) + for s.Scan() { + if err := s.Err(); err != nil { + return "", err + } + + text := s.Text() + parts := strings.Split(text, ":") + if cgroupPath == parts[2] && cgroupPath != "" { + continue + } else if cgroupPath == "" && parts[2] != "" { + cgroupPath = parts[2] + } else { + return "", fmt.Errorf("error path with cgroup controllers") + } + } + + if cgroupPath == "" { + return "", fmt.Errorf("can not get cgroup path") + } + return cgroupPath, nil +} + +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 { + if _, err := filepath.Rel(*expectedPath, actualPath); err != nil { + logrus.Warnf("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 +444,7 @@ func validate(context *cli.Context) error { validateRootFS, validateProcess, validateCapabilities, + validateCgroupsPath, validateHostname, validateRlimits, validateMountsExist,