Files
Pulse/internal/alerts/cleanup_test.go
rcourtman 6eee5a1641 chore(alerts): delete the legacy transition-state maps
The reducer core became the authoritative transition state for every
per-observation family in the Phase 2 cutovers; the manager's tracking
maps (offlineConfirmations, offlineRecoveryConfirmations,
nodeOfflineCount, connectionDegradedCount, dockerOfflineCount,
dockerStateConfirm, pendingAlerts) had been reduced to write-only
mirrors. This deletes them, per the plan's retirement list
(docs/ALERT_ENGINE_EVOLUTION.md).

Hygiene the maps' cleanup loops used to provide moves into the core:

- reducer.PruneStalePending reaps pending runs whose resource stopped
  being observed (Cleanup at 10 minutes, cleanupStaleMaps at the stale
  threshold) — previously the pendingAlerts age sweep.
- Docker container cleanup drops core pending runs for containers no
  longer in the seen set (reducer.PendingResourceIDs +
  DropPendingForResource) — previously the dockerStateConfirm loops.
- HandleHostOnline / HandleDockerHostOnline apply a healthy
  observation to the core so an in-flight offline confirmation run
  ends — previously a map delete.

Two real gaps surfaced by the test conversion, fixed at the root:

- Config-change auto-resolution removed alerts without mirroring the
  forget into the core, leaving the incident firing after a policy
  disabled it.
- Intent pending state created by evaluateIntentNoLock carried no
  ResourceID/ResourceType/TrackingKey, so per-resource clears
  (guest suppression) could not match it.

Guest node-move migration no longer re-keys pending runs: a move
restarts an in-flight pending run (firing continuity still comes from
alert adoption). Deliberate simplification, noted in the helper.

Tests convert their map seeds and asserts to the core seams
(testCoreConfirmations / testCoreRecoveryCount / testCoreHasIncident /
testCoreIsPending, direct ApplyDiscrete seeding); assertions that only
tested the deleted maps' bookkeeping are removed.
2026-08-27 01:00:12 +01:00

128 lines
3.9 KiB
Go

package alerts
import (
"testing"
"time"
)
func TestCleanupStaleMaps(t *testing.T) {
// t.Parallel()
m := newTestManager(t)
// Populate maps with old data
oldTime := time.Now().Add(-25 * time.Hour)
recentTime := time.Now().Add(-1 * time.Minute)
m.mu.Lock()
// Flapping history
m.flappingHistory["stale-flapping"] = []time.Time{oldTime}
m.flappingHistory["recent-flapping"] = []time.Time{recentTime}
m.flappingActive["stale-flapping"] = true
// Suppressed until
m.suppressedUntil["expired-suppression"] = oldTime
m.suppressedUntil["active-suppression"] = time.Now().Add(1 * time.Hour)
// Should not be cleaned if active alert exists
m.flappingHistory["active-alert-flapping"] = []time.Time{oldTime}
m.activeAlerts["active-alert-flapping"] = &Alert{ID: "active-alert-flapping"}
// Empty history should be cleaned
m.flappingHistory["empty-flapping"] = []time.Time{}
m.mu.Unlock()
// Run cleanup
m.cleanupStaleMaps()
// Verify
m.mu.Lock()
if _, exists := m.flappingHistory["stale-flapping"]; exists {
t.Error("stale-flapping should get removed")
}
if _, exists := m.flappingActive["stale-flapping"]; exists {
t.Error("stale-flapping active flag should get removed")
}
if _, exists := m.flappingHistory["recent-flapping"]; !exists {
t.Error("recent-flapping should NOT get removed")
}
if _, exists := m.flappingHistory["empty-flapping"]; exists {
t.Error("empty-flapping should get removed")
}
if _, exists := m.suppressedUntil["expired-suppression"]; exists {
t.Error("expired-suppression should get removed")
}
if _, exists := m.suppressedUntil["active-suppression"]; !exists {
t.Error("active-suppression should NOT get removed")
}
if _, exists := m.flappingHistory["active-alert-flapping"]; !exists {
t.Error("active-alert-flapping should NOT get removed")
}
m.mu.Unlock()
m.mu.Lock()
// Docker restart tracking
m.dockerRestartTracking["stale-restart"] = &dockerRestartRecord{lastChecked: oldTime}
m.dockerRestartTracking["recent-restart"] = &dockerRestartRecord{lastChecked: recentTime}
// Alert rate limit
m.alertRateLimit["stale-rate"] = []time.Time{oldTime.Add(-2 * time.Hour)}
m.alertRateLimit["mixed-rate"] = []time.Time{oldTime, recentTime}
// Recent alerts
m.recentAlerts["stale-recent"] = &Alert{ID: "stale-recent", LastSeen: oldTime}
m.recentAlerts["recent-recent"] = &Alert{ID: "recent-recent", LastSeen: recentTime}
// Ack state
m.ackState["stale-ack"] = ackRecord{inactiveAt: oldTime}
m.ackState["stale-ack-no-inactive"] = ackRecord{time: oldTime}
m.ackState["recent-ack"] = ackRecord{inactiveAt: recentTime}
m.activeAlerts["active-ack"] = &Alert{ID: "active-ack"}
m.ackState["active-ack"] = ackRecord{inactiveAt: oldTime}
m.mu.Unlock()
// Run cleanup
m.cleanupStaleMaps()
// Verify additional maps
m.mu.Lock()
defer m.mu.Unlock()
if _, exists := m.dockerRestartTracking["stale-restart"]; exists {
t.Error("stale-restart should be removed")
}
if _, exists := m.dockerRestartTracking["recent-restart"]; !exists {
t.Error("recent-restart should NOT be removed")
}
if _, exists := m.alertRateLimit["stale-rate"]; exists {
t.Error("stale-rate should be removed")
}
if times, exists := m.alertRateLimit["mixed-rate"]; !exists || len(times) != 1 {
t.Errorf("mixed-rate should have 1 entry, got %v", len(times))
}
if _, exists := m.recentAlerts["stale-recent"]; exists {
t.Error("stale-recent should be removed")
}
if _, exists := m.recentAlerts["recent-recent"]; !exists {
t.Error("recent-recent should NOT be removed")
}
if _, exists := m.ackState["stale-ack"]; exists {
t.Error("stale-ack should be removed")
}
if _, exists := m.ackState["stale-ack-no-inactive"]; exists {
t.Error("stale-ack-no-inactive should be removed")
}
if _, exists := m.ackState["recent-ack"]; !exists {
t.Error("recent-ack should NOT be removed")
}
if _, exists := m.ackState["active-ack"]; !exists {
t.Error("active-ack should NOT be removed")
}
}