| 1 |
#!/bin/sh |
| 2 |
set -u |
| 3 |
|
| 4 |
# Unit tests for scripts/functions |
| 5 |
|
| 6 |
TEST_DIR="${0%/*}" |
| 7 |
ROOT_DIR="$TEST_DIR/.." |
| 8 |
CR=" |
| 9 |
" |
| 10 |
|
| 11 |
oneTimeSetUp() { |
| 12 |
. "$ROOT_DIR/scripts/functions" |
| 13 |
} |
| 14 |
|
| 15 |
HOSTS_FOOTER=" |
| 16 |
# The following lines are desirable for IPv6 capable hosts |
| 17 |
::1 localhost ip6-localhost ip6-loopback |
| 18 |
ff02::1 ip6-allnodes |
| 19 |
ff02::2 ip6-allrouters" |
| 20 |
|
| 21 |
test_generate_hosts_content_with_domain() { |
| 22 |
hosts_content=$(_generate_hosts_content example com) |
| 23 |
assertEquals "127.0.0.1 localhost${CR}127.0.1.1 example.com example${CR}${HOSTS_FOOTER}" "$hosts_content" |
| 24 |
} |
| 25 |
|
| 26 |
test_generate_hosts_content_without_domain() { |
| 27 |
hosts_content=$(_generate_hosts_content example "") |
| 28 |
assertEquals "127.0.0.1 localhost${CR}127.0.1.1 example${CR}${HOSTS_FOOTER}" "$hosts_content" |
| 29 |
} |
| 30 |
|
| 31 |
test_netinfo_to_resolv_conf_IPv4() { |
| 32 |
resolv_conf=$(netinfo_to_resolv_conf - "$TEST_DIR/netinfo/net-eth0.conf") |
| 33 |
assertEquals "domain example.net${CR}nameserver 192.0.2.42${CR}search example.net. example.com." "$resolv_conf" |
| 34 |
} |
| 35 |
|
| 36 |
test_netinfo_to_resolv_conf_duplicates() { |
| 37 |
resolv_conf=$(netinfo_to_resolv_conf - "$TEST_DIR/netinfo/net-eth0.conf" "$TEST_DIR/netinfo/net-eth0.conf") |
| 38 |
assertEquals "domain example.net${CR}nameserver 192.0.2.42${CR}search example.net. example.com." "$resolv_conf" |
| 39 |
} |
| 40 |
|
| 41 |
test_netinfo_to_resolv_conf_mutliple() { |
| 42 |
resolv_conf=$(netinfo_to_resolv_conf - "$TEST_DIR/netinfo/net-eth0.conf" "$TEST_DIR/netinfo/net-eth1.conf") |
| 43 |
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" |
| 44 |
} |
| 45 |
|
| 46 |
test_netinfo_to_resolv_conf_write_output() { |
| 47 |
tmpfile=$(mktemp -t initramfs-tools.XXXXXXXXXX) |
| 48 |
netinfo_to_resolv_conf "$tmpfile" "$TEST_DIR/netinfo/net-eth0.conf" |
| 49 |
assertEquals "domain example.net${CR}nameserver 192.0.2.42${CR}search example.net. example.com." "$(cat "$tmpfile")" |
| 50 |
rm -f "$tmpfile" |
| 51 |
} |
| 52 |
|
| 53 |
# Load shUnit2. |
| 54 |
. shunit2 |