Remove BackupLastSuccessfulTimestampCount, use Metrics() in test

Remove the exported method that was only used in tests. Use the
existing Metrics() getter to access the gauge directly in the
backup controller test instead.

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>
This commit is contained in:
Shubham Pampattiwar
2026-07-22 09:17:59 -07:00
parent 4357ad8976
commit 6ea95548d6
2 changed files with 16 additions and 19 deletions
+16 -2
View File
@@ -31,6 +31,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -2049,10 +2050,11 @@ func Test_updateTotalBackupMetric_prunesStaleTimestamps(t *testing.T) {
require.NoError(t, err)
m := metrics.NewServerMetrics()
gauge := m.Metrics()["backup_last_successful_timestamp"].(*prometheus.GaugeVec)
// Simulate a previous resync that set the metric for "deleted-schedule"
m.SetBackupLastSuccessfulTimestamp("deleted-schedule", baseTime)
require.Equal(t, 1, m.BackupLastSuccessfulTimestampCount())
require.Equal(t, 1, collectGaugeCount(t, gauge))
// Current backups only contain entries for "active-schedule"
backups := []velerov1api.Backup{
@@ -2070,7 +2072,19 @@ func Test_updateTotalBackupMetric_prunesStaleTimestamps(t *testing.T) {
}
// Only "active-schedule" should remain; "deleted-schedule" should be pruned
assert.Equal(t, 1, m.BackupLastSuccessfulTimestampCount())
assert.Equal(t, 1, collectGaugeCount(t, gauge))
}
func collectGaugeCount(t *testing.T, g *prometheus.GaugeVec) int {
t.Helper()
ch := make(chan prometheus.Metric, 10)
g.Collect(ch)
close(ch)
count := 0
for range ch {
count++
}
return count
}
// Unit tests to make sure that the backup's status is updated correctly during reconcile.
-17
View File
@@ -765,23 +765,6 @@ func (m *ServerMetrics) ResetBackupLastSuccessfulTimestamp() {
}
}
// BackupLastSuccessfulTimestampCount returns the number of active time series
// in the backupLastSuccessfulTimestamp gauge.
func (m *ServerMetrics) BackupLastSuccessfulTimestampCount() int {
g, ok := m.metrics[backupLastSuccessfulTimestamp].(*prometheus.GaugeVec)
if !ok {
return 0
}
ch := make(chan prometheus.Metric, 100)
g.Collect(ch)
close(ch)
count := 0
for range ch {
count++
}
return count
}
// SetBackupTarballSizeBytesGauge records the size, in bytes, of a backup tarball.
func (m *ServerMetrics) SetBackupTarballSizeBytesGauge(backupSchedule string, size int64) {
if g, ok := m.metrics[backupTarballSizeBytesGauge].(*prometheus.GaugeVec); ok {