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): cover delivery warning projection lifecycle

pulse-triage[bot]4af5d5216ede
diff --git a/internal/monitoring/system_alerts_test.go b/internal/monitoring/system_alerts_test.go
index 40d8168bc3..c436362545 100644
--- a/internal/monitoring/system_alerts_test.go
+++ b/internal/monitoring/system_alerts_test.go
@@ -199,3 +199,86 @@ func TestProjectNotificationDeliveryHealthOrdersSnapshots(t *testing.T) {
})
}
}
+
+// Exercise the real monitor-to-alert-manager boundary, not just a fingerprint
+// helper or hand-built SystemAlertInput. No queue, database or destination is
+// needed: health snapshots are the input to this projection.
+func TestProjectNotificationDeliveryHealthLifecycle(t *testing.T) {
+ manager := alerts.NewManagerWithDataDir(t.TempDir())
+ t.Cleanup(manager.Stop)
+ config := manager.GetConfig()
+ config.ActivationState = alerts.ActivationActive
+ manager.UpdateConfig(config)
+ manager.SetAlertCallback(func(*alerts.Alert) {})
+ m := &Monitor{}
+
+ healthy := notifications.ClassifyQueueHealth(nil)
+ failed := notifications.ClassifyQueueHealth(map[string]int{string(notifications.QueueStatusFailed): 1})
+ drifted := notifications.ClassifyQueueHealth(map[string]int{string(notifications.QueueStatusFailed): 4})
+ mixed := notifications.ClassifyQueueHealth(map[string]int{
+ string(notifications.QueueStatusFailed): 4,
+ string(notifications.QueueStatusDLQ): 2,
+ })
+ var previous *alerts.Alert
+ var lastIncidentStart time.Time
+ for _, step := range []struct {
+ name string
+ health notifications.DeliveryHealth
+ notify bool
+ }{
+ {"initially healthy", healthy, false},
+ {"first failure", failed, true},
+ {"unchanged failure", failed, false},
+ {"count drift", drifted, false},
+ {"additional failure class", mixed, true},
+ {"recovery", healthy, false},
+ {"repeated recovery", healthy, false},
+ {"same cause recurs", mixed, true},
+ } {
+ if !t.Run(step.name, func(t *testing.T) {
+ m.projectNotificationDeliveryHealth(manager, func() notifications.DeliveryHealth { return step.health })
+ active := manager.GetActiveAlerts()
+ if step.health.Healthy {
+ if len(active) != 0 {
+ t.Fatalf("healthy projection left %d active alerts", len(active))
+ }
+ previous = nil
+ return
+ }
+ if len(active) != 1 {
+ t.Fatalf("active alerts = %d, want exactly one", len(active))
+ }
+ current := active[0]
+ if current.ID != alerts.SystemAlertID(alerts.NotificationDeliveryAlertType) || current.ResourceID != "" || current.Level != alerts.AlertLevelWarning {
+ t.Fatalf("unexpected delivery warning identity or level: %s, %s, %s", current.ID, current.ResourceID, current.Level)
+ }
+ if current.Message != notificationDeliveryAlertMessage(step.health) ||
+ current.Metadata["failedDeliveries"] != step.health.Failed ||
+ current.Metadata["deadLetterCount"] != step.health.DeadLetter ||
+ current.Metadata["deliveryStatus"] != string(step.health.Status) {
+ t.Fatal("warning presentation did not refresh to the current health")
+ }
+ // Dispatch stamps LastNotified under the manager lock before any
+ // asynchronous callback. This tests silence without sleep-based
+ // assertions about a callback that may not have run yet.
+ if current.LastNotified == nil {
+ t.Fatal("active delivery warning was never dispatched")
+ }
+ if previous != nil {
+ if !current.StartTime.Equal(previous.StartTime) {
+ t.Fatal("standing warning was replaced instead of refreshed")
+ }
+ notified := !current.LastNotified.Equal(*previous.LastNotified)
+ if notified != step.notify {
+ t.Fatalf("dispatch timestamp changed = %v, want %v", notified, step.notify)
+ }
+ } else if !current.StartTime.After(lastIncidentStart) {
+ t.Fatal("recurrence did not start a new incident")
+ }
+ lastIncidentStart = current.StartTime
+ previous = &current
+ }) {
+ return // Later steps depend on the preceding lifecycle state.
+ }
+ }
+}