From 595f6e3f69702aa50f17d37eb49d9050257307a0 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 21 Nov 2026 16:17:35 +0000 Subject: tests: the run script in qemu, and a NixOS VM test of modules/host Both were only in the sandbox the project was developed in so far. tests/qemu builds the run script with a qemu stand-in for Firecracker (same kernel, initrd, drives, vsock via vhost-device-vsock; network via qemu's user networking, the guest's port 22 on 127.0.0.1:2222) and a test layer running a script at the end of boot, its output on the console. The run script still takes a tap, so it needs modules/host where it runs. tests/vm-host.nix is a NixOS VM test of modules/host, with network namespaces on veths in place of taps as guests: a VM's static address by name, tcp:// rules over IPv4 and IPv6 and by name, DNS, anti-spoofing, POST /check, the path units and the timer. --- tests/vm-host.nix | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 tests/vm-host.nix (limited to 'tests/vm-host.nix') diff --git a/tests/vm-host.nix b/tests/vm-host.nix new file mode 100644 index 0000000..04f66de --- /dev/null +++ b/tests/vm-host.nix @@ -0,0 +1,118 @@ +# NixOS VM test of modules/host: the module for real (systemd units, path +# units, timer, nftables, dnsmasq, nginx's checks), with network +# namespaces on veths in place of tv's taps fctap1 and fctap2 as guests: +# g1 as the VM box, with box's address (172.16.0.10, MAC 06:00:ac:10:00:0a), +# g2 without a name, with fctap2's own (172.16.0.202, ...:ca); SLAAC gives +# them the IPv6 addresses the host expects from those MACs. Another netns +# is an upstream with 42::1 and 198.51.100.1. Covers static addresses by +# name, the tcp:// rules (IPv4, IPv6, names), DNS, IPv6 port 443, +# anti-spoofing, POST /check; not the HTTPS proxy. +# +# nix-build tests/vm-host.nix # needs kvm +# nix-build -E '(import ./tests/vm-host.nix {}).driver' -o driver +# ./driver/bin/nixos-test-driver -o out # without kvm: TCG, slow +{ pkgs ? import {} }: +pkgs.testers.runNixOSTest { + name = "fc-host"; + nodes.host = { pkgs, ... }: { + # alice: fctap0, tv: fctap1 and fctap2 + imports = [ (import ../modules/host { + users = { tv = 2; alice = 1; }; + vms.box = { user = "tv"; ip = "172.16.0.10"; }; + }) ]; + users.users.tv.isNormalUser = true; + users.users.alice.isNormalUser = true; + networking.useNetworkd = true; + networking.nameservers = [ "127.0.0.1" ]; + networking.hosts."42::1" = [ "sshhost6.test" ]; + networking.hosts."198.51.100.1" = [ "sshhost.test" ]; + # As the host's firewall has to (see README.md). + networking.firewall.trustedInterfaces = [ "fcbr0" ]; + environment.systemPackages = [ pkgs.socat pkgs.dig pkgs.curl ]; + virtualisation.memorySize = 1024; + }; + testScript = '' + g1_ip6 = "fdfc:fc::400:acff:fe10:a" + g2_ip6 = "fdfc:fc::400:acff:fe10:ca" + + host.wait_for_unit("multi-user.target") + host.wait_for_unit("fcbr0-dnsmasq.service") + host.wait_for_unit("fcbr0-firewall.service") + # box on fctap1, as the run script does it + host.succeed("su tv -c 'echo box > /run/fc-egress/fctap1.name'") + host.wait_until_succeeds("test \"$(head -n 1 /run/fcbr0-addr/fctap1)\" = 172.16.0.10", timeout=60) + host.succeed(""" + set -e + guest() { # N TAP IP MAC + ip link del $2 + ip netns add g$1 + ip link add $2 type veth peer name eth0 netns g$1 + ip link set $2 master fcbr0 up + ip netns exec g$1 sysctl -qw net.ipv6.conf.all.use_tempaddr=0 net.ipv6.conf.default.use_tempaddr=0 + ip netns exec g$1 ip link set eth0 address $4 + ip netns exec g$1 ip link set lo up + ip netns exec g$1 ip addr add $3/24 dev eth0 + ip netns exec g$1 ip link set eth0 up + ip netns exec g$1 ip route add default via 172.16.0.1 + } + guest 1 fctap1 172.16.0.10 06:00:ac:10:00:0a + guest 2 fctap2 172.16.0.202 06:00:ac:10:00:ca + ip netns add U + ip link add up0 type veth peer name eth0 netns U + ip link set up0 up + ip addr add 42::fe/16 dev up0 nodad + ip addr add 198.51.100.254/24 dev up0 + ip netns exec U ip link set lo up + ip netns exec U ip link set eth0 up + ip netns exec U ip addr add 42::1/16 dev eth0 nodad + ip netns exec U ip addr add 198.51.100.1/24 dev eth0 + ip netns exec U ip -6 route add default via 42::fe + ip netns exec U ip route add default via 198.51.100.254 + """) + host.succeed("ip netns exec U socat TCP6-LISTEN:22,fork,reuseaddr SYSTEM:'echo ssh-ok' >&2 &") + host.succeed("ip netns exec U socat TCP6-LISTEN:443,fork,reuseaddr SYSTEM:'echo https-direct' >&2 &") + # networkd can take minutes under TCG + host.wait_until_succeeds(f"ip netns exec g1 ip -6 addr show dev eth0 | grep -q '{g1_ip6}/64'", timeout=300) + host.wait_until_succeeds(f"ip netns exec g2 ip -6 addr show dev eth0 | grep -q '{g2_ip6}/64'", timeout=60) + + def tcp(g, dst, port, extra=""): + return host.succeed(f"ip netns exec {g} timeout 5 socat -T3 - TCP:{dst}:{port}{extra} /dev/null || echo blocked").strip() + + def dig(g, t, name): + return host.execute(f"ip netns exec {g} dig +short {t} {name} @172.16.0.1")[1].strip() + + def check_uris(g, *uris): + body = "\\n".join(uris) + return host.succeed(f"printf '{body}\\n' | ip netns exec {g} curl -sS --data-binary @- http://172.16.0.1/check").split() + + def check(what, got, want): + print(f"{what}: {got}") + assert got == want, f"{what}: got {got!r}, want {want!r}" + + # rules via fc-egress and the path units + host.succeed("su tv -c 'fc-egress -l box allow tcp://[42::1]:22 tcp://[42::1]:443 tcp://sshhost6.test:22 tcp://198.51.100.1:22'") + host.fail("su tv -c 'fc-egress -l box allow tcp://[42::1/129]:22'") + host.succeed("su tv -c 'echo box > /run/fc-egress/fctap1'") + host.wait_until_succeeds("nft list set inet firecracker allow6_fctap1 | grep -q '42::1'", timeout=60) + + check("host reaches the upstream", host.succeed("timeout 5 socat -T3 - TCP6:[42::1]:22