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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
package unifiedresources
import (
"encoding/json"
"reflect"
"slices"
"strings"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
)
func TestGuestDiskObservationDoesNotChangeWireOrIdentity(t *testing.T) {
vm := models.VM{ID: "pve:node:105", VMID: 105, Name: "guest", Instance: "pve", Node: "node", Type: "qemu", Status: "running", LastSeen: time.Now(),
Disk: models.Disk{Total: 1000, Used: 400, Usage: 40},
DiskObservation: models.GuestDiskObservation{Source: "guest-agent", ObservedAt: time.Now().Add(-time.Minute)}}
resource, identity := resourceFromVM(vm)
wire, err := json.Marshal(resource.Proxmox)
if err != nil || strings.Contains(string(wire), "Observation") || strings.Contains(string(wire), "observedAt") {
t.Fatalf("internal disk evidence changed the served facet: %s / %v", wire, err)
}
vm.DiskObservation = models.GuestDiskObservation{}
legacyResource, legacyIdentity := resourceFromVM(vm)
legacyWire, err := json.Marshal(legacyResource.Proxmox)
if err != nil || string(wire) != string(legacyWire) || !reflect.DeepEqual(identity, legacyIdentity) {
t.Fatal("internal disk evidence changed guest identity or any public facet byte")
}
}
func TestGuestAgentEvidenceDoesNotChangeWireOrIdentity(t *testing.T) {
vm := models.VM{ID: "pve:node:105", VMID: 105, Name: "guest", Instance: "pve", Node: "node", Type: "qemu", Status: "running", LastSeen: time.Now(),
GuestAgentEvidence: models.GuestAgentEvidence{Explicit: true, ObservedAt: time.Now().Add(-time.Minute)}}
resource, identity := resourceFromVM(vm)
wire, err := json.Marshal(resource)
if err != nil {
t.Fatal(err)
}
vm.GuestAgentEvidence = models.GuestAgentEvidence{}
legacyResource, legacyIdentity := resourceFromVM(vm)
// Construction captures the wall clock independently; hold that unrelated
// timestamp fixed while comparing every public byte of these same inputs.
legacyResource.UpdatedAt = resource.UpdatedAt
legacyWire, err := json.Marshal(legacyResource)
if err != nil || string(wire) != string(legacyWire) || !reflect.DeepEqual(identity, legacyIdentity) {
t.Fatal("internal admission evidence changed guest identity or public bytes")
}
}
func TestRefreshCanonicalIdentityPrefersTargetsAndCanonicalHostData(t *testing.T) {
resource := Resource{
ID: "agent-1",
Type: ResourceTypeAgent,
Name: "Tower",
Identity: ResourceIdentity{
Hostnames: []string{"tower.local"},
MachineID: "machine-1",
},
DiscoveryTarget: &DiscoveryTarget{
ResourceType: "agent",
AgentID: "host-1",
ResourceID: "host-1",
},
MetricsTarget: &MetricsTarget{
ResourceType: "docker-host",
ResourceID: "docker-runtime-1",
},
Proxmox: &ProxmoxData{
NodeName: "pve1",
},
Agent: &AgentData{
AgentID: "host-1",
Hostname: "tower-agent.local",
},
Docker: &DockerData{
HostSourceID: "docker-runtime-1",
Hostname: "tower-docker.local",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "Tower" {
t.Fatalf("displayName = %q, want Tower", got)
}
if got := resource.Canonical.Hostname; got != "tower.local" {
t.Fatalf("hostname = %q, want tower.local", got)
}
if got := resource.Canonical.PlatformID; got != "pve1" {
t.Fatalf("platformId = %q, want pve1", got)
}
if got := resource.Canonical.PrimaryID; got != "docker-host:docker-runtime-1" {
t.Fatalf("primaryId = %q, want docker-host:docker-runtime-1", got)
}
wantAliases := []string{
"docker-host:docker-runtime-1",
"docker-runtime-1",
"host-1",
"pve1",
"tower.local",
"machine-1",
"agent-1",
"agent:host-1",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestDockerChildIdentityDoesNotClaimItsCommandHost(t *testing.T) {
for _, kind := range []ResourceType{
ResourceTypeAppContainer, ResourceTypeDockerService, ResourceTypeDockerImage,
ResourceTypeDockerVolume, ResourceTypeDockerNetwork, ResourceTypeDockerTask,
ResourceTypeDockerSwarmNode, ResourceTypeDockerSecret, ResourceTypeDockerConfig,
} {
t.Run(string(kind), func(t *testing.T) {
resource := Resource{
ID: "child-1", Type: kind, Name: "workload",
Identity: ResourceIdentity{Hostnames: []string{"workload"}},
Docker: &DockerData{HostSourceID: "native-host", AgentID: "native-host", Hostname: "command-host", NodeID: "swarm-node", NodeName: "swarm-worker"},
MetricsTarget: &MetricsTarget{ResourceType: string(kind), ResourceID: "native-child"},
DiscoveryTarget: &DiscoveryTarget{ResourceType: string(kind), ResourceID: "workload", AgentID: "native-host", Hostname: "command-host"},
}
RefreshCanonicalIdentity(&resource)
for _, alias := range resource.Canonical.Aliases {
if alias == "native-host" || alias == "agent:native-host" || alias == "command-host" || alias == "docker-host:native-host" {
t.Fatalf("child claimed parent identity %q: %+v", alias, resource.Canonical)
}
}
if resource.Canonical.PlatformID != "native-child" || resource.Canonical.Hostname != "workload" {
t.Fatalf("child presentation borrowed parent placement: %+v", resource.Canonical)
}
for _, reference := range []string{"child-1", "native-child", "workload", string(kind) + ":native-child"} {
if !slices.Contains(resource.Canonical.Aliases, reference) {
t.Fatalf("child lost its own reference %q: %+v", reference, resource.Canonical)
}
}
// Losing discovery detail must not turn the child into its parent.
resource.MetricsTarget = nil
resource.DiscoveryTarget = &DiscoveryTarget{ResourceType: "agent", ResourceID: "native-host", AgentID: "native-host"}
resource.Identity = ResourceIdentity{}
RefreshCanonicalIdentity(&resource)
if resource.Canonical.PrimaryID == "agent:native-host" || resource.Canonical.PrimaryID == "docker-host:native-host" || resource.Canonical.Hostname != "" {
t.Fatalf("incomplete child became its parent: %+v", resource.Canonical)
}
if slices.Contains(resource.Canonical.Aliases, "native-host") {
t.Fatalf("parent discovery target became a child alias: %+v", resource.Canonical)
}
})
}
}
func TestDockerCollectionModeDoesNotChangeCanonicalIdentity(t *testing.T) {
resource := Resource{
ID: "docker-host-1",
Type: ResourceTypeAgent,
Name: "docker-one",
Docker: &DockerData{
HostSourceID: "docker-source-1",
Hostname: "docker-one.local",
CollectionMode: "typed-helper-summary",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatal("canonical identity is nil")
}
want := *resource.Canonical
resource.Docker.CollectionMode = ""
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil ||
resource.Canonical.PrimaryID != want.PrimaryID ||
resource.Canonical.Hostname != want.Hostname ||
!reflect.DeepEqual(resource.Canonical.Aliases, want.Aliases) {
t.Fatalf("collection completeness changed canonical identity: got %+v, want %+v", resource.Canonical, want)
}
}
func TestPBSLinkedAgentDoesNotChangeServiceCanonicalIdentityOrTarget(t *testing.T) {
resource := Resource{
ID: "pbs-service",
Type: ResourceTypePBS,
Name: "Backup connection",
PBS: &PBSData{InstanceID: "pbs-service", Hostname: "10.0.0.5"},
MetricsTarget: &MetricsTarget{ResourceType: "agent", ResourceID: "pbs-service"},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatal("PBS canonical identity is nil")
}
want := *resource.Canonical
wantTarget := *resource.MetricsTarget
resource.PBS.LinkedAgentID = "agent-uuid"
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil || !reflect.DeepEqual(*resource.Canonical, want) {
t.Fatalf("presentation-only host link changed PBS service identity: got %+v, want %+v", resource.Canonical, want)
}
if resource.MetricsTarget == nil || *resource.MetricsTarget != wantTarget {
t.Fatalf("presentation-only host link changed PBS service target: got %+v, want %+v", resource.MetricsTarget, wantTarget)
}
encoded, err := json.Marshal(resource)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(encoded), `"linkedAgentId":"agent-uuid"`) {
t.Fatalf("PBS host link missing from resource JSON: %s", encoded)
}
}
func TestRefreshCanonicalIdentityKeepsProxmoxPresentationSeparateFromNativeAliases(t *testing.T) {
resource := Resource{
ID: "production-pve1",
Type: ResourceTypeAgent,
Name: "Render East",
Identity: ResourceIdentity{
Hostnames: []string{"pve1", "pve-old"},
},
Proxmox: &ProxmoxData{
Instance: "production-api",
NodeIdentity: "production-pve1",
NodeName: "pve1",
NodeAliases: []string{"pve-old"},
NodeDisplayName: "Render East",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil ||
resource.Canonical.DisplayName != "Render East" ||
resource.Canonical.PrimaryID == "Render East" {
t.Fatalf("display override became canonical identity: %+v", resource.Canonical)
}
for _, want := range []string{"production-pve1", "pve1", "pve-old"} {
found := false
for _, alias := range resource.Canonical.Aliases {
if alias == want {
found = true
break
}
}
if !found {
t.Fatalf("canonical aliases %v missing native identity %q", resource.Canonical.Aliases, want)
}
}
}
func TestRefreshCanonicalIdentityKeepsParentNodeIdentityOffProxmoxChildren(t *testing.T) {
parentNode := &ProxmoxData{
Instance: "homelab",
NodeIdentity: "homelab-minipc",
NodeName: "minipc",
NodeAliases: []string{"minipc-old"},
}
for _, resourceType := range []ResourceType{ResourceTypeVM, ResourceTypeSystemContainer, ResourceTypeStorage} {
resource := Resource{
ID: string(resourceType) + "-1",
Type: resourceType,
Name: "cloudflared",
Proxmox: &ProxmoxData{
SourceID: "homelab:minipc:104",
Instance: parentNode.Instance,
NodeIdentity: parentNode.NodeIdentity,
NodeName: parentNode.NodeName,
NodeAliases: append([]string(nil), parentNode.NodeAliases...),
},
MetricsTarget: &MetricsTarget{ResourceType: string(resourceType), ResourceID: "homelab:minipc:104"},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("%s: canonical identity missing", resourceType)
}
for _, alias := range resource.Canonical.Aliases {
if alias == "homelab-minipc" || alias == "minipc-old" {
t.Fatalf("%s carries parent node identity %q as an alias, so node alerts attach to it: %v",
resourceType, alias, resource.Canonical.Aliases)
}
}
}
}
func TestProxmoxDiskAlertAliasIsScopedAndNotPrimaryIdentity(t *testing.T) {
resource := Resource{
ID: "physical-disk-1", Type: ResourceTypePhysicalDisk,
Proxmox: &ProxmoxData{Instance: "pve-a", NodeName: "node-1"},
PhysicalDisk: &PhysicalDiskMeta{DevPath: "/dev/sda"},
}
assertAlias := func(want string, present bool) {
t.Helper()
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil || resource.Canonical.PrimaryID != resource.ID {
t.Fatalf("disk alert alias changed primary identity: %+v", resource.Canonical)
}
found := false
for _, alias := range resource.Canonical.Aliases {
found = found || alias == want
}
if found != present {
t.Fatalf("alias %q present = %v, want %v; aliases = %v", want, found, present, resource.Canonical.Aliases)
}
}
alertID := ProxmoxPhysicalDiskAlertResourceID("pve-a", "node-1", "/dev/sda")
assertAlias(alertID, true)
resource.Proxmox.NodeName = "node-2"
assertAlias(alertID, false)
assertAlias(ProxmoxPhysicalDiskAlertResourceID("pve-a", "node-2", "/dev/sda"), true)
resource.Type = ResourceTypeStorage
assertAlias(ProxmoxPhysicalDiskAlertResourceID("pve-a", "node-2", "/dev/sda"), false)
resource.Type = ResourceTypePhysicalDisk
resource.PhysicalDisk.DevPath = ""
assertAlias(ProxmoxPhysicalDiskAlertResourceID("pve-a", "node-2", ""), false)
}
func TestProxmoxPhysicalDiskAlertResourceIDPreservesEmptyDevicePath(t *testing.T) {
if got, want := ProxmoxPhysicalDiskAlertResourceID("pve-a", "node-1", ""), "pve-a:node-1:disk:"; got != want {
t.Fatalf("empty device path reference = %q, want %q", got, want)
}
if got, want := ProxmoxPhysicalDiskAlertResourceID("pve-a", "node-1", "/"), "pve-a:node-1:disk:root"; got != want {
t.Fatalf("root device path reference = %q, want %q", got, want)
}
}
func TestUnavailableMemoryFacetsDoNotChangeCanonicalIdentity(t *testing.T) {
total := int64(8 << 30)
resource := Resource{
ID: "vm:proxmox-a:1501",
Type: ResourceTypeVM,
Name: "linux-guest",
Identity: ResourceIdentity{
Hostnames: []string{"linux-guest.example"},
},
Proxmox: &ProxmoxData{
Instance: "proxmox-a",
VMID: 1501,
},
Agent: &AgentData{
AgentID: "agent-1501",
},
Docker: &DockerData{
HostSourceID: "docker-1501",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatal("canonical identity is nil")
}
wantPrimaryID := resource.Canonical.PrimaryID
wantHostname := resource.Canonical.Hostname
resource.Proxmox.Memory = &models.Memory{
Total: total,
UsageUnavailable: true,
}
resource.Agent.Memory = &AgentMemoryMeta{
Total: total,
UsageUnavailable: true,
}
resource.Docker.Memory = &AgentMemoryMeta{
Total: total,
UsageUnavailable: true,
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil ||
resource.Canonical.PrimaryID != wantPrimaryID ||
resource.Canonical.Hostname != wantHostname {
t.Fatalf(
"canonical identity = %+v, want primary ID %q and hostname %q unchanged",
resource.Canonical,
wantPrimaryID,
wantHostname,
)
}
}
func TestVMGuestAgentIncidentUsesCanonicalSourceID(t *testing.T) {
vm := models.VM{
ID: "cluster-a:pve-a:101",
Name: "win-app",
Node: "pve-a",
Instance: "cluster-a",
VMID: 101,
Status: "running",
Type: "qemu",
GuestAgentStatus: "expected-unreachable",
GuestAgentExpected: true,
}
resource, _ := resourceFromVM(vm)
if len(resource.Incidents) != 1 {
t.Fatalf("expected one incident, got %+v", resource.Incidents)
}
if got, want := resource.Incidents[0].NativeID, proxmoxVMSourceID(vm); got != want {
t.Fatalf("incident NativeID = %q, want %q", got, want)
}
}
func TestRefreshCanonicalIdentityFallsBackWithoutTargets(t *testing.T) {
resource := Resource{
ID: "pbs-1",
Type: ResourceTypePBS,
Name: "",
PBS: &PBSData{
InstanceID: "pbs-main",
Hostname: "pbs.example",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "pbs.example" {
t.Fatalf("displayName = %q, want pbs.example", got)
}
if got := resource.Canonical.Hostname; got != "pbs.example" {
t.Fatalf("hostname = %q, want pbs.example", got)
}
if got := resource.Canonical.PlatformID; got != "pbs.example" {
t.Fatalf("platformId = %q, want pbs.example", got)
}
if got := resource.Canonical.PrimaryID; got != "pbs:pbs-main" {
t.Fatalf("primaryId = %q, want pbs:pbs-main", got)
}
}
func TestRefreshCanonicalIdentityUsesPMGInstanceIdentity(t *testing.T) {
resource := Resource{
ID: "pmg-resource-1",
Type: ResourceTypePMG,
Name: "",
PMG: &PMGData{
InstanceID: "pmg-main",
Hostname: "mail.example",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "mail.example" {
t.Fatalf("displayName = %q, want mail.example", got)
}
if got := resource.Canonical.Hostname; got != "mail.example" {
t.Fatalf("hostname = %q, want mail.example", got)
}
if got := resource.Canonical.PrimaryID; got != "pmg:pmg-main" {
t.Fatalf("primaryId = %q, want pmg:pmg-main", got)
}
}
func TestRefreshCanonicalIdentityUsesAvailabilityTargetIdentity(t *testing.T) {
resource := Resource{
ID: "availability:energy-meter",
Type: ResourceTypeNetworkEndpoint,
Name: "Energy meter",
Availability: &AvailabilityData{
TargetID: "energy-meter",
Address: "192.0.2.44",
Protocol: "icmp",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "Energy meter" {
t.Fatalf("displayName = %q, want Energy meter", got)
}
if got := resource.Canonical.Hostname; got != "192.0.2.44" {
t.Fatalf("hostname = %q, want 192.0.2.44", got)
}
if got := resource.Canonical.PlatformID; got != "192.0.2.44" {
t.Fatalf("platformId = %q, want 192.0.2.44", got)
}
if got := resource.Canonical.PrimaryID; got != "availability:energy-meter" {
t.Fatalf("primaryId = %q, want availability:energy-meter", got)
}
wantAliases := []string{
"availability:energy-meter",
"energy-meter",
"192.0.2.44",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalIdentityUsesDockerSwarmNodeIdentity(t *testing.T) {
resource := Resource{
ID: "docker-swarm-node-resource-1",
Type: ResourceTypeDockerSwarmNode,
Name: "manager-1",
Identity: ResourceIdentity{
Hostnames: []string{"manager-1"},
},
Docker: &DockerData{
HostSourceID: "docker-host-1",
Hostname: "docker-host",
NodeID: "node-1",
NodeName: "manager-1",
NodeRole: "manager",
Swarm: &DockerSwarmInfo{
ClusterID: "cluster-1",
ClusterName: "prod-swarm",
},
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "manager-1" {
t.Fatalf("displayName = %q, want manager-1", got)
}
if got := resource.Canonical.PrimaryID; got != "docker-swarm-node:cluster-1:node-1" {
t.Fatalf("primaryId = %q, want docker-swarm-node:cluster-1:node-1", got)
}
wantAliases := []string{
"docker-swarm-node:cluster-1:node-1",
"node-1",
"manager-1",
"docker-swarm-node-resource-1",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalIdentityPrefersProxmoxNodePrimaryIDForAgentResources(t *testing.T) {
resource := Resource{
ID: "agent-1",
Type: ResourceTypeAgent,
Name: "pve1",
Proxmox: &ProxmoxData{
SourceID: "instance-pve1",
NodeName: "pve1",
},
DiscoveryTarget: &DiscoveryTarget{
ResourceType: "agent",
ResourceID: "host-1",
AgentID: "host-1",
},
Agent: &AgentData{
AgentID: "host-1",
Hostname: "pve1",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.PrimaryID; got != "node:instance-pve1" {
t.Fatalf("primaryId = %q, want node:instance-pve1", got)
}
}
func TestRefreshCanonicalIdentityKeepsAgentHostProfileOutOfCanonicalIDs(t *testing.T) {
resource := Resource{
ID: "agent:tower",
Type: ResourceTypeAgent,
Name: "tower",
Agent: &AgentData{
AgentID: "host-1",
Hostname: "tower",
Platform: "linux",
HostProfile: "unraid",
OSName: "Unraid",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.PlatformID; got != "tower" {
t.Fatalf("platformId = %q, want tower", got)
}
if got := resource.Canonical.PrimaryID; got != "agent:host-1" {
t.Fatalf("primaryId = %q, want agent:host-1", got)
}
for _, alias := range resource.Canonical.Aliases {
if alias == "unraid" {
t.Fatalf("agent host profile leaked into canonical aliases: %+v", resource.Canonical.Aliases)
}
}
}
func TestRefreshCanonicalIdentityCanonicalizesTargetResourceTypeAliases(t *testing.T) {
resource := Resource{
ID: "agent-1",
Type: ResourceTypeAgent,
Name: "Tower",
MetricsTarget: &MetricsTarget{
ResourceType: " HOST ",
ResourceID: " host-1 ",
},
DiscoveryTarget: &DiscoveryTarget{
ResourceType: "docker_host",
ResourceID: "docker-runtime-1",
AgentID: "host-1",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.PrimaryID; got != "host:host-1" {
t.Fatalf("primaryId = %q, want host:host-1", got)
}
wantAliases := []string{
"host:host-1",
"host-1",
"docker-runtime-1",
"Tower",
"agent-1",
"agent:host-1",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalIdentityFeedsPolicyMetadata(t *testing.T) {
resource := Resource{
ID: "pbs-1",
Type: ResourceTypePBS,
Name: "primary-backup",
PBS: &PBSData{
InstanceID: "pbs-main",
Hostname: "backup.internal",
},
Identity: ResourceIdentity{
IPAddresses: []string{"10.0.0.20"},
},
}
RefreshCanonicalIdentity(&resource)
RefreshPolicyMetadata(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if resource.Policy == nil {
t.Fatalf("expected policy metadata")
}
if got := resource.Policy.Sensitivity; got != ResourceSensitivitySensitive {
t.Fatalf("policy sensitivity = %q, want %q", got, ResourceSensitivitySensitive)
}
wantRedactions := []ResourceRedactionHint{
ResourceRedactionHostname,
ResourceRedactionIPAddress,
ResourceRedactionPlatformID,
ResourceRedactionAlias,
}
if len(resource.Policy.Routing.Redact) != len(wantRedactions) {
t.Fatalf("redaction hint len = %d, want %d", len(resource.Policy.Routing.Redact), len(wantRedactions))
}
for i, want := range wantRedactions {
if got := resource.Policy.Routing.Redact[i]; got != want {
t.Fatalf("redaction[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalMetadataDerivesRuntimePlatformScopes(t *testing.T) {
resource := Resource{
ID: "docker-container-frigate-141",
Type: ResourceTypeAppContainer,
Name: "frigate",
Sources: []DataSource{SourceDocker},
Docker: &DockerData{
HostSourceID: "proxmox-lxc-docker:pve-a:node-a:141",
ContainerID: "frigate",
Runtime: "docker",
},
}
RefreshCanonicalMetadata(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.PlatformScopes; !reflect.DeepEqual(got, []string{"proxmox-pve", "docker"}) {
t.Fatalf("platform scopes = %#v, want proxmox-pve + docker", got)
}
}
func TestRefreshCanonicalIdentityIgnoresProxmoxPoolAsPlatformIdentity(t *testing.T) {
resource := Resource{
ID: "vm-101",
Type: ResourceTypeVM,
Name: "app-vm",
Proxmox: &ProxmoxData{
NodeName: "pve-a",
Pool: "prod-vms",
VMID: 101,
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.PlatformID; got != "pve-a" {
t.Fatalf("platformId = %q, want pve-a", got)
}
for _, alias := range resource.Canonical.Aliases {
if alias == "prod-vms" {
t.Fatalf("expected pool name not to become canonical identity alias: %+v", resource.Canonical.Aliases)
}
}
}
func TestRefreshCanonicalIdentityScopesVMwarePrimaryIDToConnection(t *testing.T) {
resource := Resource{
ID: "vm-1",
Type: ResourceTypeVM,
Name: "db-vm",
VMware: &VMwareData{
ConnectionID: "vc-1",
ManagedObjectID: "vm-101",
EntityType: "vm",
HostUUID: "uuid-host-1",
},
Identity: ResourceIdentity{
Hostnames: []string{"db-vm.lab.local"},
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.PrimaryID; got != "vmware:vc-1:vm:vm-101" {
t.Fatalf("primaryId = %q, want vmware:vc-1:vm:vm-101", got)
}
wantAliases := []string{
"vmware:vc-1:vm:vm-101",
"vm-101",
"uuid-host-1",
"db-vm",
"db-vm.lab.local",
"vm-1",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalIdentityScopesVMwareManagedObjectsByConnection(t *testing.T) {
resource := Resource{
ID: "vmware-host-1",
Type: ResourceTypeAgent,
Name: "esxi-01.lab.local",
Identity: ResourceIdentity{
Hostnames: []string{"esxi-01.lab.local"},
},
VMware: &VMwareData{
ConnectionID: "vc-1",
ConnectionName: "Lab VC",
VCenterHost: "vc.lab.local",
ManagedObjectID: "host-101",
EntityType: "host",
HostUUID: "uuid-host-1",
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "esxi-01.lab.local" {
t.Fatalf("displayName = %q, want esxi-01.lab.local", got)
}
if got := resource.Canonical.Hostname; got != "esxi-01.lab.local" {
t.Fatalf("hostname = %q, want esxi-01.lab.local", got)
}
if got := resource.Canonical.PlatformID; got != "esxi-01.lab.local" {
t.Fatalf("platformId = %q, want esxi-01.lab.local", got)
}
if got := resource.Canonical.PrimaryID; got != "vmware:vc-1:host:host-101" {
t.Fatalf("primaryId = %q, want vmware:vc-1:host:host-101", got)
}
wantAliases := []string{
"vmware:vc-1:host:host-101",
"host-101",
"uuid-host-1",
"esxi-01.lab.local",
"vmware-host-1",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalIdentityIgnoresVMwarePlacementDetailAliases(t *testing.T) {
scsiBus := int64(0)
scsiUnit := int64(1)
capacityBytes := int64(107374182400)
autoUpdateSupported := true
installAttempts := int64(1)
toolsVersionNumber := int64(12352)
guestRebootRequested := true
bootDelayMilliseconds := int64(5000)
bootRetry := true
coresPerSocket := int64(2)
memoryHotAddLimitMiB := int64(16384)
resource := Resource{
ID: "vmware-vm-1",
Type: ResourceTypeVM,
Name: "db-vm",
Identity: ResourceIdentity{
Hostnames: []string{"db-vm.guest.lab.local"},
MachineID: "vm-instance-uuid-1",
},
VMware: &VMwareData{
ConnectionID: "vc-1",
ManagedObjectID: "vm-101",
EntityType: "vm",
DatacenterName: "DC-1",
ComputeResourceName: "Compute-A",
ClusterName: "Cluster-A",
FolderName: "Production",
ResourcePoolName: "Prod-VMs",
RuntimeHostName: "esxi-01.lab.local",
DatastoreNames: []string{"vmfs-prod-01", "vmfs-prod-02"},
GuestHostname: "db-vm.guest.lab.local",
GuestIPAddresses: []string{"10.0.0.10", "10.0.0.11"},
InstanceUUID: "vm-instance-uuid-1",
BIOSUUID: "vm-bios-uuid-1",
CurrentSnapshotID: "snapshot-202",
NetworkAdapters: []VMwareNetworkAdapterData{{
NIC: "4000",
Label: "Network adapter 1",
Type: "VMXNET3",
MACType: "GENERATED",
MACAddress: "00:50:56:aa:bb:cc",
BackingType: "STANDARD_PORTGROUP",
NetworkID: "network-101",
NetworkName: "VM Network",
State: "CONNECTED",
StartConnected: true,
AllowGuestControl: true,
}},
VirtualDisks: []VMwareVirtualDiskData{{
Disk: "2000",
Label: "Hard disk 1",
Type: "SCSI",
SCSIBus: &scsiBus,
SCSIUnit: &scsiUnit,
BackingType: "VMDK_FILE",
VMDKFile: "[vmfs-prod-01] db-vm/db-vm.vmdk",
DatastoreName: "vmfs-prod-01",
CapacityBytes: &capacityBytes,
}},
Tools: &VMwareToolsData{
AutoUpdateSupported: &autoUpdateSupported,
InstallAttemptCount: &installAttempts,
ErrorMessage: "Tools upgrade failed",
VersionNumber: &toolsVersionNumber,
Version: "12.4.0",
UpgradePolicy: "MANUAL",
VersionStatus: "CURRENT",
InstallType: "OPEN_VM_TOOLS",
RunState: "RUNNING",
GuestRebootRequested: &guestRebootRequested,
GuestRebootComponents: []string{"drivers"},
},
Hardware: &VMwareVMHardwareData{
GuestOS: "UBUNTU_64",
Version: "VMX_20",
UpgradePolicy: "AFTER_CLEAN_SHUTDOWN",
UpgradeVersion: "VMX_21",
UpgradeStatus: "PENDING",
UpgradeErrorMessage: "Virtual hardware upgrade failed",
BootType: "EFI",
BootNetworkProtocol: "IPV4",
BootDelayMilliseconds: &bootDelayMilliseconds,
BootRetry: &bootRetry,
BootDevices: []VMwareBootDeviceData{{Type: "DISK", Disks: []string{"2000"}}},
CPUCoresPerSocket: &coresPerSocket,
MemoryHotAddLimitMiB: &memoryHotAddLimitMiB,
},
SnapshotTree: []VMwareSnapshotData{{
Snapshot: "snapshot-201",
Name: "pre-upgrade",
Description: "Before application upgrade",
State: "poweredOn",
Quiesced: true,
Children: []VMwareSnapshotData{{
Snapshot: "snapshot-202",
Name: "post-upgrade",
Current: true,
}},
}},
},
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.PrimaryID; got != "vmware:vc-1:vm:vm-101" {
t.Fatalf("primaryId = %q, want vmware:vc-1:vm:vm-101", got)
}
wantAliases := []string{
"vmware:vc-1:vm:vm-101",
"vm-101",
"db-vm",
"db-vm.guest.lab.local",
"vm-instance-uuid-1",
"vmware-vm-1",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
disallowedAliases := []string{
"DC-1",
"Compute-A",
"Cluster-A",
"Production",
"Prod-VMs",
"esxi-01.lab.local",
"vmfs-prod-01",
"vmfs-prod-02",
"10.0.0.10",
"10.0.0.11",
"vm-bios-uuid-1",
"snapshot-201",
"snapshot-202",
"pre-upgrade",
"post-upgrade",
"Before application upgrade",
"poweredOn",
"4000",
"Network adapter 1",
"VMXNET3",
"GENERATED",
"00:50:56:aa:bb:cc",
"STANDARD_PORTGROUP",
"network-101",
"VM Network",
"CONNECTED",
"2000",
"Hard disk 1",
"SCSI",
"VMDK_FILE",
"[vmfs-prod-01] db-vm/db-vm.vmdk",
"Tools upgrade failed",
"12.4.0",
"MANUAL",
"CURRENT",
"OPEN_VM_TOOLS",
"RUNNING",
"drivers",
"UBUNTU_64",
"VMX_20",
"AFTER_CLEAN_SHUTDOWN",
"VMX_21",
"PENDING",
"Virtual hardware upgrade failed",
"EFI",
"IPV4",
}
for _, disallowed := range disallowedAliases {
for _, alias := range resource.Canonical.Aliases {
if alias == disallowed {
t.Fatalf("expected VMware topology/detail value %q not to become canonical alias: %+v", disallowed, resource.Canonical.Aliases)
}
}
}
}
func TestRefreshCanonicalIdentityPreservesTrueNASAppContainerIdentity(t *testing.T) {
resource := Resource{
ID: "truenas-app-nextcloud",
Type: ResourceTypeAppContainer,
Name: "Nextcloud",
TrueNAS: &TrueNASData{
Hostname: "truenas-a.local",
App: &TrueNASApp{
ID: "nextcloud",
Name: "Nextcloud",
},
},
MetricsTarget: &MetricsTarget{
ResourceType: string(ResourceTypeAppContainer),
ResourceID: "nextcloud",
},
}
if got := ContractResourceType(resource); got != ResourceTypeAppContainer {
t.Fatalf("ContractResourceType = %q, want %q", got, ResourceTypeAppContainer)
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "Nextcloud" {
t.Fatalf("displayName = %q, want Nextcloud", got)
}
if got := resource.Canonical.Hostname; got != "truenas-a.local" {
t.Fatalf("hostname = %q, want truenas-a.local", got)
}
if got := resource.Canonical.PlatformID; got != "truenas-a.local" {
t.Fatalf("platformId = %q, want truenas-a.local", got)
}
if got := resource.Canonical.PrimaryID; got != "app-container:nextcloud" {
t.Fatalf("primaryId = %q, want app-container:nextcloud", got)
}
wantAliases := []string{
"app-container:nextcloud",
"nextcloud",
"truenas-a.local",
"truenas-app-nextcloud",
}
if len(resource.Canonical.Aliases) != len(wantAliases) {
t.Fatalf("aliases len = %d, want %d (%v)", len(resource.Canonical.Aliases), len(wantAliases), resource.Canonical.Aliases)
}
for i, want := range wantAliases {
if got := resource.Canonical.Aliases[i]; got != want {
t.Fatalf("alias[%d] = %q, want %q", i, got, want)
}
}
}
func TestRefreshCanonicalIdentityPreservesTrueNASVMIdentity(t *testing.T) {
resource := Resource{
ID: "truenas-vm-windows-lab",
Type: ResourceTypeVM,
Name: "windows-lab",
TrueNAS: &TrueNASData{
Hostname: "truenas-a.local",
VM: &TrueNASVM{
ID: "42",
Name: "windows-lab",
UUID: "vm-uuid-1",
},
},
MetricsTarget: &MetricsTarget{
ResourceType: string(ResourceTypeVM),
ResourceID: "42",
},
}
if got := ContractResourceType(resource); got != ResourceTypeVM {
t.Fatalf("ContractResourceType = %q, want %q", got, ResourceTypeVM)
}
RefreshCanonicalIdentity(&resource)
if resource.Canonical == nil {
t.Fatalf("expected canonical identity")
}
if got := resource.Canonical.DisplayName; got != "windows-lab" {
t.Fatalf("displayName = %q, want windows-lab", got)
}
if got := resource.Canonical.Hostname; got != "truenas-a.local" {
t.Fatalf("hostname = %q, want truenas-a.local", got)
}
if got := resource.Canonical.PlatformID; got != "truenas-a.local" {
t.Fatalf("platformId = %q, want truenas-a.local", got)
}
if got := resource.Canonical.PrimaryID; got != "vm:42" {
t.Fatalf("primaryId = %q, want vm:42", got)
}
}
// TestStorageTopologyAndVDevLayoutAreDistinctIdentityFields pins the separation
// restored after 599c8e634. StorageMeta.Topology is the closed discriminator
// vocabulary consumers use to identify a resource ("pool", "dataset", "array",
// "datastore"); the ZFS vdev layout is presentation and lives in VDevLayout.
// Collapsing the two, or dropping either through a clone, silently changes
// resource identity for every storage consumer.
func TestStorageTopologyAndVDevLayoutAreDistinctIdentityFields(t *testing.T) {
meta := &StorageMeta{
Type: "zfs-pool",
Platform: "truenas",
Topology: "pool",
VDevLayout: "mirror+special",
AliasIDs: []string{"legacy-pool-id"},
}
cloned := cloneStorageMeta(meta)
if cloned == nil {
t.Fatal("cloneStorageMeta returned nil")
}
if cloned.Topology != "pool" {
t.Fatalf("cloned topology = %q, want \"pool\"", cloned.Topology)
}
if cloned.VDevLayout != "mirror+special" {
t.Fatalf("cloned vdev layout = %q, want \"mirror+special\"", cloned.VDevLayout)
}
if len(cloned.AliasIDs) != 1 || cloned.AliasIDs[0] != "legacy-pool-id" {
t.Fatalf("cloned alias IDs = %v, want compatibility identity", cloned.AliasIDs)
}
// A pool with no reported vdevs still carries the discriminator, and the
// layout stays absent rather than being backfilled from it.
bare := cloneStorageMeta(&StorageMeta{Topology: "pool"})
if bare.Topology != "pool" || bare.VDevLayout != "" {
t.Fatalf("bare pool meta = %+v, want topology \"pool\" and empty layout", bare)
}
encoded, err := json.Marshal(meta)
if err != nil {
t.Fatalf("marshal storage meta: %v", err)
}
var decoded map[string]any
if err := json.Unmarshal(encoded, &decoded); err != nil {
t.Fatalf("unmarshal storage meta: %v", err)
}
if decoded["topology"] != "pool" {
t.Fatalf("wire topology = %v, want \"pool\"", decoded["topology"])
}
if decoded["vdevLayout"] != "mirror+special" {
t.Fatalf("wire vdevLayout = %v, want \"mirror+special\"", decoded["vdevLayout"])
}
if aliases, ok := decoded["aliasIds"].([]any); !ok || len(aliases) != 1 || aliases[0] != "legacy-pool-id" {
t.Fatalf("wire aliasIds = %v, want [legacy-pool-id]", decoded["aliasIds"])
}
}