| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Test Framework for update-systemd-resolved. |
| 4 |
# Copyright (C) 2016, Jonathan Wright <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
|
| 16 |
# Set colour escape sequences |
| 17 |
RED='\033[0;31m' |
| 18 |
YELLOW='\033[1;33m' |
| 19 |
ORANGE='\033[0;33m' |
| 20 |
GREEN='\033[0;32m' |
| 21 |
DARK='\033[1;30m' |
| 22 |
RESET='\033[0m' |
| 23 |
# Set Pass/Fail signatures |
| 24 |
PASS="✓" |
| 25 |
FAIL="✗" |
| 26 |
# Counters |
| 27 |
COUNT_PASS=0 |
| 28 |
COUNT_FAIL=0 |
| 29 |
# Flag for determining whether a test script called the `runtest' function |
| 30 |
RUNTEST_CALLED=0 |
| 31 |
|
| 32 |
AUTOMATED_TESTING=1 |
| 33 |
|
| 34 |
function busctl { |
| 35 |
shift 4 |
| 36 |
_log "busctl called with: ${@}" |
| 37 |
# Set that busctl has been called |
| 38 |
busctl_called=1 |
| 39 |
|
| 40 |
case "${1}" in |
| 41 |
SetLinkDNS) |
| 42 |
shift 2 |
| 43 |
if [[ "${TEST_BUSCTL_DNS}" == "" ]]; then |
| 44 |
[[ "${ip_ifindex} ${TEST_BUSCTL_DNS}" == "${@}" ]] || \ |
| 45 |
_fail "SetLinkDNS was called and should not be: '${@}'" |
| 46 |
else |
| 47 |
[[ "${ip_ifindex} ${TEST_BUSCTL_DNS}" == "${@}" ]] && \ |
| 48 |
_pass "SetLinkDNS was called correctly" || \ |
| 49 |
_fail "SetLinkDNS was not given the correct arguments:\n" \ |
| 50 |
" Expected: '${ip_ifindex} ${TEST_BUSCTL_DNS}'\n" \ |
| 51 |
" Received: '${@}'" |
| 52 |
fi |
| 53 |
;; |
| 54 |
SetLinkDomains) |
| 55 |
shift 2 |
| 56 |
if [[ "${TEST_BUSCTL_DOMAINS}" == "" ]]; then |
| 57 |
[[ "${ip_ifindex} ${TEST_BUSCTL_DOMAINS}" == "${@}" ]] || \ |
| 58 |
_fail "SetLinkDomains was called and should not be: '${@}'" |
| 59 |
else |
| 60 |
[[ "${ip_ifindex} ${TEST_BUSCTL_DOMAINS}" == "${@}" ]] && \ |
| 61 |
_pass "SetLinkDomains was called correctly" || \ |
| 62 |
_fail "SetLinkDomains was not given the correct arguments:\n" \ |
| 63 |
" Expected: '${ip_ifindex} ${TEST_BUSCTL_DOMAINS}'\n" \ |
| 64 |
" Received: '${@}'" |
| 65 |
fi |
| 66 |
;; |
| 67 |
SetLinkDNSSEC) |
| 68 |
shift 2 |
| 69 |
[[ "${ip_ifindex} ${TEST_BUSCTL_DNSSEC}x" == "${@}x" ]] && \ |
| 70 |
_pass "SetLinkDNSSEC was called correctly" || \ |
| 71 |
_fail "SetLinkDNSSEC was not given the correct arguments:\n" \ |
| 72 |
" Expected: '${ip_ifindex} ${TEST_BUSCTL_DNSSEC}'\n" \ |
| 73 |
" Received: '${@}'" |
| 74 |
;; |
| 75 |
*) |
| 76 |
_fail "Unknown command called on busctl: ${1}" |
| 77 |
;; |
| 78 |
esac |
| 79 |
} |
| 80 |
|
| 81 |
function ip { |
| 82 |
_log "ip called with: ${@}" |
| 83 |
|
| 84 |
[[ "${1} ${2} ${3} ${4}" == "link show dev ${dev}" ]] && \ |
| 85 |
_pass "ip was called correctly" || \ |
| 86 |
_fail "ip was called with incorrect or unknown arguments" |
| 87 |
|
| 88 |
# Return fake ip statement |
| 89 |
echo -e "${ip_ifindex}: ${dev}: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP>" \ |
| 90 |
" mtu 1500 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen" \ |
| 91 |
" 100\n link/none" |
| 92 |
} |
| 93 |
|
| •94 |
function resolvectl { |
| 95 |
_log "resolvectl called with: ${@}" |
| 96 |
} |
| 97 |
|
| 98 |
function logger { |
| 99 |
# Remove standard options |
| 100 |
local message="$*" |
| 101 |
_log "-- ${message##* -- }" |
| 102 |
} |
| 103 |
|
| 104 |
function exit { |
| 105 |
# Override |
| 106 |
_log "exit called with status ${1}" |
| 107 |
} |
| 108 |
|
| 109 |
function _log { |
| 110 |
( >&2 echo -e " ${DARK}${@}${RESET}" ) |
| 111 |
} |
| 112 |
|
| 113 |
function _pass { |
| 114 |
COUNT_PASS=$((COUNT_PASS+1)) |
| 115 |
( >&2 echo -e " ${GREEN}${PASS}${RESET} ${@}" ) |
| 116 |
} |
| 117 |
|
| 118 |
function _fail { |
| 119 |
COUNT_FAIL=$((COUNT_FAIL+1)) |
| 120 |
( >&2 echo -e " ${RED}${FAIL} ${@}${RESET}" ) |
| 121 |
} |
| 122 |
|
| 123 |
function runtest { |
| 124 |
# Increment counter so that we don't double-execute if a test script calls |
| 125 |
# this function. |
| 126 |
: ${RUNTEST_CALLED:=0} |
| 127 |
(( RUNTEST_CALLED += 1 )) |
| 128 |
|
| 129 |
echo -e "${GREEN}- Testing ${TEST_TITLE:-a nameless test}${RESET}" |
| 130 |
|
| 131 |
# Source, don't run, so we don't need to export and internal functions override |
| 132 |
# external calls out to system commands |
| 133 |
source update-systemd-resolved |
| 134 |
exit_status="$?" |
| 135 |
exit_message="script exited with a ${exit_status} exit status" |
| 136 |
|
| 137 |
if [[ "$(( exit_status > 0 ))" == "${EXPECT_FAILURE:-0}" ]]; then |
| 138 |
_pass "$exit_message" |
| 139 |
else |
| 140 |
_fail "$exit_message" |
| 141 |
fi |
| 142 |
|
| 143 |
if [[ ${TEST_BUSCTL_CALLED} -eq 0 ]]; then |
| 144 |
[[ ${busctl_called} -eq 0 ]] && \ |
| 145 |
_pass "busctl was not called, as expected" || \ |
| 146 |
_fail "busctl was called, not expected" |
| 147 |
else |
| 148 |
[[ ${busctl_called} -eq 0 ]] && \ |
| 149 |
_fail "busctl was not called, not expected" |
| 150 |
fi |
| 151 |
|
| 152 |
echo |
| 153 |
} |
| 154 |
|
| 155 |
echo "update-systemd-resolved Test Suite" |
| 156 |
echo |
| 157 |
|
| 158 |
for TEST in tests/*.sh; do |
| 159 |
# Set/Reset loop variables |
| 160 |
RUNTEST_CALLED=0 |
| 161 |
EXPECT_FAILURE=0 |
| 162 |
busctl_called=0 |
| 163 |
# Set/Reset expected results |
| 164 |
TEST_BUSCTL_DNS="" |
| 165 |
TEST_BUSCTL_DOMAINS="" |
| 166 |
TEST_BUSCTL_DNSSEC="" |
| 167 |
# Keep this random, as we will never know the ifindex up-front |
| 168 |
ip_ifindex=$((RANDOM%=64)) |
| 169 |
# Clear foreign_option_* |
| 170 |
foreign_option_1="" |
| 171 |
foreign_option_2="" |
| 172 |
foreign_option_3="" |
| 173 |
foreign_option_4="" |
| 174 |
foreign_option_5="" |
| 175 |
foreign_option_6="" |
| 176 |
|
| 177 |
# Import the test configuration |
| 178 |
source "${TEST}" |
| 179 |
|
| 180 |
(( RUNTEST_CALLED > 0 )) || runtest |
| 181 |
done |
| 182 |
|
| 183 |
echo -e " ${GREEN}${PASS} ${COUNT_PASS} Passed${RESET}" |
| 184 |
echo -e " ${RED}${FAIL} ${COUNT_FAIL} Failed${RESET}" |
| 185 |
|
| 186 |
# Make sure we fail if there are failed tests |
| 187 |
[[ ${COUNT_FAIL} -eq 0 ]] |