@@ -64,6 +64,22 @@ func (m *MockPowerMonitor) TriggerUpdate() {
6464 }
6565}
6666
67+ func assertMetricLabelValues (t * testing.T , registry * prometheus.Registry , metricName string , expectedLabels map [string ]string ) {
68+ metrics , err := registry .Gather ()
69+ assert .NoError (t , err )
70+
71+ for _ , metric := range metrics {
72+ if metric .GetName () == metricName {
73+ for _ , m := range metric .GetMetric () {
74+ for labelName , expectedValue := range expectedLabels {
75+ actualValue := valueOfLabel (m , labelName )
76+ assert .Equal (t , expectedValue , actualValue , "%s should have %s label with value %s" , metricName , labelName , expectedValue )
77+ }
78+ }
79+ }
80+ }
81+ }
82+
6783func TestPowerCollector (t * testing.T ) {
6884 // Create a logger that writes to stderr for testing
6985 logger := slog .New (slog .NewTextHandler (os .Stderr , & slog.HandlerOptions {Level : slog .LevelError }))
@@ -327,83 +343,27 @@ func TestPowerCollector(t *testing.T) {
327343 })
328344
329345 t .Run ("Process Metrics Node Name Label" , func (t * testing.T ) {
330- metrics , err := registry .Gather ()
331- assert .NoError (t , err )
332-
333- for _ , metric := range metrics {
334- if metric .GetName () == "kepler_process_cpu_joules_total" {
335- for _ , m := range metric .GetMetric () {
336- nodeName := valueOfLabel (m , "node_name" )
337- assert .Equal (t , "test-node" , nodeName , "Process metrics should have node_name label" )
338- }
339- }
340- if metric .GetName () == "kepler_process_cpu_watts" {
341- for _ , m := range metric .GetMetric () {
342- nodeName := valueOfLabel (m , "node_name" )
343- assert .Equal (t , "test-node" , nodeName , "Process metrics should have node_name label" )
344- }
345- }
346- }
346+ expectedLabels := map [string ]string {"node_name" : "test-node" }
347+ assertMetricLabelValues (t , registry , "kepler_process_cpu_joules_total" , expectedLabels )
348+ assertMetricLabelValues (t , registry , "kepler_process_cpu_watts" , expectedLabels )
347349 })
348350
349351 t .Run ("Container Metrics Node Name Label" , func (t * testing.T ) {
350- metrics , err := registry .Gather ()
351- assert .NoError (t , err )
352-
353- for _ , metric := range metrics {
354- if metric .GetName () == "kepler_container_cpu_joules_total" {
355- for _ , m := range metric .GetMetric () {
356- nodeName := valueOfLabel (m , "node_name" )
357- assert .Equal (t , "test-node" , nodeName , "Container metrics should have node_name label" )
358- }
359- }
360- if metric .GetName () == "kepler_container_cpu_watts" {
361- for _ , m := range metric .GetMetric () {
362- nodeName := valueOfLabel (m , "node_name" )
363- assert .Equal (t , "test-node" , nodeName , "Container metrics should have node_name label" )
364- }
365- }
366- }
352+ expectedLabels := map [string ]string {"node_name" : "test-node" }
353+ assertMetricLabelValues (t , registry , "kepler_container_cpu_joules_total" , expectedLabels )
354+ assertMetricLabelValues (t , registry , "kepler_container_cpu_watts" , expectedLabels )
367355 })
368356
369357 t .Run ("VM Metrics Node Name Label" , func (t * testing.T ) {
370- metrics , err := registry .Gather ()
371- assert .NoError (t , err )
372-
373- for _ , metric := range metrics {
374- if metric .GetName () == "kepler_vm_cpu_joules_total" {
375- for _ , m := range metric .GetMetric () {
376- nodeName := valueOfLabel (m , "node_name" )
377- assert .Equal (t , "test-node" , nodeName , "VM metrics should have node_name label" )
378- }
379- }
380- if metric .GetName () == "kepler_vm_cpu_watts" {
381- for _ , m := range metric .GetMetric () {
382- nodeName := valueOfLabel (m , "node_name" )
383- assert .Equal (t , "test-node" , nodeName , "VM metrics should have node_name label" )
384- }
385- }
386- }
358+ expectedLabels := map [string ]string {"node_name" : "test-node" }
359+ assertMetricLabelValues (t , registry , "kepler_vm_cpu_joules_total" , expectedLabels )
360+ assertMetricLabelValues (t , registry , "kepler_vm_cpu_watts" , expectedLabels )
387361 })
388362
389363 t .Run ("Pod Metrics Node Name Label" , func (t * testing.T ) {
390- metrics , err := registry .Gather ()
391- assert .NoError (t , err )
392-
393- for _ , metric := range metrics {
394- if metric .GetName () == "kepler_pod_cpu_joules_total" {
395- for _ , m := range metric .GetMetric () {
396- nodeName := valueOfLabel (m , "node_name" )
397- assert .Equal (t , "test-node" , nodeName , "Pod metrics should have node_name label" )
398- }
399- }
400- if metric .GetName () == "kepler_pod_cpu_watts" {
401- for _ , m := range metric .GetMetric () {
402- nodeName := valueOfLabel (m , "node_name" )
403- assert .Equal (t , "test-node" , nodeName , "Pod metrics should have node_name label" )
404- }
405- }
406- }
364+ expectedLabels := map [string ]string {"node_name" : "test-node" }
365+ assertMetricLabelValues (t , registry , "kepler_pod_cpu_joules_total" , expectedLabels )
366+ assertMetricLabelValues (t , registry , "kepler_pod_cpu_watts" , expectedLabels )
407367 })
408368
409369 // Verify mock expectations
0 commit comments