Skip to content

Commit 22ba326

Browse files
committed
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.
1 parent 67f3128 commit 22ba326

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

staging/src/k8s.io/component-base/metrics/counter_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,16 @@ func TestCounterWithLabelValueAllowList(t *testing.T) {
245245

246246
for _, test := range tests {
247247
t.Run(test.desc, func(t *testing.T) {
248-
SetLabelAllowListFromCLI(labelAllowValues)
248+
labelValueAllowLists = map[string]*MetricLabelAllowList{}
249+
249250
registry := newKubeRegistry(apimachineryversion.Info{
250251
Major: "1",
251252
Minor: "15",
252253
GitVersion: "v1.15.0-alpha-1.12345",
253254
})
254255
c := NewCounterVec(opts, labels)
255256
registry.MustRegister(c)
256-
257+
SetLabelAllowListFromCLI(labelAllowValues)
257258
for _, lv := range test.labelValues {
258259
c.WithLabelValues(lv...).Inc()
259260
}

staging/src/k8s.io/component-base/metrics/gauge_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,16 @@ func TestGaugeWithLabelValueAllowList(t *testing.T) {
305305

306306
for _, test := range tests {
307307
t.Run(test.desc, func(t *testing.T) {
308-
SetLabelAllowListFromCLI(labelAllowValues)
308+
labelValueAllowLists = map[string]*MetricLabelAllowList{}
309+
309310
registry := newKubeRegistry(apimachineryversion.Info{
310311
Major: "1",
311312
Minor: "15",
312313
GitVersion: "v1.15.0-alpha-1.12345",
313314
})
314315
g := NewGaugeVec(opts, labels)
315316
registry.MustRegister(g)
316-
317+
SetLabelAllowListFromCLI(labelAllowValues)
317318
for _, lv := range test.labelValues {
318319
g.WithLabelValues(lv...).Set(100.0)
319320
}

staging/src/k8s.io/component-base/metrics/histogram_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,15 @@ func TestHistogramWithLabelValueAllowList(t *testing.T) {
271271

272272
for _, test := range tests {
273273
t.Run(test.desc, func(t *testing.T) {
274-
SetLabelAllowListFromCLI(labelAllowValues)
274+
labelValueAllowLists = map[string]*MetricLabelAllowList{}
275275
registry := newKubeRegistry(apimachineryversion.Info{
276276
Major: "1",
277277
Minor: "15",
278278
GitVersion: "v1.15.0-alpha-1.12345",
279279
})
280280
c := NewHistogramVec(opts, labels)
281281
registry.MustRegister(c)
282+
SetLabelAllowListFromCLI(labelAllowValues)
282283

283284
for _, lv := range test.labelValues {
284285
c.WithLabelValues(lv...).Observe(1.0)

staging/src/k8s.io/component-base/metrics/summary_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,15 @@ func TestSummaryWithLabelValueAllowList(t *testing.T) {
235235

236236
for _, test := range tests {
237237
t.Run(test.desc, func(t *testing.T) {
238-
SetLabelAllowListFromCLI(labelAllowValues)
238+
labelValueAllowLists = map[string]*MetricLabelAllowList{}
239239
registry := newKubeRegistry(apimachineryversion.Info{
240240
Major: "1",
241241
Minor: "15",
242242
GitVersion: "v1.15.0-alpha-1.12345",
243243
})
244244
c := NewSummaryVec(opts, labels)
245245
registry.MustRegister(c)
246+
SetLabelAllowListFromCLI(labelAllowValues)
246247

247248
for _, lv := range test.labelValues {
248249
c.WithLabelValues(lv...).Observe(1.0)

staging/src/k8s.io/component-base/metrics/timing_histogram_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ func TestTimingHistogramWithLabelValueAllowList(t *testing.T) {
313313

314314
for _, test := range tests {
315315
t.Run(test.desc, func(t *testing.T) {
316-
SetLabelAllowListFromCLI(labelAllowValues)
316+
labelValueAllowLists = map[string]*MetricLabelAllowList{}
317+
317318
registry := newKubeRegistry(apimachineryversion.Info{
318319
Major: "1",
319320
Minor: "15",
@@ -323,6 +324,7 @@ func TestTimingHistogramWithLabelValueAllowList(t *testing.T) {
323324
clk := testclock.NewFakePassiveClock(t0)
324325
c := NewTestableTimingHistogramVec(clk.Now, opts, labels)
325326
registry.MustRegister(c)
327+
SetLabelAllowListFromCLI(labelAllowValues)
326328
var v0 float64 = 13
327329
for _, lv := range test.labelValues {
328330
c.WithLabelValues(lv...).Set(v0)

0 commit comments

Comments
 (0)