Skip to content

Commit e6c19f3

Browse files
committed
cri_provider, unit tests: ensure container-level metrics are collected
Signed-off-by: Itamar Holder <[email protected]>
1 parent 748b52a commit e6c19f3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

pkg/kubelet/stats/cri_stats_provider_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ func TestCRIListPodStats(t *testing.T) {
279279
checkCRIPodCPUAndMemoryStats(assert, p0, infos[sandbox0Cgroup].Stats[0])
280280
checkCRIPodSwapStats(assert, p0, infos[sandbox0Cgroup].Stats[0])
281281

282+
checkContainersSwapStats(t, p0, infos[container0.Id], infos[container1.Id])
283+
282284
p1 := podStatsMap[statsapi.PodReference{Name: "sandbox1-name", UID: "sandbox1-uid", Namespace: "sandbox1-ns"}]
283285
assert.Equal(sandbox1.CreatedAt, p1.StartTime.UnixNano())
284286
assert.Len(p1.Containers, 1)
@@ -296,6 +298,8 @@ func TestCRIListPodStats(t *testing.T) {
296298
checkCRIPodCPUAndMemoryStats(assert, p1, infos[sandbox1Cgroup].Stats[0])
297299
checkCRIPodSwapStats(assert, p1, infos[sandbox1Cgroup].Stats[0])
298300

301+
checkContainersSwapStats(t, p1, infos[container2.Id])
302+
299303
p2 := podStatsMap[statsapi.PodReference{Name: "sandbox2-name", UID: "sandbox2-uid", Namespace: "sandbox2-ns"}]
300304
assert.Equal(sandbox2.CreatedAt, p2.StartTime.UnixNano())
301305
assert.Len(p2.Containers, 1)
@@ -315,6 +319,8 @@ func TestCRIListPodStats(t *testing.T) {
315319
checkCRIPodCPUAndMemoryStats(assert, p2, infos[sandbox2Cgroup].Stats[0])
316320
checkCRIPodSwapStats(assert, p2, infos[sandbox2Cgroup].Stats[0])
317321

322+
checkContainersSwapStats(t, p2, infos[container4.Id])
323+
318324
p3 := podStatsMap[statsapi.PodReference{Name: "sandbox3-name", UID: "sandbox3-uid", Namespace: "sandbox3-ns"}]
319325
assert.Equal(sandbox3.CreatedAt, p3.StartTime.UnixNano())
320326
assert.Len(p3.Containers, 1)

pkg/kubelet/stats/provider_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package stats
1919
import (
2020
"context"
2121
"fmt"
22+
"runtime"
23+
"strings"
2224
"testing"
2325
"time"
2426

@@ -505,6 +507,39 @@ func checkFsStats(t *testing.T, label string, seed int, stats *statsapi.FsStats)
505507
assert.EqualValues(t, seed+offsetFsInodesFree, *stats.InodesFree, label+".InodesFree")
506508
}
507509

510+
func checkContainersSwapStats(t *testing.T, podStats statsapi.PodStats, containerStats ...cadvisorapiv2.ContainerInfo) {
511+
if runtime.GOOS != "linux" {
512+
return
513+
}
514+
515+
podContainers := make(map[string]struct{}, len(podStats.Containers))
516+
for _, container := range podStats.Containers {
517+
podContainers[container.Name] = struct{}{}
518+
}
519+
520+
for _, container := range containerStats {
521+
found := false
522+
containerName := container.Spec.Labels["io.kubernetes.container.name"]
523+
for _, containerPodStats := range podStats.Containers {
524+
if containerPodStats.Name == containerName {
525+
assert.Equal(t, container.Stats[0].Memory.Swap, *containerPodStats.Swap.SwapUsageBytes)
526+
found = true
527+
}
528+
}
529+
assert.True(t, found, "container %s not found in pod stats", container.Spec.Labels["io.kubernetes.container.name"])
530+
delete(podContainers, containerName)
531+
}
532+
533+
var missingContainerNames []string
534+
for containerName := range podContainers {
535+
missingContainerNames = append(missingContainerNames, containerName)
536+
}
537+
assert.Emptyf(t, podContainers, "containers not found in pod stats: %v", strings.Join(missingContainerNames, " "))
538+
if len(missingContainerNames) > 0 {
539+
assert.FailNow(t, "containers not found in pod stats")
540+
}
541+
}
542+
508543
func checkEphemeralStats(t *testing.T, label string, containerSeeds []int, volumeSeeds []int, containerLogStats []*volume.Metrics, stats *statsapi.FsStats) {
509544
var usedBytes, inodeUsage int
510545
for _, cseed := range containerSeeds {

0 commit comments

Comments
 (0)