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 = { pkgs, ... }: {
18 imports = [ common/user-account.nix ];
19 networking.extraHosts = ''
20 2001:db8::1 somehost.test
21 192.0.2.1 somehost.test
24 systemd.services.sockdump = {
25 wantedBy = [ "multi-user.target" ];
27 # necessary for bcc to unpack kernel headers and invoke modprobe
32 environment.PYTHONUNBUFFERED = "1";
35 ExecStart = "${pkgs.sockdump}/bin/sockdump /var/run/nscd/socket";
36 Restart = "on-failure";
43 withGlibcNscd.configuration = { ... }: {
44 services.nscd.enableNsncd = false;
46 withUnscd.configuration = { ... }: {
47 services.nscd.enableNsncd = false;
48 services.nscd.package = pkgs.unscd;
53 testScript = { nodes, ... }:
55 specialisations = "${nodes.machine.system.build.toplevel}/specialisation";
58 # Regression test for https://github.com/NixOS/nixpkgs/issues/50273
59 def test_dynamic_user():
60 with subtest("DynamicUser actually allocates a user"):
61 assert "iamatest" in machine.succeed(
62 "systemd-run --pty --property=Type=oneshot --property=DynamicUser=yes --property=User=iamatest whoami"
65 # Test resolution of somehost.test with getent', to make sure we go via
67 def test_host_lookups():
68 with subtest("host lookups via nscd protocol"):
70 output = machine.succeed("${getent'} ahosts somehost.test")
71 assert "192.0.2.1" in output
72 assert "2001:db8::1" in output
75 output = machine.succeed("${getent'} ahostsv4 somehost.test")
76 assert "192.0.2.1" in output
77 assert "2001:db8::1" not in output
80 output = machine.succeed("${getent'} ahostsv6 somehost.test")
81 assert "192.0.2.1" not in output
82 assert "2001:db8::1" in output
84 # reverse lookups (hosts)
85 assert "somehost.test" in machine.succeed("${getent'} hosts 2001:db8::1")
86 assert "somehost.test" in machine.succeed("${getent'} hosts 192.0.2.1")
89 # Test host resolution via nss modules works
90 # We rely on nss-myhostname in this case, which resolves *.localhost and
92 # We don't need to use getent' here, as non-glibc nss modules can only be
93 # discovered via nscd.
94 def test_nss_myhostname():
95 with subtest("nss-myhostname provides hostnames (ahosts)"):
97 output = machine.succeed("getent ahosts foobar.localhost")
98 assert "::1" in output
99 assert "127.0.0.1" in output
102 output = machine.succeed("getent ahostsv4 foobar.localhost")
103 assert "::1" not in output
104 assert "127.0.0.1" in output
107 output = machine.succeed("getent ahostsv6 foobar.localhost")
108 assert "::1" in output
109 assert "127.0.0.1" not in output
112 machine.wait_for_unit("default.target")
114 # give sockdump some time to finish attaching.
117 # Test all tests with glibc-nscd.
120 test_nss_myhostname()
122 with subtest("glibc-nscd"):
123 machine.succeed('${specialisations}/withGlibcNscd/bin/switch-to-configuration test')
124 machine.wait_for_unit("default.target")
128 test_nss_myhostname()
130 with subtest("unscd"):
131 machine.succeed('${specialisations}/withUnscd/bin/switch-to-configuration test')
132 machine.wait_for_unit("default.target")
134 # known to fail, unscd doesn't load external NSS modules
135 # test_dynamic_user()
139 # known to fail, unscd doesn't load external NSS modules
140 # test_nss_myhostname()