Skip to content

Commit

Permalink
Fix test accordingly pr 5174
Browse files Browse the repository at this point in the history
Signed-off-by: rickbrouwer <[email protected]>
  • Loading branch information
rickbrouwer authored Nov 26, 2024
1 parent 7ec52cf commit 3a7c955
Showing 1 changed file with 40 additions and 104 deletions.
144 changes: 40 additions & 104 deletions tests/sequential/prometheus_metrics/prometheus_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ func TestPrometheusMetrics(t *testing.T) {
testScaledObjectErrors(t, data)
testScaledJobErrors(t, data)
testScalerErrors(t, data)
testScalerErrorsTotal(t, data)
testOperatorMetrics(t, kc, data)
testMetricServerMetrics(t)
testWebhookMetrics(t, data)
Expand Down Expand Up @@ -520,6 +519,8 @@ func testScalerMetricValue(t *testing.T) {
}
}
assert.Equal(t, true, found)
} else {
t.Errorf("metric keda_scaler_metrics_value not available")
}
}

Expand All @@ -534,29 +535,21 @@ func testScaledObjectErrors(t *testing.T, data templateData) {
time.Sleep(20 * time.Second)

family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaled_object_errors"]
assert.True(t, ok, "keda_scaled_object_errors not available")
valTotal, okTotal := family["keda_scaled_object_errors_total"]
assert.True(t, okTotal, "keda_scaled_object_errors_total not available")
if ok && okTotal {
val, ok := family["keda_scaled_object_errors_total"]
assert.True(t, ok, "keda_scaled_object_errors_total not available")
if ok {
errCounterVal1 := getErrorMetricsValue(val)
errCounterValTotal1 := getErrorMetricsValue(valTotal)

// wait for 2 seconds as pollinginterval is 2
time.Sleep(2 * time.Second)

family = fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaled_object_errors"]
assert.True(t, ok, "keda_scaled_object_errors not available")
valTotal, okTotal := family["keda_scaled_object_errors_total"]
assert.True(t, okTotal, "keda_scaled_object_errors_total not available")
if ok && okTotal {
val, ok := family["keda_scaled_object_errors_total"]
assert.True(t, ok, "keda_scaled_object_errors_total not available")
if ok {
errCounterVal2 := getErrorMetricsValue(val)
errCounterValTotal2 := getErrorMetricsValue(valTotal)
assert.NotEqual(t, errCounterVal2, float64(0))
assert.NotEqual(t, errCounterValTotal2, float64(0))
assert.GreaterOrEqual(t, errCounterVal2, errCounterVal1)
assert.GreaterOrEqual(t, errCounterValTotal2, errCounterValTotal1)
}
}

Expand All @@ -576,34 +569,26 @@ func testScaledJobErrors(t *testing.T, data templateData) {
time.Sleep(20 * time.Second)

family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaled_job_errors"]
assert.True(t, ok, "keda_scaled_job_errors not available")
valTotal, okTotal := family["keda_scaled_job_errors_total"]
assert.True(t, okTotal, "keda_scaled_job_errors_total not available")
if ok && okTotal {
val, ok := family["keda_scaled_job_errors_total"]
assert.True(t, ok, "keda_scaled_job_errors_total not available")
if ok {
errCounterVal1 := getErrorMetricsValue(val)
errCounterValTotal1 := getErrorMetricsValue(valTotal)

// wait for 2 seconds as pollinginterval is 2
time.Sleep(2 * time.Second)

family = fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaled_job_errors"]
assert.True(t, ok, "keda_scaled_job_errors not available")
valTotal, okTotal := family["keda_scaled_job_errors_total"]
assert.True(t, okTotal, "keda_scaled_job_errors_total not available")
if ok && okTotal {
val, ok := family["keda_scaled_job_errors_total"]
assert.True(t, ok, "keda_scaled_job_errors_total not available")
if ok {
errCounterVal2 := getErrorMetricsValue(val)
errCounterValTotal2 := getErrorMetricsValue(valTotal)
assert.NotEqual(t, errCounterVal2, float64(0))
assert.NotEqual(t, errCounterValTotal2, float64(0))
assert.GreaterOrEqual(t, errCounterVal2, errCounterVal1)
assert.GreaterOrEqual(t, errCounterValTotal2, errCounterValTotal1)
} else {
t.Errorf("metric not available")
t.Errorf("metric keda_scaled_job_errors_total not available")
}
} else {
t.Errorf("metric not available")
t.Errorf("metric keda_scaled_job_errors_total not available")
}

KubectlDeleteWithTemplate(t, data, "wrongScaledJobTemplate", wrongScaledJobTemplate)
Expand All @@ -622,39 +607,6 @@ func testScalerErrors(t *testing.T, data templateData) {
time.Sleep(2 * time.Second)
KubectlApplyWithTemplate(t, data, "wrongScaledJobTemplate", wrongScaledJobTemplate)

family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaler_errors"]
assert.True(t, ok, "keda_scaler_errors not available")
if ok {
errCounterVal1 := getErrorMetricsValue(val)

// wait for 20 seconds to correctly fetch metrics.
time.Sleep(20 * time.Second)

family = fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaler_errors"]
assert.True(t, ok, "keda_scaler_errors not available")
if ok {
errCounterVal2 := getErrorMetricsValue(val)
assert.NotEqual(t, errCounterVal2, float64(0))
assert.GreaterOrEqual(t, errCounterVal2, errCounterVal1)
}
}
KubectlDeleteWithTemplate(t, data, "wrongScaledJobTemplate", wrongScaledJobTemplate)
time.Sleep(2 * time.Second)
KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate)

KubectlDeleteWithTemplate(t, data, "wrongScaledObjectTemplate", wrongScaledObjectTemplate)
time.Sleep(2 * time.Second)
KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
}

func testScalerErrorsTotal(t *testing.T, data templateData) {
t.Log("--- testing scaler errors total ---")

KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
KubectlApplyWithTemplate(t, data, "wrongScaledObjectTemplate", wrongScaledObjectTemplate)

family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaler_errors_total"]
assert.True(t, ok, "keda_scaler_errors_total not available")
Expand All @@ -664,8 +616,8 @@ func testScalerErrorsTotal(t *testing.T, data templateData) {
errCounterVal1 := getErrorMetricsValue(val)
errCounterValDetail1 := getErrorMetricsValue(valDetail)

// wait for 2 seconds as pollinginterval is 2
time.Sleep(2 * time.Second)
// wait for 20 seconds to correctly fetch metrics.
time.Sleep(20 * time.Second)

family = fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
val, ok := family["keda_scaler_errors_total"]
Expand All @@ -682,35 +634,24 @@ func testScalerErrorsTotal(t *testing.T, data templateData) {
}
}

KubectlDeleteWithTemplate(t, data, "wrongScaledJobTemplate", wrongScaledJobTemplate)
time.Sleep(2 * time.Second)
KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate)

KubectlDeleteWithTemplate(t, data, "wrongScaledObjectTemplate", wrongScaledObjectTemplate)
time.Sleep(2 * time.Second)
KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate)
}

func getErrorMetricsValue(val *prommodel.MetricFamily) float64 {
switch val.GetName() {
case "keda_scaler_errors_total":
metrics := val.GetMetric()
for _, metric := range metrics {
return metric.GetCounter().GetValue()
}
case "keda_scaler_detail_errors_total":
metrics := val.GetMetric()
result := 0.
for _, metric := range metrics {
result += metric.GetCounter().GetValue()
}
return result
case "keda_scaled_object_errors":
metrics := val.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == "scaledObject" && *label.Value == wrongScaledObjectName {
return *metric.Counter.Value
}
}
}
case "keda_scaled_object_errors_total":
metrics := val.GetMetric()
for _, metric := range metrics {
Expand All @@ -721,16 +662,6 @@ func getErrorMetricsValue(val *prommodel.MetricFamily) float64 {
}
}
}
case "keda_scaled_job_errors":
metrics := val.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
for _, label := range labels {
if *label.Name == "scaledJob" && *label.Value == wrongScaledJobName {
return *metric.Counter.Value
}
}
}
case "keda_scaled_job_errors_total":
metrics := val.GetMetric()
for _, metric := range metrics {
Expand All @@ -741,7 +672,7 @@ func getErrorMetricsValue(val *prommodel.MetricFamily) float64 {
}
}
}
case "keda_scaler_errors":
case "keda_scaler_errors_total":
metrics := val.GetMetric()
for _, metric := range metrics {
labels := metric.GetLabel()
Expand Down Expand Up @@ -819,6 +750,8 @@ func testScalerMetricLatency(t *testing.T) {
}
}
assert.Equal(t, true, found)
} else {
t.Errorf("metric keda_scaler_metrics_latency_seconds not available")
}
}

