Skip to content

Commit

Permalink
Unit tests: cadvisor swap stats
Browse files Browse the repository at this point in the history
Signed-off-by: Itamar Holder <iholder@redhat.com>
  • Loading branch information
iholder101 committed Jul 17, 2023
1 parent c74ee80 commit 053d7ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/kubelet/stats/cadvisor_stats_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,13 @@ func TestCadvisorListPodStats(t *testing.T) {
assert.EqualValues(t, testTime(creationTime, seedPod0Container0).Unix(), con.StartTime.Time.Unix())
checkCPUStats(t, "Pod0Container0", seedPod0Container0, con.CPU)
checkMemoryStats(t, "Pod0Conainer0", seedPod0Container0, infos["/pod0-c0"], con.Memory)
checkSwapStats(t, "Pod0Conainer0", seedPod0Container0, infos["/pod0-c0"], con.Swap)

con = indexCon[cName01]
assert.EqualValues(t, testTime(creationTime, seedPod0Container1).Unix(), con.StartTime.Time.Unix())
checkCPUStats(t, "Pod0Container1", seedPod0Container1, con.CPU)
checkMemoryStats(t, "Pod0Container1", seedPod0Container1, infos["/pod0-c1"], con.Memory)
checkSwapStats(t, "Pod0Container1", seedPod0Container1, infos["/pod0-c1"], con.Swap)

assert.EqualValues(t, p0Time.Unix(), ps.StartTime.Time.Unix())
checkNetworkStats(t, "Pod0", seedPod0Infra, ps.Network)
Expand All @@ -309,6 +311,9 @@ func TestCadvisorListPodStats(t *testing.T) {
if ps.Memory != nil {
checkMemoryStats(t, "Pod0", seedPod0Infra, infos["/pod0-i"], ps.Memory)
}
if ps.Swap != nil {
checkSwapStats(t, "Pod0", seedPod0Infra, infos["/pod0-i"], ps.Swap)
}

// Validate Pod1 Results
ps, found = indexPods[prf1]
Expand All @@ -318,6 +323,7 @@ func TestCadvisorListPodStats(t *testing.T) {
assert.Equal(t, cName10, con.Name)
checkCPUStats(t, "Pod1Container0", seedPod1Container, con.CPU)
checkMemoryStats(t, "Pod1Container0", seedPod1Container, infos["/pod1-c0"], con.Memory)
checkSwapStats(t, "Pod1Container0", seedPod1Container, infos["/pod1-c0"], con.Swap)
checkNetworkStats(t, "Pod1", seedPod1Infra, ps.Network)

// Validate Pod2 Results
Expand All @@ -328,6 +334,7 @@ func TestCadvisorListPodStats(t *testing.T) {
assert.Equal(t, cName20, con.Name)
checkCPUStats(t, "Pod2Container0", seedPod2Container, con.CPU)
checkMemoryStats(t, "Pod2Container0", seedPod2Container, infos["/pod2-c0"], con.Memory)
checkSwapStats(t, "Pod2Container0", seedPod2Container, infos["/pod2-c0"], con.Swap)
checkNetworkStats(t, "Pod2", seedPod2Infra, ps.Network)

// Validate Pod3 Results
Expand All @@ -344,6 +351,7 @@ func TestCadvisorListPodStats(t *testing.T) {
assert.Equal(t, cName31, con.Name)
checkCPUStats(t, "Pod3Container1", seedPod3Container1, con.CPU)
checkMemoryStats(t, "Pod3Container1", seedPod3Container1, infos["/pod3-c1"], con.Memory)
checkSwapStats(t, "Pod3Container1", seedPod3Container1, infos["/pod3-c1"], con.Swap)
}

func TestCadvisorListPodCPUAndMemoryStats(t *testing.T) {
Expand Down
20 changes: 19 additions & 1 deletion pkg/kubelet/stats/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const (
offsetFsBaseUsageBytes
offsetFsInodeUsage
offsetAcceleratorDutyCycle
offsetMemSwapUsageBytes
)

var (
Expand Down Expand Up @@ -101,6 +102,7 @@ func TestGetCgroupStats(t *testing.T) {
checkCPUStats(t, "", containerInfoSeed, cs.CPU)
checkMemoryStats(t, "", containerInfoSeed, containerInfo, cs.Memory)
checkNetworkStats(t, "", containerInfoSeed, ns)
checkSwapStats(t, "", containerInfoSeed, containerInfo, cs.Swap)

assert.Equal(cgroupName, cs.Name)
assert.Equal(metav1.NewTime(containerInfo.Spec.CreationTime), cs.StartTime)
Expand Down Expand Up @@ -497,7 +499,8 @@ func getTestContainerInfo(seed int, podName string, podNamespace string, contain
HasNetwork: true,
Labels: labels,
Memory: cadvisorapiv2.MemorySpec{
Limit: unlimitedMemory,
Limit: unlimitedMemory,
SwapLimit: unlimitedMemory,
},
CustomMetrics: generateCustomMetricSpec(),
}
Expand All @@ -518,6 +521,7 @@ func getTestContainerInfo(seed int, podName string, podNamespace string, contain
Pgfault: uint64(seed + offsetMemPageFaults),
Pgmajfault: uint64(seed + offsetMemMajorPageFaults),
},
Swap: uint64(seed + offsetMemSwapUsageBytes),
},
Network: &cadvisorapiv2.NetworkStats{
Interfaces: []cadvisorapiv1.InterfaceStats{{
Expand Down Expand Up @@ -696,6 +700,20 @@ func checkMemoryStats(t *testing.T, label string, seed int, info cadvisorapiv2.C
}
}

func checkSwapStats(t *testing.T, label string, seed int, info cadvisorapiv2.ContainerInfo, stats *statsapi.SwapStats) {
label += ".Swap"

assert.EqualValues(t, testTime(timestamp, seed).Unix(), stats.Time.Time.Unix(), label+".Time")
assert.EqualValues(t, seed+offsetMemSwapUsageBytes, *stats.SwapUsageBytes, label+".SwapUsageBytes")

if !info.Spec.HasMemory || isMemoryUnlimited(info.Spec.Memory.SwapLimit) {
assert.Nil(t, stats.SwapAvailableBytes, label+".SwapAvailableBytes")
} else {
expected := info.Spec.Memory.Limit - *stats.SwapUsageBytes
assert.EqualValues(t, expected, *stats.SwapAvailableBytes, label+".AvailableBytes")
}
}

func checkFsStats(t *testing.T, label string, seed int, stats *statsapi.FsStats) {
assert.EqualValues(t, seed+offsetFsCapacity, *stats.CapacityBytes, label+".CapacityBytes")
assert.EqualValues(t, seed+offsetFsAvailable, *stats.AvailableBytes, label+".AvailableBytes")
Expand Down

0 comments on commit 053d7ac

Please sign in to comment.