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