| 1 |
/* |
| 2 |
Copyright 2025 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package validation |
| 18 |
|
| 19 |
import ( |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/component-base/metrics/legacyregistry" |
| 24 |
"k8s.io/component-base/metrics/testutil" |
| 25 |
) |
| 26 |
|
| 27 |
// TestDeclarativeValidationMismatchMetric tests that the mismatch metric correctly increments once |
| 28 |
func TestDeclarativeValidationMismatchMetric(t *testing.T) { |
| 29 |
defer legacyregistry.Reset() |
| 30 |
defer ResetValidationMetricsInstance() |
| 31 |
|
| 32 |
// Increment the metric once |
| 33 |
Metrics.IncDeclarativeValidationMismatchMetric() |
| 34 |
|
| 35 |
expected := ` |
| 36 |
# HELP apiserver_validation_declarative_validation_mismatch_total [BETA] Number of times declarative validation results differed from handwritten validation results for core types. |
| 37 |
# TYPE apiserver_validation_declarative_validation_mismatch_total counter |
| 38 |
apiserver_validation_declarative_validation_mismatch_total 1 |
| 39 |
` |
| 40 |
|
| 41 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), "declarative_validation_mismatch_total"); err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// TestDeclarativeValidationPanicMetric tests that the panic metric correctly increments once |
| 47 |
func TestDeclarativeValidationPanicMetric(t *testing.T) { |
| 48 |
defer legacyregistry.Reset() |
| 49 |
defer ResetValidationMetricsInstance() |
| 50 |
|
| 51 |
// Increment the metric once |
| 52 |
Metrics.IncDeclarativeValidationPanicMetric() |
| 53 |
|
| 54 |
expected := ` |
| 55 |
# HELP apiserver_validation_declarative_validation_panic_total [BETA] Number of times declarative validation has panicked during validation. |
| 56 |
# TYPE apiserver_validation_declarative_validation_panic_total counter |
| 57 |
apiserver_validation_declarative_validation_panic_total 1 |
| 58 |
` |
| 59 |
|
| 60 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), "declarative_validation_panic_total"); err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// TestDeclarativeValidationMismatchMetricMultiple tests that the mismatch metric correctly increments multiple times |
| 66 |
func TestDeclarativeValidationMismatchMetricMultiple(t *testing.T) { |
| 67 |
defer legacyregistry.Reset() |
| 68 |
defer ResetValidationMetricsInstance() |
| 69 |
|
| 70 |
// Increment the metric three times |
| 71 |
Metrics.IncDeclarativeValidationMismatchMetric() |
| 72 |
Metrics.IncDeclarativeValidationMismatchMetric() |
| 73 |
Metrics.IncDeclarativeValidationMismatchMetric() |
| 74 |
|
| 75 |
expected := ` |
| 76 |
# HELP apiserver_validation_declarative_validation_mismatch_total [BETA] Number of times declarative validation results differed from handwritten validation results for core types. |
| 77 |
# TYPE apiserver_validation_declarative_validation_mismatch_total counter |
| 78 |
apiserver_validation_declarative_validation_mismatch_total 3 |
| 79 |
` |
| 80 |
|
| 81 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), "declarative_validation_mismatch_total"); err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// TestDeclarativeValidationPanicMetricMultiple tests that the panic metric correctly increments multiple times |
| 87 |
func TestDeclarativeValidationPanicMetricMultiple(t *testing.T) { |
| 88 |
defer legacyregistry.Reset() |
| 89 |
defer ResetValidationMetricsInstance() |
| 90 |
|
| 91 |
// Increment the metric three times |
| 92 |
Metrics.IncDeclarativeValidationPanicMetric() |
| 93 |
Metrics.IncDeclarativeValidationPanicMetric() |
| 94 |
Metrics.IncDeclarativeValidationPanicMetric() |
| 95 |
|
| 96 |
expected := ` |
| 97 |
# HELP apiserver_validation_declarative_validation_panic_total [BETA] Number of times declarative validation has panicked during validation. |
| 98 |
# TYPE apiserver_validation_declarative_validation_panic_total counter |
| 99 |
apiserver_validation_declarative_validation_panic_total 3 |
| 100 |
` |
| 101 |
|
| 102 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), "declarative_validation_panic_total"); err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// TestDeclarativeValidationMetricsReset tests that the Reset function correctly resets the metrics to zero |
| 108 |
func TestDeclarativeValidationMetricsReset(t *testing.T) { |
| 109 |
defer legacyregistry.Reset() |
| 110 |
defer ResetValidationMetricsInstance() |
| 111 |
|
| 112 |
// Increment both metrics |
| 113 |
Metrics.IncDeclarativeValidationMismatchMetric() |
| 114 |
Metrics.IncDeclarativeValidationPanicMetric() |
| 115 |
|
| 116 |
// Reset the metrics |
| 117 |
Metrics.Reset() |
| 118 |
|
| 119 |
// Verify they've been reset to zero |
| 120 |
expected := ` |
| 121 |
# HELP apiserver_validation_declarative_validation_mismatch_total [BETA] Number of times declarative validation results differed from handwritten validation results for core types. |
| 122 |
# TYPE apiserver_validation_declarative_validation_mismatch_total counter |
| 123 |
apiserver_validation_declarative_validation_mismatch_total 0 |
| 124 |
# HELP apiserver_validation_declarative_validation_panic_total [BETA] Number of times declarative validation has panicked during validation. |
| 125 |
# TYPE apiserver_validation_declarative_validation_panic_total counter |
| 126 |
apiserver_validation_declarative_validation_panic_total 0 |
| 127 |
` |
| 128 |
|
| 129 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), "declarative_validation_mismatch_total", "declarative_validation_panic_total"); err != nil { |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
|
| 133 |
// Increment the metrics again to ensure they're still functional |
| 134 |
Metrics.IncDeclarativeValidationMismatchMetric() |
| 135 |
Metrics.IncDeclarativeValidationPanicMetric() |
| 136 |
|
| 137 |
// Verify they've been incremented correctly |
| 138 |
expected = ` |
| 139 |
# HELP apiserver_validation_declarative_validation_mismatch_total [BETA] Number of times declarative validation results differed from handwritten validation results for core types. |
| 140 |
# TYPE apiserver_validation_declarative_validation_mismatch_total counter |
| 141 |
apiserver_validation_declarative_validation_mismatch_total 1 |
| 142 |
# HELP apiserver_validation_declarative_validation_panic_total [BETA] Number of times declarative validation has panicked during validation. |
| 143 |
# TYPE apiserver_validation_declarative_validation_panic_total counter |
| 144 |
apiserver_validation_declarative_validation_panic_total 1 |
| 145 |
` |
| 146 |
|
| 147 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expected), "declarative_validation_mismatch_total", "declarative_validation_panic_total"); err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
} |