Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

test(monitoring): pin the recovery-point reconcile wiring and availability fixed-interval provider

rcourtman7675f573497e
diff --git a/internal/monitoring/availability_fixed_interval_test.go b/internal/monitoring/availability_fixed_interval_test.go
new file mode 100644
index 0000000000..9fa6fda1bd
--- /dev/null
+++ b/internal/monitoring/availability_fixed_interval_test.go
@@ -0,0 +1,72 @@
+package monitoring
+
+import (
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+)
+
+// Regression tests for the provider half of #1582: the scheduler already pins
+// InstanceDescriptor.FixedInterval exactly (TestBuildPlan_FixedInterval), but
+// the availability provider must actually populate that descriptor from the
+// target's configured interval, or the adaptive scheduler takes over again.
+
+func availabilityFixedIntervalTestMonitor(t *testing.T, targets []config.AvailabilityTarget) *Monitor {
+ t.Helper()
+ persistence := config.NewConfigPersistence(t.TempDir())
+ if err := persistence.SaveAvailabilityTargets(targets); err != nil {
+ t.Fatalf("SaveAvailabilityTargets() error = %v", err)
+ }
+ return &Monitor{configPersist: persistence}
+}
+
+func TestAvailabilityPollProviderFixedInstanceInterval(t *testing.T) {
+ monitor := availabilityFixedIntervalTestMonitor(t, []config.AvailabilityTarget{
+ {ID: "plex", Name: "Plex", Address: "plex.local", Protocol: config.AvailabilityProbeICMP, Enabled: true, PollIntervalSecs: 120},
+ {ID: "defaulted", Name: "Defaulted", Address: "defaulted.local", Protocol: config.AvailabilityProbeICMP, Enabled: true},
+ {ID: "oversized", Name: "Oversized", Address: "oversized.local", Protocol: config.AvailabilityProbeICMP, Enabled: true, PollIntervalSecs: 7200},
+ {ID: "paused", Name: "Paused", Address: "paused.local", Protocol: config.AvailabilityProbeICMP, Enabled: false, PollIntervalSecs: 120},
+ })
+ provider := availabilityPollProvider{}
+
+ if got := provider.FixedInstanceInterval(monitor, "plex"); got != 120*time.Second {
+ t.Fatalf("FixedInstanceInterval(plex) = %v, want 120s", got)
+ }
+ wantDefault := time.Duration(config.DefaultAvailabilityPollIntervalSecs) * time.Second
+ if got := provider.FixedInstanceInterval(monitor, "defaulted"); got != wantDefault {
+ t.Fatalf("FixedInstanceInterval(defaulted) = %v, want %v", got, wantDefault)
+ }
+ if got := provider.FixedInstanceInterval(monitor, "oversized"); got != time.Hour {
+ t.Fatalf("FixedInstanceInterval(oversized) = %v, want clamp to 1h", got)
+ }
+ if got := provider.FixedInstanceInterval(monitor, "paused"); got != 0 {
+ t.Fatalf("FixedInstanceInterval(paused) = %v, want 0 for disabled target", got)
+ }
+ if got := provider.FixedInstanceInterval(monitor, "missing"); got != 0 {
+ t.Fatalf("FixedInstanceInterval(missing) = %v, want 0 for unknown target", got)
+ }
+}
+
+// End-to-end wiring: the scheduler descriptor for an availability target must
+// carry the configured interval as FixedInterval.
+func TestDescribeInstancesForSchedulerPinsAvailabilityFixedInterval(t *testing.T) {
+ monitor := availabilityFixedIntervalTestMonitor(t, []config.AvailabilityTarget{
+ {ID: "plex", Name: "Plex", Address: "plex.local", Protocol: config.AvailabilityProbeICMP, Enabled: true, PollIntervalSecs: 120},
+ })
+
+ descriptors := monitor.describeInstancesForScheduler()
+ var found *InstanceDescriptor
+ for i := range descriptors {
+ if descriptors[i].Type == InstanceTypeAvailability && descriptors[i].Name == "plex" {
+ found = &descriptors[i]
+ break
+ }
+ }
+ if found == nil {
+ t.Fatalf("no availability descriptor for plex in %+v", descriptors)
+ }
+ if found.FixedInterval != 120*time.Second {
+ t.Fatalf("descriptor FixedInterval = %v, want 120s", found.FixedInterval)
+ }
+}
diff --git a/internal/monitoring/recovery_ingest_test.go b/internal/monitoring/recovery_ingest_test.go
new file mode 100644
index 0000000000..b3997b5015
--- /dev/null
+++ b/internal/monitoring/recovery_ingest_test.go
@@ -0,0 +1,164 @@
+package monitoring
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ "github.com/rcourtman/pulse-go-rewrite/internal/recovery"
+ recoverymanager "github.com/rcourtman/pulse-go-rewrite/internal/recovery/manager"
+)
+
+func recoveryIngestTestMonitor(t *testing.T) (*Monitor, *recoverymanager.Manager) {
+ t.Helper()
+ mtp := config.NewMultiTenantPersistence(t.TempDir())
+ manager := recoverymanager.New(mtp)
+ return &Monitor{recoveryManager: manager}, manager
+}
+
+func recoveryIngestTestPoint(id string, instance string, at time.Time) recovery.RecoveryPoint {
+ return recovery.RecoveryPoint{
+ ID: id,
+ Provider: recovery.ProviderProxmoxPVE,
+ Kind: recovery.KindBackup,
+ Mode: recovery.ModeLocal,
+ Outcome: recovery.OutcomeSuccess,
+ StartedAt: &at,
+ CompletedAt: &at,
+ Details: map[string]any{"instance": instance},
+ }
+}
+
+func listRecoveryPointIDs(t *testing.T, manager *recoverymanager.Manager) map[string]bool {
+ t.Helper()
+ store, err := manager.StoreForOrg("default")
+ if err != nil {
+ t.Fatalf("StoreForOrg(default): %v", err)
+ }
+ points, _, err := store.ListPoints(context.Background(), recovery.ListPointsOptions{})
+ if err != nil {
+ t.Fatalf("ListPoints(): %v", err)
+ }
+ ids := make(map[string]bool, len(points))
+ for _, p := range points {
+ ids[p.ID] = true
+ }
+ return ids
+}
+
+// Regression test for the monitoring-layer wiring of #1580: a poll cycle's
+// complete enumeration must delete in-scope points the source stopped
+// reporting, so a backup deleted upstream cannot keep re-raising its alert.
+func TestIngestRecoveryPointsBestEffortReconcilesScope(t *testing.T) {
+ m, manager := recoveryIngestTestMonitor(t)
+ store, err := manager.StoreForOrg("default")
+ if err != nil {
+ t.Fatalf("StoreForOrg(default): %v", err)
+ }
+
+ now := time.Date(2026, 7, 1, 12, 0, 0, 0, time.UTC)
+ seed := []recovery.RecoveryPoint{
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-100", "pve1", now),
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-101", "pve1", now),
+ // Same class, different instance: out of scope, must survive.
+ recoveryIngestTestPoint("pve-backup:pve2-vzdump-100", "pve2", now),
+ }
+ if err := store.UpsertPoints(context.Background(), seed); err != nil {
+ t.Fatalf("UpsertPoints(): %v", err)
+ }
+
+ // The next poll of pve1 enumerates only vzdump-100.
+ m.ingestRecoveryPointsBestEffort(context.Background(), recoveryIngestBatch{
+ points: []recovery.RecoveryPoint{
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-100", "pve1", now),
+ },
+ reconcile: &recoveryReconcileScope{
+ provider: string(recovery.ProviderProxmoxPVE),
+ idPrefix: "pve-backup:",
+ instance: "pve1",
+ },
+ })
+
+ ids := listRecoveryPointIDs(t, manager)
+ if ids["pve-backup:pve1-vzdump-101"] {
+ t.Fatal("stale point pve-backup:pve1-vzdump-101 should have been reconciled away")
+ }
+ if !ids["pve-backup:pve1-vzdump-100"] {
+ t.Fatal("still-enumerated point pve-backup:pve1-vzdump-100 must survive")
+ }
+ if !ids["pve-backup:pve2-vzdump-100"] {
+ t.Fatal("other-instance point pve-backup:pve2-vzdump-100 must survive")
+ }
+}
+
+// An empty enumeration is meaningful: the source reports no backups at all,
+// so the whole scope clears (the delete-all-backups case from #1580).
+func TestIngestRecoveryPointsBestEffortEmptyEnumerationClearsScope(t *testing.T) {
+ m, manager := recoveryIngestTestMonitor(t)
+ store, err := manager.StoreForOrg("default")
+ if err != nil {
+ t.Fatalf("StoreForOrg(default): %v", err)
+ }
+
+ now := time.Date(2026, 7, 1, 12, 0, 0, 0, time.UTC)
+ if err := store.UpsertPoints(context.Background(), []recovery.RecoveryPoint{
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-100", "pve1", now),
+ recoveryIngestTestPoint("pve-backup:pve2-vzdump-100", "pve2", now),
+ }); err != nil {
+ t.Fatalf("UpsertPoints(): %v", err)
+ }
+
+ m.ingestRecoveryPointsBestEffort(context.Background(), recoveryIngestBatch{
+ points: nil,
+ reconcile: &recoveryReconcileScope{
+ provider: string(recovery.ProviderProxmoxPVE),
+ idPrefix: "pve-backup:",
+ instance: "pve1",
+ },
+ })
+
+ ids := listRecoveryPointIDs(t, manager)
+ if ids["pve-backup:pve1-vzdump-100"] {
+ t.Fatal("pve1 scope should be empty after an empty enumeration")
+ }
+ if !ids["pve-backup:pve2-vzdump-100"] {
+ t.Fatal("other-instance point must survive an empty pve1 enumeration")
+ }
+}
+
+// A failed upsert must not reconcile: deleting on top of a failed batch could
+// drop points the source still reports.
+func TestIngestRecoveryPointsBestEffortSkipsReconcileWhenUpsertFails(t *testing.T) {
+ m, manager := recoveryIngestTestMonitor(t)
+ store, err := manager.StoreForOrg("default")
+ if err != nil {
+ t.Fatalf("StoreForOrg(default): %v", err)
+ }
+
+ now := time.Date(2026, 7, 1, 12, 0, 0, 0, time.UTC)
+ if err := store.UpsertPoints(context.Background(), []recovery.RecoveryPoint{
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-100", "pve1", now),
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-101", "pve1", now),
+ }); err != nil {
+ t.Fatalf("UpsertPoints(): %v", err)
+ }
+
+ cancelled, cancel := context.WithCancel(context.Background())
+ cancel()
+ m.ingestRecoveryPointsBestEffort(cancelled, recoveryIngestBatch{
+ points: []recovery.RecoveryPoint{
+ recoveryIngestTestPoint("pve-backup:pve1-vzdump-100", "pve1", now),
+ },
+ reconcile: &recoveryReconcileScope{
+ provider: string(recovery.ProviderProxmoxPVE),
+ idPrefix: "pve-backup:",
+ instance: "pve1",
+ },
+ })
+
+ ids := listRecoveryPointIDs(t, manager)
+ if !ids["pve-backup:pve1-vzdump-101"] {
+ t.Fatal("reconcile must be skipped when the upsert fails; vzdump-101 was deleted")
+ }
+}