1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

test: add a couple of tests for nss-myhostname

This commit is contained in:
Frantisek Sumsal 2023-06-27 16:15:24 +02:00
parent 7f5f781cf4
commit 1a36d2672f
3 changed files with 131 additions and 0 deletions

View File

@ -3,6 +3,7 @@
set -e
TEST_DESCRIPTION="test hostnamed"
NSPAWN_ARGUMENTS="--private-network"
# shellcheck source=test/test-functions
. "${TEST_BASE_DIR:?}/test-functions"

View File

@ -132,6 +132,97 @@ EOF
assert_not_in 'Firmware Date' "$(hostnamectl)"
}
testcase_nss-myhostname() {
local database host i
HOSTNAME="$(hostnamectl hostname)"
# Set up a dummy network for _gateway and _outbound labels
ip link add foo type dummy
ip link set up dev foo
ip addr add 10.0.0.2/24 dev foo
for i in {128..150}; do
ip addr add "10.0.0.$i/24" dev foo
done
ip route add 10.0.0.1 dev foo
ip route add default via 10.0.0.1 dev foo
# Note: `getent hosts` probes gethostbyname2(), whereas `getent ahosts` probes gethostbyname3()
# and gethostbyname4() (through getaddrinfo() -> gaih_inet() -> get_nss_addresses())
getent hosts -s myhostname
getent ahosts -s myhostname
# With IPv6 disabled
sysctl -w net.ipv6.conf.all.disable_ipv6=1
# Everything under .localhost and .localhost.localdomain should resolve to localhost
for host in {foo.,foo.bar.baz.,.,}localhost{,.} {foo.,foo.bar.baz.,.,}localhost.localdomain{,.}; do
run_and_grep "^127\.0\.0\.1\s+localhost$" getent hosts -s myhostname "$host"
run_and_grep "^127\.0\.0\.1\s+STREAM\s+localhost" getent ahosts -s myhostname "$host"
run_and_grep "^127\.0\.0\.1\s+STREAM\s+localhost" getent ahostsv4 -s myhostname "$host"
(! getent ahostsv6 -s myhostname localhost)
done
for i in 2 {128..150}; do
run_and_grep "^10\.0\.0\.$i\s+$HOSTNAME$" getent hosts -s myhostname "$HOSTNAME"
run_and_grep "^10\.0\.0\.$i\s+" getent ahosts -s myhostname "$HOSTNAME"
run_and_grep "^10\.0\.0\.$i\s+" getent ahostsv4 -s myhostname "$HOSTNAME"
run_and_grep "^10\.0\.0\.$i\s+$HOSTNAME$" getent hosts -s myhostname "10.0.0.$i"
run_and_grep "^10\.0\.0\.$i\s+STREAM\s+10\.0\.0\.$i$" getent ahosts -s myhostname "10.0.0.$i"
run_and_grep "^10\.0\.0\.$i\s+STREAM\s+10\.0\.0\.$i$" getent ahostsv4 -s myhostname "10.0.0.$i"
done
for database in hosts ahosts ahostsv4 ahostsv6; do
(! getent "$database" -s myhostname ::1)
done
(! getent ahostsv6 -s myhostname "$HOSTNAME")
run_and_grep -n "^fe80:[^ ]+\s+STREAM$" getent ahosts -s myhostname "$HOSTNAME"
# With IPv6 enabled
sysctl -w net.ipv6.conf.all.disable_ipv6=0
# Everything under .localhost and .localhost.localdomain should resolve to localhost
for host in {foo.,foo.bar.baz.,.,}localhost{,.} {foo.,foo.bar.baz.,.,}localhost.localdomain{,.}; do
run_and_grep "^::1\s+localhost$" getent hosts -s myhostname "$host"
run_and_grep "^::1\s+STREAM" getent ahosts -s myhostname "$host"
run_and_grep "^127\.0\.0\.1\s+STREAM" getent ahosts -s myhostname "$host"
run_and_grep "^127\.0\.0\.1\s+STREAM" getent ahostsv4 -s myhostname "$host"
run_and_grep -n "^::1\s+STREAM" getent ahostsv4 -s myhostname "$host"
run_and_grep "^::1\s+STREAM" getent ahostsv6 -s myhostname "$host"
run_and_grep -n "^127\.0\.0\.1\s+STREAM" getent ahostsv6 -s myhostname "$host"
done
for i in 2 {128..150}; do
run_and_grep "^10\.0\.0\.$i\s+" getent ahosts -s myhostname "$HOSTNAME"
run_and_grep "^10\.0\.0\.$i\s+" getent ahostsv4 -s myhostname "$HOSTNAME"
run_and_grep "^10\.0\.0\.$i\s+STREAM\s+10\.0\.0\.$i$" getent ahosts -s myhostname "10.0.0.$i"
run_and_grep "^10\.0\.0\.$i\s+STREAM\s+10\.0\.0\.$i$" getent ahostsv4 -s myhostname "10.0.0.$i"
done
run_and_grep "^fe80:[^ ]+\s+$HOSTNAME$" getent hosts -s myhostname "$HOSTNAME"
run_and_grep "^fe80:[^ ]+\s+STREAM" getent ahosts -s myhostname "$HOSTNAME"
run_and_grep "^127\.0\.0\.1\s+localhost$" getent hosts -s myhostname 127.0.0.1
run_and_grep "^127\.0\.0\.1\s+STREAM\s+127\.0\.0\.1$" getent ahosts -s myhostname 127.0.0.1
run_and_grep "^::ffff:127\.0\.0\.1\s+STREAM\s+127\.0\.0\.1$" getent ahostsv6 -s myhostname 127.0.0.1
run_and_grep "^127\.0\.0\.2\s+$HOSTNAME$" getent hosts -s myhostname 127.0.0.2
run_and_grep "^::1\s+localhost $HOSTNAME$" getent hosts -s myhostname ::1
run_and_grep "^::1\s+STREAM\s+::1$" getent ahosts -s myhostname ::1
(! getent ahostsv4 -s myhostname ::1)
# _gateway
for host in _gateway{,.} 10.0.0.1; do
run_and_grep "^10\.0\.0\.1\s+_gateway$" getent hosts -s myhostname "$host"
run_and_grep "^10\.0\.0\.1\s+STREAM" getent ahosts -s myhostname "$host"
done
# _outbound
for host in _outbound{,.} 10.0.0.2; do
run_and_grep "^10\.0\.0\.2\s+" getent hosts -s myhostname "$host"
run_and_grep "^10\.0\.0\.2\s+STREAM" getent ahosts -s myhostname "$host"
done
# Non-existent records
for database in hosts ahosts ahostsv4 ahostsv6; do
(! getent "$database" -s myhostname this.should.not.exist)
done
(! getent hosts -s myhostname 10.254.254.1)
(! getent hosts -s myhostname fd00:dead:beef:cafe::1)
}
: >/failed
run_testcases

