@@ -21,6 +21,7 @@ import (
2121 "os"
2222 "path/filepath"
2323 "strings"
24+ "time"
2425
2526 . "github.com/onsi/ginkgo/v2"
2627 . "github.com/onsi/gomega"
@@ -166,16 +167,26 @@ var _ = framework.KubeDescribe("PodSandbox", func() {
166167
167168 By ("create container in pod" )
168169 ic := f .CRIClient .CRIImageClient
169- containerID := framework .CreatePauseContainer (rc , ic , podID , podConfig , "container-for-metrics-" )
170+ containerID := framework .CreateDefaultContainer (rc , ic , podID , podConfig , "container-for-metrics-" )
170171
171172 By ("start container" )
172173 startContainer (rc , containerID )
173174
175+ _ , _ , err := rc .ExecSync (
176+ context .TODO (), containerID , []string {"/bin/sh" , "-c" , "for i in $(seq 1 10); do echo hi >> /var/lib/mydisktest/inode_test_file_$i; done; sync" },
177+ time .Duration (defaultExecSyncTimeout )* time .Second ,
178+ )
179+
180+ Expect (err ).ToNot (HaveOccurred ())
181+
182+ By ("list metric descriptors" )
183+ descs := listMetricDescriptors (rc )
184+
174185 By ("list pod sandbox metrics" )
175186 metrics := listPodSandboxMetrics (rc )
176187
177188 By ("verify pod metrics are present" )
178- testPodSandboxMetrics (metrics , podID )
189+ testPodSandboxMetrics (metrics , descs , podID )
179190 })
180191 })
181192})
@@ -309,6 +320,7 @@ func createPodSandboxWithLogDirectory(c internalapi.RuntimeService) (sandboxID s
309320// testMetricDescriptors verifies that all expected metric descriptors are present.
310321func testMetricDescriptors (descs []* runtimeapi.MetricDescriptor ) {
311322 returnedDescriptors := make (map [string ]* runtimeapi.MetricDescriptor )
323+
312324 for _ , desc := range descs {
313325 returnedDescriptors [desc .GetName ()] = desc
314326 Expect (desc .GetHelp ()).NotTo (BeEmpty (), "Metric descriptor %q should have help text" , desc .GetName ())
@@ -339,7 +351,7 @@ func listPodSandboxMetrics(c internalapi.RuntimeService) []*runtimeapi.PodSandbo
339351}
340352
341353// testPodSandboxMetrics verifies that metrics are present for the specified pod.
342- func testPodSandboxMetrics (allMetrics []* runtimeapi.PodSandboxMetrics , podID string ) {
354+ func testPodSandboxMetrics (allMetrics []* runtimeapi.PodSandboxMetrics , descs [] * runtimeapi. MetricDescriptor , podID string ) {
343355 var podMetrics * runtimeapi.PodSandboxMetrics
344356
345357 for _ , m := range allMetrics {
@@ -352,29 +364,38 @@ func testPodSandboxMetrics(allMetrics []*runtimeapi.PodSandboxMetrics, podID str
352364
353365 Expect (podMetrics ).NotTo (BeNil (), "Metrics for pod %q should be present" , podID )
354366
355- metricNamesFound := make (map [string ]bool )
367+ metricNamesFound := make (map [string ][] string )
356368
357369 for _ , metric := range podMetrics .GetMetrics () {
358- if ! metricNamesFound [metric .GetName ()] {
359- metricNamesFound [metric .GetName ()] = true
370+ if len ( metricNamesFound [metric .GetName ()]) == 0 {
371+ metricNamesFound [metric .GetName ()] = metric . GetLabelValues ()
360372 }
361373 }
362374
363375 for _ , containerMetric := range podMetrics .GetContainerMetrics () {
364376 for _ , metric := range containerMetric .GetMetrics () {
365- if ! metricNamesFound [metric .GetName ()] {
366- metricNamesFound [metric .GetName ()] = true
377+ if len ( metricNamesFound [metric .GetName ()]) == 0 {
378+ metricNamesFound [metric .GetName ()] = metric . GetLabelValues ()
367379 }
368380 }
369381 }
370382
371383 missingMetrics := []string {}
372384
373385 for _ , expectedName := range expectedMetricDescriptorNames {
374- if ! metricNamesFound [expectedName ] {
386+ if len ( metricNamesFound [expectedName ]) == 0 {
375387 missingMetrics = append (missingMetrics , expectedName )
376388 }
377389 }
378390
379391 Expect (missingMetrics ).To (BeEmpty (), "Expected %s metrics to be present and they were not" , strings .Join (missingMetrics , " " ))
392+
393+ mismatchedLabels := []string {}
394+ for _ , desc := range descs {
395+ if len (metricNamesFound [desc .GetName ()]) != len (desc .GetLabelKeys ()) {
396+ mismatchedLabels = append (mismatchedLabels , desc .GetName ())
397+ }
398+ }
399+
400+ Expect (mismatchedLabels ).To (BeEmpty (), "Expected %s metrics to have same set of labels in ListMetricDescriptors and ListPodSandboxMetrics" , strings .Join (mismatchedLabels , "," ))
380401}
0 commit comments