at 24.11-pre 2.4 kB view raw
1{ system ? builtins.currentSystem 2, config ? { } 3, pkgs ? import ../.. { inherit system config; } 4}: 5 6with import ../lib/testing-python.nix { inherit system pkgs; }; 7with pkgs.lib; 8 9let 10 makeHostNameTest = hostName: domain: fqdnOrNull: 11 let 12 fqdn = hostName + (optionalString (domain != null) ".${domain}"); 13 getStr = str: # maybeString2String 14 let res = builtins.tryEval str; 15 in if (res.success && res.value != null) then res.value else "null"; 16 in 17 makeTest { 18 name = "hostname-${fqdn}"; 19 meta = with pkgs.lib.maintainers; { 20 maintainers = [ primeos blitz ]; 21 }; 22 23 nodes.machine = { lib, ... }: { 24 networking.hostName = hostName; 25 networking.domain = domain; 26 27 environment.systemPackages = with pkgs; [ 28 inetutils 29 ]; 30 }; 31 32 testScript = { nodes, ... }: '' 33 start_all() 34 35 machine = ${hostName} 36 37 machine.systemctl("start network-online.target") 38 machine.wait_for_unit("network-online.target") 39 40 # Test if NixOS computes the correct FQDN (either a FQDN or an error/null): 41 assert "${getStr nodes.machine.networking.fqdn}" == "${getStr fqdnOrNull}" 42 43 # The FQDN, domain name, and hostname detection should work as expected: 44 assert "${fqdn}" == machine.succeed("hostname --fqdn").strip() 45 assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip() 46 assert ( 47 "${hostName}" 48 == machine.succeed( 49 'hostnamectl status | grep "Static hostname" | cut -d: -f2' 50 ).strip() 51 ) 52 53 # 127.0.0.1 and ::1 should resolve back to "localhost": 54 assert ( 55 "localhost" == machine.succeed("getent hosts 127.0.0.1 | awk '{print $2}'").strip() 56 ) 57 assert "localhost" == machine.succeed("getent hosts ::1 | awk '{print $2}'").strip() 58 59 # 127.0.0.2 should resolve back to the FQDN and hostname: 60 fqdn_and_host_name = "${optionalString (domain != null) "${hostName}.${domain} "}${hostName}" 61 assert ( 62 fqdn_and_host_name 63 == machine.succeed("getent hosts 127.0.0.2 | awk '{print $2,$3}'").strip() 64 ) 65 ''; 66 }; 67 68in 69{ 70 noExplicitDomain = makeHostNameTest "ahost" null null; 71 72 explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain"; 73}