Add integration test for updateTotalBackupMetric resync

Add a test that exercises the actual updateTotalBackupMetric goroutine
with a fake client to verify stale backupLastSuccessfulTimestamp
entries are pruned during a real resync cycle.

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>
This commit is contained in:
Shubham Pampattiwar
2026-07-22 09:17:59 -07:00
parent bfeccba0a8
commit b9d9dcfc38
+39
View File
@@ -18,6 +18,7 @@ package controller
import (
"bytes"
"context"
"fmt"
"io"
"reflect"
@@ -2087,6 +2088,44 @@ func collectGaugeCount(t *testing.T, g *prometheus.GaugeVec) int {
return count
}
// Test_updateTotalBackupMetric_prunesStaleTimestamps_integration tests the actual
// updateTotalBackupMetric goroutine with a fake client to verify stale metrics are
// pruned during a real resync cycle.
func Test_updateTotalBackupMetric_prunesStaleTimestamps_integration(t *testing.T) {
baseTime, err := time.Parse(time.RFC1123, time.RFC1123)
require.NoError(t, err)
m := metrics.NewServerMetrics()
gauge := m.Metrics()["backup_last_successful_timestamp"].(*prometheus.GaugeVec)
activeBackup := builder.ForBackup("velero", "b1").
ObjectMeta(builder.WithLabels(velerov1api.ScheduleNameLabel, "active-schedule")).
Phase(velerov1api.BackupPhaseCompleted).
CompletionTimestamp(baseTime).
Result()
fakeClient := velerotest.NewFakeControllerRuntimeClient(t, activeBackup)
m.SetBackupLastSuccessfulTimestamp("deleted-schedule", baseTime)
require.Equal(t, 1, collectGaugeCount(t, gauge))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := &backupReconciler{
ctx: ctx,
kbClient: fakeClient,
logger: logrus.StandardLogger(),
metrics: m,
}
c.updateTotalBackupMetric()
time.Sleep(7 * time.Second)
cancel()
assert.Equal(t, 1, collectGaugeCount(t, gauge))
}
// Unit tests to make sure that the backup's status is updated correctly during reconcile.
// To clear up confusion whether status can be updated with Patch alone without status writer and not kbClient.Status().Patch()
func TestPatchResourceWorksWithStatus(t *testing.T) {