| 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 cel |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
"k8s.io/component-base/metrics/legacyregistry" |
| 26 |
"k8s.io/component-base/metrics/testutil" |
| 27 |
) |
| 28 |
|
| 29 |
func TestRecordAuthorizationMatchConditionEvaluationFailure(t *testing.T) { |
| 30 |
testCases := []struct { |
| 31 |
desc string |
| 32 |
metrics []string |
| 33 |
name string |
| 34 |
authztype string |
| 35 |
want string |
| 36 |
}{ |
| 37 |
{ |
| 38 |
desc: "evaluation failure total", |
| 39 |
metrics: []string{ |
| 40 |
"apiserver_authorization_match_condition_evaluation_errors_total", |
| 41 |
"apiserver_authorization_match_condition_exclusions_total", |
| 42 |
"apiserver_authorization_match_condition_evaluation_seconds", |
| 43 |
}, |
| 44 |
name: "wh1.example.com", |
| 45 |
authztype: "Webhook", |
| 46 |
want: ` |
| 47 |
# HELP apiserver_authorization_match_condition_evaluation_errors_total [ALPHA] Total number of errors when an authorization webhook encounters a match condition error split by authorizer type and name. |
| 48 |
# TYPE apiserver_authorization_match_condition_evaluation_errors_total counter |
| 49 |
apiserver_authorization_match_condition_evaluation_errors_total{name="wh1.example.com",type="Webhook"} 1 |
| 50 |
# HELP apiserver_authorization_match_condition_evaluation_seconds [ALPHA] Authorization match condition evaluation time in seconds, split by authorizer type and name. |
| 51 |
# TYPE apiserver_authorization_match_condition_evaluation_seconds histogram |
| 52 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.001"} 0 |
| 53 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.005"} 0 |
| 54 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.01"} 0 |
| 55 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.025"} 0 |
| 56 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.1"} 0 |
| 57 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.2"} 0 |
| 58 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="0.25"} 0 |
| 59 |
apiserver_authorization_match_condition_evaluation_seconds_bucket{name="wh1.example.com",type="Webhook",le="+Inf"} 1 |
| 60 |
apiserver_authorization_match_condition_evaluation_seconds_sum{name="wh1.example.com",type="Webhook"} 1 |
| 61 |
apiserver_authorization_match_condition_evaluation_seconds_count{name="wh1.example.com",type="Webhook"} 1 |
| 62 |
# HELP apiserver_authorization_match_condition_exclusions_total [ALPHA] Total number of exclusions when an authorization webhook is skipped because match conditions exclude it. |
| 63 |
# TYPE apiserver_authorization_match_condition_exclusions_total counter |
| 64 |
apiserver_authorization_match_condition_exclusions_total{name="wh1.example.com",type="Webhook"} 1 |
| 65 |
`, |
| 66 |
}, |
| 67 |
} |
| 68 |
|
| 69 |
for _, tt := range testCases { |
| 70 |
t.Run(tt.desc, func(t *testing.T) { |
| 71 |
ResetMetricsForTest() |
| 72 |
m := NewMatcherMetrics() |
| 73 |
m.RecordAuthorizationMatchConditionEvaluationFailure(context.Background(), tt.authztype, tt.name) |
| 74 |
m.RecordAuthorizationMatchConditionExclusion(context.Background(), tt.authztype, tt.name) |
| 75 |
m.RecordAuthorizationMatchConditionEvaluation(context.Background(), tt.authztype, tt.name, time.Duration(1*time.Second)) |
| 76 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tt.want), tt.metrics...); err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
}) |
| 80 |
} |
| 81 |
} |