Skip to content

Commit

Permalink
Update WPA controller unit-tests to check new upscale-downscale metrics
Browse files Browse the repository at this point in the history
The commmit also modify the way the unit-tests valid the generated metrics that the
controller emit when it does an action. Now the unit-tests will only validate
the metric values that are provided inside the "wantPromMetrics" or each test.
  • Loading branch information
clamoriniere committed Jul 8, 2024
1 parent 642a80c commit bc2f113
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions controllers/datadoghq/watermarkpodautoscaler_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ func TestReconcileWatermarkPodAutoscaler_reconcileWPA(t *testing.T) {
"restrictedScalingDownCap": 0.0,
"restrictedScalingUpCap": 0.0,
"restrictedScalingOk": 0.0,
"downscale": 1.0,
},
},
{
Expand Down Expand Up @@ -1345,7 +1346,6 @@ func TestReconcileWatermarkPodAutoscaler_reconcileWPA(t *testing.T) {
err := r.reconcileWPA(context.TODO(), logf.Log.WithName(tt.name), originalWPAStatus, wpa)
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileWatermarkPodAutoscaler.Reconcile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantFunc != nil {
if err := tt.wantFunc(r.Client, tt.args.wantReplicasCount, wpa); err != nil {
Expand Down Expand Up @@ -2430,6 +2430,15 @@ func getGaugeVal(t *testing.T, metric prometheus.Metric) float64 {
return dtoMetric.GetGauge().GetValue()
}

func getCounterVal(t *testing.T, metric prometheus.Metric) float64 {
dtoMetric := dto.Metric{}
err := metric.Write(&dtoMetric)
if err != nil {
t.Error("Couldn't get Prometheus metrics")
}
return dtoMetric.GetCounter().GetValue()
}

func getMetricKeys() []string {
return []string{"dryRun",
"value",
Expand All @@ -2441,6 +2450,8 @@ func getMetricKeys() []string {
"replicaEffective",
"replicaMin",
"replicaMax",
"upscale",
"downscale",
"restrictedScalingDownCap",
"restrictedScalingUpCap",
"restrictedScalingOk",
Expand Down Expand Up @@ -2468,6 +2479,8 @@ func getPromMetrics(t *testing.T, wpa *v1alpha1.WatermarkPodAutoscaler) map[stri
"replicaMin": getGaugeVal(t, replicaMin.With(getPromBaseLabels(wpa))),
"replicaMax": getGaugeVal(t, replicaMax.With(getPromBaseLabels(wpa))),
"dryRun": getGaugeVal(t, dryRun.With(getPromBaseLabels(wpa))),
"upscale": getCounterVal(t, upscale.With(getPromBaseLabels(wpa))),
"downscale": getCounterVal(t, downscale.With(getPromBaseLabels(wpa))),

"transitionCountdownUp": getGaugeVal(t, transitionCountdown.With(getTransitionCountdownLabels(wpa, "downscale"))),
"transitionCountdownDown": getGaugeVal(t, transitionCountdown.With(getTransitionCountdownLabels(wpa, "upscale"))),
Expand All @@ -2494,6 +2507,8 @@ func resetPromMetrics(wpa *v1alpha1.WatermarkPodAutoscaler) {
replicaMin.With(getPromBaseLabels(wpa)).Set(0.0)
replicaMax.With(getPromBaseLabels(wpa)).Set(0.0)
dryRun.With(getPromBaseLabels(wpa)).Set(0.0)
upscale.Reset()
downscale.Reset()

transitionCountdown.With(getTransitionCountdownLabels(wpa, "downscale")).Set(0.0)
transitionCountdown.With(getTransitionCountdownLabels(wpa, "upscale")).Set(0.0)
Expand All @@ -2513,7 +2528,8 @@ func assertZeroMetrics(t *testing.T, actual map[string]float64) {
func assertWantPromMetrics(t *testing.T, want map[string]float64, wpa *v1alpha1.WatermarkPodAutoscaler) {
actual := getPromMetrics(t, wpa)
printPromMetrics(t, actual)
for _, key := range getMetricKeys() {
// only look at the keys we care about by loop against `want` map
for key := range want {
t.Log("comparing for key", key, fmt.Sprintf("want %.1f actual %.1f", want[key], actual[key]))
assert.InDelta(t, want[key], actual[key], 0.00001, "didn't match the values", key)
}
Expand Down

0 comments on commit bc2f113

Please sign in to comment.