2020-09-22 20:26:44 +00:00
|
|
|
package cgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCountCPUs(t *testing.T) {
|
|
|
|
f := func(s string, nExpected int) {
|
|
|
|
t.Helper()
|
|
|
|
n := countCPUs(s)
|
|
|
|
if n != nExpected {
|
|
|
|
t.Fatalf("unexpected result from countCPUs(%q); got %d; want %d", s, n, nExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", -1)
|
|
|
|
f("1", 1)
|
|
|
|
f("234", 1)
|
|
|
|
f("1,2", 2)
|
|
|
|
f("0-1", 2)
|
|
|
|
f("0-0", 1)
|
|
|
|
f("1-2,3,5-9,200-210", 19)
|
|
|
|
f("0-3", 4)
|
|
|
|
f("0-6", 7)
|
|
|
|
}
|
2021-05-13 06:02:13 +00:00
|
|
|
|
2021-05-13 06:26:20 +00:00
|
|
|
func TestGetCPUQuotaV2(t *testing.T) {
|
2021-05-13 06:02:13 +00:00
|
|
|
f := func(sysPrefix, cgroupPath string, expectedCPU float64) {
|
|
|
|
t.Helper()
|
2021-05-13 06:26:20 +00:00
|
|
|
got, err := getCPUQuotaV2(sysPrefix, cgroupPath)
|
2021-05-13 06:02:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s, sysPrefix: %s, cgroupPath: %s", err, sysPrefix, cgroupPath)
|
|
|
|
}
|
|
|
|
if got != expectedCPU {
|
2021-05-13 06:26:20 +00:00
|
|
|
t.Fatalf("unexpected result from getCPUQuotaV2(%s, %s), got %f, want %f", sysPrefix, cgroupPath, got, expectedCPU)
|
2021-05-13 06:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f("testdata/cgroup", "testdata/self/cgroupv2", 2)
|
|
|
|
}
|