From fa47f95872ed265ff20a1edcc4b0ea0e5ee7fbcf Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 28 Sep 2020 18:43:19 -0700 Subject: [PATCH] libct/int/newTemplateConfig: add systemd support ... and properly set ScopePrefix, Name and Parent for a systemd case. Signed-off-by: Kir Kolyshkin --- libcontainer/integration/exec_test.go | 45 ++++++++++++----------- libcontainer/integration/template_test.go | 19 ++++++++-- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 23ac876ca..7e8bd014c 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -514,7 +514,10 @@ func testFreeze(t *testing.T, systemd bool) { ok(t, err) defer remove(rootfs) - config := newTemplateConfig(&tParam{rootfs: rootfs}) + config := newTemplateConfig(&tParam{ + rootfs: rootfs, + systemd: systemd, + }) container, err := newContainerWithName("test", config) ok(t, err) defer container.Destroy() @@ -571,10 +574,10 @@ func testCpuShares(t *testing.T, systemd bool) { ok(t, err) defer remove(rootfs) - config := newTemplateConfig(&tParam{rootfs: rootfs}) - if systemd { - config.Cgroups.Parent = "system.slice" - } + config := newTemplateConfig(&tParam{ + rootfs: rootfs, + systemd: systemd, + }) config.Cgroups.Resources.CpuShares = 1 _, _, err = runContainer(config, "", "ps") @@ -603,10 +606,10 @@ func testPids(t *testing.T, systemd bool) { ok(t, err) defer remove(rootfs) - config := newTemplateConfig(&tParam{rootfs: rootfs}) - if systemd { - config.Cgroups.Parent = "system.slice" - } + config := newTemplateConfig(&tParam{ + rootfs: rootfs, + systemd: systemd, + }) config.Cgroups.Resources.PidsLimit = -1 // Running multiple processes. @@ -689,10 +692,10 @@ func testRunWithKernelMemory(t *testing.T, systemd bool) { ok(t, err) defer remove(rootfs) - config := newTemplateConfig(&tParam{rootfs: rootfs}) - if systemd { - config.Cgroups.Parent = "system.slice" - } + config := newTemplateConfig(&tParam{ + rootfs: rootfs, + systemd: systemd, + }) config.Cgroups.Resources.KernelMemory = 52428800 _, _, err = runContainer(config, "", "ps") @@ -723,10 +726,10 @@ func testCgroupResourcesUnifiedErrorOnV1(t *testing.T, systemd bool) { ok(t, err) defer remove(rootfs) - config := newTemplateConfig(&tParam{rootfs: rootfs}) - if systemd { - config.Cgroups.Parent = "system.slice" - } + config := newTemplateConfig(&tParam{ + rootfs: rootfs, + systemd: systemd, + }) config.Cgroups.Resources.Unified = map[string]string{ "memory.min": "10240", } @@ -758,13 +761,13 @@ func testCgroupResourcesUnified(t *testing.T, systemd bool) { ok(t, err) defer remove(rootfs) - config := newTemplateConfig(&tParam{rootfs: rootfs}) + config := newTemplateConfig(&tParam{ + rootfs: rootfs, + systemd: systemd, + }) config.Cgroups.Resources.Memory = 536870912 // 512M config.Cgroups.Resources.MemorySwap = 536870912 // 512M, i.e. no swap config.Namespaces.Add(configs.NEWCGROUP, "") - if systemd { - config.Cgroups.Parent = "system.slice" - } testCases := []struct { name string diff --git a/libcontainer/integration/template_test.go b/libcontainer/integration/template_test.go index ac7f1797a..51a6b5361 100644 --- a/libcontainer/integration/template_test.go +++ b/libcontainer/integration/template_test.go @@ -1,6 +1,9 @@ package integration import ( + "math/rand" + "strconv" + "github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/specconv" @@ -17,8 +20,9 @@ var standardEnvironment = []string{ const defaultMountFlags = unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV type tParam struct { - rootfs string - userns bool + rootfs string + userns bool + systemd bool } // newTemplateConfig returns a base template for running a container @@ -122,7 +126,6 @@ func newTemplateConfig(p *tParam) *configs.Config { {Type: configs.NEWNET}, }), Cgroups: &configs.Cgroup{ - Path: "/sys/fs/cgroup/", Resources: &configs.Resources{ MemorySwappiness: nil, Devices: allowedDevices, @@ -209,5 +212,15 @@ func newTemplateConfig(p *tParam) *configs.Config { }) } + if p.systemd { + id := strconv.FormatUint(rand.Uint64(), 36) + config.Cgroups.Name = "test" + id + // do not change Parent (see newContainerWithName) + config.Cgroups.Parent = "system.slice" + config.Cgroups.ScopePrefix = "runc-test" + } else { + config.Cgroups.Path = "/test/integration" + } + return config }