From 5b2621acbc27ceda93b3ad59a812e4bc5d030c24 Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Sun, 11 Oct 2026 00:59:32 -0400 Subject: [PATCH] networking tests --- flake.nix | 1 + modules/hermenix/packages.nix | 2 +- tests/host-vm-test.nix | 100 ++++++++++++++++++++++++++++++++++ tests/network-modes-check.nix | 34 +++++++++++- 4 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 tests/host-vm-test.nix diff --git a/flake.nix b/flake.nix index 2422fd4..00f40c6 100644 --- a/flake.nix +++ b/flake.nix @@ -123,6 +123,7 @@ }) // { network-modes = import ./tests/network-modes-check.nix { inherit pkgs; }; + host-vm = import ./tests/host-vm-test.nix { inherit pkgs self; }; }; }; } diff --git a/modules/hermenix/packages.nix b/modules/hermenix/packages.nix index bd45f13..d989273 100644 --- a/modules/hermenix/packages.nix +++ b/modules/hermenix/packages.nix @@ -14,7 +14,7 @@ in (pkgs.writeTextFile { name = "hermenix-completions"; destination = "/share/nushell/vendor/autoload/hermenix.nu"; - text = builtins.readFile ../nu/completions.nu; + text = builtins.readFile ../../nu/completions.nu; }) (pkgs.symlinkJoin { diff --git a/tests/host-vm-test.nix b/tests/host-vm-test.nix new file mode 100644 index 0000000..897e02a --- /dev/null +++ b/tests/host-vm-test.nix @@ -0,0 +1,100 @@ +# Host-side VM test: boots a NixOS host with services.hermenix enabled and +# asserts the runtime plumbing the module sets up - the bridge, the tap, the +# addresses, the nftables ruleset, the MAC clone, the on-bridge DHCP server, +# and the host binary cache. The QEMU process is stubbed so no nested +# virtualization is needed; the real QEMU command line is covered statically +# by tests/network-modes-check.nix. +# +# Run by the operator/CI, never by an agent (see GUIDELINES.md): +# nix build .#checks.x86_64-linux.host-vm -L +{ pkgs, self }: + +let + hostNode = + { lib, ... }: + { + imports = [ self.nixosModules.default ]; + + services.hermenix = { + enable = true; + network = { + externalInterface = "eth1"; + bridged = true; + lanAddress = "10.7.7.50"; + hostLanAddress = "10.7.7.2"; + lanGateway = "10.7.7.1"; + }; + snapshots.enable = false; + backup.enable = false; + }; + + # The hermes-vm unit joins the kvm group; a bare test node has none + # (libvirtd creates it on a real host). + users.groups.kvm = { }; + + # eth0 = SLiRP (management); eth1 = the "uplink" vlan the module bridges. + virtualisation.vlans = [ 1 ]; + + # Dummy images so hermes-vm's preStart passes, owned by the unit user so + # it can drop OVMF_VARS.fd alongside them. + systemd.tmpfiles.rules = [ + "d /var/lib/hermes-vm 0750 hermes-vm hermes-vm" + "d /var/lib/hermes-vm/system 0750 hermes-vm hermes-vm" + "d /var/lib/hermes-vm/state 0750 hermes-vm hermes-vm" + "f /var/lib/hermes-vm/system/system.raw 0600 hermes-vm hermes-vm -" + "f /var/lib/hermes-vm/state/state.raw 0600 hermes-vm hermes-vm -" + ]; + + # Stub QEMU: this test covers the host plumbing, not the guest. + systemd.services.hermes-vm.serviceConfig.ExecStart = + lib.mkForce "${pkgs.coreutils}/bin/sleep infinity"; + }; +in +pkgs.testers.runNixOSTest { + name = "hermenix-host"; + + globalTimeout = 600; + + # The module wraps pkgs.hermenix; a real host supplies it via an overlay. + # runNixOSTest pins node.pkgs, so extend it here. + node.pkgs = pkgs.lib.mkForce ( + pkgs.extend (_final: prev: { hermenix = self.packages.${prev.system}.hermenix; }) + ); + + nodes.host = hostNode; + + testScript = '' + host.start() + host.wait_for_unit("multi-user.target") + + for unit in [ + "hermes-vm-tap.service", + "hermes-vm.service", + "hermes-bridge-mac.service", + "hermes-lan-dhcp.service", + ]: + host.wait_for_unit(unit) + + # Bridge, tap, and the absorbed uplink. + host.succeed("ip link show hermes-br") + host.succeed("ip link show vm-hermes") + host.succeed("bridge link show dev vm-hermes | grep -q hermes-br") + host.succeed("bridge link show dev eth1 | grep -q hermes-br") + + # Service-net address plus the bridged host LAN address. + host.succeed("ip -4 addr show hermes-br | grep -q '10.77.0.1/24'") + host.succeed("ip -4 addr show hermes-br | grep -q '10.7.7.2/24'") + + # The bridge cloned the uplink MAC, so the host keeps its LAN identity. + host.succeed( + 'test "$(cat /sys/class/net/hermes-br/address)" = "$(cat /sys/class/net/eth1/address)"' + ) + + # The bridged L2 table and the guest-mark rule are loaded. + host.succeed("nft list ruleset | grep -q hermenix-bridge") + host.succeed("nft list ruleset | grep -q 'meta mark set 18501'") + + # The host binary cache answers on the service net. + host.wait_until_succeeds("${pkgs.curl}/bin/curl -fsS http://10.77.0.1:5000/nix-cache-info") + ''; +} diff --git a/tests/network-modes-check.nix b/tests/network-modes-check.nix index 472388b..915d6df 100644 --- a/tests/network-modes-check.nix +++ b/tests/network-modes-check.nix @@ -1,5 +1,6 @@ # Static evaluation check for the host module's two networking modes (NAT -# and bridged) and the guest's mode-agnostic NIC configuration. +# and bridged), its QEMU command line and unit sandbox, and the guest's +# mode-agnostic NIC configuration. # # Nothing here is built or booted: the check evaluates services.hermenix # (modules/host.nix) in both modes plus guest/common.nix, and asserts on the @@ -142,6 +143,8 @@ let brInput = br.config.networking.firewall.extraInputRules; brForward = br.config.networking.firewall.extraForwardRules; + vm = nat.config.systemd.services."hermes-vm".serviceConfig; + hostResults = { # --- NAT mode: the original behaviour has to be unchanged ---------- "nat: no bridged-mode bridge table" = !(natNft ? "hermenix-bridge"); @@ -209,6 +212,33 @@ let brNetworks."11-vm-hermes".networkConfig.Bridge == "hermes-br"; }; + # The QEMU command line and the unit's sandbox, which the host VM test + # (tests/host-vm-test.nix) cannot exercise without nested virtualization. + vmResults = { + "vm: qemu runs under kvm with smm" = has "-machine q35,accel=kvm,smm=on" vm.ExecStart; + "vm: tap netdev wired to the guest NIC" = + has "-netdev tap,id=net0,ifname=vm-hermes" vm.ExecStart + && has "-device virtio-net-pci,netdev=net0,mac=02:00:00:7e:00:01" vm.ExecStart; + "vm: system and state disks attached" = + has "id=system,format=raw" vm.ExecStart && has "id=state,format=raw" vm.ExecStart; + "vm: qmp socket exposed for hermenix vm control" = + has "qmp unix:/run/hermes-vm/qmp.sock" vm.ExecStart; + "vm: default devices (rng, vsock, qga) present" = + has "-device virtio-rng-pci" vm.ExecStart + && has "-device vhost-vsock-pci,guest-cid=77" vm.ExecStart + && has "name=org.qemu.guest_agent.0" vm.ExecStart; + "vm: optional devices off by default" = + !(has "-vga" vm.ExecStart) && !(has "virtio-balloon" vm.ExecStart); + "vm: sandbox hardening intact" = + vm.ProtectSystem == "strict" && vm.NoNewPrivileges == true && vm.DevicePolicy == "closed"; + "vm: device allowlist covers kvm, tun, vsock" = + vm.DeviceAllow == [ + "/dev/kvm rw" + "/dev/net/tun rw" + "/dev/vhost-vsock rw" + ]; + }; + # Guest module: must produce one NIC config that works in both modes. # # Evaluated against the full NixOS option set. # guest = lib.evalModules { @@ -239,7 +269,7 @@ let ]; }; - results = hostResults // guestResults; + results = hostResults // vmResults // guestResults; failing = builtins.filter (n: !results.${n}) (builtins.attrNames results); in