First phase of splitting the daemon god-struct into focused services with explicit ownership. HostNetwork now owns everything host-networking: the TAP interface pool (initializeTapPool / ensureTapPool / acquireTap / releaseTap / createTap), bridge + socket dir setup, firecracker process primitives (find/resolve/kill/wait/ensureSocketAccess/sendCtrlAltDel), DM snapshot lifecycle, NAT rule enforcement, guest DNS server lifecycle + routing setup, and the vsock-agent readiness probe. That's 7 files whose receivers flipped from *Daemon to *HostNetwork, plus a new host_network.go that declares the struct, its hostNetworkDeps, and the factored firecracker + DNS helpers that used to live in vm.go. Daemon gives up the tapPool and vmDNS fields entirely; they're now HostNetwork's business. Construction goes through newHostNetwork in Daemon.Open with an explicit dependency bag (runner, logger, config, layout, closing). A lazy-init hostNet() helper on Daemon supports test literals that don't wire net explicitly — production always populates it eagerly. Signature tightenings where the old receiver reached into VM-service state: - ensureNAT(ctx, vm, enable) → ensureNAT(ctx, guestIP, tap, enable). Callers resolve tap from the handle cache themselves. - initializeTapPool(ctx) → initializeTapPool(usedTaps []string). Daemon.Open enumerates VMs, collects taps from handles, hands the slice in. rebuildDNS stays on *Daemon as the orchestrator — it filters by vm-alive (a VMService concern handles will move to in phase 4) then calls HostNetwork.replaceDNS with the already-filtered map. Capability hooks continue to take *Daemon; they now use it as a facade to reach services (d.net.ensureNAT, d.hostNet().*). Planned CapabilityHost interface extraction is orthogonal, left for later. Tests: dns_routing_test.go + fastpath_test.go + nat_test.go + snapshot_test.go + open_close_test.go were touched to construct HostNetwork literals where they exercise its methods directly, or route through d.hostNet() where they exercise the Daemon entry points. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"banger/internal/model"
|
|
"banger/internal/vmdns"
|
|
)
|
|
|
|
func TestSyncVMDNSResolverRoutingConfiguresResolved(t *testing.T) {
|
|
origLookup := lookupExecutableFunc
|
|
origAddr := vmDNSAddrFunc
|
|
t.Cleanup(func() {
|
|
lookupExecutableFunc = origLookup
|
|
vmDNSAddrFunc = origAddr
|
|
})
|
|
lookupExecutableFunc = func(name string) (string, error) {
|
|
if name == "resolvectl" {
|
|
return "/usr/bin/resolvectl", nil
|
|
}
|
|
return "", nil
|
|
}
|
|
vmDNSAddrFunc = func(*vmdns.Server) string { return "127.0.0.1:42069" }
|
|
|
|
runner := &scriptedRunner{
|
|
t: t,
|
|
steps: []runnerStep{
|
|
{call: runnerCall{name: "ip", args: []string{"link", "show", model.DefaultBridgeName}}, out: []byte("1: br-fc\n")},
|
|
sudoStep("", nil, "resolvectl", "dns", model.DefaultBridgeName, "127.0.0.1:42069"),
|
|
sudoStep("", nil, "resolvectl", "domain", model.DefaultBridgeName, vmResolverRouteDomain),
|
|
sudoStep("", nil, "resolvectl", "default-route", model.DefaultBridgeName, "no"),
|
|
},
|
|
}
|
|
cfg := model.DaemonConfig{BridgeName: model.DefaultBridgeName}
|
|
n := &HostNetwork{runner: runner, config: cfg, vmDNS: new(vmdns.Server)}
|
|
|
|
if err := n.syncVMDNSResolverRouting(context.Background()); err != nil {
|
|
t.Fatalf("syncVMDNSResolverRouting: %v", err)
|
|
}
|
|
runner.assertExhausted()
|
|
}
|
|
|
|
func TestClearVMDNSResolverRoutingRevertsBridgeConfig(t *testing.T) {
|
|
origLookup := lookupExecutableFunc
|
|
t.Cleanup(func() {
|
|
lookupExecutableFunc = origLookup
|
|
})
|
|
lookupExecutableFunc = func(name string) (string, error) {
|
|
if name == "resolvectl" {
|
|
return "/usr/bin/resolvectl", nil
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
runner := &scriptedRunner{
|
|
t: t,
|
|
steps: []runnerStep{
|
|
{call: runnerCall{name: "ip", args: []string{"link", "show", model.DefaultBridgeName}}, out: []byte("1: br-fc\n")},
|
|
sudoStep("", nil, "resolvectl", "revert", model.DefaultBridgeName),
|
|
},
|
|
}
|
|
cfg := model.DaemonConfig{BridgeName: model.DefaultBridgeName}
|
|
n := &HostNetwork{runner: runner, config: cfg}
|
|
|
|
if err := n.clearVMDNSResolverRouting(context.Background()); err != nil {
|
|
t.Fatalf("clearVMDNSResolverRouting: %v", err)
|
|
}
|
|
runner.assertExhausted()
|
|
}
|