From 22ba326f5041b8f129cfe1e59256ac7ace39a93d Mon Sep 17 00:00:00 2001 From: yongruilin Date: Tue, 15 Oct 2024 11:26:35 -0700 Subject: [PATCH] refactor: update test setup order for testing metric label allow list This update covers the case that metrics are initialized before allow-metric-label flag is applied. --- staging/src/k8s.io/component-base/metrics/counter_test.go | 5 +++-- staging/src/k8s.io/component-base/metrics/gauge_test.go | 5 +++-- staging/src/k8s.io/component-base/metrics/histogram_test.go | 3 ++- staging/src/k8s.io/component-base/metrics/summary_test.go | 3 ++- .../k8s.io/component-base/metrics/timing_histogram_test.go | 4 +++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/staging/src/k8s.io/component-base/metrics/counter_test.go b/staging/src/k8s.io/component-base/metrics/counter_test.go index d7196f48c9f..5e89e45d00d 100644 --- a/staging/src/k8s.io/component-base/metrics/counter_test.go +++ b/staging/src/k8s.io/component-base/metrics/counter_test.go @@ -245,7 +245,8 @@ func TestCounterWithLabelValueAllowList(t *testing.T) { for _, test := range tests { t.Run(test.desc, func(t *testing.T) { - SetLabelAllowListFromCLI(labelAllowValues) + labelValueAllowLists = map[string]*MetricLabelAllowList{} + registry := newKubeRegistry(apimachineryversion.Info{ Major: "1", Minor: "15", @@ -253,7 +254,7 @@ func TestCounterWithLabelValueAllowList(t *testing.T) { }) c := NewCounterVec(opts, labels) registry.MustRegister(c) - + SetLabelAllowListFromCLI(labelAllowValues) for _, lv := range test.labelValues { c.WithLabelValues(lv...).Inc() } diff --git a/staging/src/k8s.io/component-base/metrics/gauge_test.go b/staging/src/k8s.io/component-base/metrics/gauge_test.go index c6c09dfed1a..d41baebb8f1 100644 --- a/staging/src/k8s.io/component-base/metrics/gauge_test.go +++ b/staging/src/k8s.io/component-base/metrics/gauge_test.go @@ -305,7 +305,8 @@ func TestGaugeWithLabelValueAllowList(t *testing.T) { for _, test := range tests { t.Run(test.desc, func(t *testing.T) { - SetLabelAllowListFromCLI(labelAllowValues) + labelValueAllowLists = map[string]*MetricLabelAllowList{} + registry := newKubeRegistry(apimachineryversion.Info{ Major: "1", Minor: "15", @@ -313,7 +314,7 @@ func TestGaugeWithLabelValueAllowList(t *testing.T) { }) g := NewGaugeVec(opts, labels) registry.MustRegister(g) - + SetLabelAllowListFromCLI(labelAllowValues) for _, lv := range test.labelValues { g.WithLabelValues(lv...).Set(100.0) } diff --git a/staging/src/k8s.io/component-base/metrics/histogram_test.go b/staging/src/k8s.io/component-base/metrics/histogram_test.go index 51c5601f312..d5fc24f7c7c 100644 --- a/staging/src/k8s.io/component-base/metrics/histogram_test.go +++ b/staging/src/k8s.io/component-base/metrics/histogram_test.go @@ -271,7 +271,7 @@ func TestHistogramWithLabelValueAllowList(t *testing.T) { for _, test := range tests { t.Run(test.desc, func(t *testing.T) { - SetLabelAllowListFromCLI(labelAllowValues) + labelValueAllowLists = map[string]*MetricLabelAllowList{} registry := newKubeRegistry(apimachineryversion.Info{ Major: "1", Minor: "15", @@ -279,6 +279,7 @@ func TestHistogramWithLabelValueAllowList(t *testing.T) { }) c := NewHistogramVec(opts, labels) registry.MustRegister(c) + SetLabelAllowListFromCLI(labelAllowValues) for _, lv := range test.labelValues { c.WithLabelValues(lv...).Observe(1.0) diff --git a/staging/src/k8s.io/component-base/metrics/summary_test.go b/staging/src/k8s.io/component-base/metrics/summary_test.go index ca9b50b7981..02fa895e7a2 100644 --- a/staging/src/k8s.io/component-base/metrics/summary_test.go +++ b/staging/src/k8s.io/component-base/metrics/summary_test.go @@ -235,7 +235,7 @@ func TestSummaryWithLabelValueAllowList(t *testing.T) { for _, test := range tests { t.Run(test.desc, func(t *testing.T) { - SetLabelAllowListFromCLI(labelAllowValues) + labelValueAllowLists = map[string]*MetricLabelAllowList{} registry := newKubeRegistry(apimachineryversion.Info{ Major: "1", Minor: "15", @@ -243,6 +243,7 @@ func TestSummaryWithLabelValueAllowList(t *testing.T) { }) c := NewSummaryVec(opts, labels) registry.MustRegister(c) + SetLabelAllowListFromCLI(labelAllowValues) for _, lv := range test.labelValues { c.WithLabelValues(lv...).Observe(1.0) diff --git a/staging/src/k8s.io/component-base/metrics/timing_histogram_test.go b/staging/src/k8s.io/component-base/metrics/timing_histogram_test.go index de10a87bd79..80fcb6340c0 100644 --- a/staging/src/k8s.io/component-base/metrics/timing_histogram_test.go +++ b/staging/src/k8s.io/component-base/metrics/timing_histogram_test.go @@ -313,7 +313,8 @@ func TestTimingHistogramWithLabelValueAllowList(t *testing.T) { for _, test := range tests { t.Run(test.desc, func(t *testing.T) { - SetLabelAllowListFromCLI(labelAllowValues) + labelValueAllowLists = map[string]*MetricLabelAllowList{} + registry := newKubeRegistry(apimachineryversion.Info{ Major: "1", Minor: "15", @@ -323,6 +324,7 @@ func TestTimingHistogramWithLabelValueAllowList(t *testing.T) { clk := testclock.NewFakePassiveClock(t0) c := NewTestableTimingHistogramVec(clk.Now, opts, labels) registry.MustRegister(c) + SetLabelAllowListFromCLI(labelAllowValues) var v0 float64 = 13 for _, lv := range test.labelValues { c.WithLabelValues(lv...).Set(v0)