diff options
Diffstat (limited to 'runsc/test')
-rw-r--r-- | runsc/test/root/BUILD | 1 | ||||
-rw-r--r-- | runsc/test/root/cgroup_test.go | 77 | ||||
-rw-r--r-- | runsc/test/testutil/docker.go | 8 | ||||
-rw-r--r-- | runsc/test/testutil/testutil.go | 5 |
4 files changed, 78 insertions, 13 deletions
diff --git a/runsc/test/root/BUILD b/runsc/test/root/BUILD index 75826a521..7ded78baa 100644 --- a/runsc/test/root/BUILD +++ b/runsc/test/root/BUILD @@ -24,6 +24,7 @@ go_test( "local", ], deps = [ + "//runsc/cgroup", "//runsc/specutils", "//runsc/test/root/testdata", "//runsc/test/testutil", diff --git a/runsc/test/root/cgroup_test.go b/runsc/test/root/cgroup_test.go index 0eabf9561..91839048c 100644 --- a/runsc/test/root/cgroup_test.go +++ b/runsc/test/root/cgroup_test.go @@ -15,16 +15,45 @@ package root import ( + "bufio" + "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "strconv" "strings" "testing" + "gvisor.googlesource.com/gvisor/runsc/cgroup" "gvisor.googlesource.com/gvisor/runsc/test/testutil" ) +func verifyPid(pid int, path string) error { + f, err := os.Open(path) + if err != nil { + return err + } + defer f.Close() + + var gots []int + scanner := bufio.NewScanner(f) + for scanner.Scan() { + got, err := strconv.Atoi(scanner.Text()) + if err != nil { + return err + } + if got == pid { + return nil + } + gots = append(gots, got) + } + if scanner.Err() != nil { + return scanner.Err() + } + return fmt.Errorf("got: %s, want: %d", gots, pid) +} + // TestCgroup sets cgroup options and checks that cgroup was properly configured. func TestCgroup(t *testing.T) { if err := testutil.Pull("alpine"); err != nil { @@ -161,12 +190,48 @@ func TestCgroup(t *testing.T) { } for _, ctrl := range controllers { path := filepath.Join("/sys/fs/cgroup", ctrl, "docker", gid, "cgroup.procs") - out, err := ioutil.ReadFile(path) - if err != nil { - t.Fatalf("failed to read %q: %v", path, err) - } - if got := string(out); !strings.Contains(got, strconv.Itoa(pid)) { - t.Errorf("cgroup control %s processes, got: %q, want: %q", ctrl, got, pid) + if err := verifyPid(pid, path); err != nil { + t.Errorf("cgroup control %q processes: %v", ctrl, err) } } } + +func TestCgroupParent(t *testing.T) { + if err := testutil.Pull("alpine"); err != nil { + t.Fatal("docker pull failed:", err) + } + d := testutil.MakeDocker("cgroup-test") + + parent := testutil.RandomName("runsc") + if err := d.Run("--cgroup-parent", parent, "alpine", "sleep", "10000"); err != nil { + t.Fatal("docker create failed:", err) + } + defer d.CleanUp() + gid, err := d.ID() + if err != nil { + t.Fatalf("Docker.ID() failed: %v", err) + } + t.Logf("cgroup ID: %s", gid) + + // Check that sandbox is inside cgroup. + pid, err := d.SandboxPid() + if err != nil { + t.Fatalf("SandboxPid: %v", err) + } + + // Finds cgroup for the sandbox's parent process to check that cgroup is + // created in the right location relative to the parent. + cmd := fmt.Sprintf("grep PPid: /proc/%d/status | sed 's/PPid:\\s//'", pid) + ppid, err := exec.Command("bash", "-c", cmd).CombinedOutput() + if err != nil { + t.Fatalf("Executing %q: %v", cmd, err) + } + cgroups, err := cgroup.LoadPaths(strings.TrimSpace(string(ppid))) + if err != nil { + t.Fatalf("cgroup.LoadPath(%s): %v", ppid, err) + } + path := filepath.Join("/sys/fs/cgroup/memory", cgroups["memory"], parent, gid, "cgroup.procs") + if err := verifyPid(pid, path); err != nil { + t.Errorf("cgroup control %q processes: %v", "memory", err) + } +} diff --git a/runsc/test/testutil/docker.go b/runsc/test/testutil/docker.go index 5a92a5835..bce609061 100644 --- a/runsc/test/testutil/docker.go +++ b/runsc/test/testutil/docker.go @@ -18,7 +18,6 @@ import ( "fmt" "io/ioutil" "log" - "math/rand" "os" "os/exec" "path" @@ -31,10 +30,6 @@ import ( "github.com/kr/pty" ) -func init() { - rand.Seed(time.Now().UnixNano()) -} - func getRuntime() string { r := os.Getenv("RUNSC_RUNTIME") if r == "" { @@ -162,8 +157,7 @@ type Docker struct { // MakeDocker sets up the struct for a Docker container. // Names of containers will be unique. func MakeDocker(namePrefix string) Docker { - suffix := fmt.Sprintf("-%06d", rand.Int())[:7] - return Docker{Name: namePrefix + suffix, Runtime: getRuntime()} + return Docker{Name: RandomName(namePrefix), Runtime: getRuntime()} } // Create calls 'docker create' with the arguments provided. diff --git a/runsc/test/testutil/testutil.go b/runsc/test/testutil/testutil.go index a84530287..79f0a8b6b 100644 --- a/runsc/test/testutil/testutil.go +++ b/runsc/test/testutil/testutil.go @@ -461,3 +461,8 @@ func WriteTmpFile(pattern, text string) (string, error) { } return file.Name(), nil } + +// RandomName create a name with a 6 digit random number appended to it. +func RandomName(prefix string) string { + return fmt.Sprintf("%s-%06d", prefix, rand.Int31n(1000000)) +} |