···
1
+
import ./make-test-python.nix ({ pkgs, ... }:
3
+
# build a getent that itself doesn't see anything in /etc/hosts and
4
+
# /etc/nsswitch.conf, by using libredirect to steer its own requests to
6
+
# This means is /has/ to go via nscd to actuallly resolve any of the
7
+
# additionally configured hosts.
8
+
getent' = pkgs.writeScript "getent-without-etc-hosts" ''
9
+
export NIX_REDIRECTS=/etc/hosts=/dev/null:/etc/nsswitch.conf=/dev/null
10
+
export LD_PRELOAD=${pkgs.libredirect}/lib/libredirect.so
17
+
nodes.machine = { lib, ... }: {
18
+
imports = [ common/user-account.nix ];
19
+
networking.extraHosts = ''
20
+
2001:db8::1 somehost.test
21
+
192.0.2.1 somehost.test
27
+
machine.wait_for_unit("default.target")
29
+
# Regression test for https://github.com/NixOS/nixpkgs/issues/50273
30
+
with subtest("DynamicUser actually allocates a user"):
31
+
assert "iamatest" in machine.succeed(
32
+
"systemd-run --pty --property=Type=oneshot --property=DynamicUser=yes --property=User=iamatest whoami"
35
+
# Test resolution of somehost.test with getent', to make sure we go via nscd
36
+
with subtest("host lookups via nscd"):
38
+
output = machine.succeed("${getent'} ahosts somehost.test")
39
+
assert "192.0.2.1" in output
40
+
assert "2001:db8::1" in output
43
+
output = machine.succeed("${getent'} ahostsv4 somehost.test")
44
+
assert "192.0.2.1" in output
45
+
assert "2001:db8::1" not in output
48
+
output = machine.succeed("${getent'} ahostsv6 somehost.test")
49
+
assert "192.0.2.1" not in output
50
+
assert "2001:db8::1" in output
52
+
# reverse lookups (hosts)
53
+
assert "somehost.test" in machine.succeed("${getent'} hosts 2001:db8::1")
54
+
assert "somehost.test" in machine.succeed("${getent'} hosts 192.0.2.1")
57
+
# Test host resolution via nss modules works
58
+
# We rely on nss-myhostname in this case, which resolves *.localhost and
60
+
# We don't need to use getent' here, as non-glibc nss modules can only be
61
+
# discovered via nscd.
62
+
with subtest("nss-myhostname provides hostnames (ahosts)"):
64
+
output = machine.succeed("getent ahosts foobar.localhost")
65
+
assert "::1" in output
66
+
assert "127.0.0.1" in output
69
+
output = machine.succeed("getent ahostsv4 foobar.localhost")
70
+
assert "::1" not in output
71
+
assert "127.0.0.1" in output
74
+
output = machine.succeed("getent ahostsv6 foobar.localhost")
75
+
assert "::1" in output
76
+
assert "127.0.0.1" not in output
79
+
output = machine.succeed("getent ahosts _gateway")
81
+
# returns something like the following:
82
+
# 10.0.2.2 STREAM _gateway
89
+
# Verify we see both ip addresses
90
+
assert "10.0.2.2" in output
91
+
assert "fe80::2" in output