mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 10:33:40 +01:00
A native connection has an agent-shaped History identity without a Pulse Agent report. Require genuine agent evidence in the same canonical resource snapshot, before presentation inference. Retain remembered agents and the native provider without turning its metrics identity into a host control. Change-source: pulse-maintainer
540 lines
17 KiB
Go
540 lines
17 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/truenas"
|
|
unifiedresources "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
)
|
|
|
|
func TestBuildConnectedInfrastructure_AppliesIgnoreStateToActiveSurfaces(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "tower-resource",
|
|
Name: "Tower",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Agent: &unifiedresources.AgentData{
|
|
AgentID: "tower-agent",
|
|
AgentVersion: "1.2.3",
|
|
Hostname: "tower.local",
|
|
Platform: "linux",
|
|
},
|
|
Docker: &unifiedresources.DockerData{
|
|
HostSourceID: "tower-docker",
|
|
AgentID: "tower-agent",
|
|
},
|
|
PBS: &unifiedresources.PBSData{
|
|
Version: "3.4.1",
|
|
},
|
|
Identity: unifiedresources.ResourceIdentity{
|
|
Hostnames: []string{"tower.local"},
|
|
},
|
|
},
|
|
}, models.StateSnapshot{
|
|
RemovedDockerHosts: []models.RemovedDockerHost{
|
|
{ID: "tower-docker", RemovedAt: now},
|
|
},
|
|
})
|
|
|
|
if len(items) != 2 {
|
|
t.Fatalf("expected 2 connected infrastructure items, got %d", len(items))
|
|
}
|
|
|
|
var active *models.ConnectedInfrastructureItemFrontend
|
|
var ignored *models.ConnectedInfrastructureItemFrontend
|
|
for i := range items {
|
|
item := &items[i]
|
|
switch item.Status {
|
|
case "active":
|
|
active = item
|
|
case "ignored":
|
|
ignored = item
|
|
}
|
|
}
|
|
|
|
if active == nil {
|
|
t.Fatal("expected active connected infrastructure item")
|
|
}
|
|
if ignored == nil {
|
|
t.Fatal("expected ignored connected infrastructure item")
|
|
}
|
|
|
|
if len(active.Surfaces) != 2 {
|
|
t.Fatalf("expected host-managed active surfaces to remain without docker, got %#v", active.Surfaces)
|
|
}
|
|
for _, surface := range active.Surfaces {
|
|
if surface.Kind == "docker" {
|
|
t.Fatalf("expected docker surface to be removed from active item, got %#v", active.Surfaces)
|
|
}
|
|
}
|
|
|
|
if len(ignored.Surfaces) != 1 || ignored.Surfaces[0].Kind != "docker" {
|
|
t.Fatalf("expected ignored docker surface, got %#v", ignored.Surfaces)
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_UsesSharedDisplayNameFallback(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "tower-resource",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Canonical: &unifiedresources.CanonicalIdentity{
|
|
DisplayName: "Canonical Tower",
|
|
},
|
|
Agent: &unifiedresources.AgentData{
|
|
AgentID: "tower-agent",
|
|
AgentVersion: "1.2.3",
|
|
Hostname: "tower.local",
|
|
Platform: "linux",
|
|
},
|
|
Identity: unifiedresources.ResourceIdentity{
|
|
Hostnames: []string{"tower.local"},
|
|
},
|
|
},
|
|
}, models.StateSnapshot{})
|
|
|
|
if len(items) != 1 {
|
|
t.Fatalf("expected 1 connected infrastructure item, got %d", len(items))
|
|
}
|
|
|
|
item := items[0]
|
|
if item.Name != "Canonical Tower" {
|
|
t.Fatalf("expected canonical display name to drive name, got %q", item.Name)
|
|
}
|
|
if item.DisplayName != "Canonical Tower" {
|
|
t.Fatalf("expected canonical display name to drive displayName, got %q", item.DisplayName)
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_UsesSharedTopLevelSystemGrouping(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "tower-agent",
|
|
Name: "Tower Agent",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Agent: &unifiedresources.AgentData{
|
|
AgentID: "tower-agent",
|
|
AgentVersion: "1.2.3",
|
|
Hostname: "tower.local",
|
|
},
|
|
Identity: unifiedresources.ResourceIdentity{
|
|
Hostnames: []string{"tower.local"},
|
|
},
|
|
},
|
|
{
|
|
ID: "tower-pbs",
|
|
Name: "Tower PBS",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
PBS: &unifiedresources.PBSData{
|
|
InstanceID: "pbs-1",
|
|
Hostname: "tower.local",
|
|
HostURL: "https://tower.local:8007",
|
|
Version: "3.4.1",
|
|
},
|
|
},
|
|
}, models.StateSnapshot{})
|
|
|
|
if len(items) != 1 {
|
|
t.Fatalf("expected one connected infrastructure item after shared grouping, got %d", len(items))
|
|
}
|
|
|
|
item := items[0]
|
|
if len(item.Surfaces) != 2 {
|
|
t.Fatalf("expected host telemetry and PBS surfaces on one item, got %#v", item.Surfaces)
|
|
}
|
|
if item.ID == "resource:tower-pbs" {
|
|
t.Fatalf("expected PBS surface to attach to the shared top-level system group, got %q", item.ID)
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_KeepsPlatformConnectionsActiveWhenHostTelemetryIgnored(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "tower-agent",
|
|
Name: "Tower Agent",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Agent: &unifiedresources.AgentData{
|
|
AgentID: "tower-agent",
|
|
AgentVersion: "1.2.3",
|
|
Hostname: "tower.local",
|
|
},
|
|
Identity: unifiedresources.ResourceIdentity{
|
|
Hostnames: []string{"tower.local"},
|
|
},
|
|
},
|
|
{
|
|
ID: "tower-pbs",
|
|
Name: "Tower PBS",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
PBS: &unifiedresources.PBSData{
|
|
InstanceID: "pbs-1",
|
|
Hostname: "tower.local",
|
|
Version: "3.4.1",
|
|
},
|
|
},
|
|
}, models.StateSnapshot{
|
|
RemovedHostAgents: []models.RemovedHostAgent{
|
|
{ID: "tower-agent", Hostname: "tower.local", DisplayName: "Tower", RemovedAt: now},
|
|
},
|
|
})
|
|
|
|
if len(items) != 2 {
|
|
t.Fatalf("expected active platform item and ignored host item, got %d", len(items))
|
|
}
|
|
|
|
var active *models.ConnectedInfrastructureItemFrontend
|
|
var ignored *models.ConnectedInfrastructureItemFrontend
|
|
for i := range items {
|
|
item := &items[i]
|
|
switch item.Status {
|
|
case "active":
|
|
active = item
|
|
case "ignored":
|
|
ignored = item
|
|
}
|
|
}
|
|
|
|
if active == nil {
|
|
t.Fatal("expected active connected infrastructure item")
|
|
}
|
|
if ignored == nil {
|
|
t.Fatal("expected ignored connected infrastructure item")
|
|
}
|
|
|
|
if len(active.Surfaces) != 1 || active.Surfaces[0].Kind != "pbs" {
|
|
t.Fatalf("expected active platform surface to remain after host ignore, got %#v", active.Surfaces)
|
|
}
|
|
if len(ignored.Surfaces) != 1 || ignored.Surfaces[0].Kind != "agent" {
|
|
t.Fatalf("expected ignored agent surface, got %#v", ignored.Surfaces)
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_ProjectsTrueNASSurface(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "truenas-main",
|
|
Name: "Tower NAS",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
TrueNAS: &unifiedresources.TrueNASData{
|
|
Hostname: "truenas.local",
|
|
Version: "25.04.0",
|
|
},
|
|
},
|
|
}, models.StateSnapshot{})
|
|
|
|
if len(items) != 1 {
|
|
t.Fatalf("expected one connected infrastructure item, got %d", len(items))
|
|
}
|
|
|
|
item := items[0]
|
|
if item.Hostname != "truenas.local" {
|
|
t.Fatalf("expected truenas hostname to drive connected infrastructure hostname, got %q", item.Hostname)
|
|
}
|
|
if item.Version != "25.04.0" {
|
|
t.Fatalf("expected truenas version to drive connected infrastructure version, got %q", item.Version)
|
|
}
|
|
if len(item.Surfaces) != 1 {
|
|
t.Fatalf("expected one truenas surface, got %#v", item.Surfaces)
|
|
}
|
|
if item.Surfaces[0].Kind != "truenas" {
|
|
t.Fatalf("expected truenas surface kind, got %#v", item.Surfaces[0])
|
|
}
|
|
}
|
|
|
|
func TestConnectedInfrastructureTrueNASDoesNotImitateAHostAgent(t *testing.T) {
|
|
const connectionID, hostname, realID = "native-connection", "nas", "reporting-agent"
|
|
now := time.Now().UTC()
|
|
provider := truenas.NewLiveProviderForConnection(nil, connectionID)
|
|
registry := unifiedresources.NewRegistry(nil)
|
|
registry.IngestRecords(unifiedresources.SourceTrueNAS, provider.RecordsFromSnapshot(&truenas.FixtureSnapshot{
|
|
CollectedAt: now,
|
|
System: truenas.SystemInfo{Hostname: hostname, Version: "25.10.8", Healthy: true},
|
|
}))
|
|
native := registry.ListByType(unifiedresources.ResourceTypeAgent)
|
|
if len(native) != 1 || native[0].Agent == nil || native[0].Agent.AgentID != connectionID {
|
|
t.Fatal("fixture must contain the native adapter's agent-shaped history identity")
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
sources []unifiedresources.DataSource
|
|
agent *unifiedresources.AgentData
|
|
addReporter bool
|
|
retained bool
|
|
wantHost bool
|
|
}{
|
|
{name: "native only"},
|
|
{
|
|
name: "inferred presentation source and real namesake",
|
|
sources: []unifiedresources.DataSource{unifiedresources.SourceTrueNAS, unifiedresources.SourceAgent},
|
|
addReporter: true,
|
|
},
|
|
{
|
|
name: "actual reporter on the appliance",
|
|
sources: []unifiedresources.DataSource{unifiedresources.SourceTrueNAS, unifiedresources.SourceAgent},
|
|
agent: &unifiedresources.AgentData{
|
|
AgentID: realID, Hostname: hostname, AgentVersion: "6.9.0",
|
|
LinkedVMID: "fixture-node-110", CommandsEnabled: true,
|
|
},
|
|
addReporter: true,
|
|
wantHost: true,
|
|
},
|
|
{
|
|
name: "saved reporter waiting after restart",
|
|
sources: []unifiedresources.DataSource{unifiedresources.SourceTrueNAS, unifiedresources.SourceAgent},
|
|
agent: &unifiedresources.AgentData{
|
|
AgentID: realID, Hostname: hostname, AgentVersion: "6.9.0",
|
|
},
|
|
addReporter: true,
|
|
retained: true,
|
|
wantHost: true,
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := native[0]
|
|
if tc.sources != nil {
|
|
r.Sources = tc.sources
|
|
}
|
|
if tc.agent != nil {
|
|
r.Agent = tc.agent
|
|
}
|
|
adapter := unifiedresources.NewMonitorAdapter(registry)
|
|
observations := adapter.GetAll()
|
|
if tc.addReporter {
|
|
host := models.Host{ID: realID, Hostname: hostname, Status: "online", LastSeen: now}
|
|
if tc.retained {
|
|
host.Status = "pending"
|
|
}
|
|
report := unifiedresources.HostIngestRecord(host)
|
|
if tc.retained {
|
|
overlay := unifiedresources.ReadStateWithHostContinuity(adapter, []unifiedresources.IngestRecord{report})
|
|
observations = overlay.(*unifiedresources.MonitorAdapter).GetAll()
|
|
} else {
|
|
capture := unifiedresources.NewRegistry(nil)
|
|
capture.IngestResources(observations)
|
|
capture.IngestRecords(unifiedresources.SourceAgent, []unifiedresources.IngestRecord{report})
|
|
observations = capture.List()
|
|
}
|
|
}
|
|
input := []unifiedresources.Resource{r}
|
|
// The continuity branch is present in the read view but not in
|
|
// snapshot.Hosts. No fake SourceAgent on the rendered row can
|
|
// pass without the corresponding identity in that view.
|
|
items := buildConnectedInfrastructure(observations, input, models.StateSnapshot{})
|
|
if len(items) != 1 {
|
|
t.Fatalf("want one monitored NAS system, got %d", len(items))
|
|
}
|
|
item := items[0]
|
|
kinds := make([]string, 0, len(item.Surfaces))
|
|
for _, surface := range item.Surfaces {
|
|
kinds = append(kinds, surface.Kind)
|
|
if surface.Kind == "agent" && surface.ControlID != realID {
|
|
t.Fatalf("agent surface targets %q, want %q", surface.ControlID, realID)
|
|
}
|
|
}
|
|
wantKinds := []string{"truenas"}
|
|
if tc.wantHost {
|
|
wantKinds = []string{"agent", "truenas"}
|
|
if item.ScopeAgentID != realID || item.UninstallAgentID != realID ||
|
|
item.LinkedVMID != tc.agent.LinkedVMID || item.CommandsEnabled != tc.agent.CommandsEnabled {
|
|
t.Fatalf("missing actual reporter target or association: %+v", item)
|
|
}
|
|
} else if item.ScopeAgentID != "" || item.UninstallAgentID != "" || item.CommandsEnabled ||
|
|
item.Version != "25.10.8" {
|
|
t.Fatalf("provider-only system either gained agent controls or lost its version: %+v", item)
|
|
}
|
|
if !slices.Equal(kinds, wantKinds) || item.Hostname != hostname || input[0].Agent != r.Agent {
|
|
t.Fatalf("got surfaces %v, hostname %q, or a mutated provider fact", kinds, item.Hostname)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_PreservesLinkedGuestIdentity(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "guest-agent",
|
|
Name: "debian-go",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Agent: &unifiedresources.AgentData{
|
|
AgentID: "guest-agent",
|
|
AgentVersion: "1.2.3",
|
|
Hostname: "debian-go.local",
|
|
LinkedVMID: "101",
|
|
LinkedContainerID: "",
|
|
},
|
|
Identity: unifiedresources.ResourceIdentity{
|
|
Hostnames: []string{"debian-go.local"},
|
|
},
|
|
},
|
|
}, models.StateSnapshot{
|
|
RemovedHostAgents: []models.RemovedHostAgent{
|
|
{
|
|
ID: "ignored-guest-agent",
|
|
Hostname: "archive-guest.local",
|
|
DisplayName: "archive-guest",
|
|
LinkedContainerID: "102",
|
|
RemovedAt: now,
|
|
},
|
|
},
|
|
})
|
|
|
|
if len(items) != 2 {
|
|
t.Fatalf("expected active and ignored guest rows, got %d", len(items))
|
|
}
|
|
|
|
var active *models.ConnectedInfrastructureItemFrontend
|
|
var ignored *models.ConnectedInfrastructureItemFrontend
|
|
for i := range items {
|
|
item := &items[i]
|
|
switch item.Status {
|
|
case "active":
|
|
active = item
|
|
case "ignored":
|
|
ignored = item
|
|
}
|
|
}
|
|
|
|
if active == nil || ignored == nil {
|
|
t.Fatalf("expected both active and ignored connected infrastructure items, got %#v", items)
|
|
}
|
|
if active.LinkedVMID != "101" {
|
|
t.Fatalf("expected active linked VM id to round-trip, got %q", active.LinkedVMID)
|
|
}
|
|
if ignored.LinkedContainerID != "102" {
|
|
t.Fatalf("expected ignored linked container id to round-trip, got %q", ignored.LinkedContainerID)
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_IgnoresChildPlatformResources(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "vm-100",
|
|
Type: unifiedresources.ResourceTypeVM,
|
|
Name: "cloudflared",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Proxmox: &unifiedresources.ProxmoxData{
|
|
SourceID: "100",
|
|
NodeName: "pve-a",
|
|
},
|
|
},
|
|
{
|
|
ID: "storage-local-zfs",
|
|
Type: unifiedresources.ResourceTypeStorage,
|
|
Name: "local-zfs",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Proxmox: &unifiedresources.ProxmoxData{
|
|
SourceID: "local-zfs",
|
|
NodeName: "pve-a",
|
|
},
|
|
},
|
|
{
|
|
ID: "pbs-datastore-main",
|
|
Type: unifiedresources.ResourceTypeStorage,
|
|
Name: "main",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
PBS: &unifiedresources.PBSData{
|
|
InstanceID: "pbs-main",
|
|
Hostname: "pbs.local",
|
|
},
|
|
},
|
|
}, models.StateSnapshot{})
|
|
|
|
if len(items) != 0 {
|
|
t.Fatalf("expected child platform resources to stay out of connected infrastructure, got %#v", items)
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_ResolvesLegacyAgentUpgradePlatform(t *testing.T) {
|
|
now := time.Unix(1_700_000_000, 0)
|
|
cases := []struct {
|
|
name string
|
|
platform string
|
|
want string
|
|
}{
|
|
// Legacy v5 agents report gopsutil's host.Info().Platform verbatim
|
|
// instead of a canonical token (refs #1555).
|
|
{"legacy windows caption", "microsoft windows 11 pro", "windows"},
|
|
{"canonical windows", "windows", "windows"},
|
|
{"darwin", "darwin", "macos"},
|
|
{"freebsd caption", "FreeBSD 14.1-RELEASE", "freebsd"},
|
|
{"linux distro", "ubuntu", "linux"},
|
|
{"empty", "", "linux"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
items := buildConnectedInfrastructure(nil, []unifiedresources.Resource{
|
|
{
|
|
ID: "host-resource",
|
|
Name: "Host",
|
|
LastSeen: now,
|
|
Status: unifiedresources.StatusOnline,
|
|
Agent: &unifiedresources.AgentData{
|
|
AgentID: "host-agent",
|
|
AgentVersion: "5.1.36",
|
|
Hostname: "host.local",
|
|
Platform: tc.platform,
|
|
},
|
|
Identity: unifiedresources.ResourceIdentity{
|
|
Hostnames: []string{"host.local"},
|
|
},
|
|
},
|
|
}, models.StateSnapshot{})
|
|
|
|
if len(items) != 1 {
|
|
t.Fatalf("expected 1 connected infrastructure item, got %d", len(items))
|
|
}
|
|
if items[0].UpgradePlatform != tc.want {
|
|
t.Fatalf("expected upgrade platform %q for reported platform %q, got %q", tc.want, tc.platform, items[0].UpgradePlatform)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildConnectedInfrastructure_AmbiguousFallbackKeepsEverySurface(t *testing.T) {
|
|
resources := []unifiedresources.Resource{
|
|
{ID: "platform-a", Type: unifiedresources.ResourceTypeAgent, Name: "Platform A", Proxmox: &unifiedresources.ProxmoxData{SourceID: "pve-a", NodeName: "tower.lab"}},
|
|
{ID: "platform-b", Type: unifiedresources.ResourceTypeAgent, Name: "Platform B", Proxmox: &unifiedresources.ProxmoxData{SourceID: "pve-b", NodeName: "tower.lab"}},
|
|
{ID: "host", Type: unifiedresources.ResourceTypeAgent, Name: "Host", Agent: &unifiedresources.AgentData{AgentID: "host-agent", Hostname: "tower"}},
|
|
}
|
|
items := buildConnectedInfrastructure(nil, resources, models.StateSnapshot{})
|
|
if len(items) != 3 {
|
|
t.Fatalf("ambiguous hostname attached to arbitrary platform: %d items", len(items))
|
|
}
|
|
surfaces := map[string]bool{}
|
|
for _, item := range items {
|
|
for _, surface := range item.Surfaces {
|
|
if surfaces[surface.ID] {
|
|
t.Fatalf("duplicated surface %q", surface.ID)
|
|
}
|
|
surfaces[surface.ID] = true
|
|
}
|
|
}
|
|
for _, id := range []string{"proxmox:pve-a", "proxmox:pve-b", "agent:host-agent"} {
|
|
if !surfaces[id] {
|
|
t.Fatalf("ambiguity dropped surface %q (got %v)", id, surfaces)
|
|
}
|
|
}
|
|
}
|