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: Add comprehensive tests for clearStorageOfflineAlert function

rcourtman547bc61e449a
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index 5246dba07a..9e4ba9ee9c 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -3095,3 +3095,101 @@ func TestDockerServiceResourceID(t *testing.T) {
})
}
}
+
+func TestClearStorageOfflineAlert(t *testing.T) {
+ t.Parallel()
+
+ t.Run("clears existing offline alert", func(t *testing.T) {
+ t.Parallel()
+ m := NewManager()
+
+ storage := models.Storage{
+ ID: "storage-1",
+ Name: "local-lvm",
+ Node: "pve1",
+ }
+
+ alertID := fmt.Sprintf("storage-offline-%s", storage.ID)
+
+ // Create an existing offline alert
+ m.mu.Lock()
+ m.activeAlerts[alertID] = &Alert{
+ ID: alertID,
+ Type: "storage-offline",
+ Level: "critical",
+ StartTime: time.Now().Add(-10 * time.Minute),
+ }
+ m.offlineConfirmations[storage.ID] = 3
+ m.mu.Unlock()
+
+ var resolvedID string
+ m.SetResolvedCallback(func(id string) {
+ resolvedID = id
+ })
+
+ m.clearStorageOfflineAlert(storage)
+
+ m.mu.RLock()
+ _, alertExists := m.activeAlerts[alertID]
+ _, confirmExists := m.offlineConfirmations[storage.ID]
+ m.mu.RUnlock()
+
+ if alertExists {
+ t.Error("expected alert to be cleared")
+ }
+ if confirmExists {
+ t.Error("expected offline confirmation to be cleared")
+ }
+ if resolvedID != alertID {
+ t.Errorf("expected resolved callback with %q, got %q", alertID, resolvedID)
+ }
+ })
+
+ t.Run("noop when no alert exists", func(t *testing.T) {
+ t.Parallel()
+ m := NewManager()
+
+ storage := models.Storage{
+ ID: "storage-2",
+ Name: "local-zfs",
+ Node: "pve1",
+ }
+
+ var callbackCalled bool
+ m.SetResolvedCallback(func(id string) {
+ callbackCalled = true
+ })
+
+ m.clearStorageOfflineAlert(storage)
+
+ if callbackCalled {
+ t.Error("expected no callback when no alert exists")
+ }
+ })
+
+ t.Run("clears offline confirmation even when no alert", func(t *testing.T) {
+ t.Parallel()
+ m := NewManager()
+
+ storage := models.Storage{
+ ID: "storage-3",
+ Name: "ceph-pool",
+ Node: "pve2",
+ }
+
+ // Set confirmation without alert
+ m.mu.Lock()
+ m.offlineConfirmations[storage.ID] = 2
+ m.mu.Unlock()
+
+ m.clearStorageOfflineAlert(storage)
+
+ m.mu.RLock()
+ _, confirmExists := m.offlineConfirmations[storage.ID]
+ m.mu.RUnlock()
+
+ if confirmExists {
+ t.Error("expected offline confirmation to be cleared")
+ }
+ })
+}