Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: failed to get correct cgroup path in test #2688

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}