banger/internal/daemon/nat_test.go
Thales Maciel 687fcf0b59
vm state: split transient kernel/process handles off the durable schema
Separates what a VM IS (durable intent + identity + deterministic
derived paths — `VMRuntime`) from what is CURRENTLY TRUE about it
(firecracker PID, tap device, loop devices, dm-snapshot target — new
`VMHandles`). The durable state lives in the SQLite `vms` row; the
transient state lives in an in-memory cache on the daemon plus a
per-VM `handles.json` scratch file inside VMDir, rebuilt at startup
from OS inspection. Nothing kernel-level rides the SQLite schema
anymore.

Why:

  Persisting ephemeral process handles to SQLite forced reconcile to
  treat "running with a stale PID" as a first-class case and mix it
  with real state transitions. The schema described what we last
  observed, not what the VM is. Every time the observation model
  shifted (tap pool, DM naming, pgrep fallback) the reconcile logic
  grew a new branch. Splitting lets each layer own what it's good at:
  durable records describe intent, in-memory cache + scratch file
  describe momentary reality.

Shape:

  - `model.VMHandles` = PID, TapDevice, BaseLoop, COWLoop, DMName,
    DMDev. Never in SQLite.
  - `VMRuntime` keeps: State, GuestIP, APISockPath, VSockPath,
    VSockCID, LogPath, MetricsPath, DNSName, VMDir, SystemOverlay,
    WorkDiskPath, LastError. All durable or deterministic.
  - `handleCache` on `*Daemon` — mutex-guarded map + scratch-file
    plumbing (`writeHandlesFile` / `readHandlesFile` /
    `rediscoverHandles`). See `internal/daemon/vm_handles.go`.
  - `d.vmAlive(vm)` replaces the 20+ inline
    `vm.State==Running && ProcessRunning(vm.Runtime.PID, apiSock)`
    spreads. Single source of truth for liveness.
  - Startup reconcile: per running VM, load the scratch file, pgrep
    the api sock, either keep (cache seeded from scratch) or demote
    to stopped (scratch handles passed to cleanupRuntime first so DM
    / loops / tap actually get torn down).

Verification:

  - `go test ./...` green.
  - Live: `banger vm run --name handles-test -- cat /etc/hostname`
    starts; `handles.json` appears in VMDir with the expected PID,
    tap, loops, DM.
  - `kill -9 $(pgrep bangerd)` while the VM is running, re-invoke the
    CLI, daemon auto-starts, reconcile recognises the VM as alive,
    `banger vm ssh` still connects, `banger vm delete` cleans up.

Tests added:

  - vm_handles_test.go: scratch-file roundtrip, missing/corrupt file
    behaviour, cache concurrency, rediscoverHandles prefers pgrep
    over scratch, returns scratch contents even when process is
    dead (so cleanup can tear down kernel state).
  - vm_test.go: reconcile test rewritten to exercise the new flow
    (write scratch → reconcile reads it → verifies process is gone →
    issues dmsetup/losetup teardown).

ARCHITECTURE.md updated; `handles` added to Daemon field docs.
2026-04-19 14:18:13 -03:00

123 lines
3.6 KiB
Go

