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.wait_for_unit("network-online.target")
38
39 # Test if NixOS computes the correct FQDN (either a FQDN or an error/null):
40 assert "${getStr nodes.machine.networking.fqdn}" == "${getStr fqdnOrNull}"
41
42 # The FQDN, domain name, and hostname detection should work as expected:
43 assert "${fqdn}" == machine.succeed("hostname --fqdn").strip()
44 assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip()
45 assert (
46 "${hostName}"
47 == machine.succeed(
48 'hostnamectl status | grep "Static hostname" | cut -d: -f2'
49 ).strip()
50 )
51
52 # 127.0.0.1 and ::1 should resolve back to "localhost":
53 assert (
54 "localhost" == machine.succeed("getent hosts 127.0.0.1 | awk '{print $2}'").strip()
55 )
56 assert "localhost" == machine.succeed("getent hosts ::1 | awk '{print $2}'").strip()
57
58 # 127.0.0.2 should resolve back to the FQDN and hostname:
59 fqdn_and_host_name = "${optionalString (domain != null) "${hostName}.${domain} "}${hostName}"
60 assert (
61 fqdn_and_host_name
62 == machine.succeed("getent hosts 127.0.0.2 | awk '{print $2,$3}'").strip()
63 )
64 '';
65 };
66
67in
68{
69 noExplicitDomain = makeHostNameTest "ahost" null null;
70
71 explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
72}