View File

@ -57,6 +57,45 @@ assert_rc() {(
assert_eq "$rc" "$exp"
)}
run_and_grep() {(
set +ex
local expression
local log ec
local exp_ec=0
# Invert the grep condition - i.e. check if the expression is _not_ in command's output
if [[ "${1:?}" == "-n" ]]; then
exp_ec=1
shift
fi
expression="${1:?}"
shift
if [[ $# -eq 0 ]]; then
echo >&2 "FAIL: Not enough arguments for ${FUNCNAME[0]}()"
return 1
fi
log="$(mktemp)"
if ! "$@" |& tee "${log:?}"; then
echo >&2 "FAIL: Command '$*' failed"
return 1
fi
grep -qE "$expression" "$log" && ec=0 || ec=$?
if [[ "$exp_ec" -eq 0 && "$ec" -ne 0 ]]; then
echo >&2 "FAIL: Expression '$expression' not found in the output of '$*'"
return 1
elif [[ "$exp_ec" -ne 0 && "$ec" -eq 0 ]]; then
echo >&2 "FAIL: Expression '$expression' found in the output of '$*'"
return 1
fi
rm -f "$log"
)}
get_cgroup_hierarchy() {
case "$(stat -c '%T' -f /sys/fs/cgroup)" in
cgroup2fs)