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 blitz
27 ];
28 };
29
30 nodes.machine =
31 { lib, ... }:
32 {
33 networking.hostName = hostName;
34 networking.domain = domain;
35
36 environment.systemPackages = with pkgs; [
37 inetutils
38 ];
39 };
40
41 testScript =
42 { nodes, ... }:
43 ''
44 start_all()
45
46 machine = ${hostName}
47
48 machine.systemctl("start network-online.target")
49 machine.wait_for_unit("network-online.target")
50
51 # Test if NixOS computes the correct FQDN (either a FQDN or an error/null):
52 assert "${getStr nodes.machine.networking.fqdn}" == "${getStr fqdnOrNull}"
53
54 # The FQDN, domain name, and hostname detection should work as expected:
55 assert "${fqdn}" == machine.succeed("hostname --fqdn").strip()
56 assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip()
57 assert (
58 "${hostName}"
59 == machine.succeed(
60 'hostnamectl status | grep "Static hostname" | cut -d: -f2'
61 ).strip()
62 )
63
64 # 127.0.0.1 and ::1 should resolve back to "localhost":
65 assert (
66 "localhost" == machine.succeed("getent hosts 127.0.0.1 | awk '{print $2}'").strip()
67 )
68 assert "localhost" == machine.succeed("getent hosts ::1 | awk '{print $2}'").strip()
69
70 # 127.0.0.2 should resolve back to the FQDN and hostname:
71 fqdn_and_host_name = "${optionalString (domain != null) "${hostName}.${domain} "}${hostName}"
72 assert (
73 fqdn_and_host_name
74 == machine.succeed("getent hosts 127.0.0.2 | awk '{print $2,$3}'").strip()
75 )
76
77 assert "${fqdn}" == machine.succeed("getent hosts ${hostName} | awk '{print $2}'").strip()
78 '';
79 };
80
81in
82{
83 noExplicitDomain = makeHostNameTest "ahost" null null;
84
85 explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
86}