Skip to content

Commit 23e41ef

Browse files
authored
Merge pull request #3960 from kolyshkin/local-ci-v2
Fix running tests under Docker/Podman and cgroup v2
2 parents f0a5e6b + f88a765 commit 23e41ef

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ ENV PKG_CONFIG_PATH=/opt/libseccomp/lib/pkgconfig
6363
RUN git config --global --add safe.directory /go/src/github.com/opencontainers/runc
6464

6565
WORKDIR /go/src/github.com/opencontainers/runc
66+
67+
# Fixup for cgroup v2.
68+
COPY script/prepare-cgroup-v2.sh /
69+
ENTRYPOINT [ "/prepare-cgroup-v2.sh" ]

libcontainer/cgroups/file_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ func TestOpenat2(t *testing.T) {
5858
{"/sys/fs/cgroup", "/cgroup.controllers"},
5959
{"/sys/fs/cgroup/", "cgroup.controllers"},
6060
{"/sys/fs/cgroup/", "/cgroup.controllers"},
61-
{"/sys/fs/cgroup/user.slice", "cgroup.controllers"},
62-
{"/sys/fs/cgroup/user.slice/", "/cgroup.controllers"},
6361
{"/", "/sys/fs/cgroup/cgroup.controllers"},
6462
{"/", "sys/fs/cgroup/cgroup.controllers"},
6563
{"/sys/fs/cgroup/cgroup.controllers", ""},

libcontainer/cgroups/manager/manager_test.go

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,53 @@ package manager
33
import (
44
"testing"
55

6+
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
67
"github.com/opencontainers/runc/libcontainer/configs"
78
)
89

910
// TestNilResources checks that a cgroup manager do not panic when
1011
// config.Resources is nil. While it does not make sense to use a
1112
// manager with no resources, it should not result in a panic.
1213
//
13-
// This tests either v1 or v2 managers (both fs and systemd),
14-
// depending on what cgroup version is available on the host.
14+
// This tests either v1 or v2 fs cgroup manager, depending on which
15+
// cgroup version is available.
1516
func TestNilResources(t *testing.T) {
16-
for _, sd := range []bool{false, true} {
17-
cg := &configs.Cgroup{} // .Resources is nil
18-
cg.Systemd = sd
19-
mgr, err := New(cg)
17+
testNilResources(t, false)
18+
}
19+
20+
// TestNilResourcesSystemd is the same as TestNilResources,
21+
// only checking the systemd cgroup manager.
22+
func TestNilResourcesSystemd(t *testing.T) {
23+
if !systemd.IsRunningSystemd() {
24+
t.Skip("requires systemd")
25+
}
26+
testNilResources(t, true)
27+
}
28+
29+
func testNilResources(t *testing.T, systemd bool) {
30+
cg := &configs.Cgroup{} // .Resources is nil
31+
cg.Systemd = systemd
32+
mgr, err := New(cg)
33+
if err != nil {
34+
// Some managers require non-nil Resources during
35+
// instantiation -- provide and retry. In such case
36+
// we're mostly testing Set(nil) below.
37+
cg.Resources = &configs.Resources{}
38+
mgr, err = New(cg)
2039
if err != nil {
21-
// Some managers require non-nil Resources during
22-
// instantiation -- provide and retry. In such case
23-
// we're mostly testing Set(nil) below.
24-
cg.Resources = &configs.Resources{}
25-
mgr, err = New(cg)
26-
if err != nil {
27-
t.Error(err)
28-
continue
29-
}
40+
t.Fatal(err)
3041
}
31-
_ = mgr.Apply(-1)
32-
_ = mgr.Set(nil)
33-
_ = mgr.Freeze(configs.Thawed)
34-
_ = mgr.Exists()
35-
_, _ = mgr.GetAllPids()
36-
_, _ = mgr.GetCgroups()
37-
_, _ = mgr.GetFreezerState()
38-
_ = mgr.Path("")
39-
_ = mgr.GetPaths()
40-
_, _ = mgr.GetStats()
41-
_, _ = mgr.OOMKillCount()
42-
_ = mgr.Destroy()
4342
}
43+
_ = mgr.Apply(-1)
44+
_ = mgr.Set(nil)
45+
_ = mgr.Freeze(configs.Thawed)
46+
_ = mgr.Exists()
47+
_, _ = mgr.GetAllPids()
48+
_, _ = mgr.GetCgroups()
49+
_, _ = mgr.GetFreezerState()
50+
_ = mgr.Path("")
51+
_ = mgr.GetPaths()
52+
_, _ = mgr.GetStats()
53+
_, _ = mgr.OOMKillCount()
54+
_ = mgr.Destroy()
4455
}

script/prepare-cgroup-v2.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
#
3+
# This script is used from ../Dockerfile as the ENTRYPOINT. It sets up cgroup
4+
# delegation for cgroup v2 to make sure runc tests can be properly run inside
5+
# a container.
6+
7+
# Only do this for cgroup v2.
8+
if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
9+
set -x
10+
# Move the current process to a sub-cgroup.
11+
mkdir /sys/fs/cgroup/init
12+
echo 0 >/sys/fs/cgroup/init/cgroup.procs
13+
# Enable all controllers.
14+
sed 's/\b\w/+\0/g' <"/sys/fs/cgroup/cgroup.controllers" >"/sys/fs/cgroup/cgroup.subtree_control"
15+
fi
16+
17+
exec "$@"

tests/integration/update.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,5 +854,5 @@ EOF
854854
# The container will be OOM killed, and runc might either succeed
855855
# or fail depending on the timing, so we don't check its exit code.
856856
runc update test_update --memory 1024
857-
testcontainer test_update stopped
857+
wait_for_container 10 1 test_update stopped
858858
}

0 commit comments

Comments
 (0)