2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # shellcheck disable=SC2016
7 # shellcheck source=test/units/util.sh
8 .
"$(dirname "$0")"/util.sh
13 machinectl
kill --signal=KILL nss-mymachines-
{noip
,singleip
,manyips
}
14 mountpoint
-q /var
/lib
/machines
&& timeout
10 sh
-c "until umount /var/lib/machines; do sleep .5; done"
15 rm -f /run
/systemd
/nspawn
/*.nspawn
20 # Mount temporary directory over /var/lib/machines to not pollute the image
21 mkdir
-p /var
/lib
/machines
22 mount
--bind "$(mktemp --tmpdir=/var/tmp -d)" /var
/lib
/machines
24 # Create a bunch of containers that:
25 # 1) Have no IP addresses assigned
26 create_dummy_container
/var
/lib
/machines
/nss-mymachines-noip
27 cat >/var
/lib
/machines
/nss-mymachines-noip
/sbin
/init
<<\EOF
30 ip addr show dev ve-noip
33 # Run the sleep command asynchronously, so bash is able to process signals
38 # 2) Have one IP address assigned (IPv4 only)
39 create_dummy_container
/var
/lib
/machines
/nss-mymachines-singleip
40 cat >/var
/lib
/machines
/nss-mymachines-singleip
/sbin
/init
<<\EOF
43 ip addr add
10.1.0.2/24 dev ve-singleip
44 ip addr show dev ve-singleip
51 # 3) Have bunch of IP addresses assigned (both IPv4 and IPv6)
52 create_dummy_container
/var
/lib
/machines
/nss-mymachines-manyips
53 cat >/var
/lib
/machines
/nss-mymachines-manyips
/sbin
/init
<<\EOF
56 ip addr add
10.2.0.2/24 dev ve-manyips
57 for i
in {100.
.120}; do
58 ip addr add
10.2.0.
$i/24 dev ve-manyips
60 ip addr add fd00
:dead
:beef
:cafe
::2/64 dev ve-manyips nodad
61 ip addr show dev ve-manyips
68 # Create the respective .nspawn config files
69 mkdir
-p /run
/systemd
/nspawn
70 for container
in noip singleip manyips
; do
71 cat >"/run/systemd/nspawn/nss-mymachines-$container.nspawn" <<EOF
76 VirtualEthernetExtra=ve-$container
80 # Start the containers and wait until all of them are initialized
81 machinectl start nss-mymachines-
{noip
,singleip
,manyips
}
82 for container
in nss-mymachines-
{noip
,singleip
,manyips
}; do
83 timeout
30 bash
-xec "while [[ ! -e /var/lib/machines/$container/initialized ]]; do sleep .5; done"
86 # We need to configure the dummy interfaces on the "outside" as well for `getent {ahosts4,ahosts6}` to work
87 # properly. This is caused by getaddrinfo() calling _check_pf() that iterates through all interfaces and
88 # notes if any of them has an IPv4/IPv6 - this is then used together with AF_INET/AF_INET6 to determine if we
89 # can ever return a valid answer, and if we configured the container interfaces only in the container, we
90 # would have no valid IPv4/IPv6 on the "outside" (as we don't set up any other netdev) which would make
91 # getaddrinfo() return EAI_NONAME without ever asking nss-mymachines.
92 ip addr add
10.1.0.1/24 dev ve-singleip
93 ip addr add
10.2.0.1/24 dev ve-manyips
94 ip addr add fd00
:dead
:beef
:cafe
::1/64 dev ve-manyips nodad
96 getent hosts
-s mymachines
97 getent ahosts
-s mymachines
99 # And finally check if we can resolve the containers via nss-mymachines
100 for database
in hosts ahosts
{,v4
,v6
}; do
101 (! getent
"$database" -s mymachines nss-mymachines-noip
)
104 run_and_grep
"^10\.1\.0\.2\s+nss-mymachines-singleip$" getent hosts
-s mymachines nss-mymachines-singleip
105 run_and_grep
"^10\.1\.0\.2\s+STREAM" getent ahosts
-s mymachines nss-mymachines-singleip
106 run_and_grep
"^10\.1\.0\.2\s+STREAM" getent ahostsv4
-s mymachines nss-mymachines-singleip
107 run_and_grep
"^::ffff:10\.1\.0\.2\s+STREAM" getent ahostsv6
-s mymachines nss-mymachines-singleip
109 run_and_grep
"^fd00:dead:beef:cafe::2\s+nss-mymachines-manyips$" getent hosts
-s mymachines nss-mymachines-manyips
110 run_and_grep
"^fd00:dead:beef:cafe::2\s+STREAM" getent ahosts
-s mymachines nss-mymachines-manyips
111 run_and_grep
"^10\.2\.0\.2\s+STREAM" getent ahosts
-s mymachines nss-mymachines-manyips
112 for i
in {100.
.120}; do
113 run_and_grep
"^10\.2\.0\.$i\s+STREAM" getent ahosts
-s mymachines nss-mymachines-manyips
114 run_and_grep
"^10\.2\.0\.$i\s+STREAM" getent ahostsv4
-s mymachines nss-mymachines-manyips
116 run_and_grep
"^fd00:dead:beef:cafe::2\s+STREAM" getent ahostsv6
-s mymachines nss-mymachines-manyips
117 (! run_and_grep
"^fd00:" getent ahostsv4
-s mymachines nss-mymachines-manyips
)
118 (! run_and_grep
"^10\.2:" getent ahostsv6
-s mymachines nss-mymachines-manyips
)
120 # Multiple machines at once
121 run_and_grep
"^10\.1\.0\.2\s+nss-mymachines-singleip$" getent hosts
-s mymachines nss-mymachines-
{singleip
,manyips
}
122 run_and_grep
"^fd00:dead:beef:cafe::2\s+nss-mymachines-manyips$" getent hosts
-s mymachines nss-mymachines-
{singleip
,manyips
}
123 run_and_grep
"^10\.1\.0\.2\s+STREAM" getent ahosts
-s mymachines nss-mymachines-
{singleip
,manyips
}
124 run_and_grep
"^10\.2\.0\.2\s+STREAM" getent ahosts
-s mymachines nss-mymachines-
{singleip
,manyips
}
125 run_and_grep
"^fd00:dead:beef:cafe::2\s+STREAM" getent ahosts
-s mymachines nss-mymachines-
{singleip
,manyips
}
127 for database
in hosts ahosts ahostsv4 ahostsv6
; do
128 (! getent
"$database" -s mymachines foo-bar-baz
)
131 # getgrid(), getgrnam(), getpwuid(), and getpwnam() are explicitly handled by nss-mymachines, so probe them
133 (! getent group
-s mymachines foo
11)
134 (! getent passwd
-s mymachines foo
11)
136 machinectl stop nss-mymachines-
{noip
,singleip
,manyips
}