Skip to content

Commit

Permalink
fix: failed to get correct cgroup path in test
Browse files Browse the repository at this point in the history
runc get cgroup parent through `/proc/self/cgroup`, so can not get cgroup path just
join string like "/sys/fs/cgroup"+"subsystem"+"cgroup-parent"+"containerid", get corrent
cgroup path from `/proc/self/cgroup` in test.

fixes: #2685

Signed-off-by: Ace-Tang <[email protected]>
  • Loading branch information
Ace-Tang authored and fuweid committed Jan 24, 2019
1 parent f672b9c commit b3e63d1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
18 changes: 8 additions & 10 deletions test/cli_run_cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/alibaba/pouch/test/command"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/util"

"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
Expand Down Expand Up @@ -51,18 +53,14 @@ func testRunWithCgroupParent(c *check.C, cgroupParent, name string) {
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

containerID, err := inspectFilter(name, ".ID")
c.Assert(err, check.IsNil)

// this code slice may not robust, but for this test case is enough.
cgroupParent = strings.TrimPrefix(cgroupParent, "/")
res = command.PouchRun("exec", name, "cat", "/proc/self/cgroup")
res.Assert(c, icmd.Success)
cgroupPaths := util.ParseCgroupFile(res.Stdout())

if cgroupParent == "" {
cgroupParent = "default"
}
cgroupMount, err := util.FindCgroupMountpoint("memory")
c.Assert(err, check.IsNil)

file := "/sys/fs/cgroup/memory/" + cgroupParent + "/" +
containerID + "/memory.limit_in_bytes"
file := filepath.Join(cgroupMount, cgroupPaths["memory"], "memory.limit_in_bytes")
if _, err := os.Stat(file); err != nil {
c.Fatalf("container %s cgroup mountpoint not exists", name)
}
Expand Down
47 changes: 47 additions & 0 deletions test/util/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package util

import (
"bufio"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -80,3 +82,48 @@ func StringSliceTrimSpace(input []string) ([]string, error) {

return output, nil
}

// ParseCgroupFile parse cgroup path from cgroup file
func ParseCgroupFile(text string) map[string]string {
cgroups := make(map[string]string)
for _, t := range strings.Split(text, "\n") {
parts := strings.SplitN(t, ":", 3)
if len(parts) < 3 {
continue
}
for _, sub := range strings.Split(parts[1], ",") {
cgroups[sub] = parts[2]
}
}
return cgroups
}

// FindCgroupMountpoint find cgroup mountpoint for a specified subsystem
func FindCgroupMountpoint(subsystem string) (string, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return "", err
}
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
txt := scanner.Text()
fields := strings.Fields(txt)
if len(fields) < 5 {
continue
}
if strings.Contains(txt, "cgroup") {
for _, opt := range strings.Split(fields[len(fields)-1], ",") {
if opt == subsystem {
return fields[4], nil
}
}
}
}
if err := scanner.Err(); err != nil {
return "", err
}

return "", fmt.Errorf("failed to find %s cgroup mountpoint", subsystem)
}

0 comments on commit b3e63d1

Please sign in to comment.