Files
2026-09-23 19:29:28 -04:00

128 lines
5.7 KiB
Nix

# Guest-config VM test, bridged mode: the guest on a LAN that hands it one
# fixed address (the production shape of services.hermenix.network.bridged
# = true: the host-side DHCP server reserves lanAddress for the guest's MAC,
# so the guest is effectively static without knowing it). A second node acts
# as the LAN: the reservation-style DHCP server, a throwaway HTTP service,
# and a plain device that connects back to the guest. This is the
# guest-facing half of the module; the module's own bridge/enforcement rules
# are covered statically by tests/network-modes-check.nix.
#
# The guest node and the mode-independent assertions (seeding, idempotency,
# account, SSH) are shared with guest-vm-unbridged via
# common.nix.
#
# Build with (never run by an agent, only by the operator/CI):
# nix build .#checks.x86_64-linux.guest-vm-bridged --impure -L
{ pkgs, determinate }:
let
common = import ./common.nix { inherit determinate; };
in
pkgs.testers.runNixOSTest {
name = "hermenix-guest-config-bridged";
# Boots once, reboots once, and waits on DHCP lease negotiation plus a few
# cross-node checks - still only minutes. Bound it far below the
# framework's 1-hour default so a genuine hang fails fast.
globalTimeout = 900;
nodes = {
machine = common.guestNode;
# The "LAN": hands the guest its single fixed address and hosts a
# throwaway HTTP service, standing in for the router and another LAN
# device. Its firewall is off so the guest<->LAN checks exercise the
# network path itself, not this throwaway node's own firewall.
lan =
{ pkgs, lib, ... }:
{
networking.firewall.enable = false;
# Same production-shape networking as the guest node: kill the test
# framework's SLiRP user-mode NIC so the only NIC left is the one
# shared with the guest.
virtualisation.qemu.networkingOptions = lib.mkForce [ ];
virtualisation.vlans = [ 1 ];
# The framework's own name-based static assignment missed the
# shared NIC when this suite actually ran (the NIC came up as eth1,
# not the eth0 the framework expected), and real interface names
# vary anyway - and node numbering is attr order, which is not
# something to trust. The shared NIC's MAC is deterministic
# (52:54:00:12:<vlan>:<node>, see qemu-common.nix), so pin the
# address in networkd by MAC. Two candidate rules cover either node
# numbering; the one that matches this node's actual NIC wins, and
# nothing depends on an interface name.
networking.useNetworkd = true;
systemd.network.networks."10-hermes-lan-1" = {
matchConfig.MACAddress = "52:54:00:12:01:01"; # vlan 1, node #1
address = [ "192.168.1.2/24" ];
networkConfig.LinkLocalAddressing = "no";
};
systemd.network.networks."10-hermes-lan-2" = {
matchConfig.MACAddress = "52:54:00:12:01:02"; # vlan 1, node #2
address = [ "192.168.1.2/24" ];
networkConfig.LinkLocalAddressing = "no";
};
# DHCP only, and reservation-style: a single-address range, exactly
# how the host-side hermes-lan-dhcp hands the real guest its fixed
# lanAddress (port = 0 disables dnsmasq's DNS half; the guest keeps
# its own QUAD9 resolvers regardless). Bound to the node's static
# vlan address rather than an interface name - the framework's
# interface naming is not something this test should depend on.
services.dnsmasq = {
enable = true;
settings = {
port = 0;
listen-address = "192.168.1.2";
bind-interfaces = true;
dhcp-range = "192.168.1.100,192.168.1.100,12h";
dhcp-option = [
"3,192.168.1.2" # default gateway -> this node
];
};
};
environment.systemPackages = [ pkgs.python3 ];
};
};
testScript = ''
# Bring the LAN up early so the guest's DHCP client (which retries for
# as long as it runs) can get a lease as soon as the guest is online.
lan.start()
''
+ common.commonScript
+ ''
# --- bridged-mode networking ---------------------------------------
# The LAN handed the guest its fixed address alongside its static
# service-net address. (Both test nodes share one vlan, standing in for
# the bridged uplink on a real host.)
machine.wait_until_succeeds("ip -4 route show default | grep -q 'metric 100'")
addrs = machine.succeed("ip -4 -o addr show | grep -v '^1: lo'").strip().splitlines()
guest_lan_ip = None
for line in addrs:
ip = line.split()[3].split("/")[0]
if not ip.startswith("10.77."):
guest_lan_ip = ip
assert any("10.77.0.2/24" in line for line in addrs), f"static service-net address missing in bridged mode: {addrs}"
assert guest_lan_ip == "192.168.1.100", f"guest should have been handed its fixed LAN address, got: {addrs}"
# Both default routes are in place: the LAN's (metric 100, in use) and
# the host service-net fallback (metric 200) that NAT mode relies on.
machine.succeed("ip -4 route show default | grep -q 'default via 192.168.1.2'")
machine.succeed("ip -4 route show default | grep -q 'default via 10.77.0.1'")
# Guest -> LAN: the guest reaches a service on the LAN node.
lan.succeed("systemd-run --collect -u lanhttpd python3 -m http.server 8000 --bind 0.0.0.0")
machine.wait_until_succeeds("curl -fsS http://192.168.1.2:8000/ >/dev/null")
# LAN -> guest: a plain device on the LAN can connect to a port the
# guest exposes - the guest's own firewall still decides what answers
# (here, sshd on 22).
lan.wait_until_succeeds(f"bash -c 'exec 3<>/dev/tcp/{guest_lan_ip}/22'")
'';
}