mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +00:00
test
This commit is contained in:
parent
714dced0d1
commit
71d7477c21
@ -81,6 +81,37 @@ func TestObserveWebhook(t *testing.T) {
|
|||||||
expectHistogramCountTotal(t, "apiserver_admission_webhook_admission_duration_seconds", wantLabels, 1)
|
expectHistogramCountTotal(t, "apiserver_admission_webhook_admission_duration_seconds", wantLabels, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestObserveWebhookRejection(t *testing.T) {
|
||||||
|
Metrics.reset()
|
||||||
|
Metrics.ObserveWebhookRejection("x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 500)
|
||||||
|
Metrics.ObserveWebhookRejection("x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 654)
|
||||||
|
Metrics.ObserveWebhookRejection("x", stepValidate, string(admission.Update), WebhookRejectionCallingWebhookError, 0)
|
||||||
|
wantLabels := map[string]string{
|
||||||
|
"name": "x",
|
||||||
|
"operation": string(admission.Create),
|
||||||
|
"type": "admit",
|
||||||
|
"error_type": "no_error",
|
||||||
|
"rejection_code": "500",
|
||||||
|
}
|
||||||
|
wantLabels600 := map[string]string{
|
||||||
|
"name": "x",
|
||||||
|
"operation": string(admission.Create),
|
||||||
|
"type": "admit",
|
||||||
|
"error_type": "no_error",
|
||||||
|
"rejection_code": "600",
|
||||||
|
}
|
||||||
|
wantLabelsCallingWebhookError := map[string]string{
|
||||||
|
"name": "x",
|
||||||
|
"operation": string(admission.Update),
|
||||||
|
"type": "validate",
|
||||||
|
"error_type": "calling_webhook_error",
|
||||||
|
"rejection_code": "0",
|
||||||
|
}
|
||||||
|
expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabels, 1)
|
||||||
|
expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabels600, 1)
|
||||||
|
expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabelsCallingWebhookError, 1)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWithMetrics(t *testing.T) {
|
func TestWithMetrics(t *testing.T) {
|
||||||
Metrics.reset()
|
Metrics.reset()
|
||||||
|
|
||||||
|
@ -89,3 +89,35 @@ func expectHistogramCountTotal(t *testing.T, name string, labelFilter map[string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// expectCounterValue ensures that the counts of metrics matching the labelFilter is as
|
||||||
|
// expected.
|
||||||
|
func expectCounterValue(t *testing.T, name string, labelFilter map[string]string, wantCount int) {
|
||||||
|
metrics, err := prometheus.DefaultGatherer.Gather()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to gather metrics: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
counterSum := 0
|
||||||
|
for _, mf := range metrics {
|
||||||
|
if mf.GetName() != name {
|
||||||
|
continue // Ignore other metrics.
|
||||||
|
}
|
||||||
|
for _, metric := range mf.GetMetric() {
|
||||||
|
if !labelsMatch(metric, labelFilter) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
counterSum += int(metric.GetCounter().GetValue())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if wantCount != counterSum {
|
||||||
|
t.Errorf("Wanted count %d, got %d for metric %s with labels %#+v", wantCount, counterSum, name, labelFilter)
|
||||||
|
for _, mf := range metrics {
|
||||||
|
if mf.GetName() == name {
|
||||||
|
for _, metric := range mf.GetMetric() {
|
||||||
|
t.Logf("\tnear match: %s", metric.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user