-
Notifications
You must be signed in to change notification settings - Fork 94
/
run-tests
executable file
·415 lines (351 loc) · 9.87 KB
/
run-tests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env bash
#
# Test Framework for update-systemd-resolved.
# Copyright (C) 2016, Jonathan Wright <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# Set colour escape sequences
PS4='+ ${BASH_SOURCE:-$0}@${LINENO:-0}${FUNCNAME:+#${FUNCNAME}()}: '
if [[ -n ${NO_COLOR-} ]] || ! colors="$(tput colors 2> /dev/null)" || (("${colors:-0}" < 16)); then
RED=''
YELLOW=''
ORANGE=''
GREEN=''
DARK=''
RESET=''
else
RED='\033[0;31m'
YELLOW='\033[1;33m'
ORANGE='\033[0;33m'
GREEN='\033[0;32m'
DARK='\033[1;30m'
RESET='\033[0m'
fi
# Set Pass/Fail signatures
PASS="✓"
FAIL="✗"
# Counters
COUNT_PASS=0
COUNT_FAIL=0
# Names of files in which test cases failed
declare -A FAILED
# Flag for determining whether a test script called the `runtest' function
RUNTEST_CALLED=0
AUTOMATED_TESTING=1
if ! toplevel="$(git rev-parse --show-toplevel 2> /dev/null)"; then
if ! toplevel="$(readlink -f "${BASH_SOURCE[0]%/*}/.." 2> /dev/null)"; then
toplevel="${PWD:-$(pwd)}"
fi
fi
# Ensure that things like "source update-systemd-resolved" pick up the script
# in this repository's toplevel rather than, say,
# /usr/bin/update-systemd-resolved.
export PATH="${toplevel}${PATH:+:${PATH}}"
unset toplevel
busctl2var() {
if (("$#" != 2)); then
printf 1>&2 -- 'usage: %s VARIABLE_NAME BUSCTL_CALL\n' "${FUNCNAME[0]}"
return 1
fi
local -n var_ref="$1"
shift
# Compatible with busybox; with GNU sed's `\U` extension we could all the
# string-munging work with sed alone and not require `tr` in the pipeline.
local varname_suffix
varname_suffix="$(
printf -- '%s' "$1" |
sed -e '
s/[^[:alnum:]]//g
s/\([a-z]\)\([A-Z]\)/\1_\2/g
s/\([A-Z]\)\([A-Z][a-z]\)/\1_\2/g
' |
tr '[:lower:]' '[:upper:]' |
sed 's/^SET_LINK_//'
)"
# `var_ref` is a reference variable; we use it by assigning to it.
# shellcheck disable=SC2034
var_ref="TEST_BUSCTL_${varname_suffix}"
}
busctl() {
# Short-circuit if we got "busctl status <...>". If TEST_BUSCTL_STATUS_RC is
# unset or empty, assume this is the call from the "main" function in
# update-systemd-resolved, rather than an automated test checking the
# behaviour of "busctl status <...>".
case "${1-}" in
status)
if [[ -n ${TEST_BUSCTL_STATUS_RC-} ]]; then
busctl_called=1
_log "busctl status: returning ${TEST_BUSCTL_STATUS_RC}"
return "$TEST_BUSCTL_STATUS_RC"
else
return 0
fi
;;
esac
shift 4
_log "busctl called with: ${*@Q}"
# Set that busctl has been called
busctl_called=1
if (("$#" < 1)); then
_fail "busctl called without arguments"
return
fi
local call="$1"
shift
local indir
busctl2var indir "$call" || return
if [[ -v $indir ]]; then
local -a expected=()
local -a expanded
read -r -a expanded <<< "${!indir}"
if (("$#" > 0)); then
local signature="$1"
local elem etype
local -i i
for ((i = 0; i < "${#expanded[@]}"; i++)); do
elem="${expanded[${i}]}"
etype="${signature:i:1}"
# Support "true" and "false" for boolstrings; we expect these to be
# coerced to "yes" and "no", respectively. Similarly, support "yes"
# and "no" for booleans; we expect these to be coerced to "true" and
# "false", respectively.
case "${elem,,}" in
true)
if [[ $etype == s ]]; then
elem=yes
fi
;;
false)
if [[ $etype == s ]]; then
elem=no
fi
;;
yes)
if [[ $etype == b ]]; then
elem=true
fi
;;
no)
if [[ $etype == b ]]; then
elem=false
fi
;;
default)
elem=""
;;
esac
expected+=("$elem")
done
else
expected=("${expanded[@]}")
fi
fi
case "$call" in
SetLinkDNS) ;;
FlushCaches | ResetServerFeatures | ResetStatistics)
argdesc=zero
argcount=0
;;
SetLinkDomains | SetLinkDNSSEC | SetLinkDNSOverTLS | SetLinkDefaultRoute | SetLinkLLMNR | SetLinkMulticastDNS)
argdesc=three
argcount=3
;;
RevertLink)
# Called upon `down` action
return
;;
*)
_fail "Unknown command called on busctl: ${call}"
;;
esac
# XXX *NOT* `[[ -v expected ]]`, which returns a nonzero status when the
# `expected` is declared but empty...
if ! declare -p expected &> /dev/null; then
_fail "${call} was called unexpectedly"
return
elif [[ ${expected[0]:-} == SKIP ]]; then
return
elif (("$#" != "${argcount:-${#}}")); then
_fail "${call} must be called with exactly ${argdesc?} (${argcount?}) arguments"
elif (("${argcount:-0}" > 0)) && [[ ${expected[0]} != "${if_index?}" ]]; then
_fail "${call} not called with the expected interface index as first argument: expected ${if_index}, got ${expected[0]}"
else
local -a actual=("${@:2:$(("$#" - 1))}")
local actual_size="${#expected[@]}"
local expected_size="${#actual[@]}"
local max
if ((actual_size > expected_size)); then
max="$actual_size"
else
max="$expected_size"
fi
local i actual_arg expected_arg
for ((i = 0; i < max; i++)); do
actual_arg="${actual[i]:-'<undef>'}"
expected_arg="${expected[i]:-'<undef>'}"
if [[ $actual_arg != "$expected_arg" ]]; then
_fail "${call} not called with the expected setting value in position ${i}: expected ${expected_arg@Q}, got ${actual_arg@Q}"
fi
done
fi
}
ip() {
_log "ip called with: ${*@Q}"
if [[ "${1} ${2} ${3} ${4}" == "link show dev ${dev}" ]]; then
_pass "ip was called correctly"
else
_fail "ip was called with incorrect or unknown arguments"
fi
# Return fake ip statement
echo -e "${ip_ifindex}: ${dev}: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP>" \
" mtu 1500 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen" \
" 100\n link/none"
}
resolvectl() {
_log "resolvectl called with: ${*@Q}"
}
logger() {
# Remove standard options
if [[ $* == *' --' ]]; then
set --
else
while (("$#" > 0)); do
case "$1" in
--)
shift
break
;;
*)
shift
;;
esac
done
fi
if (("$#" == 0)) && ! [[ -t 0 ]]; then
local message
while read -r message; do
_log "-- ${message}"
done
else
_log "-- $*"
fi
}
exit() {
# Override "exit" builtin. Note that "exit" is equivalent to "exit $?", so
# handle that case.
_log "exit called with status ${1:-$?}"
}
_log() {
(echo >&2 -e " ${DARK}${*}${RESET}")
}
_pass() {
COUNT_PASS=$((COUNT_PASS + 1))
(echo >&2 -e " ${GREEN}${PASS}${RESET} ${*}")
}
_fail() {
COUNT_FAIL=$((COUNT_FAIL + 1))
if [[ -v TEST ]]; then
FAILED["$TEST"]=1
fi
(echo >&2 -e " ${RED}${FAIL} ${*}${RESET}")
}
checktest() {
# Increment counter so that we don't double-execute if a test script calls
# this function.
: ${RUNTEST_CALLED:=0}
((RUNTEST_CALLED += 1))
echo -e "${GREEN}- Testing ${TEST_TITLE:-a nameless test}${RESET}"
if (($# < 1)); then
set -- source update-systemd-resolved
fi
# Source, don't run, so we don't need to export and internal functions override
# external calls out to system commands
exit_status=0
"$@" || exit_status="$?"
exit_message="script exited with a ${exit_status} exit status"
if [[ "$((exit_status > 0))" == "${EXPECT_FAILURE:-0}" ]]; then
_pass "$exit_message"
else
_fail "$exit_message"
fi
}
runtest() {
checktest
if [[ ${TEST_BUSCTL_CALLED-} == 0 ]]; then
if ((busctl_called == 0)); then
_pass "busctl was not called, as expected"
else
_fail "busctl was called, not expected"
fi
elif ((busctl_called == 0)); then
_fail "busctl was not called, not expected"
fi
echo
}
evaltest() {
TEST="${1?}"
# Set/Reset loop variables
RUNTEST_CALLED=0
EXPECT_FAILURE=0
busctl_called=0
# Set/Reset expected results
# We don't expect any `busctl` calls by default...
unset "${!TEST_BUSCTL_@}"
# Except for `FlushCaches`, which should be called with no arguments.
TEST_BUSCTL_FLUSH_CACHES=""
# Set/Reset expected `busctl` exit status
unset TEST_BUSCTL_STATUS_RC
# Keep this random, as we will never know the ifindex up-front
ip_ifindex=$((RANDOM %= 64))
# Same for the device
dev="tun${RANDOM}"
# Clear foreign_option_*
unset "${!foreign_option_@}"
# Import the test configuration
# shellcheck source-path=SCRIPTDIR source=tests/01_no_updates.sh
source "$TEST" || return
if ((RUNTEST_CALLED > 0)); then
return
fi
declare -a foreign_options || return
local i=0
local opt
for opt in "${foreign_options[@]}" in; do
declare "foreign_option_$((i += 1))=${opt}"
done
runtest
}
echo "update-systemd-resolved Test Suite"
echo
if (("$#" < 1)); then
set -- ./tests
fi
for path in "$@"; do
if [[ -d $path ]]; then
for test in "$path"/*.sh; do
if [[ -f $test ]]; then
evaltest "$test"
fi
done
else
evaltest "$path"
fi
done
echo -e " ${GREEN}${PASS} ${COUNT_PASS} Passed${RESET}"
echo -e " ${RED}${FAIL} ${COUNT_FAIL} Failed${RESET}"
if [[ -v FAILED ]] && (("${#FAILED[@]}" > 0)); then
echo -e "\n ${YELLOW}The following files have failing test cases:${RESET}"
for failed in "${!FAILED[@]}"; do
echo -e " ${ORANGE}${failed}${RESET}"
done
fi
# Make sure we fail if there are failed tests
((COUNT_FAIL == 0))