Files
visor/pkg/bitmap/format_test.go
T
Shailend ChandandgVisor bot dde347d9fc Add initial cgroup2 support.
The cgroup v2 fs is initialized early in the boot process, so that every task
in the sandbox resides in at least the root v2 cgroup. A new runsc flag
--mount-cgroup-v2 has been added: this controls whether runsc mounts a
cgroupv2 filesystem at /sys/fs/cgroup automatically (currently it always
mounts a v1 hierarchy at this path). This flag also gates whether userspace
can mount a cgroup2 fs. When this flag is false, which is the default, all
tasks in the sandbox reside forever in the root cgroup.

Like in Linux, the v2 hierarchy is deemed the default owner of a controller,
that is, until the corresponding v1 hierarchy is mounted: then, the v1 tree
steals ownership of that controller and it remains this way till as long as
a v1 mount exists. In other words, hybrid mode is supported, even if such
workloads are rare these days.

The pids controller is the only controller implemented in full: it serves
as validation for the unified hierarchy. The memory controller can be used
to learn the usage stats of a subtree: but like the current v1 implementation,
and unlike Linux, it doesn't achieve this through charging up the hierarchy
during allocation, instead it queries pgalloc for every active controller's
id. And it does no enforcing. The cpu and cpuset controllers are mere stubs,
like in the current v1 implementation.

The following are supported:
* General matters of topology: cgroup.subtree_control, cgroup.controllers,
                               cgroup.procs etc
* CLONE_INTO_CGROUP
* cgroup.kill
* cgroup.events
* A working pids controller and its events files
* A minimal memory controller that just reports mem usage
* Stubbed cpu and cpuset controllers

The following are NOT supported:
* threaded cgroups
* cgroup namespaces
* BPF_PROG_TYPE_CGROUP_DEVICE
* cgroup.freeze

Updates #13007

PiperOrigin-RevId: 943519748
2026-07-06 15:17:54 -07:00

98 lines
2.7 KiB
Go

// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bitmap
import (
"fmt"
"slices"
"testing"
)
func TestFormat(t *testing.T) {
tests := []struct {
input []uint32
output string
}{
{[]uint32{1, 2, 3, 4, 7}, "1-4,7"},
{[]uint32{2}, "2"},
{[]uint32{0, 1, 2}, "0-2"},
{[]uint32{}, ""},
{[]uint32{1, 3, 4, 5, 6, 9, 11, 13, 14, 15, 16, 17}, "1,3-6,9,11,13-17"},
{[]uint32{2, 3, 10, 12, 13, 14, 15, 16, 20, 21, 33, 34, 47}, "2-3,10,12-16,20-21,33-34,47"},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
b := New(64)
for _, v := range tt.input {
b.Add(v)
}
s := FormatList(&b)
if s != tt.output {
t.Errorf("Expected %q, got %q", tt.output, s)
}
b1, err := ParseList(s, 64)
if err != nil {
t.Fatalf("Failed to parse formatted bitmap: %v", err)
}
if got, want := b1.ToSlice(), b.ToSlice(); !slices.Equal(got, want) {
t.Errorf("Parsing formatted output doesn't result in the original bitmap. Got %v, want %v", got, want)
}
})
}
}
func TestParse(t *testing.T) {
tests := []struct {
input string
output []uint32
shouldFail bool
}{
{"1", []uint32{1}, false},
{"", []uint32{}, false},
{"1,2,3,4", []uint32{1, 2, 3, 4}, false},
{"1-4", []uint32{1, 2, 3, 4}, false},
{"1,2-4", []uint32{1, 2, 3, 4}, false},
{"1,2-3,4", []uint32{1, 2, 3, 4}, false},
{"1-2,3,4,10,11", []uint32{1, 2, 3, 4, 10, 11}, false},
{"1,2-4,5,16", []uint32{1, 2, 3, 4, 5, 16}, false},
{"abc", []uint32{}, true},
{"1,3-2,4", []uint32{}, true},
{"1,3-3,4", []uint32{}, true},
{"1,2,3\000,4", []uint32{1, 2, 3}, false},
{"1,2,3\n,4", []uint32{1, 2, 3}, false},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
b, err := ParseList(tt.input, 64)
if tt.shouldFail {
if err == nil {
t.Fatalf("Expected parsing of %q to fail, but it didn't", tt.input)
}
return
}
if err != nil {
t.Fatalf("Failed to parse bitmap: %v", err)
return
}
got := b.ToSlice()
if !slices.Equal(got, tt.output) {
t.Errorf("Parsed bitmap doesn't match what we expected. Got %v, want %v", got, tt.output)
}
})
}
}