k8s.io/apiserver@v0.31.1/pkg/server/options/authorizationconfig/metrics/metrics_test.go (about) 1 /* 2 Copyright 2024 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/legacyregistry" 24 "k8s.io/component-base/metrics/testutil" 25 ) 26 27 const ( 28 testAPIServerID = "testAPIServerID" 29 testAPIServerIDHash = "sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37" 30 ) 31 32 func TestRecordAuthorizationConfigAutomaticReloadFailure(t *testing.T) { 33 expectedValue := ` 34 # HELP apiserver_authorization_config_controller_automatic_reloads_total [ALPHA] Total number of automatic reloads of authorization configuration split by status and apiserver identity. 35 # TYPE apiserver_authorization_config_controller_automatic_reloads_total counter 36 apiserver_authorization_config_controller_automatic_reloads_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="failure"} 1 37 ` 38 metrics := []string{ 39 namespace + "_" + subsystem + "_automatic_reloads_total", 40 } 41 42 authorizationConfigAutomaticReloadsTotal.Reset() 43 RegisterMetrics() 44 45 RecordAuthorizationConfigAutomaticReloadFailure(testAPIServerID) 46 if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expectedValue), metrics...); err != nil { 47 t.Fatal(err) 48 } 49 } 50 51 func TestRecordAuthorizationConfigAutomaticReloadSuccess(t *testing.T) { 52 expectedValue := ` 53 # HELP apiserver_authorization_config_controller_automatic_reloads_total [ALPHA] Total number of automatic reloads of authorization configuration split by status and apiserver identity. 54 # TYPE apiserver_authorization_config_controller_automatic_reloads_total counter 55 apiserver_authorization_config_controller_automatic_reloads_total {apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="success"} 1 56 ` 57 metrics := []string{ 58 namespace + "_" + subsystem + "_automatic_reloads_total", 59 } 60 61 authorizationConfigAutomaticReloadsTotal.Reset() 62 RegisterMetrics() 63 64 RecordAuthorizationConfigAutomaticReloadSuccess(testAPIServerID) 65 if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(expectedValue), metrics...); err != nil { 66 t.Fatal(err) 67 } 68 } 69 70 func TestAuthorizationConfigAutomaticReloadLastTimestampSeconds(t *testing.T) { 71 testCases := []struct { 72 expectedValue string 73 resultLabel string 74 timestamp int64 75 }{ 76 { 77 expectedValue: ` 78 # HELP apiserver_authorization_config_controller_automatic_reload_last_timestamp_seconds [ALPHA] Timestamp of the last automatic reload of authorization configuration split by status and apiserver identity. 79 # TYPE apiserver_authorization_config_controller_automatic_reload_last_timestamp_seconds gauge 80 apiserver_authorization_config_controller_automatic_reload_last_timestamp_seconds{apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="failure"} 1.689101941e+09 81 `, 82 resultLabel: "failure", 83 timestamp: 1689101941, 84 }, 85 { 86 expectedValue: ` 87 # HELP apiserver_authorization_config_controller_automatic_reload_last_timestamp_seconds [ALPHA] Timestamp of the last automatic reload of authorization configuration split by status and apiserver identity. 88 # TYPE apiserver_authorization_config_controller_automatic_reload_last_timestamp_seconds gauge 89 apiserver_authorization_config_controller_automatic_reload_last_timestamp_seconds{apiserver_id_hash="sha256:14f9d63e669337ac6bfda2e2162915ee6a6067743eddd4e5c374b572f951ff37",status="success"} 1.689101941e+09 90 `, 91 resultLabel: "success", 92 timestamp: 1689101941, 93 }, 94 } 95 96 metrics := []string{ 97 namespace + "_" + subsystem + "_automatic_reload_last_timestamp_seconds", 98 } 99 RegisterMetrics() 100 101 for _, tc := range testCases { 102 authorizationConfigAutomaticReloadLastTimestampSeconds.Reset() 103 authorizationConfigAutomaticReloadLastTimestampSeconds.WithLabelValues(tc.resultLabel, testAPIServerIDHash).Set(float64(tc.timestamp)) 104 105 if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.expectedValue), metrics...); err != nil { 106 t.Fatal(err) 107 } 108 } 109 }