k8s.io/apiserver@v0.31.1/pkg/server/options/encryptionconfig/metrics/metrics_test.go (about) 1 /* 2 Copyright 2023 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 metrics 18 19 import ( 20 "strings" 21 "testing" 22 23 "k8s.io/component-base/metrics" 24 "k8s.io/component-base/metrics/testutil" 25 ) 26 27 const ( 28 testAPIServerID = "testAPIServerID" 29 testAPIServerIDHash = "sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37" 30 ) 31 32 func testMetricsRegistry(t *testing.T) metrics.KubeRegistry { 33 // setting the version to 1.30.0 to test deprecation 34 // of deprecatedEncryptionConfigAutomaticReloadFailureTotal and deprecatedEncryptionConfigAutomaticReloadSuccessTotal 35 registry := testutil.NewFakeKubeRegistry("1.30.0") 36 registry.MustRegister(encryptionConfigAutomaticReloadsTotal) 37 registry.MustRegister(deprecatedEncryptionConfigAutomaticReloadFailureTotal) 38 registry.MustRegister(deprecatedEncryptionConfigAutomaticReloadSuccessTotal) 39 registry.MustRegister(encryptionConfigAutomaticReloadLastTimestampSeconds) 40 41 t.Cleanup(func() { registry.Reset() }) 42 43 return registry 44 } 45 46 func TestRecordEncryptionConfigAutomaticReloadFailure(t *testing.T) { 47 registry := testMetricsRegistry(t) 48 49 expectedValue := ` 50 # HELP apiserver_encryption_config_controller_automatic_reload_failures_total [ALPHA] (Deprecated since 1.30.0) Total number of failed automatic reloads of encryption configuration split by apiserver identity. 51 # TYPE apiserver_encryption_config_controller_automatic_reload_failures_total counter 52 apiserver_encryption_config_controller_automatic_reload_failures_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37"} 1 53 # HELP apiserver_encryption_config_controller_automatic_reloads_total [ALPHA] Total number of reload successes and failures of encryption configuration split by apiserver identity. 54 # TYPE apiserver_encryption_config_controller_automatic_reloads_total counter 55 apiserver_encryption_config_controller_automatic_reloads_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="failure"} 1 56 ` 57 metricNames := []string{ 58 namespace + "_" + subsystem + "_automatic_reload_failures_total", 59 namespace + "_" + subsystem + "_automatic_reloads_total", 60 } 61 62 deprecatedEncryptionConfigAutomaticReloadFailureTotal.Reset() 63 encryptionConfigAutomaticReloadsTotal.Reset() 64 RegisterMetrics() 65 66 RecordEncryptionConfigAutomaticReloadFailure(testAPIServerID) 67 if err := testutil.GatherAndCompare(registry, strings.NewReader(expectedValue), metricNames...); err != nil { 68 t.Fatal(err) 69 } 70 } 71 72 func TestRecordEncryptionConfigAutomaticReloadSuccess(t *testing.T) { 73 registry := testMetricsRegistry(t) 74 75 expectedValue := ` 76 # HELP apiserver_encryption_config_controller_automatic_reload_success_total [ALPHA] (Deprecated since 1.30.0) Total number of successful automatic reloads of encryption configuration split by apiserver identity. 77 # TYPE apiserver_encryption_config_controller_automatic_reload_success_total counter 78 apiserver_encryption_config_controller_automatic_reload_success_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37"} 1 79 # HELP apiserver_encryption_config_controller_automatic_reloads_total [ALPHA] Total number of reload successes and failures of encryption configuration split by apiserver identity. 80 # TYPE apiserver_encryption_config_controller_automatic_reloads_total counter 81 apiserver_encryption_config_controller_automatic_reloads_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="success"} 1 82 ` 83 metricNames := []string{ 84 namespace + "_" + subsystem + "_automatic_reload_success_total", 85 namespace + "_" + subsystem + "_automatic_reloads_total", 86 } 87 88 deprecatedEncryptionConfigAutomaticReloadSuccessTotal.Reset() 89 encryptionConfigAutomaticReloadsTotal.Reset() 90 RegisterMetrics() 91 92 RecordEncryptionConfigAutomaticReloadSuccess(testAPIServerID) 93 if err := testutil.GatherAndCompare(registry, strings.NewReader(expectedValue), metricNames...); err != nil { 94 t.Fatal(err) 95 } 96 } 97 98 func TestEncryptionConfigAutomaticReloadLastTimestampSeconds(t *testing.T) { 99 testCases := []struct { 100 expectedValue string 101 resultLabel string 102 timestamp int64 103 }{ 104 { 105 expectedValue: ` 106 # HELP apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds [ALPHA] Timestamp of the last successful or failed automatic reload of encryption configuration split by apiserver identity. 107 # TYPE apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds gauge 108 apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds{apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="failure"} 1.689101941e+09 109 `, 110 resultLabel: "failure", 111 timestamp: 1689101941, 112 }, 113 { 114 expectedValue: ` 115 # HELP apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds [ALPHA] Timestamp of the last successful or failed automatic reload of encryption configuration split by apiserver identity. 116 # TYPE apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds gauge 117 apiserver_encryption_config_controller_automatic_reload_last_timestamp_seconds{apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="success"} 1.689101941e+09 118 `, 119 resultLabel: "success", 120 timestamp: 1689101941, 121 }, 122 } 123 124 metricNames := []string{ 125 namespace + "_" + subsystem + "_automatic_reload_last_timestamp_seconds", 126 } 127 RegisterMetrics() 128 129 for _, tc := range testCases { 130 registry := testMetricsRegistry(t) 131 132 encryptionConfigAutomaticReloadLastTimestampSeconds.Reset() 133 encryptionConfigAutomaticReloadLastTimestampSeconds.WithLabelValues(tc.resultLabel, testAPIServerIDHash).Set(float64(tc.timestamp)) 134 135 if err := testutil.GatherAndCompare(registry, strings.NewReader(tc.expectedValue), metricNames...); err != nil { 136 t.Fatal(err) 137 } 138 } 139 }