Skip to content

Commit

Permalink
common: add GetSpec tests
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Mar 29, 2021
1 parent 5bffc55 commit 70b339d
Show file tree
Hide file tree
Showing 22 changed files with 146 additions and 0 deletions.
125 changes: 125 additions & 0 deletions container/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100010
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1025
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0-5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123456789
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13579
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24680
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1027
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test1/cpu.max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20000 100010
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test1/cpu.weight
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0-5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24680
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test1/memory.max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123456789
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
13579
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test1/pids.max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1027
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test2/cpu.max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max 100010
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test2/cpu.weight
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test2/memory.max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max
1 change: 1 addition & 0 deletions container/common/test_resources/cgroup_v2/test2/pids.max
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max

0 comments on commit 70b339d

Please sign in to comment.