2023-03-07 12:28:02 +00:00
|
|
|
{ system ? builtins.currentSystem
|
|
|
|
, config ? { }
|
|
|
|
, pkgs ? import ../.. { inherit system config; }
|
2020-05-12 21:48:27 +00:00
|
|
|
}:
|
|
|
|
|
|
|
|
with import ../lib/testing-python.nix { inherit system pkgs; };
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
2020-10-10 11:30:47 +00:00
|
|
|
makeHostNameTest = hostName: domain: fqdnOrNull:
|
2020-05-12 21:48:27 +00:00
|
|
|
let
|
|
|
|
fqdn = hostName + (optionalString (domain != null) ".${domain}");
|
2020-10-10 11:30:47 +00:00
|
|
|
getStr = str: # maybeString2String
|
|
|
|
let res = builtins.tryEval str;
|
|
|
|
in if (res.success && res.value != null) then res.value else "null";
|
2020-05-12 21:48:27 +00:00
|
|
|
in
|
2023-03-07 12:28:02 +00:00
|
|
|
makeTest {
|
|
|
|
name = "hostname-${fqdn}";
|
|
|
|
meta = with pkgs.lib.maintainers; {
|
|
|
|
maintainers = [ primeos blitz ];
|
|
|
|
};
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
nodes.machine = { lib, ... }: {
|
|
|
|
networking.hostName = hostName;
|
|
|
|
networking.domain = domain;
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
environment.systemPackages = with pkgs; [
|
|
|
|
inetutils
|
|
|
|
];
|
|
|
|
};
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
testScript = { nodes, ... }: ''
|
|
|
|
start_all()
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
machine = ${hostName}
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
machine.wait_for_unit("network-online.target")
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
# Test if NixOS computes the correct FQDN (either a FQDN or an error/null):
|
|
|
|
assert "${getStr nodes.machine.config.networking.fqdn}" == "${getStr fqdnOrNull}"
|
2020-10-10 11:30:47 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
# The FQDN, domain name, and hostname detection should work as expected:
|
|
|
|
assert "${fqdn}" == machine.succeed("hostname --fqdn").strip()
|
|
|
|
assert "${optionalString (domain != null) domain}" == machine.succeed("dnsdomainname").strip()
|
|
|
|
assert (
|
|
|
|
"${hostName}"
|
|
|
|
== machine.succeed(
|
|
|
|
'hostnamectl status | grep "Static hostname" | cut -d: -f2'
|
|
|
|
).strip()
|
|
|
|
)
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
# 127.0.0.1 and ::1 should resolve back to "localhost":
|
|
|
|
assert (
|
|
|
|
"localhost" == machine.succeed("getent hosts 127.0.0.1 | awk '{print $2}'").strip()
|
|
|
|
)
|
|
|
|
assert "localhost" == machine.succeed("getent hosts ::1 | awk '{print $2}'").strip()
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2023-03-07 12:28:02 +00:00
|
|
|
# 127.0.0.2 should resolve back to the FQDN and hostname:
|
|
|
|
fqdn_and_host_name = "${optionalString (domain != null) "${hostName}.${domain} "}${hostName}"
|
|
|
|
assert (
|
|
|
|
fqdn_and_host_name
|
|
|
|
== machine.succeed("getent hosts 127.0.0.2 | awk '{print $2,$3}'").strip()
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2020-05-12 21:48:27 +00:00
|
|
|
|
|
|
|
in
|
|
|
|
{
|
2020-10-10 11:30:47 +00:00
|
|
|
noExplicitDomain = makeHostNameTest "ahost" null null;
|
2020-05-12 21:48:27 +00:00
|
|
|
|
2020-10-10 11:30:47 +00:00
|
|
|
explicitDomain = makeHostNameTest "ahost" "adomain" "ahost.adomain";
|
2020-05-12 21:48:27 +00:00
|
|
|
}
|