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 <[email protected]>
  • Loading branch information
Ma Shimiao committed Aug 1, 2016
1 parent 032bc62 commit 8d6fcb6
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion 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,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)
Expand All @@ -387,14 +434,15 @@ func validate(context *cli.Context) error {
validateRootFS,
validateProcess,
validateCapabilities,
validateCgroupsPath,
validateHostname,
validateRlimits,
validateMountsExist,
}

linuxValidations := []validation{
validateDefaultFS,
validateDefaultDevices,
// validateDefaultDevices,
validateSysctls,
validateMaskedPaths,
validateROPaths,
Expand Down

0 comments on commit 8d6fcb6

Please sign in to comment.