# Inspired by this file: /nixpkgs/nixos/tests/nsd.nix { packageSets, vhack, ... }: let pkgs = packageSets.stable; common = {...}: { networking.firewall.enable = false; networking.dhcpcd.enable = false; }; mkClient = version: { lib, nodes, ... }: let name = if version == "ipv4" then "primaryIPAddress" else if version == "ipv6" then "primaryIPv6Address" else builtins.throw "Not a valid ip version: ${version}"; in { environment.systemPackages = [pkgs.dig pkgs.dig.dnsutils]; imports = [common]; networking.nameservers = lib.mkForce [ nodes.server.networking.${name} ]; }; in vhack.runTest { name = "dns"; # Problem: The `runTest` function wants to setup TLS certificates for everything and # sets up it's own dns server for that. That unfortunately conflicts with our test # here. But we are implicitly testing dns anyhow with the other tests, so this test # is not really necessary. ignore = true; nodes = { server = { config, lib, ... }: { imports = [ common ]; vhack = { dns = { enable = true; interfaces = [ config.networking.primaryIPAddress config.networking.primaryIPv6Address ]; zones = { "example.com" = { SOA = { nameServer = "ns"; adminEmail = "admin@example.com"; serial = 2024012301; }; useOrigin = false; NS = [ "ns.example.com." ]; subdomains = { ns = { A = ["192.168.1.3"]; }; ipv4 = { A = ["1.2.3.4"]; }; ipv6 = { AAAA = ["dead:beef::1"]; }; openpgpkey = { TXT = ["Hi!"]; }; }; }; }; }; }; }; clientV4 = mkClient "ipv4"; clientV6 = mkClient "ipv6"; }; services = [ {clientV4 = "network.target";} {clientV6 = "network.target";} {server = "nsd.service";} ]; ports = [ {server = 53;} ]; testScript = {nodes, ...}: /* python */ '' def assert_host(ipVersion, dnsRecordType, dnsQuery, expected): self = clientV4 if ipVersion == 4 else clientV6 out = self.succeed(f"host -{ipVersion} -t {dnsRecordType} {dnsQuery}").rstrip() self.log(f"output: {out}") import re assert re.search(expected, out), f"DNS IPv{ipVersion} dnsQuery on {dnsQuery} gave '{out}' instead of '{expected}'" for ipv in 4, 6: with subtest(f"IPv{ipv}"): assert_host(ipv, "a", "example.com", "has no [^ ]+ record") assert_host(ipv, "aaaa", "example.com", "has no [^ ]+ record") assert_host(ipv, "soa", "example.com", "SOA.*?admin\\.example\\.com") assert_host(ipv, "a", "ipv4.example.com", "address 1.2.3.4$") assert_host(ipv, "aaaa", "ipv6.example.com", "address dead:beef::1$") assert_host(ipv, "txt", "openpgpkey.example.com", "descriptive text \"Hi!\"$") ''; }