Skip to content

Commit

Permalink
runtimetest: add validation of cgroups path
Browse files Browse the repository at this point in the history
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
  • Loading branch information
Ma Shimiao committed Aug 2, 2016
1 parent 032bc62 commit 7a670e8
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cmd/runtimetest/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -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)
Expand All @@ -387,6 +444,7 @@ func validate(context *cli.Context) error {
validateRootFS,
validateProcess,
validateCapabilities,
validateCgroupsPath,
validateHostname,
validateRlimits,
validateMountsExist,
Expand Down

0 comments on commit 7a670e8

Please sign in to comment.