-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtest.sh
executable file
·364 lines (310 loc) · 9.56 KB
/
runtest.sh
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
#!/bin/bash
start_logger() {
# redirect stdout+stderr into stdout and logger (force line buffering)
export PYTHONUNBUFFERED=1
# make pipes, use $TAG as $NMTEST is not defined yet
rm -rf .tmp/{logger,tee}_${TAG}_p
mkfifo .tmp/logger_${TAG}_p
mkfifo .tmp/tee_${TAG}_p
# start logger and tee processes outside main program group
# - will not be killed by timeout (or harness)
setsid -f -w logger -t "runtest" < .tmp/logger_${TAG}_p &!
LOGGER_PID=$!
setsid -f -w stdbuf -i0 -o0 -e0 tee .tmp/logger_${TAG}_p < .tmp/tee_${TAG}_p &!
TEE_PID=$!
# redirect output of this shell to tee pipe
exec > .tmp/tee_${TAG}_p 2>&1
set -x
}
wait_for_process_finish() {
# do not log this, continue logging before exit and return
set +x
local PID="$1"
local INTERVAL="$2"
local RETRIES="$3"
for i in $(seq 1 "$RETRIES"); do
if kill -0 "$PID" &> /dev/null; then
sleep "$INTERVAL"
continue
fi
echo "Process $PID finished"
set -x
return
done
echo "Killing $PID with SIGKILL"
kill -9 "$PID"
set -x
}
report_result() {
echo "Report results"
# If RESULT is unset, process was killed by watchdog (or CTRL-c)
[ -z $RESULT ] && RESULT=FAIL
# If we have running harness.py then upload logs
if ps aux | grep -v grep | grep -q harness.py; then
# check for empty file: -s means nonempty
if [ -s "$NMTEST_REPORT" ]; then
timeout 1m rstrnt-report-result -o "$NMTEST_REPORT" "$NMTEST" "$RESULT"
else
echo "removing empty report file"
rm -f "$NMTEST_REPORT"
timeout 1m rstrnt-report-result -o "" "$NMTEST" "$RESULT"
fi
fi
cp -f "$NMTEST_REPORT" ./.tmp/last_report.html
echo "Testsuite time elapsed: $(date -u -d "$TS seconds ago" +%H:%M:%S)"
echo "------------ Test result: $RESULT ------------"
}
finish_runtest() {
# do not execute again - EXIT is executed with SIGINT and SIGTERM
trap - EXIT SIGINT SIGTERM
# Executed on clean run as well as on watchdog kill (or CTRL-c)
# Behave handles cleanups internally, we just have to wait for PID
echo "Wait for behave to finish"
wait_for_process_finish "$BEHAVE_PID" 0.1 100
# When behave is finished, upload logs
report_result
# Cleanup logger
echo "Stop logging processes and remove pipes"
kill $LOGGER_PID $TEE_PID
rm -rf .tmp/{logger,tee}"_${TAG}_p"
exit $rc
}
trap finish_runtest EXIT SIGINT SIGTERM
die() {
printf '%s\n' "$*"
exit 1
}
array_contains() {
local tag="$1"
shift
for a; do
if [ "$tag" = "$a" ]; then
return 0
fi
done
return 1
}
get_rhel_compose() {
for file in /etc/yum.repos.d/repofile.repo /etc/yum.repos.d/beaker-BaseOS.repo; do
if [ -f "$file" ]; then
grep -F -e baseurl= -e BaseOS "$file" 2>/dev/null | grep -o "RHEL-[^/]*" | tail -n 1
return
fi
done
grep DISTRO= /etc/motd | grep -o "RHEL-[^/]*"
}
export -f get_rhel_compose
get_timestamp() {
if [ -f /tmp/nm_tests_timestamp ]; then
cat /tmp/nm_tests_timestamp
else
date +%s | tee /tmp/nm_tests_timestamp
fi
}
running_NM_version_check() {
local running_ver
local dist_ver
running_ver="$(busctl get-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager Version | tr -d 's "')"
dist_ver="$(NetworkManager -V)"
if [[ "$dist_ver" != "$running_ver"* ]]; then
echo -e '\n\033[0;31mWARNING!!! Running NetworkManager version differs from installed package
Did you forgot to restart after install?\033[0m\n'
fi
}
version_control() {
local out
local rc
local A
out="$("$DIR/nmci/helpers/version_control.py" "$1")"
rc=$?
if [ $rc -eq 0 ]; then
local IFS=$'\n'
A=( $out )
FEATURE_FILE="${A[0]}"
TEST_NAME="${A[1]}"
ALL_TAGS=("${A[@]:2}")
return 0
elif [ $rc -eq 77 ]; then
unset FEATURE_FILE
unset TEST_NAME
unset ALL_TAGS
return 77
else
die "Invalid test name $1: $out"
fi
}
call_behave() {
local FEATURE_FILE="$1"
shift
local NMTEST_REPORT="$1"
shift
local a
local TAGS
TAGS=()
for a; do
TAGS+=("-t" "$a")
done
rc=1
# start behave in background to remember PID
behave "$FEATURE_FILE" "${TAGS[@]}" --no-capture --no-skipped -f html-pretty -o "$NMTEST_REPORT" -f plain &
BEHAVE_PID=$!
wait $BEHAVE_PID
}
###############################################################################
function gsm_hub_runtest () {
local TEST_NAME=${1:?"Error: test name is missing."}
local MODEM_INDEX=${2:?"Error: modem index is missing."}
local rc=0
local VERSION_CONTROL
local ALL_TAGS
version_control "$TEST_NAME"
rc=$?
[ -n "$FEATURE_FILE" ] || return 0
call_behave "$FEATURE_FILE" /tmp/report.html "${ALL_TAGS[@]}"
rc=$?
# Create unique IDs of embedded sections in the HTML report.
# Allow joining of two or more reports with collapsible sections.
sed -i -e "s/embed_\([0-9]\+\)/embed_${MODEM_INDEX}_${TEST_NAME}_\1/g" /tmp/report.html
return $rc
}
function gsm_hub_test() {
# Number of modems that are plugged into Acroname USB hub.
local MODEM_COUNT=5
# Number of ports Acroname USB hub has.
local PORT_COUNT=8
# Return code
local rc=0
touch /tmp/usb_hub
echo "USB_HUB" > /tmp/report_$NMTEST.html
for M in $(seq 1 1 $((MODEM_COUNT-1)) ); do
for P in $(seq 0 1 $((PORT_COUNT-1)) ); do
$DIR/prepare/acroname.py --port $P --disable
done
# systemctl stop ModemManager
# modprobe -r qmi_wwan
# modprobe qmi_wwan
# sleep 2
# systemctl restart ModemManager
$DIR/prepare/acroname.py --port $M --enable
# wait up to 300s for device to appear in NM
sh $DIR/prepare/initialize_modem.sh
# Run just one test to be as quick as possible
if [[ $NMTEST == *gsm_hub_simple ]]; then
GSM_TESTS='gsm_create_default_connection'
else
# Run the full set of tests on the 1st modem.
if [ $M == 0 ]; then
GSM_TESTS_ALL='
gsm_create_assisted_connection
gsm_create_default_connection
gsm_disconnect
gsm_create_one_minute_ping
gsm_mtu
gsm_route_metric
gsm_load_from_file
gsm_connectivity_check
'
# gsm_up_up
# gsm_up_down_up
GSM_TESTS=$GSM_TESTS_ALL
else
GSM_TESTS='
gsm_create_default_connection
gsm_disconnect
gsm_create_one_minute_ping
gsm_mtu
'
# gsm_up_up
# gsm_up_down_up
fi
fi
for T in $GSM_TESTS; do
gsm_hub_runtest $T $M || rc=1
cat /tmp/report.html >> /tmp/report_$NMTEST.html
# Adding sleep 10 just to make tests more stable
sleep 10
done
done
return $rc
}
###############################################################################
# if $1 starts with @, cut it away
TAG="${1#@}"
start_logger
echo "Running test $TAG"
export PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
DIR=$(pwd)
TS=$(get_timestamp)
# set TEST variable for version_control script
if [ -z "$TEST" ]; then
echo "setting test name to NetworkManager_Test0_$TAG"
NMTEST="NetworkManager-ci_Test0_$TAG"
elif ! [ "$TEST" == "sanity-tests" ]; then
NMTEST="$TEST"
fi
if [ -z "$NMTEST" ]; then
echo "cannot set NMTEST var"
exit 128
fi
rm -f "$DIR/.tmp/nmci-random-seed"
. $DIR/prepare/envsetup.sh
( configure_environment "$TAG" ) ; conf_rc=$?
if [ $conf_rc != 0 ]; then
if ps aux|grep -v grep| grep -q harness.py; then
timeout 2m rstrnt-report-result -o "" "$NMTEST" FAIL
fi
cat /tmp/nmcli_general
exit $conf_rc
fi
export COLUMNS="1024"
NMTEST_REPORT="/tmp/report_$NMTEST.html"
LOG_CURSOR=$(journalctl --lines=0 --show-cursor |awk '/^-- cursor:/ {print "--after-cursor="$NF; exit}')
version_control "$NMTEST"
rc=$?
if [ -n "$FEATURE_FILE" ]; then
if [[ "$TEST_NAME" == gsm_hub* ]];then
# Test all modems on USB hub with 8 ports.
gsm_hub_test
rc=$?
else
# Test nmtui and nmcli
echo "Running $NMTEST with tags '${ALL_TAGS[@]}'"
call_behave "$FEATURE_FILE" "$NMTEST_REPORT" "${ALL_TAGS[@]}"
rc=$?
fi
if array_contains xfail "${ALL_TAGS[@]}"; then
if [ "$rc" = 0 ]; then
rc=1
elif [ "$rc" != 77 ]; then
rc=0
fi
elif array_contains may_fail "${ALL_TAGS[@]}"; then
if [ "$rc" != 77 ]; then
rc=0
fi
fi
fi
if [ $rc -eq 0 ]; then
RESULT="PASS"
elif [ $rc -eq 77 ]; then
RESULT="SKIP"
if [ "$CENTOS_CI" != 1 ]; then
rc=0
fi
else
RESULT="FAIL"
fi
# If we FAILED and no report, dump journal to report
if [ "$RESULT" = "FAIL" -a ! -s "$NMTEST_REPORT" ]; then
(
echo -e "No report generated, dumping NM journal log\n\n"
echo "<pre>"
journalctl _SYSTEMD_UNIT=NetworkManager.service + \
SYSLOG_IDENTIFIER=runtest + \
SYSLOG_IDENTIFIER=nmci \
--no-pager -o cat "$LOG_CURSOR" | \
sed 's/</\</g;s/>/\>/g'
echo "</pre>"
) > "$NMTEST_REPORT"
fi
running_NM_version_check