Skip to content

Commit 9c75d4b

Browse files
committed
Add TestRunCPUSharesCgroupV2
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 8466806 commit 9c75d4b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

cmd/nerdctl/container/container_run_cgroup_linux_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"os/exec"
2525
"path/filepath"
26+
"regexp"
2627
"strconv"
2728
"strings"
2829
"testing"
@@ -39,6 +40,7 @@ import (
3940

4041
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
4142
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
43+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
4244
"github.com/containerd/nerdctl/v2/pkg/testutil"
4345
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
4446
)
@@ -709,3 +711,30 @@ func TestRunCPURealTimeSettingCgroupV1(t *testing.T) {
709711

710712
testCase.Run(t)
711713
}
714+
715+
func TestRunCPUSharesCgroupV2(t *testing.T) {
716+
nerdtest.Setup()
717+
718+
testCase := &test.Case{
719+
Require: require.All(
720+
nerdtest.CGroupV2,
721+
nerdtest.Info(
722+
func(info dockercompat.Info) error {
723+
if !info.CPUShares {
724+
return fmt.Errorf("test requires CPUShares")
725+
}
726+
return nil
727+
},
728+
),
729+
),
730+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
731+
return helpers.Command("run", "--rm", "--cpu-shares", "2000",
732+
testutil.AlpineImage, "cat", "/sys/fs/cgroup/cpu.weight")
733+
},
734+
// The value was historically 77, but with runc v1.4.0-rc.1 it became 170.
735+
// https://github.com/opencontainers/runc/issues/4896#issuecomment-3301825811
736+
Expected: test.Expects(0, nil, expect.Match(regexp.MustCompile("^(77|170)\n$"))),
737+
}
738+
739+
testCase.Run(t)
740+
}

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)