@@ -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) {
737244040192
747344040192
757442
76- 77
77750-1
78760
7977`
@@ -82,34 +80,32 @@ func TestRunCgroupV2(t *testing.T) {
828060817408
83816291456
848242
85- 77
86830-1
87840
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+ }
0 commit comments