|
@@ -7,6 +7,7 @@ import (
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
+ "path"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
"testing"
|
|
@@ -107,3 +108,81 @@ func TestRunWithUlimits(t *testing.T) {
|
|
|
|
|
|
logDone("run - ulimits are set")
|
|
|
}
|
|
|
+
|
|
|
+func getCgroupPaths(test string) map[string]string {
|
|
|
+ cgroupPaths := map[string]string{}
|
|
|
+ for _, line := range strings.Split(test, "\n") {
|
|
|
+ parts := strings.Split(line, ":")
|
|
|
+ if len(parts) != 3 {
|
|
|
+ fmt.Printf("unexpected file format for /proc/self/cgroup - %q", line)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cgroupPaths[parts[1]] = parts[2]
|
|
|
+ }
|
|
|
+ return cgroupPaths
|
|
|
+}
|
|
|
+
|
|
|
+func TestRunContainerWithCgroupParent(t *testing.T) {
|
|
|
+ testRequires(t, NativeExecDriver)
|
|
|
+ defer deleteAllContainers()
|
|
|
+
|
|
|
+ cgroupParent := "test"
|
|
|
+ data, err := ioutil.ReadFile("/proc/self/cgroup")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("failed to read '/proc/self/cgroup - %v", err)
|
|
|
+ }
|
|
|
+ selfCgroupPaths := getCgroupPaths(string(data))
|
|
|
+ selfCpuCgroup, found := selfCgroupPaths["cpu"]
|
|
|
+ if !found {
|
|
|
+ t.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
|
|
|
+ }
|
|
|
+
|
|
|
+ out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
|
|
|
+ }
|
|
|
+ cgroupPaths := getCgroupPaths(string(out))
|
|
|
+ if len(cgroupPaths) == 0 {
|
|
|
+ t.Fatalf("unexpected output - %q", string(out))
|
|
|
+ }
|
|
|
+ found = false
|
|
|
+ expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
|
|
|
+ for _, path := range cgroupPaths {
|
|
|
+ if strings.HasPrefix(path, expectedCgroupPrefix) {
|
|
|
+ found = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !found {
|
|
|
+ t.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
|
|
|
+ }
|
|
|
+ logDone("run - cgroup parent")
|
|
|
+}
|
|
|
+
|
|
|
+func TestRunContainerWithCgroupParentAbsPath(t *testing.T) {
|
|
|
+ testRequires(t, NativeExecDriver)
|
|
|
+ defer deleteAllContainers()
|
|
|
+
|
|
|
+ cgroupParent := "/cgroup-parent/test"
|
|
|
+
|
|
|
+ out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
|
|
|
+ if err != nil {
|
|
|
+ t.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
|
|
|
+ }
|
|
|
+ cgroupPaths := getCgroupPaths(string(out))
|
|
|
+ if len(cgroupPaths) == 0 {
|
|
|
+ t.Fatalf("unexpected output - %q", string(out))
|
|
|
+ }
|
|
|
+ found := false
|
|
|
+ for _, path := range cgroupPaths {
|
|
|
+ if strings.HasPrefix(path, cgroupParent) {
|
|
|
+ found = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !found {
|
|
|
+ t.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
|
|
|
+ }
|
|
|
+
|
|
|
+ logDone("run - cgroup parent with absolute cgroup path")
|
|
|
+}
|