Skip to content

Commit

Permalink
test: skip test cgroup parent when use cgroup ns
Browse files Browse the repository at this point in the history
in ##2688, I make a mistake to find container cgroup path, since when
containeruse cgroup namespace, we can not get cgroup path from container
with`exec cat proc/self/cgroups`. Skip some test if container use cgroup
namespace.

fixes: #2685

Signed-off-by: Ace-Tang <aceapril@126.com>
  • Loading branch information
Ace-Tang authored and rudyfly committed Jan 25, 2019
1 parent 439d1d1 commit 1d956ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 51 deletions.
32 changes: 13 additions & 19 deletions test/cli_run_cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/alibaba/pouch/test/command"
Expand Down Expand Up @@ -53,28 +51,24 @@ func testRunWithCgroupParent(c *check.C, cgroupParent, name string) {
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

res = command.PouchRun("exec", name, "cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes")
res.Assert(c, icmd.Success)
c.Assert(util.PartialEqual(res.Stdout(), "314572800"), check.IsNil)

res = command.PouchRun("exec", name, "cat", "/proc/self/cgroup")
res.Assert(c, icmd.Success)
cgroupPaths := util.ParseCgroupFile(res.Stdout())

cgroupMount, err := util.FindCgroupMountpoint("memory")
c.Assert(err, check.IsNil)

file := filepath.Join(cgroupMount, cgroupPaths["memory"], "memory.limit_in_bytes")
if _, err := os.Stat(file); err != nil {
c.Fatalf("failed to Stat container %s cgroup mountpoint %s: %v", name, file, err)
}

out, err := exec.Command("cat", file).Output()
if err != nil {
c.Fatalf("execute cat command failed: %v", err)
}

if !strings.Contains(string(out), "314572800") {
c.Fatalf("unexpected output %s expected %s\n",
string(out), "314572800")
for _, v := range cgroupPaths {
// NOTE: if container in child cgroup namespace, cgroup mount is /,
// skip test since we can not get total cgroup path
if v == "/" {
break
}
if !strings.Contains(v, cgroupParent) {
c.Fatalf("unexpected cgroup path %v, expect to has %s in path", v, cgroupParent)
}
}

}

// TestRunInvalidCgroupParent checks that a specially-crafted cgroup parent
Expand Down
32 changes: 0 additions & 32 deletions test/util/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package util

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

Expand Down Expand Up @@ -97,33 +95,3 @@ func ParseCgroupFile(text string) map[string]string {
}
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 1d956ba

Please sign in to comment.