mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 10:43:56 +00:00
cri_provider, unit tests: ensure container-level metrics are collected
Signed-off-by: Itamar Holder <iholder@redhat.com>
This commit is contained in:
parent
748b52a130
commit
e6c19f315f
@ -279,6 +279,8 @@ func TestCRIListPodStats(t *testing.T) {
|
|||||||
checkCRIPodCPUAndMemoryStats(assert, p0, infos[sandbox0Cgroup].Stats[0])
|
checkCRIPodCPUAndMemoryStats(assert, p0, infos[sandbox0Cgroup].Stats[0])
|
||||||
checkCRIPodSwapStats(assert, p0, infos[sandbox0Cgroup].Stats[0])
|
checkCRIPodSwapStats(assert, p0, infos[sandbox0Cgroup].Stats[0])
|
||||||
|
|
||||||
|
checkContainersSwapStats(t, p0, infos[container0.Id], infos[container1.Id])
|
||||||
|
|
||||||
p1 := podStatsMap[statsapi.PodReference{Name: "sandbox1-name", UID: "sandbox1-uid", Namespace: "sandbox1-ns"}]
|
p1 := podStatsMap[statsapi.PodReference{Name: "sandbox1-name", UID: "sandbox1-uid", Namespace: "sandbox1-ns"}]
|
||||||
assert.Equal(sandbox1.CreatedAt, p1.StartTime.UnixNano())
|
assert.Equal(sandbox1.CreatedAt, p1.StartTime.UnixNano())
|
||||||
assert.Len(p1.Containers, 1)
|
assert.Len(p1.Containers, 1)
|
||||||
@ -296,6 +298,8 @@ func TestCRIListPodStats(t *testing.T) {
|
|||||||
checkCRIPodCPUAndMemoryStats(assert, p1, infos[sandbox1Cgroup].Stats[0])
|
checkCRIPodCPUAndMemoryStats(assert, p1, infos[sandbox1Cgroup].Stats[0])
|
||||||
checkCRIPodSwapStats(assert, p1, infos[sandbox1Cgroup].Stats[0])
|
checkCRIPodSwapStats(assert, p1, infos[sandbox1Cgroup].Stats[0])
|
||||||
|
|
||||||
|
checkContainersSwapStats(t, p1, infos[container2.Id])
|
||||||
|
|
||||||
p2 := podStatsMap[statsapi.PodReference{Name: "sandbox2-name", UID: "sandbox2-uid", Namespace: "sandbox2-ns"}]
|
p2 := podStatsMap[statsapi.PodReference{Name: "sandbox2-name", UID: "sandbox2-uid", Namespace: "sandbox2-ns"}]
|
||||||
assert.Equal(sandbox2.CreatedAt, p2.StartTime.UnixNano())
|
assert.Equal(sandbox2.CreatedAt, p2.StartTime.UnixNano())
|
||||||
assert.Len(p2.Containers, 1)
|
assert.Len(p2.Containers, 1)
|
||||||
@ -315,6 +319,8 @@ func TestCRIListPodStats(t *testing.T) {
|
|||||||
checkCRIPodCPUAndMemoryStats(assert, p2, infos[sandbox2Cgroup].Stats[0])
|
checkCRIPodCPUAndMemoryStats(assert, p2, infos[sandbox2Cgroup].Stats[0])
|
||||||
checkCRIPodSwapStats(assert, p2, infos[sandbox2Cgroup].Stats[0])
|
checkCRIPodSwapStats(assert, p2, infos[sandbox2Cgroup].Stats[0])
|
||||||
|
|
||||||
|
checkContainersSwapStats(t, p2, infos[container4.Id])
|
||||||
|
|
||||||
p3 := podStatsMap[statsapi.PodReference{Name: "sandbox3-name", UID: "sandbox3-uid", Namespace: "sandbox3-ns"}]
|
p3 := podStatsMap[statsapi.PodReference{Name: "sandbox3-name", UID: "sandbox3-uid", Namespace: "sandbox3-ns"}]
|
||||||
assert.Equal(sandbox3.CreatedAt, p3.StartTime.UnixNano())
|
assert.Equal(sandbox3.CreatedAt, p3.StartTime.UnixNano())
|
||||||
assert.Len(p3.Containers, 1)
|
assert.Len(p3.Containers, 1)
|
||||||
|
@ -19,6 +19,8 @@ package stats
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -505,6 +507,39 @@ func checkFsStats(t *testing.T, label string, seed int, stats *statsapi.FsStats)
|
|||||||
assert.EqualValues(t, seed+offsetFsInodesFree, *stats.InodesFree, label+".InodesFree")
|
assert.EqualValues(t, seed+offsetFsInodesFree, *stats.InodesFree, label+".InodesFree")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkContainersSwapStats(t *testing.T, podStats statsapi.PodStats, containerStats ...cadvisorapiv2.ContainerInfo) {
|
||||||
|
if runtime.GOOS != "linux" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
podContainers := make(map[string]struct{}, len(podStats.Containers))
|
||||||
|
for _, container := range podStats.Containers {
|
||||||
|
podContainers[container.Name] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, container := range containerStats {
|
||||||
|
found := false
|
||||||
|
containerName := container.Spec.Labels["io.kubernetes.container.name"]
|
||||||
|
for _, containerPodStats := range podStats.Containers {
|
||||||
|
if containerPodStats.Name == containerName {
|
||||||
|
assert.Equal(t, container.Stats[0].Memory.Swap, *containerPodStats.Swap.SwapUsageBytes)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found, "container %s not found in pod stats", container.Spec.Labels["io.kubernetes.container.name"])
|
||||||
|
delete(podContainers, containerName)
|
||||||
|
}
|
||||||
|
|
||||||
|
var missingContainerNames []string
|
||||||
|
for containerName := range podContainers {
|
||||||
|
missingContainerNames = append(missingContainerNames, containerName)
|
||||||
|
}
|
||||||
|
assert.Emptyf(t, podContainers, "containers not found in pod stats: %v", strings.Join(missingContainerNames, " "))
|
||||||
|
if len(missingContainerNames) > 0 {
|
||||||
|
assert.FailNow(t, "containers not found in pod stats")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func checkEphemeralStats(t *testing.T, label string, containerSeeds []int, volumeSeeds []int, containerLogStats []*volume.Metrics, stats *statsapi.FsStats) {
|
func checkEphemeralStats(t *testing.T, label string, containerSeeds []int, volumeSeeds []int, containerLogStats []*volume.Metrics, stats *statsapi.FsStats) {
|
||||||
var usedBytes, inodeUsage int
|
var usedBytes, inodeUsage int
|
||||||
for _, cseed := range containerSeeds {
|
for _, cseed := range containerSeeds {
|
||||||
|
Loading…
Reference in New Issue
Block a user