Files
runc/libcontainer/cgroups/systemd/cpuset_test.go
T
Chengen, Du 77cae9addc cgroups: cpuset: fix byte order while parsing cpuset range to bits
Runc parses cpuset range to bits in the case of cgroup v2 + systemd as cgroup driver.
The byte order representation differs from systemd expectation, which will set
different cpuset range in systemd transient unit if the length of parsed byte array exceeds one.

	# cat config.json
	...
	"resources": {
		...
		"cpu": {
			"cpus": "10-23"
		}
	},
	...
	# runc --systemd-cgroup run test
	# cat /run/systemd/transient/runc-test.scope.d/50-AllowedCPUs.conf
	# This is a drop-in unit file extension, created via "systemctl set-property"
	# or an equivalent operation. Do not edit.
	[Scope]
	AllowedCPUs=0-7 10-15

The cpuset.cpus in cgroup will also be set to wrong value after reloading systemd manager configuration.

	# systemctl daemon-reload
	# cat /sys/fs/cgroup/system.slice/runc-test.scope/cpuset.cpus
	0-7,10-15

Signed-off-by: seyeongkim <seyeong.kim@canonical.com>
Signed-off-by: Chengen, Du <chengen.du@canonical.com>
2022-10-13 11:13:29 +08:00

56 lines
1.3 KiB
Go

package systemd
import (
"bytes"
"testing"
)
func TestRangeToBits(t *testing.T) {
testCases := []struct {
in string
out []byte
isErr bool
}{
{in: "", isErr: true},
{in: "0", out: []byte{1}},
{in: "1", out: []byte{2}},
{in: "0-1", out: []byte{3}},
{in: "0,1", out: []byte{3}},
{in: ",0,1,", out: []byte{3}},
{in: "0-3", out: []byte{0x0f}},
{in: "0,1,2-3", out: []byte{0x0f}},
{in: "4-7", out: []byte{0xf0}},
{in: "0-7", out: []byte{0xff}},
{in: "0-15", out: []byte{0xff, 0xff}},
{in: "16", out: []byte{0, 0, 1}},
{in: "0-3,32-33", out: []byte{0x0f, 0, 0, 0, 3}},
// extra spaces and tabs are ok
{in: "1, 2, 1-2", out: []byte{6}},
{in: " , 1 , 3 , 5-7, ", out: []byte{0xea}},
// somewhat large values
{in: "128-130,1", out: []byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7}},
{in: "-", isErr: true},
{in: "1-", isErr: true},
{in: "-3", isErr: true},
// bad range (start > end)
{in: "54-53", isErr: true},
// kernel does not allow extra spaces inside a range
{in: "1 - 2", isErr: true},
}
for _, tc := range testCases {
out, err := RangeToBits(tc.in)
if err != nil {
if !tc.isErr {
t.Errorf("case %q: unexpected error: %v", tc.in, err)
}
continue
}
if !bytes.Equal(out, tc.out) {
t.Errorf("case %q: expected %v, got %v", tc.in, tc.out, out)
}
}
}