From b9d9dcfc385583d98fec0f7c1722344168c25327 Mon Sep 17 00:00:00 2001 From: Shubham Pampattiwar Date: Tue, 21 Jul 2026 10:13:12 -0700 Subject: [PATCH] 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 --- pkg/controller/backup_controller_test.go | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pkg/controller/backup_controller_test.go b/pkg/controller/backup_controller_test.go index 3aafa2c71..9f6bf1a28 100644 --- a/pkg/controller/backup_controller_test.go +++ b/pkg/controller/backup_controller_test.go @@ -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) {