Files
Pulse/internal/mock/node_agent_link_test.go
rcourtman 4fe00744ba Send a guest start only to an agent confirmed on its node
The first commit on this branch let a node's own Pulse agent start a
stopped Proxmox guest, and chose that agent by hostname. Another
machine can hold a node's name, and the same VMID there is a different
guest. A second review of the branch found that and the faults below.

Which agent. A start now needs all of this, or it is refused with
guest_start_agent_unconfirmed: exactly one agent tied to the node, the
node on the guest's own Proxmox connection under the guest's own node
name, the agent carrying the node's name with no other monitored node
of that name, and an address on one of the agent's own interfaces that
Proxmox lists for that node and for no other. The link Pulse makes on a
matching name is not evidence, nor the address Pulse dials, which can be
a proxy, nor the agent's configured report address. This is evidence and
not proof: a machine Pulse does not monitor as a node, with the node's
name and one of its addresses, is not ruled out. An agent Pulse cannot
confirm is not told to switch commands on either, because the start
would still be refused.

Runner routes. A route planned on the action runner now demands a runner
at dispatch. A collector that took the runner's place under the same
agent ID is refused, so its answer is never judged by the runner's rule.

Agent side. The service check is the exact cgroup
/system.slice/pulse-agent.service, so a user manager's unit of that name
no longer passes. Every command client of one state directory shares one
containment, so a client created when commands are switched cannot
prepare again while an old client's start is finishing. A registration
waits five seconds for the containment probe, not fifteen, and a later
change is restated on the live session with an agent_capabilities
message.

Mock data. Every mock node listed the same address and its agent
reported an unrelated one, so the new rule refused every mock node's
agent as unconfirmed. Mock nodes now have their own addresses and each
node's agent reports them, as on a real node.

Docs said more than the code did in several places. They now match.
2026-11-11 12:35:49 +00:00

89 lines
3.0 KiB
Go

package mock
import (
"strings"
"testing"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
)
// Pulse sends a guest start only to an agent it can confirm runs on the
// guest's node: the agent carries the node's name, no other node has that
// name, and one of the agent's interfaces holds an address Proxmox lists for
// that node alone. Mock data has to be shaped like a real node and its agent,
// or every mock node reads as one whose agent cannot be confirmed.
func TestMockNodesAndTheirAgentsLookLikeOneMachine(t *testing.T) {
state := buildFixtureState(DefaultConfig)
if len(state.Nodes) == 0 {
t.Fatal("no mock nodes")
}
hosts := make(map[string]models.Host, len(state.Hosts))
for _, host := range state.Hosts {
hosts[host.ID] = host
}
names := make(map[string]string)
owners := make(map[string]string)
for _, node := range state.Nodes {
if other, taken := names[strings.ToLower(node.Name)]; taken {
t.Fatalf("nodes %s and %s share the name %q", other, node.ID, node.Name)
}
names[strings.ToLower(node.Name)] = node.ID
addresses := make(map[string]bool)
for _, nic := range node.NetworkInterfaces {
for _, address := range nic.Addresses {
normalized := models.NodeLinkAddress(address)
if normalized == "" {
continue
}
if other, taken := owners[normalized]; taken && other != node.ID {
t.Fatalf("nodes %s and %s both list %s", other, node.ID, normalized)
}
owners[normalized] = node.ID
addresses[normalized] = true
}
}
if len(addresses) == 0 {
t.Fatalf("node %s lists no address", node.ID)
}
agent, ok := hosts[node.LinkedAgentID]
if !ok || agent.LinkedNodeID != node.ID {
t.Fatalf("node %s has no agent linked back to it: %q", node.ID, node.LinkedAgentID)
}
if !strings.EqualFold(agent.Hostname, node.Name) {
t.Fatalf("node %s is named %q and its agent reports %q", node.ID, node.Name, agent.Hostname)
}
shared := false
for _, nic := range agent.NetworkInterfaces {
for _, address := range nic.Addresses {
if addresses[models.NodeLinkAddress(address)] {
shared = true
}
}
}
if !shared {
t.Fatalf("the agent on node %s reports none of the node's addresses: %+v", node.ID, agent.NetworkInterfaces)
}
}
}
func TestMockNodeBridgeAddressesAreStablePerName(t *testing.T) {
if first, again := mockNodeBridgeAddresses("pve7"), mockNodeBridgeAddresses("pve7"); strings.Join(first, ",") != strings.Join(again, ",") {
t.Fatalf("pve7 got %v then %v", first, again)
}
seen := make(map[string]string)
for _, name := range []string{"pve1", "pve2", "pve50", "pve229", "pve230", "delly", "minipc", "node-a", "node-b"} {
addresses := mockNodeBridgeAddresses(name)
if len(addresses) != 2 || models.NodeLinkAddress(addresses[0]) == "" || models.NodeLinkAddress(addresses[1]) == "" {
t.Fatalf("%s got %v", name, addresses)
}
for _, address := range addresses {
if other, taken := seen[address]; taken {
t.Fatalf("%s and %s share %s", other, name, address)
}
seen[address] = name
}
}
}