mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
test: Address globals pollution in metrics tests
Make all globals reset states during and after tests that manipulate them. Signed-off-by: Pranshu Srivastava <rexagod@gmail.com>
This commit is contained in:
@@ -28,6 +28,8 @@ import (
|
||||
)
|
||||
|
||||
func TestEnableHiddenMetrics(t *testing.T) {
|
||||
resetMetricsGlobalState(t)
|
||||
|
||||
currentVersion := apimachineryversion.Info{
|
||||
Major: "1",
|
||||
Minor: "17",
|
||||
@@ -198,6 +200,8 @@ func TestEnableHiddenStableCollector(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestShowHiddenMetric(t *testing.T) {
|
||||
resetMetricsGlobalState(t)
|
||||
|
||||
registry := newKubeRegistry(apimachineryversion.Info{
|
||||
Major: "1",
|
||||
Minor: "15",
|
||||
@@ -212,7 +216,6 @@ func TestShowHiddenMetric(t *testing.T) {
|
||||
assert.Lenf(t, ms, expectedMetricCount, "Got %v metrics, Want: %v metrics", len(ms), expectedMetricCount)
|
||||
|
||||
showHidden.Store(true)
|
||||
defer showHidden.Store(false)
|
||||
registry.MustRegister(NewCounter(
|
||||
&CounterOpts{
|
||||
Namespace: "some_namespace",
|
||||
@@ -231,6 +234,8 @@ func TestShowHiddenMetric(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDisabledMetrics(t *testing.T) {
|
||||
resetMetricsGlobalState(t)
|
||||
|
||||
o := NewOptions()
|
||||
o.DisabledMetrics = []string{"should_be_disabled", "should_be_disabled"} // should be deduplicated (disabled_metrics_total == 1)
|
||||
currentVersion := apimachineryversion.Info{
|
||||
|
||||
@@ -18,6 +18,7 @@ package metrics
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/blang/semver/v4"
|
||||
@@ -27,6 +28,33 @@ import (
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
// resetMetricsGlobalState resets all global state used by metrics tests.
|
||||
// NOTE: This should be called at the start of tests and in cleanup to ensure test isolation.
|
||||
func resetMetricsGlobalState(t *testing.T) {
|
||||
resetter := func() {
|
||||
hiddenMetricsTotal.Reset()
|
||||
disabledMetricsTotal.Reset()
|
||||
|
||||
showHidden.Store(false)
|
||||
showHiddenOnce = sync.Once{}
|
||||
|
||||
alphaCounter.ClearState()
|
||||
alphaDeprecatedCounter.ClearState()
|
||||
betaDeprecatedCounter.ClearState()
|
||||
alphaHiddenCounter.ClearState()
|
||||
|
||||
disabledMetricsLock.Lock()
|
||||
disabledMetrics = map[string]struct{}{}
|
||||
disabledMetricsLock.Unlock()
|
||||
}
|
||||
|
||||
// Reset at the start of tests
|
||||
resetter()
|
||||
|
||||
// Register cleanup to reset again after test completes
|
||||
t.Cleanup(resetter)
|
||||
}
|
||||
|
||||
var (
|
||||
v115 = semver.MustParse("1.15.0")
|
||||
alphaCounter = NewCounter(
|
||||
@@ -265,6 +293,8 @@ func TestShouldHide(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
resetMetricsGlobalState(t)
|
||||
|
||||
var tests = []struct {
|
||||
desc string
|
||||
metrics []*Counter
|
||||
@@ -334,6 +364,8 @@ func TestRegister(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMustRegister(t *testing.T) {
|
||||
resetMetricsGlobalState(t)
|
||||
|
||||
var tests = []struct {
|
||||
desc string
|
||||
metrics []*Counter
|
||||
|
||||
Reference in New Issue
Block a user