Expand All @@ -827,7 +760,7 @@ func testScalableObjectMetrics(t *testing.T) {

family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))

if val, ok := family["keda_internal_scale_loop_latency"]; ok {
if val, ok := family["keda_internal_scale_loop_latency_seconds"]; ok {
var found bool
metrics := val.GetMetric()

Expand Down Expand Up @@ -885,7 +818,7 @@ func testScalableObjectMetrics(t *testing.T) {
}
assert.Equal(t, true, found)
} else {
t.Errorf("scaledobject metric not available")
t.Errorf("keda_internal_scale_loop_latency_seconds metric not available")
}
}

Expand All @@ -910,6 +843,8 @@ func testScalerActiveMetric(t *testing.T) {
}
}
assert.Equal(t, true, found)
} else {
t.Errorf("metric keda_scaler_active not available")
}
}

Expand Down Expand Up @@ -1041,6 +976,7 @@ func checkBuildInfo(t *testing.T, families map[string]*prommodel.MetricFamily) {
family, ok := families["keda_build_info"]
assert.True(t, ok, "keda_build_info not available")
if !ok {
t.Errorf("metric keda_build_info not available")
return
}

Expand Down Expand Up @@ -1075,8 +1011,8 @@ func getLatestCommit(t *testing.T) string {
func checkTriggerTotalValues(t *testing.T, families map[string]*prommodel.MetricFamily, expectedValues map[string]int) {
t.Log("--- testing trigger total metrics ---")
expected := map[string]int{}
family, ok := families["keda_trigger_totals"]
assert.True(t, ok, "keda_trigger_totals not available")
family, ok := families["keda_trigger_registered_total"]
assert.True(t, ok, "keda_trigger_registered_total not available")
if !ok {
return
}
Expand Down Expand Up @@ -1129,8 +1065,8 @@ func checkTriggerTotalValues(t *testing.T, families map[string]*prommodel.Metric
func checkCRTotalValues(t *testing.T, families map[string]*prommodel.MetricFamily, expected map[string]map[string]int) {
t.Log("--- testing resource total metrics ---")

family, ok := families["keda_resource_totals"]
assert.True(t, ok, "keda_resource_totals not available")
family, ok := families["keda_resource_registered_total"]
assert.True(t, ok, "keda_resource_registered_total not available")
if !ok {
return
}
Expand Down Expand Up @@ -1362,9 +1298,9 @@ func checkGRPCClientMetrics(t *testing.T, families map[string]*prommodel.MetricF
func checkWebhookValues(t *testing.T, families map[string]*prommodel.MetricFamily) {
t.Log("--- testing webhook metrics ---")

family, ok := families["keda_webhook_scaled_object_validation_errors"]
family, ok := families["keda_webhook_scaled_object_validation_errors_total"]
if !ok {
t.Errorf("metric keda_webhook_scaled_object_validation_errors not available")
t.Errorf("metric keda_webhook_scaled_object_validation_errors_total not available")
return
}

Expand All @@ -1379,11 +1315,11 @@ func checkWebhookValues(t *testing.T, families map[string]*prommodel.MetricFamil
}
metricValue += *metric.Counter.Value
}
assert.GreaterOrEqual(t, metricValue, 1.0, "keda_webhook_scaled_object_validation_errors has to be greater than 0")
assert.GreaterOrEqual(t, metricValue, 1.0, "keda_webhook_scaled_object_validation_errors_total has to be greater than 0")

family, ok = families["keda_webhook_scaled_object_validation_total"]
family, ok = families["keda_webhook_scaled_object_validations_total"]
if !ok {
t.Errorf("metric keda_webhook_scaled_object_validation_total not available")
t.Errorf("metric keda_webhook_scaled_object_validations_total not available")
return
}

Expand All @@ -1398,7 +1334,7 @@ func checkWebhookValues(t *testing.T, families map[string]*prommodel.MetricFamil
}
metricValue += *metric.Counter.Value
}
assert.GreaterOrEqual(t, metricValue, 1.0, "keda_webhook_scaled_object_validation_total has to be greater than 0")
assert.GreaterOrEqual(t, metricValue, 1.0, "keda_webhook_scaled_object_validations_total has to be greater than 0")
}

func checkMetricServerValues(t *testing.T, families map[string]*prommodel.MetricFamily) {
Expand Down

0 comments on commit 3a7c955

Please sign in to comment.