package daemon
import (
"slices"
"testing"
"banger/internal/model"
)
func TestParseDefaultUplink(t *testing.T) {
t.Parallel()
output := "default via 192.168.1.1 dev enp5s0 proto dhcp src 192.168.1.40 metric 100\n"
uplink, err := parseDefaultUplink(output)
if err != nil {
t.Fatalf("parseDefaultUplink returned error: %v", err)
}
if uplink != "enp5s0" {
t.Fatalf("uplink = %q, want enp5s0", uplink)
}
}
func TestParseDefaultUplinkFailsWithoutRoute(t *testing.T) {
t.Parallel()
if _, err := parseDefaultUplink("10.0.0.0/24 dev br-fc proto kernel scope link src 10.0.0.1\n"); err == nil {
t.Fatal("expected parseDefaultUplink to fail without a default route")
}
}
func TestNATRulesForVM(t *testing.T) {
t.Parallel()
vm := model.VMRecord{
Runtime: model.VMRuntime{
GuestIP: "172.16.0.8",
},
}
rules, err := natRulesForVM(vm, "tap-fc-abcd1234", "wlan0")
if err != nil {
t.Fatalf("natRulesForVM returned error: %v", err)
}
if len(rules) != 3 {
t.Fatalf("rule count = %d, want 3", len(rules))
}
if got, want := natRuleArgs("-A", rules[0]), []string{"-t", "nat", "-A", "POSTROUTING", "-s", "172.16.0.8/32", "-o", "wlan0", "-j", "MASQUERADE"}; !slices.Equal(got, want) {
t.Fatalf("postrouting args = %v, want %v", got, want)
}
if got, want := natRuleArgs("-A", rules[1]), []string{"-A", "FORWARD", "-i", "tap-fc-abcd1234", "-o", "wlan0", "-j", "ACCEPT"}; !slices.Equal(got, want) {
t.Fatalf("forward-out args = %v, want %v", got, want)
}
if got, want := natRuleArgs("-A", rules[2]), []string{"-A", "FORWARD", "-i", "wlan0", "-o", "tap-fc-abcd1234", "-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT"}; !slices.Equal(got, want) {
t.Fatalf("forward-in args = %v, want %v", got, want)
}
}
func TestNATRulesForVMRequiresRuntimeData(t *testing.T) {
t.Parallel()
tests := []struct {
name string
vm model.VMRecord
tap string
uplink string
}{
{
name: "guest ip",
vm: model.VMRecord{},
tap: "tap-fc-abcd1234",
uplink: "eth0",
},
{
name: "tap",
vm: model.VMRecord{Runtime: model.VMRuntime{GuestIP: "172.16.0.8"}},
tap: "",
uplink: "eth0",
},
{
name: "uplink",
vm: model.VMRecord{Runtime: model.VMRuntime{GuestIP: "172.16.0.8"}},
tap: "tap-fc-abcd1234",
uplink: "",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if _, err := natRulesForVM(tt.vm, tt.tap, tt.uplink); err == nil {
t.Fatalf("expected natRulesForVM to fail for missing %s", tt.name)
}
})
}
}
func TestNATPlans(t *testing.T) {
t.Parallel()
rules := []natRule{
{Table: "nat", Chain: "POSTROUTING", Args: []string{"-s", "172.16.0.8/32", "-o", "eth0", "-j", "MASQUERADE"}},
{Chain: "FORWARD", Args: []string{"-i", "tap-fc-abcd1234", "-o", "eth0", "-j", "ACCEPT"}},
}
addPlan := natAddPlan(rules)
if len(addPlan) != 3 {
t.Fatalf("addPlan count = %d, want 3", len(addPlan))
}
if got, want := addPlan[0], []string{"sysctl", "-w", "net.ipv4.ip_forward=1"}; !slices.Equal(got, want) {
t.Fatalf("sysctl command = %v, want %v", got, want)
}
if got, want := addPlan[1], []string{"-t", "nat", "-A", "POSTROUTING", "-s", "172.16.0.8/32", "-o", "eth0", "-j", "MASQUERADE"}; !slices.Equal(got, want) {
t.Fatalf("add NAT command = %v, want %v", got, want)
}
removePlan := natRemovePlan(rules)
if len(removePlan) != 2 {
t.Fatalf("removePlan count = %d, want 2", len(removePlan))
}
if got, want := removePlan[0], []string{"-t", "nat", "-D", "POSTROUTING", "-s", "172.16.0.8/32", "-o", "eth0", "-j", "MASQUERADE"}; !slices.Equal(got, want) {
t.Fatalf("remove NAT command = %v, want %v", got, want)
}
}