lifecycle diagnostics
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-09 14:27:41 +11:00
parent 59b16db04f
commit 6dcbb9caef
8 changed files with 498 additions and 134 deletions

View File

@@ -278,6 +278,60 @@ VALUES (?,?,?,?,?,?,?,?,?,?)
}
}
func TestFetchVmLifecycleIgnoresStaleDeletionFromHourlyCache(t *testing.T) {
ctx := context.Background()
dbConn := newTestSQLiteDB(t)
if err := EnsureVmHourlyStats(ctx, dbConn); err != nil {
t.Fatalf("failed to ensure vm_hourly_stats: %v", err)
}
insertSQL := `
INSERT INTO vm_hourly_stats (
"SnapshotTime","Vcenter","VmId","VmUuid","Name","CreationTime","DeletionTime","ResourcePool",
"Datacenter","Cluster","Folder","ProvisionedDisk","VcpuCount","RamGB","IsTemplate","PoweredOn","SrmPlaceholder"
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
`
// First row carries an old deletion marker, later row proves VM is still present.
rows := [][]interface{}{
{int64(1700000000), "vc-a", "vm-1", "uuid-1", "demo-vm", int64(1699999000), int64(1700003600), "Tin", "dc", "cluster", "folder", 100.0, int64(2), int64(4), "FALSE", "TRUE", "FALSE"},
{int64(1700100000), "vc-a", "vm-1", "uuid-1", "demo-vm", int64(1699999000), int64(0), "Gold", "dc", "cluster", "folder", 120.0, int64(4), int64(8), "FALSE", "TRUE", "FALSE"},
}
for _, args := range rows {
if _, err := dbConn.ExecContext(ctx, insertSQL, args...); err != nil {
t.Fatalf("failed to insert hourly cache row: %v", err)
}
}
lifecycle, err := FetchVmLifecycle(ctx, dbConn, "vm-1", "", "")
if err != nil {
t.Fatalf("FetchVmLifecycle failed: %v", err)
}
if lifecycle.LastSeen != 1700100000 {
t.Fatalf("expected LastSeen=1700100000, got %d", lifecycle.LastSeen)
}
if lifecycle.DeletionTime != 0 {
t.Fatalf("expected stale deletion to be ignored, got %d", lifecycle.DeletionTime)
}
lifecycleDiag, diag, err := FetchVmLifecycleWithDiagnostics(ctx, dbConn, "vm-1", "", "")
if err != nil {
t.Fatalf("FetchVmLifecycleWithDiagnostics failed: %v", err)
}
if lifecycleDiag.DeletionTime != 0 {
t.Fatalf("expected stale deletion to be ignored in diagnostics path, got %d", lifecycleDiag.DeletionTime)
}
if !diag.HourlyCache.StaleDeletionIgnored {
t.Fatalf("expected hourly cache diagnostics to flag stale deletion ignore, got %#v", diag.HourlyCache)
}
if diag.HourlyCache.DeletionMax != 1700003600 {
t.Fatalf("expected hourly cache deletion max 1700003600, got %d", diag.HourlyCache.DeletionMax)
}
if diag.FinalLifecycle.LastSeen != 1700100000 || diag.FinalLifecycle.DeletionTime != 0 {
t.Fatalf("unexpected final diagnostics lifecycle: %#v", diag.FinalLifecycle)
}
}
func TestParseHourlySnapshotUnix(t *testing.T) {
cases := []struct {
table string