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.

Give mock mode running-backup coverage

rcourtmana64dd2f0052a
diff --git a/internal/mock/demo_scenarios.go b/internal/mock/demo_scenarios.go
index 4abf5d6b56..71a27a1ec4 100644
--- a/internal/mock/demo_scenarios.go
+++ b/internal/mock/demo_scenarios.go
@@ -310,6 +310,10 @@ func applyDemoScenarioGraph(graph *FixtureGraph, cfg MockConfig, now time.Time)
applyDemoHostScenario(&graph.State, now)
applyDemoStorageScenario(&graph.State, now)
applyDemoBackupScenario(&graph.State, vmProfiles, containerProfiles, now)
+ // In-flight backups attach to the guests' final curated backup stories,
+ // so the fixture runs after the backup scenario rebuilds that history
+ // (which also sweeps away the previous tick's fixture artifacts).
+ ensureMockBackupRunningFixture(&graph.State, now)
syncDemoConnectionHealth(&graph.State)
}
diff --git a/internal/mock/generator.go b/internal/mock/generator.go
index 129609a180..5c3c8b0197 100644
--- a/internal/mock/generator.go
+++ b/internal/mock/generator.go
@@ -2784,11 +2784,11 @@ func ensureMockDockerInLXCFixture(data *models.StateSnapshot, config MockConfig)
}
host := models.DockerHost{
- ID: sourceID,
- OS: "linux",
- KernelVersion: kernelVersion,
- Architecture: "x86_64",
- Runtime: "docker",
+ ID: sourceID,
+ OS: "linux",
+ KernelVersion: kernelVersion,
+ Architecture: "x86_64",
+ Runtime: "docker",
RuntimeVersion: version,
DockerVersion: version,
Containers: containers,
@@ -2809,6 +2809,186 @@ func ensureMockDockerInLXCFixture(data *models.StateSnapshot, config MockConfig)
}
}
+// mockBackupRunningIDPrefix marks the in-flight backup artifacts owned by
+// ensureMockBackupRunningFixture so a reapplied pass replaces them instead of
+// stacking duplicates.
+const mockBackupRunningIDPrefix = "mock-backup-running-"
+
+// ensureMockBackupRunningFixture flags a small slice of guests (roughly one
+// in twenty-five per age bucket) as having a backup running right now, and
+// gives each the evidence a live run actually produces: an in-flight PBS
+// snapshot shaped the way the PBS API lists one still being written (no size,
+// no verification, only the guest config blob in files, InProgress set) and a
+// running vzdump task with no end time. Guests are drawn from every
+// last-backup age bucket so the blue Running badge appears alongside fresh,
+// stale, overdue, and never-backed-up rows instead of clustering in one
+// story.
+//
+// It runs AFTER applyDemoBackupScenario: every guest is scenario-curated, so
+// each scenario pass drops the previous fixture artifacts along with the rest
+// of the curated guests' history and this fixture re-adds them, keeping the
+// per-tick reapplication idempotent. The fixture owns BackupInProgress for
+// the whole estate - cleared everywhere before selecting - so a guest that
+// falls out of the selection cannot keep a stale Running badge.
+func ensureMockBackupRunningFixture(state *models.StateSnapshot, now time.Time) {
+ if state == nil {
+ return
+ }
+
+ for i := range state.VMs {
+ state.VMs[i].BackupInProgress = false
+ }
+ for i := range state.Containers {
+ state.Containers[i].BackupInProgress = false
+ }
+
+ // Buckets mirror the badge stories: never backed up, fresh (<=1d),
+ // stale (<=7d), overdue (>7d).
+ bucketOf := func(lastBackup time.Time) int {
+ if lastBackup.IsZero() {
+ return 0
+ }
+ age := now.Sub(lastBackup)
+ switch {
+ case age <= 24*time.Hour:
+ return 1
+ case age <= 7*24*time.Hour:
+ return 2
+ default:
+ return 3
+ }
+ }
+
+ // The first profile cycle is the demo's hand-curated showcase: those
+ // exact rows are pinned by posture tests and walkthroughs (a failed run
+ // three hours ago, a guest with no recovery evidence at all, ...), and a
+ // backup running right now would mask exactly the states they exist to
+ // demonstrate. Later cycles repeat the same stories under suffixed names,
+ // so the fixture draws its running guests from them instead.
+ showcase := make(map[string]struct{}, len(demoVMProfiles)+len(demoContainerProfiles))
+ for _, profile := range demoVMProfiles {
+ showcase[profile.Name] = struct{}{}
+ }
+ for _, profile := range demoContainerProfiles {
+ showcase[profile.Name] = struct{}{}
+ }
+
+ type guestRef struct {
+ isVM bool
+ index int
+ }
+ var buckets [4][]guestRef
+ for i := range state.VMs {
+ vm := &state.VMs[i]
+ // Templates are never vzdump targets, and a node the scenario keeps
+ // offline cannot be running a backup task.
+ if vm.Template || isDemoOfflineProxmoxNode(vm.Node) {
+ continue
+ }
+ if _, curated := showcase[vm.Name]; curated {
+ continue
+ }
+ bucket := bucketOf(vm.LastBackup)
+ buckets[bucket] = append(buckets[bucket], guestRef{isVM: true, index: i})
+ }
+ for i := range state.Containers {
+ ct := &state.Containers[i]
+ if isDemoOfflineProxmoxNode(ct.Node) {
+ continue
+ }
+ if _, curated := showcase[ct.Name]; curated {
+ continue
+ }
+ bucket := bucketOf(ct.LastBackup)
+ buckets[bucket] = append(buckets[bucket], guestRef{index: i})
+ }
+
+ var inflightPBS []models.PBSBackup
+ var runningTasks []models.BackupTask
+ markRunning := func(kind, guestType, node, instance string, vmid int) {
+ // Stagger start times a few minutes so the running rows don't all
+ // share one timestamp; stable per guest across ticks.
+ startedAt := now.Add(-time.Duration(3+mockStableChoice(9, "backup-running-start", kind, fmt.Sprintf("%d", vmid))) * time.Minute)
+ configBlob := "qemu-server.conf.blob"
+ if kind == "ct" {
+ configBlob = "pct.conf.blob"
+ }
+ inflightPBS = append(inflightPBS, models.PBSBackup{
+ ID: fmt.Sprintf("%spbs-%s-%d", mockBackupRunningIDPrefix, kind, vmid),
+ Instance: "backup-vault",
+ Datastore: "primary-vault",
+ Namespace: "root",
+ BackupType: kind,
+ VMID: fmt.Sprintf("%d", vmid),
+ BackupTime: startedAt,
+ Size: 0,
+ InProgress: true,
+ Files: []string{configBlob},
+ Owner: "automation@pbs",
+ })
+ runningTasks = append(runningTasks, models.BackupTask{
+ ID: fmt.Sprintf("%stask-%s-%d", mockBackupRunningIDPrefix, kind, vmid),
+ Node: node,
+ Instance: instance,
+ Type: guestType,
+ VMID: vmid,
+ Status: "running",
+ StartTime: startedAt,
+ ObservedAt: now,
+ })
+ }
+
+ for _, bucket := range buckets {
+ if len(bucket) == 0 {
+ continue
+ }
+ count := len(bucket) / 25
+ if count < 1 {
+ count = 1
+ }
+ step := len(bucket) / count
+ for j := 0; j < count; j++ {
+ ref := bucket[j*step]
+ if ref.isVM {
+ vm := &state.VMs[ref.index]
+ vm.BackupInProgress = true
+ markRunning("vm", "qemu", vm.Node, vm.Instance, vm.VMID)
+ } else {
+ ct := &state.Containers[ref.index]
+ ct.BackupInProgress = true
+ markRunning("ct", "lxc", ct.Node, ct.Instance, ct.VMID)
+ }
+ }
+ }
+
+ // Prepend so the newest-first ordering applyDemoBackupScenario just
+ // established still holds: in-flight artifacts are minutes old, the
+ // curated history starts at ninety. Strip any leftover fixture entries
+ // first in case a pass runs without the scenario's curated sweep.
+ keptPBS := make([]models.PBSBackup, 0, len(inflightPBS)+len(state.PBSBackups))
+ keptPBS = append(keptPBS, inflightPBS...)
+ for _, backup := range state.PBSBackups {
+ if strings.HasPrefix(backup.ID, mockBackupRunningIDPrefix) {
+ continue
+ }
+ keptPBS = append(keptPBS, backup)
+ }
+ state.PBSBackups = keptPBS
+
+ keptTasks := make([]models.BackupTask, 0, len(runningTasks)+len(state.PVEBackups.BackupTasks))
+ keptTasks = append(keptTasks, runningTasks...)
+ for _, task := range state.PVEBackups.BackupTasks {
+ if strings.HasPrefix(task.ID, mockBackupRunningIDPrefix) {
+ continue
+ }
+ keptTasks = append(keptTasks, task)
+ }
+ state.PVEBackups.BackupTasks = keptTasks
+
+ state.Backups.PVE = state.PVEBackups
+ state.Backups.PBS = append([]models.PBSBackup(nil), state.PBSBackups...)
+}
+
// syncMockDockerInLXCHostFromGuest keeps a nested Docker host mirroring its
// parent LXC guest, matching what parseProxmoxGuestDockerInventory plus
// enrichGuestDockerReportFromContainer report for a real guest: the guest's
diff --git a/internal/mock/generator_test.go b/internal/mock/generator_test.go
index 26ee741bb7..0492ebac2c 100644
--- a/internal/mock/generator_test.go
+++ b/internal/mock/generator_test.go
@@ -1003,3 +1003,166 @@ func TestFixtureGraphIncludesDockerInLXCFixture(t *testing.T) {
}
}
}
+
+// assertMockBackupRunningFixture verifies the running-backup estate invariants
+// and returns the set of flagged guest keys ("vm-<vmid>" / "ct-<vmid>").
+func assertMockBackupRunningFixture(t *testing.T, state *models.StateSnapshot, now time.Time) map[string]bool {
+ t.Helper()
+
+ flagged := map[string]bool{}
+ neverBackedUp := false
+ backedUp := false
+ for _, vm := range state.VMs {
+ if !vm.BackupInProgress {
+ continue
+ }
+ flagged[fmt.Sprintf("vm-%d", vm.VMID)] = true
+ if vm.LastBackup.IsZero() {
+ neverBackedUp = true
+ } else {
+ backedUp = true
+ }
+ }
+ for _, ct := range state.Containers {
+ if !ct.BackupInProgress {
+ continue
+ }
+ flagged[fmt.Sprintf("ct-%d", ct.VMID)] = true
+ if ct.LastBackup.IsZero() {
+ neverBackedUp = true
+ } else {
+ backedUp = true
+ }
+ }
+ if len(flagged) == 0 {
+ t.Fatal("no guest flagged BackupInProgress")
+ }
+ if !neverBackedUp || !backedUp {
+ t.Fatalf("running backups must spread across the age distribution; never=%v backed-up=%v", neverBackedUp, backedUp)
+ }
+
+ guests := len(state.VMs) + len(state.Containers)
+ if len(flagged) > guests/10 {
+ t.Fatalf("%d of %d guests flagged running; expected a small fraction", len(flagged), guests)
+ }
+
+ // The first-cycle showcase guests are pinned to specific protection
+ // stories (failed run, no evidence at all); a running backup would mask
+ // those states, so the fixture must never pick them.
+ showcase := map[string]bool{}
+ for _, profile := range demoVMProfiles {
+ showcase[profile.Name] = true
+ }
+ for _, profile := range demoContainerProfiles {
+ showcase[profile.Name] = true
+ }
+ for _, vm := range state.VMs {
+ if vm.BackupInProgress && showcase[vm.Name] {
+ t.Fatalf("showcase guest %s flagged BackupInProgress", vm.Name)
+ }
+ }
+ for _, ct := range state.Containers {
+ if ct.BackupInProgress && showcase[ct.Name] {
+ t.Fatalf("showcase guest %s flagged BackupInProgress", ct.Name)
+ }
+ }
+
+ inflight := map[string]bool{}
+ for _, backup := range state.PBSBackups {
+ if !strings.HasPrefix(backup.ID, mockBackupRunningIDPrefix) {
+ if backup.InProgress {
+ t.Fatalf("non-fixture PBS backup %s marked InProgress", backup.ID)
+ }
+ continue
+ }
+ key := fmt.Sprintf("%s-%s", backup.BackupType, backup.VMID)
+ if inflight[key] {
+ t.Fatalf("duplicate in-flight PBS backup for %s", key)
+ }
+ inflight[key] = true
+ if !flagged[key] {
+ t.Fatalf("in-flight PBS backup %s has no flagged guest", backup.ID)
+ }
+ if !backup.InProgress || backup.Size != 0 || backup.Verified {
+ t.Fatalf("in-flight PBS backup %s must be InProgress with no size and no verification: %+v", backup.ID, backup)
+ }
+ wantBlob := "qemu-server.conf.blob"
+ if backup.BackupType == "ct" {
+ wantBlob = "pct.conf.blob"
+ }
+ if len(backup.Files) != 1 || backup.Files[0] != wantBlob {
+ t.Fatalf("in-flight PBS backup %s files = %v, want only %s", backup.ID, backup.Files, wantBlob)
+ }
+ if age := now.Sub(backup.BackupTime); age <= 0 || age > time.Hour {
+ t.Fatalf("in-flight PBS backup %s should have started minutes ago, started %s before now", backup.ID, age)
+ }
+ }
+ if len(inflight) != len(flagged) {
+ t.Fatalf("flagged guests = %d but in-flight PBS backups = %d", len(flagged), len(inflight))
+ }
+
+ runningTasks := map[string]bool{}
+ for _, task := range state.PVEBackups.BackupTasks {
+ if !strings.HasPrefix(task.ID, mockBackupRunningIDPrefix) {
+ continue
+ }
+ kind := "vm"
+ if task.Type == "lxc" {
+ kind = "ct"
+ }
+ key := fmt.Sprintf("%s-%d", kind, task.VMID)
+ if runningTasks[key] {
+ t.Fatalf("duplicate running task for %s", key)
+ }
+ runningTasks[key] = true
+ if !flagged[key] {
+ t.Fatalf("running task %s has no flagged guest", task.ID)
+ }
+ if task.Status != "running" || !task.EndTime.IsZero() {
+ t.Fatalf("fixture task %s must be running with no end time: %+v", task.ID, task)
+ }
+ if task.ObservedAt.Before(now.Add(-time.Minute)) {
+ t.Fatalf("fixture task %s ObservedAt %s is stale relative to %s", task.ID, task.ObservedAt, now)
+ }
+ }
+ if len(runningTasks) != len(flagged) {
+ t.Fatalf("flagged guests = %d but running tasks = %d", len(flagged), len(runningTasks))
+ }
+
+ // The canonical Backups projection must mirror the fixture artifacts.
+ mirrored := 0
+ for _, backup := range state.Backups.PBS {
+ if strings.HasPrefix(backup.ID, mockBackupRunningIDPrefix) {
+ mirrored++
+ }
+ }
+ if mirrored != len(inflight) {
+ t.Fatalf("state.Backups.PBS mirrors %d in-flight backups, want %d", mirrored, len(inflight))
+ }
+
+ return flagged
+}
+
+func TestFixtureGraphIncludesBackupRunningFixture(t *testing.T) {
+ cfg := DefaultConfig
+ now := time.Now()
+
+ graph := buildFixtureGraph(cfg, now)
+ first := assertMockBackupRunningFixture(t, &graph.State, now)
+
+ // The demo scenario layer reapplies on every metric tick; the fixture
+ // must re-select the same guests and replace - not duplicate - its
+ // in-flight artifacts.
+ tick := now.Add(time.Minute)
+ graph.UpdateMetrics(cfg, tick)
+ second := assertMockBackupRunningFixture(t, &graph.State, tick)
+
+ if len(first) != len(second) {
+ t.Fatalf("running-guest selection changed size across ticks: %d then %d", len(first), len(second))
+ }
+ for key := range second {
+ if !first[key] {
+ t.Fatalf("running-guest selection changed across ticks: %s not in first pass", key)
+ }
+ }
+}