Files
Pulse/internal/monitoring/monitor_alert_intent_test.go
rcourtman 3ca8b9647a Move the live metric status projection test clear of main
Main appended TestSilentLinkedAgentStopsRefreshingNodeTemperatureAlert
at the end of monitor_alert_handling_test.go, where this branch had
appended its own projection test, so the two edits conflict. Move the
test unchanged in substance to monitor_alert_intent_test.go, which main
has not touched, so the merge with main is clean.
2026-10-06 18:03:55 +01:00

305 lines
11 KiB
Go

package monitoring
import (
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
"github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
)
func TestInstallOperatorIntentResolverProjectsCanonicalResourcePolicy(t *testing.T) {
store := unifiedresources.NewMemoryStore()
registry := unifiedresources.NewRegistry(store)
registry.IngestResources([]unifiedresources.Resource{{
ID: "vm:101", Type: unifiedresources.ResourceTypeVM, Name: "database",
}})
if err := store.SetResourceOperatorState(unifiedresources.ResourceOperatorState{
CanonicalID: "vm:101",
MonitoringMode: unifiedresources.MonitoringModeMuted,
LifecycleState: unifiedresources.LifecycleStateRetired,
}); err != nil {
t.Fatal(err)
}
manager := alerts.NewManagerWithDataDir(t.TempDir())
t.Cleanup(manager.Stop)
monitor := &Monitor{alertManager: manager}
monitor.installOperatorIntentResolver(unifiedresources.NewMonitorAdapter(registry))
preview, err := manager.PreviewIntentPolicy(alerts.AlertIntentPolicyPreviewRequest{
ResourceID: "vm:101", ResourceType: "vm", Signal: string(alerts.AlertIntentSignalOffline), ConditionActive: true,
})
if err != nil {
t.Fatal(err)
}
if preview.Reason != "operator_retired" || preview.Status != "expected_transient" {
t.Fatalf("canonical operator policy preview = %+v", preview)
}
}
func TestProxmoxPhysicalDiskMuteResolvesAndSuppressesWearoutAlert(t *testing.T) {
store := unifiedresources.NewMemoryStore()
registry := unifiedresources.NewRegistry(store)
instance, node := "pve", "rocket"
makeDisk := func(path string) models.PhysicalDisk {
return models.PhysicalDisk{
ID: unifiedresources.ProxmoxPhysicalDiskSourceID(instance, node, path, "", ""),
Instance: instance, Node: node, DevPath: path,
Model: "KINGSTON SA400", Type: "ssd", Health: "PASSED", Wearout: 0,
LastChecked: time.Now().UTC(),
}
}
registry.IngestSnapshot(models.StateSnapshot{
PhysicalDisks: []models.PhysicalDisk{makeDisk("/dev/sda"), makeDisk("/dev/sdb")},
})
diskID := unifiedresources.ProxmoxPhysicalDiskAlertResourceID(instance, node, "/dev/sda")
canonicalID, found := registry.ResolveReferenceID(diskID)
if !found {
t.Fatalf("PVE wearout alert resource %q did not resolve to a physical disk", diskID)
}
otherID := unifiedresources.ProxmoxPhysicalDiskAlertResourceID(instance, node, "/dev/sdb")
if other, ok := registry.ResolveReferenceID(otherID); !ok || other == canonicalID {
t.Fatalf("other PVE disk identity = %q, %v; want a different disk", other, ok)
}
manager := alerts.NewManagerWithDataDir(t.TempDir())
t.Cleanup(manager.Stop)
worn := proxmox.Disk{DevPath: "/dev/sda", Model: "KINGSTON SA400", Type: "ssd", Health: "PASSED", Wearout: 0}
manager.CheckDiskHealth(instance, node, worn)
if got := len(manager.GetActiveAlerts()); got != 1 {
t.Fatalf("unmuted worn disk raised %d alerts, want one", got)
}
if err := store.SetResourceOperatorState(unifiedresources.ResourceOperatorState{
CanonicalID: canonicalID, MonitoringMode: unifiedresources.MonitoringModeMuted,
}); err != nil {
t.Fatal(err)
}
monitor := &Monitor{alertManager: manager}
monitor.installOperatorIntentResolver(unifiedresources.NewMonitorAdapter(registry))
if got := len(manager.GetActiveAlerts()); got != 0 {
t.Fatalf("mute left %d existing PVE wearout alerts active", got)
}
manager.CheckDiskHealth(instance, node, worn)
if got := len(manager.GetActiveAlerts()); got != 0 {
t.Fatalf("muted PVE disk raised %d new wearout alerts", got)
}
worn.DevPath = "/dev/sdb"
manager.CheckDiskHealth(instance, node, worn)
if got := len(manager.GetActiveAlerts()); got != 1 {
t.Fatalf("other PVE disk raised %d alerts, want one", got)
}
}
func TestInstallOperatorIntentResolverInheritsScopedMaintenanceFromParent(t *testing.T) {
store := unifiedresources.NewMemoryStore()
registry := unifiedresources.NewRegistry(store)
parentID := "node:pve-a"
registry.IngestResources([]unifiedresources.Resource{
{ID: parentID, Type: unifiedresources.ResourceTypeAgent, Name: "pve-a"},
{ID: "vm:101", Type: unifiedresources.ResourceTypeVM, Name: "database", ParentID: &parentID},
})
now := time.Now().UTC()
start, end := now.Add(-time.Hour), now.Add(2*time.Hour)
if err := store.SetResourceOperatorState(unifiedresources.ResourceOperatorState{
CanonicalID: parentID,
MaintenanceStartAt: &start,
MaintenanceEndAt: &end,
MaintenanceReason: "hypervisor patching",
MaintenanceScope: unifiedresources.MaintenanceScopeResourceAndDescendants,
}); err != nil {
t.Fatal(err)
}
manager := alerts.NewManagerWithDataDir(t.TempDir())
t.Cleanup(manager.Stop)
monitor := &Monitor{alertManager: manager}
monitor.installOperatorIntentResolver(unifiedresources.NewMonitorAdapter(registry))
preview, err := manager.PreviewIntentPolicy(alerts.AlertIntentPolicyPreviewRequest{
ResourceID: "vm:101", ResourceType: "vm", Signal: string(alerts.AlertIntentSignalOffline), ConditionActive: true,
})
if err != nil {
t.Fatal(err)
}
if preview.Reason != "operator_maintenance" || preview.Status != "expected_transient" {
t.Fatalf("inherited maintenance preview = %+v", preview)
}
if preview.EligibleAt == nil || !preview.EligibleAt.Equal(end) {
t.Fatalf("eligibleAt = %v, want %v", preview.EligibleAt, end)
}
// Scope is explicit: the same parent window must not leak to descendants
// when changed back to resource-only.
if err := store.SetResourceOperatorState(unifiedresources.ResourceOperatorState{
CanonicalID: parentID,
MaintenanceStartAt: &start,
MaintenanceEndAt: &end,
MaintenanceScope: unifiedresources.MaintenanceScopeResource,
}); err != nil {
t.Fatal(err)
}
preview, err = manager.PreviewIntentPolicy(alerts.AlertIntentPolicyPreviewRequest{
ResourceID: "vm:101", ResourceType: "vm", Signal: string(alerts.AlertIntentSignalOffline), ConditionActive: true,
})
if err != nil {
t.Fatal(err)
}
if preview.Status != "would_activate" {
t.Fatalf("resource-only parent window leaked to child: %+v", preview)
}
}
func TestPlatformMonitorResourceIdentityConstructors(t *testing.T) {
if got := PBSMonitorResourceID("backup-main"); got != "pbs-backup-main" {
t.Fatalf("PBS monitor resource ID = %q", got)
}
if got := PMGMonitorResourceID("mail-main"); got != "pmg-mail-main" {
t.Fatalf("PMG monitor resource ID = %q", got)
}
}
func TestResolveBackupIntentContextRequiresFreshActiveMatchingEvidence(t *testing.T) {
now := time.Date(2026, 7, 20, 12, 0, 0, 0, time.UTC)
state := models.NewState()
state.UpdateBackupTasksForInstance("pve-a", []models.BackupTask{
{
ID: "active-101",
Instance: "pve-a",
Node: "node-a",
VMID: 101,
Status: "running",
ObservedAt: now.Add(-time.Minute),
},
{
ID: "stale-102",
Instance: "pve-a",
Node: "node-a",
VMID: 102,
Status: "running",
ObservedAt: now.Add(-backupIntentEvidenceMaxAge - time.Second),
},
{
ID: "finished-103",
Instance: "pve-a",
Node: "node-a",
VMID: 103,
Status: "OK",
ObservedAt: now.Add(-time.Minute),
EndTime: now.Add(-30 * time.Second),
},
})
monitor := &Monitor{state: state}
context, found := monitor.resolveBackupIntentContext("", "pve-a", "node-a", 101, now)
if !found || !context.Active {
t.Fatalf("fresh active task did not resolve: found=%v context=%+v", found, context)
}
if context.ObservedAt != now.Add(-time.Minute) {
t.Fatalf("observedAt = %v, want %v", context.ObservedAt, now.Add(-time.Minute))
}
if context.Evidence != "pve_vzdump_task:active-101" {
t.Fatalf("evidence = %q, want active task identity", context.Evidence)
}
for _, tc := range []struct {
name string
instance string
node string
vmid int
}{
{name: "wrong instance", instance: "pve-b", node: "node-a", vmid: 101},
{name: "wrong node", instance: "pve-a", node: "node-b", vmid: 101},
{name: "stale", instance: "pve-a", node: "node-a", vmid: 102},
{name: "finished", instance: "pve-a", node: "node-a", vmid: 103},
} {
t.Run(tc.name, func(t *testing.T) {
if got, ok := monitor.resolveBackupIntentContext("", tc.instance, tc.node, tc.vmid, now); ok {
t.Fatalf("unexpected backup intent context: %+v", got)
}
})
}
}
// A guest covered by a running multi-guest vzdump job has no task of its own;
// pollBackupTasks synthesizes one from the job log. That synthetic task must
// count as backup-intent evidence so the guest's alerts are suppressed while
// the job is backing it up, and must stop counting once its section finishes.
func TestResolveBackupIntentContextAcceptsSynthesizedJobGuestTask(t *testing.T) {
now := time.Date(2026, 7, 25, 2, 5, 0, 0, time.UTC)
jobTask := models.BackupTask{
ID: "pve-a-UPID:node-a:000E9F2C:0AC734B2:68A1B2C3:vzdump::root@pam:",
Node: "node-a",
Instance: "pve-a",
Type: "vzdump",
StartTime: now.Add(-4 * time.Minute),
// no EndTime: the job is still running
}
synthesized := parseVzdumpJobLog(jobTask, "UPID:node-a:000E9F2C:0AC734B2:68A1B2C3:vzdump::root@pam:", []proxmox.TaskLogLine{
{LineNumber: 1, Text: "INFO: Starting Backup of VM 101 (qemu)"},
{LineNumber: 2, Text: "INFO: Finished Backup of VM 101 (00:01:30)"},
{LineNumber: 3, Text: "INFO: Starting Backup of VM 102 (lxc)"},
})
for i := range synthesized {
synthesized[i].ObservedAt = now.Add(-30 * time.Second)
}
state := models.NewState()
state.UpdateBackupTasksForInstance("pve-a", append([]models.BackupTask{jobTask}, synthesized...))
monitor := &Monitor{state: state}
context, found := monitor.resolveBackupIntentContext("", "pve-a", "node-a", 102, now)
if !found || !context.Active {
t.Fatalf("guest being backed up by running job did not resolve: found=%v context=%+v", found, context)
}
if got, ok := monitor.resolveBackupIntentContext("", "pve-a", "node-a", 101, now); ok {
t.Fatalf("guest whose job section already finished should not carry intent: %+v", got)
}
}
func TestSyncAlertsToStateCarriesLiveMetricStatusOfHeldAlert(t *testing.T) {
m := &Monitor{
state: models.NewState(),
alertManager: alerts.NewManagerWithDataDir(t.TempDir()),
}
defer m.alertManager.Stop()
cfg := m.alertManager.GetConfig()
cfg.TimeThresholds = map[string]int{"node": 0}
cfg.NodeDefaults.Temperature = &alerts.HysteresisThreshold{Trigger: 80, Clear: 75}
m.alertManager.UpdateConfig(cfg)
node := models.Node{
ID: "homelab-minipc", Name: "minipc", Instance: "homelab", Status: "online",
Temperature: &models.Temperature{Available: true, CPUPackage: 85},
}
m.alertManager.CheckNode(node)
node.Temperature = &models.Temperature{Available: true, CPUPackage: 78}
m.alertManager.CheckNode(node)
m.syncAlertsToState()
var held *models.Alert
snapshot := m.state.GetSnapshot()
for i := range snapshot.ActiveAlerts {
if snapshot.ActiveAlerts[i].Type == "temperature" {
held = &snapshot.ActiveAlerts[i]
}
}
if held == nil {
t.Fatal("expected the temperature alert to stay open at 78°C")
}
if held.Value != 85 {
t.Fatalf("legacy value = %v, want the last breach 85", held.Value)
}
status := held.MetricStatus
if status == nil {
t.Fatal("websocket state must carry the live reading")
}
if status.Phase != models.MetricAlertPhaseLatched || status.Value != 78 || status.Recovery != 75 {
t.Fatalf("live status = %+v, want latched at 78 clearing at 75", status)
}
}