Files
Pulse/internal/unifiedresources/ids_test.go
rcourtman e74fea4bbc Give Proxmox guests node-independent canonical identity
A guest's unified canonical ID hashed its node-scoped source ID
(instance:node:vmid), so a live migration to another cluster node
re-minted the resource and orphaned every operator-owned row keyed by
the old ID: explicit availability check links (fail-closed by design,
the reported symptom in #1669), alert overrides, operator state, action
audits, manual links, and recovery subjects. VMIDs are unique within a
cluster, so guests now derive their canonical ID from instance+VMID
("proxmox-guest:<instance>:<vmid>") and keep it across migrations. The
guest-metadata half of #1669 was fixed separately at the metadata-store
layer.

Existing installs converge through record-declared succession: ingest
declares the retired node-scoped IDs superseded for every node the
instance currently knows (current names plus native aliases), so rows
orphaned by pre-upgrade migrations also re-key. Successions are now
recorded durably in a canonical_id_successions table, which memoizes
the re-key (steady-state rebuilds re-declare the same eras every tick
without touching SQL) and lets change-journal reads merge retired guest
eras the way pin EraIDs do for hosts. The succession re-key also covers
manual link/exclusion rows.

Availability links resolve retired canonical IDs and old-node source
triples through a registry superseded index plus guest-triple parsing
(persistence keys only, ambiguity fails closed), and the stored
LinkedResourceID re-homes to the current canonical ID on the
alert-migration cadence. Recovery subjects converge on the same
derivation via CanonicalSubjectResourceID, the mapper's registry-miss
fallback, node-independent external guest keys, and the store's startup
backfill, which also sweeps posture rows stranded under retired subject
keys. Metrics history and frontend row identity key off the node-scoped
source ID and are deliberately unchanged.
2026-08-02 20:30:38 +01:00

168 lines
5.3 KiB
Go

package unifiedresources
import "testing"
func TestSourceSpecificIDMatchesRegistryIngest(t *testing.T) {
t.Parallel()
rr := NewRegistry(nil)
sourceID := "lab:pve-a:100"
vm := Resource{
Type: ResourceTypeVM,
Name: "vm-100",
Status: StatusOnline,
}
rr.IngestRecords(SourceProxmox, []IngestRecord{
{SourceID: sourceID, Resource: vm},
})
resources := rr.ListByType(ResourceTypeVM)
if len(resources) != 1 {
t.Fatalf("expected 1 VM resource, got %d", len(resources))
}
got := resources[0].ID
want := SourceSpecificID(ResourceTypeVM, SourceProxmox, sourceID)
if got != want {
t.Fatalf("resource ID mismatch: got %q want %q", got, want)
}
}
func TestSourceSpecificIDCanonicalizesSourceIDWhitespace(t *testing.T) {
t.Parallel()
got := SourceSpecificID(ResourceTypeVM, SourceProxmox, " lab:pve-a:100 ")
want := SourceSpecificID(ResourceTypeVM, SourceProxmox, "lab:pve-a:100")
if got != want {
t.Fatalf("SourceSpecificID should trim source ID whitespace: got %q want %q", got, want)
}
}
func TestResourceIdentityPinEraIDs(t *testing.T) {
pin := ResourceIdentityPin{
CanonicalID: buildHashID(ResourceTypeAgent, "machine:machine-1"),
ResourceType: ResourceTypeAgent,
MachineID: "machine-1",
DMIUUID: "dmi-1",
ClusterName: "homelab",
Hostname: "delly.lan",
}
got := pin.EraIDs()
// The pin preserves the full dotted hostname, and eras cover both the
// full name and the short name the historical derivation hashed.
want := []string{
buildHashID(ResourceTypeAgent, "machine:machine-1"),
buildHashID(ResourceTypeAgent, "dmi:dmi-1"),
buildHashID(ResourceTypeAgent, "cluster:homelab:delly.lan"),
buildHashID(ResourceTypeAgent, "hostname:delly.lan"),
buildHashID(ResourceTypeAgent, "cluster:homelab:delly"),
buildHashID(ResourceTypeAgent, "hostname:delly"),
}
if len(got) != len(want) {
t.Fatalf("expected %d era IDs, got %d: %v", len(want), len(got), got)
}
for _, id := range want {
found := false
for _, eraID := range got {
if eraID == id {
found = true
break
}
}
if !found {
t.Fatalf("expected era set to include %q, got %v", id, got)
}
}
}
func TestResourceIdentityPinPreservesDottedPrimaryHostname(t *testing.T) {
pin := ResourceIdentityPin{
CanonicalID: "agent-custom",
ResourceType: ResourceTypeAgent,
MachineID: "machine-1",
Hostname: "Cloud.Rnd-Lax1.",
}.normalized()
if pin.Hostname != "cloud.rnd-lax1" {
t.Fatalf("normalized pin hostname = %q, want %q", pin.Hostname, "cloud.rnd-lax1")
}
}
func TestResourceIdentityPinEraIDsSkipsWeakOnlyKeys(t *testing.T) {
pin := ResourceIdentityPin{
CanonicalID: "agent-custom",
ResourceType: ResourceTypeAgent,
Hostname: "delly",
}
got := pin.EraIDs()
want := []string{"agent-custom", buildHashID(ResourceTypeAgent, "hostname:delly")}
if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
t.Fatalf("expected era IDs %v, got %v", want, got)
}
}
func TestMachineIdentityCanonicalIDMatchesRegistryMachineArm(t *testing.T) {
registry := NewRegistry(NewMemoryStore())
want := registry.canonicalIDFromIdentity(ResourceTypeAgent, ResourceIdentity{MachineID: "machine-123"})
if got := MachineIdentityCanonicalID(ResourceTypeAgent, " machine-123 "); got != want {
t.Fatalf("MachineIdentityCanonicalID = %q, want registry machine-arm ID %q", got, want)
}
}
func TestParseProxmoxGuestSourceID(t *testing.T) {
cases := []struct {
in string
instance string
node string
vmid int
ok bool
}{
{"delly:pve1:100", "delly", "pve1", 100, true},
{"cluster:with:colons:pve2:112", "cluster:with:colons", "pve2", 112, true},
{"delly:pve1:0", "", "", 0, false},
{"delly:100", "delly", "", 0, false},
{"just-a-name", "", "", 0, false},
{"vm-1a2b3c4d5e6f7788", "", "", 0, false},
{"", "", "", 0, false},
{"delly:pve1:abc", "", "", 0, false},
}
for _, tc := range cases {
instance, node, vmid, ok := ParseProxmoxGuestSourceID(tc.in)
if ok != tc.ok {
t.Fatalf("ParseProxmoxGuestSourceID(%q) ok = %v, want %v", tc.in, ok, tc.ok)
}
if !tc.ok {
continue
}
if instance != tc.instance || node != tc.node || vmid != tc.vmid {
t.Fatalf("ParseProxmoxGuestSourceID(%q) = (%q, %q, %d), want (%q, %q, %d)", tc.in, instance, node, vmid, tc.instance, tc.node, tc.vmid)
}
}
}
// ProxmoxGuestCanonicalID must mint exactly what the registry's guest
// identity arm mints, so external consumers (recovery subjects, migrations)
// derive the same node-independent ID as live ingest.
func TestProxmoxGuestCanonicalIDMatchesRegistryDerivation(t *testing.T) {
registry := NewRegistry(nil)
want := registry.canonicalIDFromIdentity(ResourceTypeVM, ResourceIdentity{ProxmoxGuestKey: ProxmoxGuestIdentityKey("delly", 100)})
if got := ProxmoxGuestCanonicalID(ResourceTypeVM, "delly", 100); got != want {
t.Fatalf("ProxmoxGuestCanonicalID = %q, want registry guest-arm ID %q", got, want)
}
}
// A VM and a container never share a VMID inside one cluster, but a VMID can
// be destroyed and recreated as the other guest type; the type prefix keeps
// those identities distinct.
func TestProxmoxGuestCanonicalIDSeparatesTypes(t *testing.T) {
vmID := ProxmoxGuestCanonicalID(ResourceTypeVM, "delly", 200)
ctID := ProxmoxGuestCanonicalID(ResourceTypeSystemContainer, "delly", 200)
if vmID == ctID {
t.Fatalf("VM and container canonical IDs must differ, both %q", vmID)
}
}