Add unit tests for netinfo_to_resolv_conf

Use shunit2 for adding some unit tests to cover the functionality of
`_generate_hosts_content` and `netinfo_to_resolv_conf`.

Signed-off-by: Benjamin Drung <bdrung@debian.org>
This commit is contained in:
Benjamin Drung 2022-05-06 17:10:08 +02:00 committed by Benjamin Drung
parent 3d29bf9098
commit 5339f2575d
5 changed files with 94 additions and 1 deletions

2
debian/control vendored
View File

@ -3,7 +3,7 @@ Section: utils
Priority: optional
Uploaders: Michael Prokop <mika@debian.org>, Ben Hutchings <benh@debian.org>
Maintainer: Debian kernel team <debian-kernel@lists.debian.org>
Build-Depends: debhelper-compat (= 12), bash-completion
Build-Depends: debhelper-compat (= 12), bash-completion, shunit2 <!nocheck>
Rules-Requires-Root: no
Standards-Version: 4.1.5
Vcs-Browser: https://salsa.debian.org/kernel-team/initramfs-tools

5
debian/rules vendored
View File

@ -22,3 +22,8 @@ override_dh_install:
override_dh_installinitramfs:
@:
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
tests/functions_test
endif

54
tests/functions_test Executable file
View File

@ -0,0 +1,54 @@
#!/bin/sh
set -u
# Unit tests for scripts/functions
TEST_DIR="${0%/*}"
ROOT_DIR="$TEST_DIR/.."
CR="
"
oneTimeSetUp() {
. "$ROOT_DIR/scripts/functions"
}
HOSTS_FOOTER="
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters"
test_generate_hosts_content_with_domain() {
hosts_content=$(_generate_hosts_content example com)
assertEquals "127.0.0.1 localhost${CR}127.0.1.1 example.com example${CR}${HOSTS_FOOTER}" "$hosts_content"
}
test_generate_hosts_content_without_domain() {
hosts_content=$(_generate_hosts_content example "")
assertEquals "127.0.0.1 localhost${CR}127.0.1.1 example${CR}${HOSTS_FOOTER}" "$hosts_content"
}
test_netinfo_to_resolv_conf_IPv4() {
resolv_conf=$(netinfo_to_resolv_conf - "$TEST_DIR/netinfo/net-eth0.conf")
assertEquals "domain example.net${CR}nameserver 192.0.2.42${CR}search example.net. example.com." "$resolv_conf"
}
test_netinfo_to_resolv_conf_duplicates() {
resolv_conf=$(netinfo_to_resolv_conf - "$TEST_DIR/netinfo/net-eth0.conf" "$TEST_DIR/netinfo/net-eth0.conf")
assertEquals "domain example.net${CR}nameserver 192.0.2.42${CR}search example.net. example.com." "$resolv_conf"
}
test_netinfo_to_resolv_conf_mutliple() {
resolv_conf=$(netinfo_to_resolv_conf - "$TEST_DIR/netinfo/net-eth0.conf" "$TEST_DIR/netinfo/net-eth1.conf")
assertEquals "domain example.org${CR}nameserver 192.0.2.42${CR}nameserver 192.0.2.84${CR}search example.net. example.com. example.org." "$resolv_conf"
}
test_netinfo_to_resolv_conf_write_output() {
tmpfile=$(mktemp -t initramfs-tools.XXXXXXXXXX)
netinfo_to_resolv_conf "$tmpfile" "$TEST_DIR/netinfo/net-eth0.conf"
assertEquals "domain example.net${CR}nameserver 192.0.2.42${CR}search example.net. example.com." "$(cat "$tmpfile")"
rm -f "$tmpfile"
}
# Load shUnit2.
. shunit2

View File

@ -0,0 +1,17 @@
DEVICE='eth0'
PROTO='dhcp'
IPV4ADDR='192.0.2.101'
IPV4BROADCAST='192.0.2.255'
IPV4NETMASK='255.255.255.0'
IPV4GATEWAY='192.0.2.1'
IPV4DNS0='192.0.2.42'
IPV4DNS1='0.0.0.0'
HOSTNAME='example-host'
DNSDOMAIN='example.net'
NISDOMAIN=''
ROOTSERVER='192.0.2.1'
ROOTPATH=''
filename=''
UPTIME='4'
DHCPLEASETIME='86400'
DOMAINSEARCH='example.net. example.com.'

View File

@ -0,0 +1,17 @@
DEVICE='eth1'
PROTO='dhcp'
IPV4ADDR='192.0.2.150'
IPV4BROADCAST='192.0.2.255'
IPV4NETMASK='255.255.255.0'
IPV4GATEWAY='192.0.2.254'
IPV4DNS0='192.0.2.84'
IPV4DNS1='0.0.0.0'
HOSTNAME='example-host'
DNSDOMAIN='example.org'
NISDOMAIN=''
ROOTSERVER='192.0.2.254'
ROOTPATH=''
filename=''
UPTIME='4'
DHCPLEASETIME='86400'
DOMAINSEARCH='example.org.'