From b3c6d87f49dbbb13c64714d6cce09d5f33bf028a Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Mon, 3 Jun 2024 12:29:54 +0200 Subject: [PATCH] Add stats label filter test Adding a conformance test for filtering stats by container labels. Signed-off-by: Sascha Grunert --- pkg/framework/util.go | 8 +++++++- pkg/validate/container.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/framework/util.go b/pkg/framework/util.go index ba813eb866..3f94a76630 100644 --- a/pkg/framework/util.go +++ b/pkg/framework/util.go @@ -253,14 +253,20 @@ func BuildContainerMetadata(containerName string, attempt uint32) *runtimeapi.Co } } -// CreateDefaultContainer creates a default container with default options. +// CreateDefaultContainer creates a default container with default options. func CreateDefaultContainer(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string) string { + return CreateDefaultContainerWithLabels(rc, ic, podID, podConfig, prefix, nil) +} + +// CreateDefaultContainerWithLabels creates a default container with default options +func CreateDefaultContainerWithLabels(rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string, labels map[string]string) string { containerName := prefix + NewUUID() containerConfig := &runtimeapi.ContainerConfig{ Metadata: BuildContainerMetadata(containerName, DefaultAttempt), Image: &runtimeapi.ImageSpec{Image: TestContext.TestImageList.DefaultTestContainerImage}, Command: DefaultContainerCommand, Linux: &runtimeapi.LinuxContainerConfig{}, + Labels: labels, } return CreateContainer(rc, ic, containerConfig, podID, podConfig) diff --git a/pkg/validate/container.go b/pkg/validate/container.go index 9c33a8a1ed..afc0f06d81 100644 --- a/pkg/validate/container.go +++ b/pkg/validate/container.go @@ -239,6 +239,22 @@ var _ = framework.KubeDescribe("Container", func() { Expect(statFound(stats, secondContainerID)).To(BeTrue(), "Stats should be found") Expect(statFound(stats, thirdContainerID)).To(BeTrue(), "Stats should be found") }) + + It("runtime should support listing stats for containers filtered by labels [Conformance]", func() { + By("create container") + labels := map[string]string{"foo": "bar"} + containerID := framework.CreateDefaultContainerWithLabels(rc, ic, podID, podConfig, "container-for-stats-with-labels-", labels) + + By("start container") + startContainer(rc, containerID) + + By("test container stats") + stats := listContainerStats(rc, &runtimeapi.ContainerStatsFilter{LabelSelector: labels}) + Expect(statFound(stats, containerID)).To(BeTrue(), "Container should be found") + + stats = listContainerStats(rc, &runtimeapi.ContainerStatsFilter{LabelSelector: map[string]string{"foo": "baz"}}) + Expect(statFound(stats, containerID)).To(BeFalse(), "Container should be filtered") + }) }) Context("runtime should support adding volume and device", func() {