Skip to content

Commit 1bf797d

Browse files
authored
Merge pull request containerd#4521 from AkihiroSuda/fix-4519
Fix `TestRunCgroupV2` with runc v1.4.0-rc.1
2 parents 5887c13 + 9c75d4b commit 1bf797d

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

cmd/nerdctl/container/container_run_cgroup_linux_test.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25+
"regexp"
2526
"strconv"
2627
"strings"
2728
"testing"
@@ -38,6 +39,7 @@ import (
3839

3940
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
4041
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
42+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
4143
"github.com/containerd/nerdctl/v2/pkg/testutil"
4244
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
4345
)
@@ -60,9 +62,6 @@ func TestRunCgroupV2(t *testing.T) {
6062
if !info.SwapLimit {
6163
t.Skip("test requires SwapLimit")
6264
}
63-
if !info.CPUShares {
64-
t.Skip("test requires CPUShares")
65-
}
6665
if !info.CPUSet {
6766
t.Skip("test requires CPUSet")
6867
}
@@ -73,7 +72,6 @@ func TestRunCgroupV2(t *testing.T) {
7372
44040192
7473
44040192
7574
42
76-
77
7775
0-1
7876
0
7977
`
@@ -82,34 +80,32 @@ func TestRunCgroupV2(t *testing.T) {
8280
60817408
8381
6291456
8482
42
85-
77
8683
0-1
8784
0
8885
`
8986

90-
// In CgroupV2 CPUWeight replace CPUShares => weight := 1 + ((shares-2)*9999)/262142
9187
base.Cmd("run", "--rm",
9288
"--cpus", "0.42", "--cpuset-mems", "0",
9389
"--memory", "42m",
9490
"--pids-limit", "42",
95-
"--cpu-shares", "2000", "--cpuset-cpus", "0-1",
91+
"--cpuset-cpus", "0-1",
9692
"-w", "/sys/fs/cgroup", testutil.AlpineImage,
9793
"cat", "cpu.max", "memory.max", "memory.swap.max",
98-
"pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected1)
94+
"pids.max", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected1)
9995
base.Cmd("run", "--rm",
10096
"--cpu-quota", "42000", "--cpuset-mems", "0",
10197
"--cpu-period", "100000", "--memory", "42m", "--memory-reservation", "6m", "--memory-swap", "100m",
102-
"--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1",
98+
"--pids-limit", "42", "--cpuset-cpus", "0-1",
10399
"-w", "/sys/fs/cgroup", testutil.AlpineImage,
104100
"cat", "cpu.max", "memory.max", "memory.swap.max", "memory.low", "pids.max",
105-
"cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected2)
101+
"cpuset.cpus", "cpuset.mems").AssertOutExactly(expected2)
106102

107103
base.Cmd("run", "--name", testutil.Identifier(t)+"-testUpdate1", "-w", "/sys/fs/cgroup", "-d",
108104
testutil.AlpineImage, "sleep", nerdtest.Infinity).AssertOK()
109105
defer base.Cmd("rm", "-f", testutil.Identifier(t)+"-testUpdate1").Run()
110106
update := []string{"update", "--cpu-quota", "42000", "--cpuset-mems", "0", "--cpu-period", "100000",
111107
"--memory", "42m",
112-
"--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1"}
108+
"--pids-limit", "42", "--cpuset-cpus", "0-1"}
113109
if nerdtest.IsDocker() && info.CgroupVersion == "2" && info.SwapLimit {
114110
// Workaround for Docker with cgroup v2:
115111
// > Error response from daemon: Cannot update container 67c13276a13dd6a091cdfdebb355aa4e1ecb15fbf39c2b5c9abee89053e88fce:
@@ -120,7 +116,7 @@ func TestRunCgroupV2(t *testing.T) {
120116
base.Cmd(update...).AssertOK()
121117
base.Cmd("exec", testutil.Identifier(t)+"-testUpdate1",
122118
"cat", "cpu.max", "memory.max", "memory.swap.max",
123-
"pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected1)
119+
"pids.max", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected1)
124120

125121
defer base.Cmd("rm", "-f", testutil.Identifier(t)+"-testUpdate2").Run()
126122
base.Cmd("run", "--name", testutil.Identifier(t)+"-testUpdate2", "-w", "/sys/fs/cgroup", "-d",
@@ -129,11 +125,11 @@ func TestRunCgroupV2(t *testing.T) {
129125

130126
base.Cmd("update", "--cpu-quota", "42000", "--cpuset-mems", "0", "--cpu-period", "100000",
131127
"--memory", "42m", "--memory-reservation", "6m", "--memory-swap", "100m",
132-
"--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1",
128+
"--pids-limit", "42", "--cpuset-cpus", "0-1",
133129
testutil.Identifier(t)+"-testUpdate2").AssertOK()
134130
base.Cmd("exec", testutil.Identifier(t)+"-testUpdate2",
135131
"cat", "cpu.max", "memory.max", "memory.swap.max", "memory.low",
136-
"pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected2)
132+
"pids.max", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected2)
137133
base.Cmd("run", "--rm", "--security-opt", "writable-cgroups=true", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/foo").AssertOK()
138134
base.Cmd("run", "--rm", "--security-opt", "writable-cgroups=false", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/foo").AssertFail()
139135
base.Cmd("run", "--rm", testutil.AlpineImage, "mkdir", "/sys/fs/cgroup/foo").AssertFail()
@@ -736,3 +732,30 @@ func TestRunCPURealTimeSettingCgroupV1(t *testing.T) {
736732

737733
testCase.Run(t)
738734
}
735+
736+
func TestRunCPUSharesCgroupV2(t *testing.T) {
737+
nerdtest.Setup()
738+
739+
testCase := &test.Case{
740+
Require: require.All(
741+
nerdtest.CGroupV2,
742+
nerdtest.Info(
743+
func(info dockercompat.Info) error {
744+
if !info.CPUShares {
745+
return fmt.Errorf("test requires CPUShares")
746+
}
747+
return nil
748+
},
749+
),
750+
),
751+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
752+
return helpers.Command("run", "--rm", "--cpu-shares", "2000",
753+
testutil.AlpineImage, "cat", "/sys/fs/cgroup/cpu.weight")
754+
},
755+
// The value was historically 77, but with runc v1.4.0-rc.1 it became 170.
756+
// https://github.com/opencontainers/runc/issues/4896#issuecomment-3301825811
757+
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile("^(77|170)\n$"))),
758+
}
759+
760+
testCase.Run(t)
761+
}

pkg/testutil/nerdtest/requirements.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,24 @@ var Rootless = &test.Requirement{
164164
// Rootful marks a test as suitable only for rootful env
165165
var Rootful = require.Not(Rootless)
166166

167+
// Info requires that `nerdctl info` satisfies the condition function passed as argument.
168+
func Info(f func(dockercompat.Info) error) *test.Requirement {
169+
return &test.Requirement{
170+
Check: func(data test.Data, helpers test.Helpers) (ret bool, mess string) {
171+
stdout := helpers.Capture("info", "--format", "{{ json . }}")
172+
var dinf dockercompat.Info
173+
err := json.Unmarshal([]byte(stdout), &dinf)
174+
if err != nil {
175+
return false, fmt.Sprintf("failed to parse docker info: %v", err)
176+
}
177+
if err := f(dinf); err != nil {
178+
return false, err.Error()
179+
}
180+
return true, ""
181+
},
182+
}
183+
}
184+
167185
// CGroup requires that cgroup is enabled
168186
var CGroup = &test.Requirement{
169187
Check: func(data test.Data, helpers test.Helpers) (ret bool, mess string) {

0 commit comments

Comments
 (0)