2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-10-11 18:56:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package root
|
|
|
|
|
|
|
|
import (
|
2019-02-26 03:20:52 +00:00
|
|
|
"bufio"
|
2020-07-08 20:24:58 +00:00
|
|
|
"context"
|
2019-02-26 03:20:52 +00:00
|
|
|
"fmt"
|
2018-10-11 18:56:42 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-02-26 03:20:52 +00:00
|
|
|
"os/exec"
|
2018-10-11 18:56:42 +00:00
|
|
|
"path/filepath"
|
2018-12-28 21:47:19 +00:00
|
|
|
"strconv"
|
2018-10-11 18:56:42 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-11-06 01:02:15 +00:00
|
|
|
"time"
|
2018-10-11 18:56:42 +00:00
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/test/dockerutil"
|
|
|
|
"gvisor.dev/gvisor/pkg/test/testutil"
|
2019-06-13 23:49:09 +00:00
|
|
|
"gvisor.dev/gvisor/runsc/cgroup"
|
2018-10-11 18:56:42 +00:00
|
|
|
)
|
|
|
|
|
2019-02-26 03:20:52 +00:00
|
|
|
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()
|
|
|
|
}
|
2020-03-26 00:15:05 +00:00
|
|
|
return fmt.Errorf("got: %v, want: %d", gots, pid)
|
2019-02-26 03:20:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 20:24:58 +00:00
|
|
|
func TestMemCgroup(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
d := dockerutil.MakeContainer(ctx, t)
|
|
|
|
defer d.CleanUp(ctx)
|
2019-11-06 01:02:15 +00:00
|
|
|
|
|
|
|
// Start a new container and allocate the specified about of memory.
|
2020-04-23 18:32:08 +00:00
|
|
|
allocMemSize := 128 << 20
|
|
|
|
allocMemLimit := 2 * allocMemSize
|
2020-07-08 20:24:58 +00:00
|
|
|
|
|
|
|
if err := d.Spawn(ctx, dockerutil.RunOpts{
|
|
|
|
Image: "basic/ubuntu",
|
|
|
|
Memory: allocMemLimit, // Must be in bytes.
|
|
|
|
}, "python3", "-c", fmt.Sprintf("import time; s = 'a' * %d; time.sleep(100)", allocMemSize)); err != nil {
|
2020-04-23 18:32:08 +00:00
|
|
|
t.Fatalf("docker run failed: %v", err)
|
2019-11-06 01:02:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Extract the ID to lookup the cgroup.
|
2020-07-08 20:24:58 +00:00
|
|
|
gid := d.ID()
|
2019-11-06 01:02:15 +00:00
|
|
|
t.Logf("cgroup ID: %s", gid)
|
|
|
|
|
|
|
|
// Wait when the container will allocate memory.
|
2020-04-23 18:32:08 +00:00
|
|
|
memUsage := 0
|
2019-11-06 01:02:15 +00:00
|
|
|
start := time.Now()
|
2020-04-23 18:32:08 +00:00
|
|
|
for time.Since(start) < 30*time.Second {
|
|
|
|
// Sleep for a brief period of time after spawning the
|
|
|
|
// container (so that Docker can create the cgroup etc.
|
|
|
|
// or after looping below (so the application can start).
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2021-11-02 15:20:58 +00:00
|
|
|
var path string
|
|
|
|
|
|
|
|
// Read the cgroup memory limit.
|
|
|
|
if cgroup.IsOnlyV2() {
|
|
|
|
path = filepath.Join("/sys/fs/cgroup/docker", gid, "memory.max")
|
|
|
|
} else {
|
|
|
|
path = filepath.Join("/sys/fs/cgroup/memory/docker", gid, "memory.limit_in_bytes")
|
|
|
|
}
|
2020-04-23 18:32:08 +00:00
|
|
|
// Read the cgroup memory limit.
|
2019-11-06 01:02:15 +00:00
|
|
|
outRaw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2020-04-23 18:32:08 +00:00
|
|
|
// It's possible that the container does not exist yet.
|
|
|
|
continue
|
2019-11-06 01:02:15 +00:00
|
|
|
}
|
|
|
|
out := strings.TrimSpace(string(outRaw))
|
2020-04-23 18:32:08 +00:00
|
|
|
memLimit, err := strconv.Atoi(out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Atoi(%v): %v", out, err)
|
|
|
|
}
|
|
|
|
if memLimit != allocMemLimit {
|
|
|
|
// The group may not have had the correct limit set yet.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-02 15:20:58 +00:00
|
|
|
if cgroup.IsOnlyV2() {
|
|
|
|
// v2 does not have max_usage_in_bytes equivalent, so memory.current is the
|
|
|
|
// next best thing that we can use
|
|
|
|
path = filepath.Join("/sys/fs/cgroup/docker", gid, "memory.current")
|
|
|
|
} else {
|
|
|
|
path = filepath.Join("/sys/fs/cgroup/memory/docker", gid, "memory.max_usage_in_bytes")
|
|
|
|
}
|
2020-04-23 18:32:08 +00:00
|
|
|
// Read the cgroup memory usage.
|
|
|
|
outRaw, err = ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error reading usage: %v", err)
|
|
|
|
}
|
|
|
|
out = strings.TrimSpace(string(outRaw))
|
2019-11-06 01:02:15 +00:00
|
|
|
memUsage, err = strconv.Atoi(out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Atoi(%v): %v", out, err)
|
|
|
|
}
|
2020-04-23 18:32:08 +00:00
|
|
|
t.Logf("read usage: %v, wanted: %v", memUsage, allocMemSize)
|
2019-11-06 01:02:15 +00:00
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Are we done?
|
|
|
|
if memUsage >= allocMemSize {
|
2019-11-06 01:02:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 00:15:05 +00:00
|
|
|
t.Fatalf("%vMB is less than %vMB", memUsage>>20, allocMemSize>>20)
|
2019-11-06 01:02:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:56:42 +00:00
|
|
|
// TestCgroup sets cgroup options and checks that cgroup was properly configured.
|
2021-11-02 15:20:58 +00:00
|
|
|
func TestCgroupV1(t *testing.T) {
|
|
|
|
if cgroup.IsOnlyV2() {
|
|
|
|
t.Skip("skipping cgroupv1 attribute testing in cgroupv2 setup")
|
|
|
|
}
|
2020-07-08 20:24:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
d := dockerutil.MakeContainer(ctx, t)
|
|
|
|
defer d.CleanUp(ctx)
|
2018-10-11 18:56:42 +00:00
|
|
|
|
2019-09-10 07:37:46 +00:00
|
|
|
// This is not a comprehensive list of attributes.
|
|
|
|
//
|
|
|
|
// Note that we are specifically missing cpusets, which fail if specified.
|
|
|
|
// In any case, it's unclear if cpusets can be reliably tested here: these
|
|
|
|
// are often run on a single core virtual machine, and there is only a single
|
|
|
|
// CPU available in our current set, and every container's set.
|
2018-10-11 18:56:42 +00:00
|
|
|
attrs := []struct {
|
2020-07-08 20:24:58 +00:00
|
|
|
field string
|
|
|
|
value int64
|
2018-10-11 18:56:42 +00:00
|
|
|
ctrl string
|
|
|
|
file string
|
|
|
|
want string
|
|
|
|
skipIfNotFound bool
|
|
|
|
}{
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "cpu-shares",
|
|
|
|
value: 1000,
|
|
|
|
ctrl: "cpu",
|
|
|
|
file: "cpu.shares",
|
|
|
|
want: "1000",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "cpu-period",
|
|
|
|
value: 2000,
|
|
|
|
ctrl: "cpu",
|
|
|
|
file: "cpu.cfs_period_us",
|
|
|
|
want: "2000",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "cpu-quota",
|
|
|
|
value: 3000,
|
|
|
|
ctrl: "cpu",
|
|
|
|
file: "cpu.cfs_quota_us",
|
|
|
|
want: "3000",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "kernel-memory",
|
|
|
|
value: 100 << 20,
|
|
|
|
ctrl: "memory",
|
|
|
|
file: "memory.kmem.limit_in_bytes",
|
|
|
|
want: "104857600",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "memory",
|
|
|
|
value: 1 << 30,
|
|
|
|
ctrl: "memory",
|
|
|
|
file: "memory.limit_in_bytes",
|
|
|
|
want: "1073741824",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "memory-reservation",
|
|
|
|
value: 500 << 20,
|
|
|
|
ctrl: "memory",
|
|
|
|
file: "memory.soft_limit_in_bytes",
|
|
|
|
want: "524288000",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "memory-swap",
|
|
|
|
value: 2 << 30,
|
2018-10-11 18:56:42 +00:00
|
|
|
ctrl: "memory",
|
|
|
|
file: "memory.memsw.limit_in_bytes",
|
|
|
|
want: "2147483648",
|
|
|
|
skipIfNotFound: true, // swap may be disabled on the machine.
|
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "memory-swappiness",
|
|
|
|
value: 5,
|
|
|
|
ctrl: "memory",
|
|
|
|
file: "memory.swappiness",
|
|
|
|
want: "5",
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "blkio-weight",
|
|
|
|
value: 750,
|
2020-04-23 18:32:08 +00:00
|
|
|
ctrl: "blkio",
|
|
|
|
file: "blkio.weight",
|
|
|
|
want: "750",
|
|
|
|
skipIfNotFound: true, // blkio groups may not be available.
|
2018-10-11 18:56:42 +00:00
|
|
|
},
|
2020-04-25 01:15:26 +00:00
|
|
|
{
|
2020-07-08 20:24:58 +00:00
|
|
|
field: "pids-limit",
|
|
|
|
value: 1000,
|
|
|
|
ctrl: "pids",
|
|
|
|
file: "pids.max",
|
|
|
|
want: "1000",
|
2020-04-25 01:15:26 +00:00
|
|
|
},
|
2018-10-11 18:56:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 20:24:58 +00:00
|
|
|
// Make configs.
|
|
|
|
conf, hostconf, _ := d.ConfigsFrom(dockerutil.RunOpts{
|
|
|
|
Image: "basic/alpine",
|
|
|
|
}, "sleep", "10000")
|
|
|
|
|
|
|
|
// Add Cgroup arguments to configs.
|
2018-10-11 18:56:42 +00:00
|
|
|
for _, attr := range attrs {
|
2020-07-08 20:24:58 +00:00
|
|
|
switch attr.field {
|
|
|
|
case "cpu-shares":
|
|
|
|
hostconf.Resources.CPUShares = attr.value
|
|
|
|
case "cpu-period":
|
|
|
|
hostconf.Resources.CPUPeriod = attr.value
|
|
|
|
case "cpu-quota":
|
|
|
|
hostconf.Resources.CPUQuota = attr.value
|
|
|
|
case "kernel-memory":
|
|
|
|
hostconf.Resources.KernelMemory = attr.value
|
|
|
|
case "memory":
|
|
|
|
hostconf.Resources.Memory = attr.value
|
|
|
|
case "memory-reservation":
|
|
|
|
hostconf.Resources.MemoryReservation = attr.value
|
|
|
|
case "memory-swap":
|
|
|
|
hostconf.Resources.MemorySwap = attr.value
|
|
|
|
case "memory-swappiness":
|
|
|
|
val := attr.value
|
|
|
|
hostconf.Resources.MemorySwappiness = &val
|
|
|
|
case "blkio-weight":
|
|
|
|
hostconf.Resources.BlkioWeight = uint16(attr.value)
|
|
|
|
case "pids-limit":
|
|
|
|
val := attr.value
|
|
|
|
hostconf.Resources.PidsLimit = &val
|
|
|
|
}
|
2018-10-11 18:56:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 20:24:58 +00:00
|
|
|
// Create container.
|
2021-01-05 21:20:12 +00:00
|
|
|
if err := d.CreateFrom(ctx, "basic/alpine", conf, hostconf, nil); err != nil {
|
2020-07-08 20:24:58 +00:00
|
|
|
t.Fatalf("create failed with: %v", err)
|
2018-10-11 18:56:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 20:24:58 +00:00
|
|
|
// Start container.
|
|
|
|
if err := d.Start(ctx); err != nil {
|
|
|
|
t.Fatalf("start failed with: %v", err)
|
2018-10-11 18:56:42 +00:00
|
|
|
}
|
2020-07-08 20:24:58 +00:00
|
|
|
|
|
|
|
// Lookup the relevant cgroup ID.
|
|
|
|
gid := d.ID()
|
2018-10-11 18:56:42 +00:00
|
|
|
t.Logf("cgroup ID: %s", gid)
|
2018-12-28 21:47:19 +00:00
|
|
|
|
|
|
|
// Check list of attributes defined above.
|
2018-10-11 18:56:42 +00:00
|
|
|
for _, attr := range attrs {
|
|
|
|
path := filepath.Join("/sys/fs/cgroup", attr.ctrl, "docker", gid, attr.file)
|
|
|
|
out, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) && attr.skipIfNotFound {
|
|
|
|
t.Logf("skipped %s/%s", attr.ctrl, attr.file)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("failed to read %q: %v", path, err)
|
|
|
|
}
|
|
|
|
if got := strings.TrimSpace(string(out)); got != attr.want {
|
2020-07-08 20:24:58 +00:00
|
|
|
t.Errorf("field: %q, cgroup attribute %s/%s, got: %q, want: %q", attr.field, attr.ctrl, attr.file, got, attr.want)
|
2018-10-11 18:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-28 21:47:19 +00:00
|
|
|
|
|
|
|
// Check that sandbox is inside cgroup.
|
|
|
|
controllers := []string{
|
|
|
|
"blkio",
|
|
|
|
"cpu",
|
|
|
|
"cpuset",
|
|
|
|
"memory",
|
|
|
|
"net_cls",
|
|
|
|
"net_prio",
|
|
|
|
"devices",
|
|
|
|
"freezer",
|
|
|
|
"perf_event",
|
|
|
|
"pids",
|
|
|
|
"systemd",
|
|
|
|
}
|
2020-07-08 20:24:58 +00:00
|
|
|
pid, err := d.SandboxPid(ctx)
|
2018-12-28 21:47:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SandboxPid: %v", err)
|
|
|
|
}
|
|
|
|
for _, ctrl := range controllers {
|
|
|
|
path := filepath.Join("/sys/fs/cgroup", ctrl, "docker", gid, "cgroup.procs")
|
2019-02-26 03:20:52 +00:00
|
|
|
if err := verifyPid(pid, path); err != nil {
|
|
|
|
t.Errorf("cgroup control %q processes: %v", ctrl, err)
|
2018-12-28 21:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-11 18:56:42 +00:00
|
|
|
}
|
2019-02-26 03:20:52 +00:00
|
|
|
|
2021-11-02 15:20:58 +00:00
|
|
|
// TestCgroupV2 sets cgroup options and checks that cgroup was properly configured with
|
|
|
|
// cgroupv2 setup
|
|
|
|
func TestCgroupV2(t *testing.T) {
|
|
|
|
if !cgroup.IsOnlyV2() {
|
|
|
|
t.Skip("skipping cgroupv2 attribute testing in cgroupv1 setup")
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
d := dockerutil.MakeContainer(ctx, t)
|
|
|
|
defer d.CleanUp(ctx)
|
|
|
|
|
|
|
|
// This is not a comprehensive list of attributes.
|
|
|
|
//
|
|
|
|
// Note that we are specifically missing cpusets, which fail if specified.
|
|
|
|
// In any case, it's unclear if cpusets can be reliably tested here: these
|
|
|
|
// are often run on a single core virtual machine, and there is only a single
|
|
|
|
// CPU available in our current set, and every container's set.
|
|
|
|
attrs := []struct {
|
|
|
|
field string
|
|
|
|
value int64
|
|
|
|
file string
|
|
|
|
want string
|
|
|
|
skipIfNotFound bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
field: "cpu-shares",
|
|
|
|
value: 3333,
|
|
|
|
file: "cpu.weight",
|
|
|
|
want: "128",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "cpu-period",
|
|
|
|
value: 2000,
|
|
|
|
file: "cpu.max",
|
|
|
|
want: "max 2000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "memory",
|
|
|
|
value: 1 << 30,
|
|
|
|
file: "memory.max",
|
|
|
|
want: "1073741824",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "memory-reservation",
|
|
|
|
value: 500 << 20,
|
|
|
|
file: "memory.low",
|
|
|
|
want: "524288000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "memory-swap",
|
|
|
|
value: 1 << 31,
|
|
|
|
file: "memory.swap.max",
|
|
|
|
// memory.swap.max is only the swap value, unlike cgroupv1
|
|
|
|
want: fmt.Sprintf("%d", 1<<31-1<<30),
|
|
|
|
skipIfNotFound: true, // swap may be disabled on the machine.
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "blkio-weight",
|
|
|
|
value: 750,
|
|
|
|
file: "io.bfq.weight",
|
|
|
|
want: fmt.Sprintf("default %d", 750),
|
|
|
|
skipIfNotFound: true, // blkio groups may not be available.
|
|
|
|
},
|
|
|
|
{
|
|
|
|
field: "pids-limit",
|
|
|
|
value: 1000,
|
|
|
|
file: "pids.max",
|
|
|
|
want: "1000",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make configs.
|
|
|
|
conf, hostconf, _ := d.ConfigsFrom(dockerutil.RunOpts{
|
|
|
|
Image: "basic/alpine",
|
|
|
|
}, "sleep", "10000")
|
|
|
|
|
|
|
|
// Add Cgroup arguments to configs.
|
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.field {
|
|
|
|
case "cpu-shares":
|
|
|
|
hostconf.Resources.CPUShares = attr.value
|
|
|
|
case "cpu-period":
|
|
|
|
hostconf.Resources.CPUPeriod = attr.value
|
|
|
|
case "cpu-quota":
|
|
|
|
hostconf.Resources.CPUQuota = attr.value
|
|
|
|
case "kernel-memory":
|
|
|
|
hostconf.Resources.KernelMemory = attr.value
|
|
|
|
case "memory":
|
|
|
|
hostconf.Resources.Memory = attr.value
|
|
|
|
case "memory-reservation":
|
|
|
|
hostconf.Resources.MemoryReservation = attr.value
|
|
|
|
case "memory-swap":
|
|
|
|
hostconf.Resources.MemorySwap = attr.value
|
|
|
|
case "memory-swappiness":
|
|
|
|
val := attr.value
|
|
|
|
hostconf.Resources.MemorySwappiness = &val
|
|
|
|
case "blkio-weight":
|
|
|
|
// detect existence of io.bfq.weight as this is not always loaded
|
2021-11-30 02:37:42 +00:00
|
|
|
_, err := ioutil.ReadFile(filepath.Join("/sys/fs/cgroup/docker", attr.file))
|
2021-11-02 15:20:58 +00:00
|
|
|
if err == nil || !attr.skipIfNotFound {
|
|
|
|
hostconf.Resources.BlkioWeight = uint16(attr.value)
|
|
|
|
}
|
|
|
|
case "pids-limit":
|
|
|
|
val := attr.value
|
|
|
|
hostconf.Resources.PidsLimit = &val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create container.
|
|
|
|
if err := d.CreateFrom(ctx, "basic/alpine", conf, hostconf, nil); err != nil {
|
|
|
|
t.Fatalf("create failed with: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start container.
|
|
|
|
if err := d.Start(ctx); err != nil {
|
|
|
|
t.Fatalf("start failed with: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the relevant cgroup ID.
|
|
|
|
gid := d.ID()
|
|
|
|
t.Logf("cgroup ID: %s", gid)
|
|
|
|
|
|
|
|
// Check list of attributes defined above.
|
|
|
|
for _, attr := range attrs {
|
2021-11-30 02:37:42 +00:00
|
|
|
path := filepath.Join("/sys/fs/cgroup/docker", gid, attr.file)
|
2021-11-02 15:20:58 +00:00
|
|
|
out, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) && attr.skipIfNotFound {
|
|
|
|
t.Logf("skipped %s", attr.file)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("failed to read %q: %v", path, err)
|
|
|
|
}
|
|
|
|
if got := strings.TrimSpace(string(out)); got != attr.want {
|
|
|
|
t.Errorf("field: %q, cgroup attribute %s, got: %q, want: %q", attr.field, attr.file, got, attr.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that sandbox is inside cgroup.
|
|
|
|
pid, err := d.SandboxPid(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("SandboxPid: %v", err)
|
|
|
|
}
|
2021-11-30 02:37:42 +00:00
|
|
|
path := filepath.Join("/sys/fs/cgroup/docker", gid, "cgroup.procs")
|
2021-11-02 15:20:58 +00:00
|
|
|
if err := verifyPid(pid, path); err != nil {
|
|
|
|
t.Errorf("cgroup control processes: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:36:58 +00:00
|
|
|
// TestCgroupParent sets the "CgroupParent" option and checks that the child and
|
|
|
|
// parent's cgroups are created correctly relative to each other.
|
2019-02-26 03:20:52 +00:00
|
|
|
func TestCgroupParent(t *testing.T) {
|
2020-07-08 20:24:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
d := dockerutil.MakeContainer(ctx, t)
|
|
|
|
defer d.CleanUp(ctx)
|
2019-02-26 03:20:52 +00:00
|
|
|
|
2020-04-23 18:32:08 +00:00
|
|
|
// Construct a known cgroup name.
|
|
|
|
parent := testutil.RandomID("runsc-")
|
2020-07-08 20:24:58 +00:00
|
|
|
conf, hostconf, _ := d.ConfigsFrom(dockerutil.RunOpts{
|
2020-04-23 18:32:08 +00:00
|
|
|
Image: "basic/alpine",
|
2020-07-08 20:24:58 +00:00
|
|
|
}, "sleep", "10000")
|
|
|
|
hostconf.Resources.CgroupParent = parent
|
|
|
|
|
2021-01-05 21:20:12 +00:00
|
|
|
if err := d.CreateFrom(ctx, "basic/alpine", conf, hostconf, nil); err != nil {
|
2020-07-08 20:24:58 +00:00
|
|
|
t.Fatalf("create failed with: %v", err)
|
2019-02-26 03:20:52 +00:00
|
|
|
}
|
2020-04-23 18:32:08 +00:00
|
|
|
|
2020-07-08 20:24:58 +00:00
|
|
|
if err := d.Start(ctx); err != nil {
|
|
|
|
t.Fatalf("start failed with: %v", err)
|
2019-02-26 03:20:52 +00:00
|
|
|
}
|
2020-07-08 20:24:58 +00:00
|
|
|
|
|
|
|
// Extract the ID to look up the cgroup.
|
|
|
|
gid := d.ID()
|
2019-02-26 03:20:52 +00:00
|
|
|
t.Logf("cgroup ID: %s", gid)
|
|
|
|
|
|
|
|
// Check that sandbox is inside cgroup.
|
2020-07-08 20:24:58 +00:00
|
|
|
pid, err := d.SandboxPid(ctx)
|
2019-02-26 03:20:52 +00:00
|
|
|
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)
|
2021-05-06 00:36:58 +00:00
|
|
|
ppidStr, err := exec.Command("bash", "-c", cmd).CombinedOutput()
|
2019-02-26 03:20:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Executing %q: %v", cmd, err)
|
|
|
|
}
|
2021-05-06 00:36:58 +00:00
|
|
|
ppid, err := strconv.Atoi(strings.TrimSpace(string(ppidStr)))
|
2019-02-26 03:20:52 +00:00
|
|
|
if err != nil {
|
2021-05-06 00:36:58 +00:00
|
|
|
t.Fatalf("invalid PID (%s): %v", ppidStr, err)
|
2019-02-26 03:20:52 +00:00
|
|
|
}
|
2021-05-06 00:36:58 +00:00
|
|
|
cgroups, err := cgroup.NewFromPid(ppid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cgroup.NewFromPid(%d): %v", ppid, err)
|
|
|
|
}
|
|
|
|
path := filepath.Join(cgroups.MakePath("cpuacct"), parent, gid, "cgroup.procs")
|
2019-02-26 03:20:52 +00:00
|
|
|
if err := verifyPid(pid, path); err != nil {
|
|
|
|
t.Errorf("cgroup control %q processes: %v", "memory", err)
|
|
|
|
}
|
|
|
|
}
|