Skip to content

Commit

Permalink
nixos/tests/hostname: init (check system's host name)
Browse files Browse the repository at this point in the history
NixOS currently has issues with setting the FQDN of a system in a way
where standard tools work. In order to help with experimentation and
avoid regressions, add a test that checks that the hostname is
reported as the user wanted it to be.

Co-authored-by: Michael Weiss <dev.primeos@gmail.com>
  • Loading branch information
blitz and primeos committed May 25, 2020
1 parent 234d95a commit 837ec31
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ in
hitch = handleTest ./hitch {};
hocker-fetchdocker = handleTest ./hocker-fetchdocker {};
home-assistant = handleTest ./home-assistant.nix {};
hostname = handleTest ./hostname.nix {};
hound = handleTest ./hound.nix {};
hydra = handleTest ./hydra {};
hydra-db-migration = handleTest ./hydra/db-migration.nix {};
Expand Down
66 changes: 66 additions & 0 deletions nixos/tests/hostname.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{ system ? builtins.currentSystem,
config ? {},
pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;

let
makeHostNameTest = hostName: domain:
let
fqdn = hostName + (optionalString (domain != null) ".${domain}");
in
makeTest {
name = "hostname-${fqdn}";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ primeos blitz ];
};

machine = { lib, ... }: {
networking.hostName = hostName;
networking.domain = domain;

environment.systemPackages = with pkgs; [
inetutils
];
};

testScript = ''
start_all()
machine = ${hostName}
machine.wait_for_unit("network-online.target")
# 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()
)
# 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()
# 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()
)
'';
};

in
{
noExplicitDomain = makeHostNameTest "ahost" null;

explicitDomain = makeHostNameTest "ahost" "adomain";
}

0 comments on commit 837ec31

Please sign in to comment.