forked from tungstenfabric/tf-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.sh
executable file
·576 lines (523 loc) · 17 KB
/
run_tests.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/env bash
set -x
declare -a pids
trap resume_pids 10 1 2 3 6
source tools/common.sh
PYTHON=/usr/bin/python3
TESTR=$(which testr)
SUBUNIT2JUNIT=$(which subunit2junitxml)
rm -rf .testrepository
export PYTHON=${PYTHON}
function wait_till_child_process_state
{
local pid=$1
local state=$2
while true;
do
if [[ -f /proc/$pid/status ]]; then
for cpid in $(cat /proc/$pid/task/$pid/children); do
if wait_till_process_state $cpid $state 0 ; then
return 0
fi
done
sleep 30
else
break
fi
done
}
function wait_till_process_state
{
local pid=$1
local state=$2
local wait=${3:-1}
while true;
do
if [[ -f /proc/$pid/status ]]; then
if [[ $(cat /proc/$pid/status | grep State) != *${state}* ]]; then
if [ $wait -eq 1 ]; then
sleep 30
continue
else
return 1
fi
fi
fi
break
done
}
function resume_pids
{
for pid in ${pids[@]}; do
if [[ -f /proc/$pid/status ]]; then
for cpid in $(cat /proc/$pid/task/$pid/children); do
kill -18 $cpid
done
fi
wait_till_process_state $pid dead
done
}
function die
{
local message=$1
[ -z "$message" ] && message="Died"
echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${FUNCNAME[1]}: $message." >&2
exit 1
}
function usage {
echo "Usage: $0 [OPTION]..."
echo "Run Contrail test suite"
echo ""
echo " -p, --prepare Only prepare the system and exit. This is useable when somebody want to run the tests manually."
echo " -V, --virtual-env Always use virtualenv. Install automatically if not present"
echo " -N, --no-virtual-env Don't use virtualenv. Run tests in local environment"
echo " -n, --no-site-packages Isolate the virtualenv from the global Python environment"
echo " -f, --force Force a clean re-build of the virtual environment. Useful when dependencies have been added."
echo " -u, --update Update the virtual environment with any newer package versions"
echo " --upgrade Execute upgrade tests"
echo " -U, --upload Upload test logs"
echo " -s, --sanity Only run sanity tests"
echo " -t, --parallel Run testr in parallel"
echo " -C, --config Config file location"
echo " -h, --help Print this usage message"
echo " -d, --debug Run tests with testtools instead of testr. This allows you to use PDB"
echo " -l, --logging Enable logging"
echo " -L, --logging-config Logging config file location. Default is logging.conf"
echo " -m, --send-mail Send the report at the end"
echo " -F, --features Only run tests from features listed"
echo " -T, --tags Only run tests taged with tags (separated with commas)"
echo " -c, --concurrency Number of threads to be spawned"
echo " --contrail-fab-path Contrail fab path, default to /opt/contrail/utils"
echo " --test-failure-threshold Contrail test failure threshold"
echo " -- [TESTROPTIONS] After the first '--' you can pass arbitrary arguments to testr "
}
testrargs=""
path=""
tags=""
venv=.venv
with_venv=tools/with_venv.sh
always_venv=0
never_venv=1
no_site_packages=0
debug=0
force=0
wrapper=""
config_file=""
update=0
upgrade=0
upload=0
logging=0
logging_config=logging.conf
send_mail=0
concurrency=""
parallel=0
failure_threshold=''
contrail_fab_path='/opt/contrail/utils'
export SCRIPT_TS=${SCRIPT_TS:-$(date +"%Y_%m_%d_%H_%M_%S")}
if ! options=$(getopt -o pVNnfuUsthdC:lLmF:T:c: -l test-failure-threshold:,prepare,virtual-env,no-virtual-env,no-site-packages,force,upgrade,update,upload,sanity,parallel,help,debug,config:,logging,logging-config,send-mail,features:,tags:,concurrency:,contrail-fab-path: -- "$@")
then
# parse error
usage
exit 1
fi
eval set -- $options
first_uu=yes
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) usage; exit;;
-p|--prepare) prepare; exit;;
-V|--virtual-env) always_venv=1; never_venv=0;;
-N|--no-virtual-env) always_venv=0; never_venv=1;;
-n|--no-site-packages) no_site_packages=1;;
-f|--force) force=1;;
-u|--update) update=1;;
--upgrade) upgrade=1; debug=1; tags="upgrade";;
-U|--upload) upload=1;;
-d|--debug) debug=1;;
-C|--config) config_file=$2; shift;;
-s|--sanity) tags="sanity";;
-F|--features) path=$2; shift;;
-T|--tags) tags="$2"; shift;;
-t|--parallel) parallel=1;;
-l|--logging) logging=1;;
-L|--logging-config) logging_config=$2; shift;;
-m|--send-mail) send_mail=1;;
-c|--concurrency) concurrency=$2; shift;;
--contrail-fab-path) contrail_fab_path=$2; shift;;
--test-failure-threshold) failure_threshold=$2; shift;;
--) [ "yes" == "$first_uu" ] || testrargs="$testrargs $1"; first_uu=no ;;
*) testrargs+=" $1";;
esac
shift
done
# separate tags list with spaces
tags=`echo $tags | sed 's/,/ /g'`
config_file=${config_file:-$TEST_CONFIG_FILE}
if [[ -n $config_file ]]; then
export TEST_CONFIG_FILE=$config_file
fi
prepare
if [ $logging -eq 1 ]; then
if [ ! -f "$logging_config" ]; then
echo "No such logging config file: $logging_config"
exit 1
fi
logging_config=`readlink -f "$logging_config"`
export TEST_LOG_CONFIG_DIR=`dirname "$logging_config"`
export TEST_LOG_CONFIG=`basename "$logging_config"`
fi
export REPORT_DETAILS_FILE=report_details_${SCRIPT_TS}.ini
export REPORT_FILE="report/junit-noframes.html"
cd `dirname "$0"`
if [ $no_site_packages -eq 1 ]; then
installvenvopts="--no-site-packages"
fi
function testr_init {
if [ ! -d .testrepository ]; then
${wrapper} ${TESTR} init
fi
}
function send_mail {
if [ $send_mail -eq 1 ] ; then
if [ -f report/junit-noframes.html ]; then
${wrapper} ${PYTHON} tools/send_mail.py $1 $2 $3
fi
fi
echo "Sent mail to interested parties"
}
function run_tests_serial {
echo in serial_run_test
export PYTHONPATH=$PATH:$PWD:$PWD/fixtures:$PWD/serial_scripts
testr_init
${wrapper} find . -type f -name "*.pyc" -delete
export OS_TEST_PATH=${GIVEN_TEST_PATH:-./serial_scripts/$1}
export DO_XMPP_CHECK=0
if [ ! -d ${OS_TEST_PATH} ] ; then
echo "Folder ${OS_TEST_PATH} does not exist..no tests discovered"
return
fi
if [ $debug -eq 1 ]; then
if [ "$testrargs" = "" ]; then
testrargs="discover $OS_TEST_PATH"
${wrapper} ${PYTHON} -m subunit.run $testrargs | ${wrapper} ${SUBUNIT2JUNIT} -f -o $serial_result_xml
else
run_tagged_tests_in_debug_mode
fi
return $?
fi
${wrapper} ${TESTR} run --subunit $testrargs | ${wrapper} ${SUBUNIT2JUNIT} -f -o $serial_result_xml > /dev/null 2>&1
}
function check_test_discovery {
echo "Checking if test-discovery is fine"
bash -x tools/check_test_discovery.sh || die "Test discovery failed!"
}
function run_tagged_tests_in_debug_mode {
list_tagged_tests
${PYTHON} tools/parse_test_file.py mylist
IFS=$'\n' read -d '' -r -a lines < mylist
count=1
for i in "${lines[@]}"
do
result_xml='result'$count'.xml'
((count++))
if [ $upgrade -eq 1 ]; then
(exec ${wrapper} ${PYTHON} -m subunit.run $i| ${wrapper} ${SUBUNIT2JUNIT} -f -o $result_xml) &
pids[$count]=$!
wait_till_child_process_state $! stop
else
${wrapper} ${PYTHON} -m subunit.run $i| ${wrapper} ${SUBUNIT2JUNIT} -f -o $result_xml
fi
done
if [ $upgrade -eq 1 ]; then
wait
fi
}
function list_tagged_tests {
${TESTR} list-tests | grep $testrargs > mylist
}
function get_result_xml {
result_xml="result_${SCRIPT_TS}_$RANDOM.xml"
echo $result_xml
}
function run_tests {
testr_init
${wrapper} find . -type f -name "*.pyc" -delete
export PYTHONPATH=$PATH:$PWD:$PWD/fixtures:$PWD/scripts
export OS_TEST_PATH=${GIVEN_TEST_PATH:-./scripts/$1}
export DO_XMPP_CHECK=${DO_XMPP_CHECK:-1}
if [ ! -d ${OS_TEST_PATH} ] ; then
echo "Folder ${OS_TEST_PATH} does not exist..no tests discovered"
return
fi
if [ $debug -eq 1 ]; then
if [ "$testrargs" = "" ]; then
testrargs="discover $OS_TEST_PATH"
${wrapper} ${PYTHON} -m subunit.run $testrargs| ${wrapper} ${SUBUNIT2JUNIT} -f -o $result_xml
else
#If the command is run_tests.sh -d -T abcxyz, we
#need to take only those tests tagged with abcxyz.
#We first create a file, mylist,
#of tests tagged with abcxyz.The test would look like
#scripts.vm_regression.test_vm_basic.TestBasicVMVN.test_ping_within_vn[abcxyz]
#Then parse_test_file.py would remove [abcxyz] from the test string before
#passing it to subunit.
#We iterate over the list of tests in the file and run one by one
#with subunit
#function run_tagged_tests_in_debug_mode does all these activities
run_tagged_tests_in_debug_mode
fi
return $?
fi
if [ $parallel -eq 0 ]; then
echo 'running in serial'
${wrapper} ${TESTR} run --subunit $testrargs | ${wrapper} ${SUBUNIT2JUNIT} -f -o $result_xml > /dev/null 2>&1
fi
if [ $parallel -eq 1 ]; then
echo 'running in parallel'
if [[ ! -z $concurrency ]];then
echo 'concurrency:'$concurrency
${wrapper} ${TESTR} run --parallel --concurrency $concurrency --subunit $testrargs | ${wrapper} ${SUBUNIT2JUNIT} -f -o $result_xml
sleep 2
else
${wrapper} ${TESTR} run --parallel --subunit --concurrency 4 $testrargs | ${wrapper} ${SUBUNIT2JUNIT} -f -o $result_xml
sleep 2
fi
fi
}
function convert_logs_to_html {
${PYTHON} tools/convert_logs_to_html.py logs/
echo "Converted log files to html files"
}
function generate_html {
if [ -f $result_xml ]; then
${wrapper} ${PYTHON} tools/update_testsuite_properties.py $REPORT_DETAILS_FILE $result_xml
ant || die "ant job failed!"
elif [ -f $serial_result_xml ]; then
${wrapper} ${PYTHON} tools/update_testsuite_properties.py $REPORT_DETAILS_FILE $serial_result_xml
ant || die "ant job failed!"
fi
echo "Generate HTML reports in report/ folder : $REPORT_FILE"
convert_logs_to_html
}
function collect_tracebacks {
export PYTHONPATH=$PYTHONPATH:$PWD:$PWD/fixtures
${PYTHON} tools/collect_bts.py $TEST_CONFIG_FILE
}
function upload_to_web_server {
if [ $upload -eq 1 ] ; then
${wrapper} ${PYTHON} tools/upload_to_webserver.py $TEST_CONFIG_FILE $REPORT_DETAILS_FILE $REPORT_FILE
fi
echo "Uploaded reports"
}
if [ $never_venv -eq 0 ]
then
# Remove the virtual environment if --force used
if [ $force -eq 1 ]; then
echo "Cleaning virtualenv..."
rm -rf ${venv}
fi
if [ $update -eq 1 ]; then
echo "Updating virtualenv..."
${PYTHON} tools/install_venv.py $installvenvopts
fi
if [ -e ${venv} ]; then
wrapper="${with_venv}"
else
if [ $always_venv -eq 1 ]; then
# Automatically install the virtualenv
${PYTHON} tools/install_venv.py $installvenvopts
wrapper="${with_venv}"
else
echo -e "No virtual environment found...create one? (Y/n) \c"
read use_ve
if [ "x$use_ve" = "xY" -o "x$use_ve" = "x" -o "x$use_ve" = "xy" ]; then
# Install the virtualenv and run the test suite in it
${PYTHON} tools/install_venv.py $installvenvopts
wrapper=${with_venv}
fi
fi
fi
fi
function find_python_version {
output="$(python --version | grep python)"
output="$(python -c 'import sys; print(sys.version_info[:])')"
substring='2, 6, 6'
if echo "$output" | grep -q "$substring"; then
echo "matched";
return 0
else
echo "no match";
return 1
fi
}
function apply_patches {
apply_testtools_patch_for_centos
apply_junitxml_patch
apply_subunitfilters_patch
}
function apply_subunitfilters_patch {
patch_path=$PWD/tools/patches
filepath=/usr/local/lib/python3.6/site-packages/subunit
(patch -p0 -N --dry-run --silent $filepath/filters.py < $patch_path/subunit-filters.patch 2>/dev/null)
if [ $? -eq 0 ]; then
echo 'Applied subunit-filter patch for python3'
(patch -p0 -N $filepath/filters.py < $patch_path/subunit-filters.patch)
fi
}
function apply_junitxml_patch {
patch_path=$PWD/tools/patches
src_path=/usr/lib/python2.6/site-packages
if [ -d $src_path/junitxml ]; then
filepath=$src_path/junitxml
fi
# Ubuntu
src_path=/usr/local/lib/python2.7/dist-packages
if [ -d $src_path/junitxml ]; then
filepath=$src_path/junitxml
fi
# Redhat
src_path=/usr/lib/python2.7/site-packages/
if [ -d $src_path/junitxml ]; then
filepath=$src_path/junitxml
fi
(patch -d $filepath -p0 -N --dry-run --silent < $patch_path/junitxml.patch 2>/dev/null)
if [ $? -eq 0 ];
then
#apply the patch
echo 'Applied patch'
(cd $filepath; patch -p0 -N < $patch_path/junitxml.patch)
fi
filepath=/usr/local/lib/python3.6/site-packages/junitxml
(patch -d $filepath -p0 -N --dry-run --silent < $patch_path/junitxml.patch 2>/dev/null)
if [ $? -eq 0 ]; then
echo 'Applied patch for python3'
(cd $filepath; patch -p0 -N < $patch_path/junitxml.patch)
fi
}
function setup_physical_routers {
(
export PYTHONPATH=$PATH:$PWD:$PWD/fixtures;
${PYTHON} tools/setup_physical_routers.py $TEST_CONFIG_FILE
)
}
function apply_testtools_patch_for_centos {
find_python_version
if [ $? -eq 0 ];then
patch_path=$PWD/tools/patches
src_path=/usr/lib/python2.6/site-packages
patch -p0 -N --dry-run --silent $src_path/discover.py < $patch_path/unittest2-discover.patch 2>/dev/null
#If the patch has not been applied then the $? which is the exit status
#for last command would have a success status code = 0
if [ $? -eq 0 ];
then
#apply the patch
echo 'Applied patch'
patch -p0 -N $src_path/discover.py < $patch_path/unittest2-discover.patch
fi
fi
}
function parse_results {
${PYTHON} tools/parse_result.py $result_xml $REPORT_DETAILS_FILE
${PYTHON} tools/parse_result.py $serial_result_xml $REPORT_DETAILS_FILE
}
function load_vcenter_templates {
(
export PYTHONPATH=$PATH:$PWD:$PWD/fixtures;
${PYTHON} tools/vcenter/load_vcenter_templates.py $TEST_CONFIG_FILE
)
}
function create_vcenter_nas_datastore {
(
export PYTHONPATH=$PATH:$PWD:$PWD/fixtures;
${PYTHON} tools/vcenter/manage_vcenter_datastore.py $TEST_CONFIG_FILE create
)
}
function delete_vcenter_nas_datastore {
(
export PYTHONPATH=$PATH:$PWD:$PWD/fixtures;
${PYTHON} tools/vcenter/manage_vcenter_datastore.py $TEST_CONFIG_FILE delete
)
}
export PYTHONPATH=$PATH:$PWD/fixtures:$PWD/scripts:$PWD
apply_patches
export TEST_DELAY_FACTOR=${TEST_DELAY_FACTOR:-1}
export TEST_RETRY_FACTOR=${TEST_RETRY_FACTOR:-1}
rm -rf result*.xml
result_xml=`get_result_xml`
serial_result_xml=`get_result_xml`
GIVEN_TEST_PATH=${OS_TEST_PATH}
if [ ! -z $ci_image ]; then
export ci_image
fi
check_test_discovery
setup_physical_routers || die "BGP peering is not up."
#To delete nfs datastore in case of
create_vcenter_nas_datastore
#load vcenter templates
load_vcenter_templates
if [[ -n $JENKINS_TRIGGERED && $JENKINS_TRIGGERED -eq 1 ]]; then
export REPORT_DETAILS_FILE=report_details_${SCRIPT_TS}_$(date +"%Y_%m_%d_%H_%M_%S").ini
echo $REPORT_DETAILS_FILE
fi
if [[ ! -z $path ]];then
for p in $path
do
if [ $p != 'webui' ]; then
export EMAIL_SUBJECT_PREFIX=$p
fi
if [ ! -z "$tags" ];then
testrargs+=$tags
export TAGS="$tags"
fi
run_tests $p
if [ $upgrade -eq 0 ]; then
run_tests_serial $p
fi
${PYTHON} tools/report_gen.py $TEST_CONFIG_FILE $REPORT_DETAILS_FILE
parse_results
generate_html
collect_tracebacks
upload_to_web_server
sleep 2
send_mail $TEST_CONFIG_FILE $REPORT_FILE $REPORT_DETAILS_FILE
done
retval=$?
exit $retval
fi
if [ -z $testrargs ]; then
if [ ! -z "$tags" ];then
testrargs+=$tags
export TAGS="$tags"
fi
fi
if [[ ! -z $testrargs ]];then
run_tests
if [ $upgrade -eq 0 ]; then
run_tests_serial
fi
fi
if [[ -z $path ]] && [[ -z $testrargs ]];then
run_tests
if [ $upgrade -eq 0 ]; then
run_tests_serial
fi
fi
sleep 2
#To delete nfs datastore in case of
#vrouter gateway sanity
delete_vcenter_nas_datastore
${PYTHON} tools/report_gen.py $TEST_CONFIG_FILE $REPORT_DETAILS_FILE
echo "Generated report_details* file: $REPORT_DETAILS_FILE"
parse_results
generate_html
collect_tracebacks
upload_to_web_server
sleep 2
send_mail $TEST_CONFIG_FILE $REPORT_FILE $REPORT_DETAILS_FILE
retval=$?
# exit value more than 300 or so will revert the exit value in bash to a lower number, so checking that.
if [ $retval -lt 101 ]; then
exit $((100+$retval))
else
exit $retval
fi