1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
/*
Copyright The containerd 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 cgroup2
import (
"fmt"
"math"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCgroupv2CpuStats(t *testing.T) {
checkCgroupMode(t)
group := "/cpu-test-cg"
groupPath := fmt.Sprintf("%s-%d", group, os.Getpid())
var (
quota int64 = 10000
period uint64 = 8000
weight uint64 = 100
)
c, err := NewManager(defaultCgroup2Path, groupPath, &Resources{
CPU: &CPU{
Weight: &weight,
Max: NewCPUMax("a, &period),
Cpus: "0",
Mems: "0",
},
})
require.NoError(t, err, "failed to init new cgroup manager")
t.Cleanup(func() {
_ = os.Remove(c.path)
})
checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10))
checkFileContent(t, c.path, "cpu.max", "10000 8000")
checkFileContent(t, c.path, "cpuset.cpus", "0")
checkFileContent(t, c.path, "cpuset.mems", "0")
}
func TestSystemdCgroupCpuController(t *testing.T) {
checkCgroupMode(t)
group := fmt.Sprintf("testing-cpu-%d.scope", os.Getpid())
var weight uint64 = 100
c, err := NewSystemd("", group, os.Getpid(), &Resources{CPU: &CPU{Weight: &weight}})
require.NoError(t, err, "failed to init new cgroup systemd manager")
checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10))
}
func TestSystemdCgroupCpuController_NilWeight(t *testing.T) {
checkCgroupMode(t)
group := "testingCpuNilWeight.slice"
// nil weight defaults to 100
var quota int64 = 10000
var period uint64 = 8000
cpuMax := NewCPUMax("a, &period)
_, err := NewSystemd("/", group, -1, &Resources{
CPU: &CPU{
Weight: nil,
Max: cpuMax,
},
})
require.NoError(t, err, "failed to init new cgroup systemd manager")
}
func TestExtractQuotaAndPeriod(t *testing.T) {
var (
period uint64
quota int64
)
quota = 10000
period = 8000
cpuMax := NewCPUMax("a, &period)
tquota, tPeriod := cpuMax.extractQuotaAndPeriod()
assert.Equal(t, quota, tquota)
assert.Equal(t, period, tPeriod)
// case with nil quota which makes it "max" - max int val
cpuMax2 := NewCPUMax(nil, &period)
tquota2, tPeriod2 := cpuMax2.extractQuotaAndPeriod()
assert.Equal(t, int64(math.MaxInt64), tquota2)
assert.Equal(t, period, tPeriod2)
}
|