Skip to content

Commit 226e9a3

Browse files
author
Ma Shimiao
committed
runtimetest: add validation of cgrup path
Signed-off-by: Ma Shimiao <mashimiao.fnst@cn.fujitsu.com>
1 parent b610069 commit 226e9a3

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

cmd/runtimetest/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"bufio"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -244,6 +245,45 @@ func validateROPaths(spec *rspec.Spec) error {
244245
return nil
245246
}
246247

248+
func getThisCgroupPath(subsystem string) (string, error) {
249+
f, err := os.Open("/proc/self/cgroup")
250+
if err != nil {
251+
return "", err
252+
}
253+
defer f.Close()
254+
255+
s := bufio.NewScanner(f)
256+
for s.Scan() {
257+
if err := s.Err(); err != nil {
258+
return "", err
259+
}
260+
261+
text := s.Text()
262+
parts := strings.Split(text, ":")
263+
264+
for _, subs := range strings.Split(parts[1], ",") {
265+
if subs == subsystem {
266+
return parts[2], nil
267+
}
268+
}
269+
}
270+
return "", fmt.Errorf("cgroup %v not found", subsystem)
271+
}
272+
273+
func validateCgroupsPath(spec *rspec.Spec) error {
274+
fmt.Println("validating cgroupsPath")
275+
expectedPath := spec.Linux.CgroupsPath
276+
actualPath, err := getThisCgroupPath("devices")
277+
if err != nil {
278+
return err
279+
}
280+
281+
if expectedPath != nil && *expectedPath != actualPath {
282+
return fmt.Errorf("Cgroup path expected: %v, actual: %v", *expectedPath, actualPath)
283+
}
284+
return nil
285+
}
286+
247287
func main() {
248288
spec, err := loadSpecConfig()
249289
if err != nil {
@@ -254,6 +294,7 @@ func main() {
254294
validateRootFS,
255295
validateProcess,
256296
validateCapabilities,
297+
validateCgroupsPath,
257298
validateHostname,
258299
validateRlimits,
259300
validateSysctls,

0 commit comments

Comments
 (0)