From 619b69fc35c16fc7ed42f0788f43c95c4a01347c Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Mon, 29 Mar 2021 13:49:26 +0200 Subject: [PATCH] common: add GetSpec tests Signed-off-by: Giuseppe Scrivano (cherry picked from commit 70b339d81499150b1f08c780371191a79083f7d5) Signed-off-by: Kir Kolyshkin --- container/common/helpers_test.go | 125 ++++++++++++++++++ .../cgroup_v1/test1/cpu/cpu.cfs_period_us | 1 + .../cgroup_v1/test1/cpu/cpu.cfs_quota_us | 1 + .../cgroup_v1/test1/cpu/cpu.shares | 1 + .../cgroup_v1/test1/cpuset/cpuset.cpus | 1 + .../test1/memory/memory.limit_in_bytes | 1 + .../test1/memory/memory.memsw.limit_in_bytes | 1 + .../test1/memory/memory.soft_limit_in_bytes | 1 + .../cgroup_v1/test1/pids/pids.max | 1 + .../test_resources/cgroup_v2/test1/cpu.max | 1 + .../test_resources/cgroup_v2/test1/cpu.weight | 1 + .../cgroup_v2/test1/cpuset.cpus.effective | 1 + .../cgroup_v2/test1/memory.high | 1 + .../test_resources/cgroup_v2/test1/memory.max | 1 + .../cgroup_v2/test1/memory.swap.max | 1 + .../test_resources/cgroup_v2/test1/pids.max | 1 + .../test_resources/cgroup_v2/test2/cpu.max | 1 + .../test_resources/cgroup_v2/test2/cpu.weight | 1 + .../cgroup_v2/test2/memory.high | 1 + .../test_resources/cgroup_v2/test2/memory.max | 1 + .../cgroup_v2/test2/memory.swap.max | 1 + .../test_resources/cgroup_v2/test2/pids.max | 1 + 22 files changed, 146 insertions(+) create mode 100644 container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_period_us create mode 100644 container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_quota_us create mode 100644 container/common/test_resources/cgroup_v1/test1/cpu/cpu.shares create mode 100644 container/common/test_resources/cgroup_v1/test1/cpuset/cpuset.cpus create mode 100644 container/common/test_resources/cgroup_v1/test1/memory/memory.limit_in_bytes create mode 100644 container/common/test_resources/cgroup_v1/test1/memory/memory.memsw.limit_in_bytes create mode 100644 container/common/test_resources/cgroup_v1/test1/memory/memory.soft_limit_in_bytes create mode 100644 container/common/test_resources/cgroup_v1/test1/pids/pids.max create mode 100644 container/common/test_resources/cgroup_v2/test1/cpu.max create mode 100644 container/common/test_resources/cgroup_v2/test1/cpu.weight create mode 100644 container/common/test_resources/cgroup_v2/test1/cpuset.cpus.effective create mode 100644 container/common/test_resources/cgroup_v2/test1/memory.high create mode 100644 container/common/test_resources/cgroup_v2/test1/memory.max create mode 100644 container/common/test_resources/cgroup_v2/test1/memory.swap.max create mode 100644 container/common/test_resources/cgroup_v2/test1/pids.max create mode 100644 container/common/test_resources/cgroup_v2/test2/cpu.max create mode 100644 container/common/test_resources/cgroup_v2/test2/cpu.weight create mode 100644 container/common/test_resources/cgroup_v2/test2/memory.high create mode 100644 container/common/test_resources/cgroup_v2/test2/memory.max create mode 100644 container/common/test_resources/cgroup_v2/test2/memory.swap.max create mode 100644 container/common/test_resources/cgroup_v2/test2/pids.max diff --git a/container/common/helpers_test.go b/container/common/helpers_test.go index 0f416a0932..ad2005c33f 100644 --- a/container/common/helpers_test.go +++ b/container/common/helpers_test.go @@ -15,7 +15,15 @@ package common import ( + "errors" + "math" + "os" + "path/filepath" "testing" + + info "github.com/google/cadvisor/info/v1" + v2 "github.com/google/cadvisor/info/v2" + "github.com/stretchr/testify/assert" ) func BenchmarkListDirectories(b *testing.B) { @@ -72,3 +80,120 @@ func TestParseUint64String(t *testing.T) { t.Fatalf("parseUint64String(\"18446744073709551615\") != 18446744073709551615") } } + +type mockInfoProvider struct { + options v2.RequestOptions +} + +func (m *mockInfoProvider) GetRequestedContainersInfo(containerName string, options v2.RequestOptions) (map[string]*info.ContainerInfo, error) { + m.options = options + return map[string]*info.ContainerInfo{}, nil +} + +func (m *mockInfoProvider) GetVersionInfo() (*info.VersionInfo, error) { + return nil, errors.New("not supported") +} + +func (m *mockInfoProvider) GetMachineInfo() (*info.MachineInfo, error) { + return &info.MachineInfo{ + NumCores: 7, + }, nil +} + +func TestGetSpecCgroupV1(t *testing.T) { + root, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %s", err) + } + + cgroupPaths := map[string]string{ + "memory": filepath.Join(root, "test_resources/cgroup_v1/test1/memory"), + "cpu": filepath.Join(root, "test_resources/cgroup_v1/test1/cpu"), + "cpuset": filepath.Join(root, "test_resources/cgroup_v1/test1/cpuset"), + "pids": filepath.Join(root, "test_resources/cgroup_v1/test1/pids"), + } + + spec, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, false) + assert.Nil(t, err) + + assert.True(t, spec.HasMemory) + assert.EqualValues(t, spec.Memory.Limit, 123456789) + assert.EqualValues(t, spec.Memory.SwapLimit, 13579) + assert.EqualValues(t, spec.Memory.Reservation, 24680) + + assert.True(t, spec.HasCpu) + assert.EqualValues(t, spec.Cpu.Limit, 1025) + assert.EqualValues(t, spec.Cpu.Period, 100010) + assert.EqualValues(t, spec.Cpu.Quota, 20000) + + assert.EqualValues(t, spec.Cpu.Mask, "0-5") + + assert.True(t, spec.HasProcesses) + assert.EqualValues(t, spec.Processes.Limit, 1027) + + assert.False(t, spec.HasHugetlb) + assert.False(t, spec.HasDiskIo) +} + +func TestGetSpecCgroupV2(t *testing.T) { + root, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %s", err) + } + + cgroupPaths := map[string]string{ + "memory": filepath.Join(root, "test_resources/cgroup_v2/test1"), + "cpu": filepath.Join(root, "test_resources/cgroup_v2/test1"), + "cpuset": filepath.Join(root, "test_resources/cgroup_v2/test1"), + "pids": filepath.Join(root, "test_resources/cgroup_v2/test1"), + } + + spec, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, true) + assert.Nil(t, err) + + assert.True(t, spec.HasMemory) + assert.EqualValues(t, spec.Memory.Limit, 123456789) + assert.EqualValues(t, spec.Memory.SwapLimit, 13579) + assert.EqualValues(t, spec.Memory.Reservation, 24680) + + assert.True(t, spec.HasCpu) + assert.EqualValues(t, spec.Cpu.Limit, 1286) + assert.EqualValues(t, spec.Cpu.Period, 100010) + assert.EqualValues(t, spec.Cpu.Quota, 20000) + + assert.EqualValues(t, spec.Cpu.Mask, "0-5") + + assert.True(t, spec.HasProcesses) + assert.EqualValues(t, spec.Processes.Limit, 1027) + + assert.False(t, spec.HasHugetlb) + assert.False(t, spec.HasDiskIo) +} + +func TestGetSpecCgroupV2Max(t *testing.T) { + root, err := os.Getwd() + assert.Nil(t, err) + + cgroupPaths := map[string]string{ + "memory": filepath.Join(root, "test_resources/cgroup_v2/test2"), + "cpu": filepath.Join(root, "test_resources/cgroup_v2/test2"), + "pids": filepath.Join(root, "test_resources/cgroup_v2/test2"), + } + + spec, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, true) + assert.Nil(t, err) + + max := uint64(math.MaxUint64) + + assert.True(t, spec.HasMemory) + assert.EqualValues(t, spec.Memory.Limit, max) + assert.EqualValues(t, spec.Memory.SwapLimit, max) + assert.EqualValues(t, spec.Memory.Reservation, max) + + assert.True(t, spec.HasCpu) + assert.EqualValues(t, spec.Cpu.Limit, 1286) + assert.EqualValues(t, spec.Cpu.Period, 100010) + assert.EqualValues(t, spec.Cpu.Quota, 0) + + assert.EqualValues(t, spec.Processes.Limit, max) +} diff --git a/container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_period_us b/container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_period_us new file mode 100644 index 0000000000..2b6800fa56 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_period_us @@ -0,0 +1 @@ +100010 diff --git a/container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_quota_us b/container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_quota_us new file mode 100644 index 0000000000..b92677edb9 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/cpu/cpu.cfs_quota_us @@ -0,0 +1 @@ +20000 diff --git a/container/common/test_resources/cgroup_v1/test1/cpu/cpu.shares b/container/common/test_resources/cgroup_v1/test1/cpu/cpu.shares new file mode 100644 index 0000000000..bec4b5cd08 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/cpu/cpu.shares @@ -0,0 +1 @@ +1025 diff --git a/container/common/test_resources/cgroup_v1/test1/cpuset/cpuset.cpus b/container/common/test_resources/cgroup_v1/test1/cpuset/cpuset.cpus new file mode 100644 index 0000000000..82a99f2907 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/cpuset/cpuset.cpus @@ -0,0 +1 @@ +0-5 diff --git a/container/common/test_resources/cgroup_v1/test1/memory/memory.limit_in_bytes b/container/common/test_resources/cgroup_v1/test1/memory/memory.limit_in_bytes new file mode 100644 index 0000000000..28d14454c3 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/memory/memory.limit_in_bytes @@ -0,0 +1 @@ +123456789 diff --git a/container/common/test_resources/cgroup_v1/test1/memory/memory.memsw.limit_in_bytes b/container/common/test_resources/cgroup_v1/test1/memory/memory.memsw.limit_in_bytes new file mode 100644 index 0000000000..5dc07a9f6f --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/memory/memory.memsw.limit_in_bytes @@ -0,0 +1 @@ +13579 diff --git a/container/common/test_resources/cgroup_v1/test1/memory/memory.soft_limit_in_bytes b/container/common/test_resources/cgroup_v1/test1/memory/memory.soft_limit_in_bytes new file mode 100644 index 0000000000..9029be3ac7 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/memory/memory.soft_limit_in_bytes @@ -0,0 +1 @@ +24680 diff --git a/container/common/test_resources/cgroup_v1/test1/pids/pids.max b/container/common/test_resources/cgroup_v1/test1/pids/pids.max new file mode 100644 index 0000000000..327f63bda8 --- /dev/null +++ b/container/common/test_resources/cgroup_v1/test1/pids/pids.max @@ -0,0 +1 @@ +1027 diff --git a/container/common/test_resources/cgroup_v2/test1/cpu.max b/container/common/test_resources/cgroup_v2/test1/cpu.max new file mode 100644 index 0000000000..c32d506aae --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/cpu.max @@ -0,0 +1 @@ +20000 100010 diff --git a/container/common/test_resources/cgroup_v2/test1/cpu.weight b/container/common/test_resources/cgroup_v2/test1/cpu.weight new file mode 100644 index 0000000000..e373ee695f --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/cpu.weight @@ -0,0 +1 @@ +50 diff --git a/container/common/test_resources/cgroup_v2/test1/cpuset.cpus.effective b/container/common/test_resources/cgroup_v2/test1/cpuset.cpus.effective new file mode 100644 index 0000000000..82a99f2907 --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/cpuset.cpus.effective @@ -0,0 +1 @@ +0-5 diff --git a/container/common/test_resources/cgroup_v2/test1/memory.high b/container/common/test_resources/cgroup_v2/test1/memory.high new file mode 100644 index 0000000000..9029be3ac7 --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/memory.high @@ -0,0 +1 @@ +24680 diff --git a/container/common/test_resources/cgroup_v2/test1/memory.max b/container/common/test_resources/cgroup_v2/test1/memory.max new file mode 100644 index 0000000000..28d14454c3 --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/memory.max @@ -0,0 +1 @@ +123456789 diff --git a/container/common/test_resources/cgroup_v2/test1/memory.swap.max b/container/common/test_resources/cgroup_v2/test1/memory.swap.max new file mode 100644 index 0000000000..5dc07a9f6f --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/memory.swap.max @@ -0,0 +1 @@ +13579 diff --git a/container/common/test_resources/cgroup_v2/test1/pids.max b/container/common/test_resources/cgroup_v2/test1/pids.max new file mode 100644 index 0000000000..327f63bda8 --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test1/pids.max @@ -0,0 +1 @@ +1027 diff --git a/container/common/test_resources/cgroup_v2/test2/cpu.max b/container/common/test_resources/cgroup_v2/test2/cpu.max new file mode 100644 index 0000000000..82b3f0a017 --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test2/cpu.max @@ -0,0 +1 @@ +max 100010 diff --git a/container/common/test_resources/cgroup_v2/test2/cpu.weight b/container/common/test_resources/cgroup_v2/test2/cpu.weight new file mode 100644 index 0000000000..e373ee695f --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test2/cpu.weight @@ -0,0 +1 @@ +50 diff --git a/container/common/test_resources/cgroup_v2/test2/memory.high b/container/common/test_resources/cgroup_v2/test2/memory.high new file mode 100644 index 0000000000..355295a05a --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test2/memory.high @@ -0,0 +1 @@ +max diff --git a/container/common/test_resources/cgroup_v2/test2/memory.max b/container/common/test_resources/cgroup_v2/test2/memory.max new file mode 100644 index 0000000000..355295a05a --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test2/memory.max @@ -0,0 +1 @@ +max diff --git a/container/common/test_resources/cgroup_v2/test2/memory.swap.max b/container/common/test_resources/cgroup_v2/test2/memory.swap.max new file mode 100644 index 0000000000..355295a05a --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test2/memory.swap.max @@ -0,0 +1 @@ +max diff --git a/container/common/test_resources/cgroup_v2/test2/pids.max b/container/common/test_resources/cgroup_v2/test2/pids.max new file mode 100644 index 0000000000..355295a05a --- /dev/null +++ b/container/common/test_resources/cgroup_v2/test2/pids.max @@ -0,0 +1 @@ +max