forked from openstack-archive/fuel-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.sh
executable file
·580 lines (485 loc) · 14.7 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
577
578
579
580
#!/bin/bash
# Copyright 2013 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
set -eu
function usage {
echo "Usage: $0 [OPTION]..."
echo "Run Fuel-Web test suite(s)"
echo ""
echo " -a, --agent Run FUEL_AGENT unit tests"
echo " -A, --no-agent Don't run FUEL_AGENT unit tests"
echo " -h, --help Print this usage message"
echo " -k, --tasklib Run tasklib unit and functional tests"
echo " -K, --no-tasklib Don't run tasklib unit and functional tests"
echo " -l, --lint-ui Run UI linting tasks"
echo " -L, --no-lint-ui Don't run UI linting tasks"
echo " -n, --nailgun Run NAILGUN unit/integration tests"
echo " -N, --no-nailgun Don't run NAILGUN unit/integration tests"
echo " -x, --performance Run NAILGUN performance tests"
echo " -p, --flake8 Run FLAKE8 and HACKING compliance check"
echo " -P, --no-flake8 Don't run static code checks"
echo " -s, --shotgun Run SHOTGUN tests"
echo " -S, --no-shotgun Don't run SHOTGUN tests"
echo " -t, --tests Run a given test files"
echo " -u, --upgrade Run tests for UPGRADE system"
echo " -U, --no-upgrade Don't run tests for UPGRADE system"
echo " -w, --webui Run WEB-UI tests"
echo " -W, --no-webui Don't run WEB-UI tests"
echo ""
echo "Note: with no options specified, the script will try to run all available"
echo " tests with all available checks."
exit
}
function process_options {
for arg in $@; do
case "$arg" in
-h|--help) usage;;
-a|--agent) agent_tests=1;;
-A|--no-agent) no_agent_tests=1;;
-n|--nailgun) nailgun_tests=1;;
-N|--no-nailgun) no_nailgun_tests=1;;
-x|--performance) performance_tests=1;;
-k|--tasklib) tasklib_tests=1;;
-K|--no-tasklib) no_tasklib_tests=1;;
-w|--webui) webui_tests=1;;
-W|--no-webui) no_webui_tests=1;;
-u|--upgrade) upgrade_system=1;;
-U|--no-upgrade) no_upgrade_system=1;;
-s|--shotgun) shotgun_tests=1;;
-S|--no-shotgun) no_shotgun_tests=1;;
-p|--flake8) flake8_checks=1;;
-P|--no-flake8) no_flake8_checks=1;;
-l|--lint-ui) lint_ui_checks=1;;
-L|--no-lint-ui) no_lint_ui_checks=1;;
-t|--tests) certain_tests=1;;
-*) testropts="$testropts $arg";;
*) testrargs="$testrargs $arg"
esac
done
}
# settings
ROOT=$(dirname `readlink -f $0`)
TESTRTESTS="nosetests"
FLAKE8="flake8"
PEP8="pep8"
CASPERJS="./node_modules/.bin/casperjs"
GULP="./node_modules/.bin/gulp"
LINTUI="${GULP} lint"
# test options
testrargs=
testropts="--with-timer --timer-warning=10 --timer-ok=2 --timer-top-n=10"
# nosetest xunit options
NAILGUN_XUNIT=${NAILGUN_XUNIT:-"$ROOT/nailgun.xml"}
FUELUPGRADE_XUNIT=${FUELUPGRADE_XUNIT:-"$ROOT/fuelupgrade.xml"}
SHOTGUN_XUNIT=${SHOTGUN_XUNIT:-"$ROOT/shotgun.xml"}
UI_SERVER_PORT=${UI_SERVER_PORT:-5544}
NAILGUN_PORT=${NAILGUN_PORT:-8003}
TEST_NAILGUN_DB=${TEST_NAILGUN_DB:-nailgun}
NAILGUN_CHECK_URL=${NAILGUN_CHECK_URL:-"http://0.0.0.0:$NAILGUN_PORT/api/version"}
NAILGUN_START_MAX_WAIT_TIME=${NAILGUN_START_MAX_WAIT_TIME:-5}
ARTIFACTS=${ARTIFACTS:-`pwd`/test_run}
TEST_WORKERS=${TEST_WORKERS:-0}
mkdir -p $ARTIFACTS
# disabled/enabled flags that are setted from the cli.
# used for manipulating run logic.
agent_tests=0
no_agent_tests=0
nailgun_tests=0
no_nailgun_tests=0
performance_tests=0
webui_tests=0
no_webui_tests=0
upgrade_system=0
no_upgrade_system=0
shotgun_tests=0
no_shotgun_tests=0
flake8_checks=0
no_flake8_checks=0
lint_ui_checks=0
no_lint_ui_checks=0
certain_tests=0
tasklib_tests=0
no_tasklib_tests=0
function run_tests {
run_cleanup
# This variable collects all failed tests. It'll be printed in
# the end of this function as a small statistic for user.
local errors=""
# If tests was specified in command line then run only these tests
if [ $certain_tests -eq 1 ]; then
for testfile in $testrargs; do
local testfile=`readlink -f $testfile`
local tf=`echo $testfile | cut -d':' -f1`
if [ ! -e $tf ]; then
echo "ERROR: File or directory $tf not found"
exit 1
fi
guess_test_run $testfile
done
exit
fi
# Enable all tests if none was specified skipping all explicitly disabled tests.
if [[ $agent_tests -eq 0 && \
$nailgun_tests -eq 0 && \
$performance_tests -eq 0 && \
$tasklib_tests -eq 0 && \
$webui_tests -eq 0 && \
$upgrade_system -eq 0 && \
$shotgun_tests -eq 0 && \
$flake8_checks -eq 0 && \
$lint_ui_checks -eq 0 ]]; then
if [ $no_agent_tests -ne 1 ]; then agent_tests=1; fi
if [ $no_nailgun_tests -ne 1 ]; then nailgun_tests=1; fi
if [ $no_tasklib_tests -ne 1 ]; then tasklib_tests=1; fi
if [ $no_webui_tests -ne 1 ]; then webui_tests=1; fi
if [ $no_upgrade_system -ne 1 ]; then upgrade_system=1; fi
if [ $no_shotgun_tests -ne 1 ]; then shotgun_tests=1; fi
if [ $no_flake8_checks -ne 1 ]; then flake8_checks=1; fi
if [ $no_lint_ui_checks -ne 1 ]; then lint_ui_checks=1; fi
fi
# Run all enabled tests
if [ $flake8_checks -eq 1 ]; then
echo "Starting Flake8 tests..."
run_flake8 || errors+=" flake8_checks"
fi
if [ $agent_tests -eq 1 ]; then
echo "Starting Agent tests..."
run_agent_tests || errors+=" agent_tests"
fi
if [ $nailgun_tests -eq 1 ] || [ $performance_tests -eq 1 ]; then
echo "Starting Nailgun tests..."
run_nailgun_tests || errors+=" nailgun_tests"
fi
if [ $tasklib_tests -eq 1 ]; then
echo "Starting Tasklib tests"
run_tasklib_tests || errors+=" tasklib tests"
fi
if [ $webui_tests -eq 1 ]; then
echo "Starting WebUI tests..."
run_webui_tests || errors+=" webui_tests"
fi
if [ $upgrade_system -eq 1 ]; then
echo "Starting upgrade system tests..."
run_upgrade_system_tests || errors+=" upgrade_system_tests"
fi
if [ $shotgun_tests -eq 1 ]; then
echo "Starting Shotgun tests..."
run_shotgun_tests || errors+=" shotgun_tests"
fi
if [ $lint_ui_checks -eq 1 ]; then
echo "Starting lint checks..."
run_lint_ui || errors+=" lint_ui_checks"
fi
# print failed tests
if [ -n "$errors" ]; then
echo Failed tests: $errors
exit 1
fi
exit
}
# Run agent tests
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
function run_agent_tests {
local result=0
pushd $ROOT/fuel_agent >> /dev/null
# run tests
tox -epy26 || result=1
popd >> /dev/null
return $result
}
# Run both integration and unit Nailgun's tests.
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
#
# It is supposed that we prepare database (run DBMS and create schema)
# before running tests.
function run_nailgun_tests {
local TESTS="$ROOT/nailgun/nailgun/test"
local result=0
local artifacts=$ARTIFACTS/nailgun
local config=$artifacts/test.yaml
local options="-vv --cleandb --junit-xml $NAILGUN_XUNIT"
if [ $nailgun_tests -eq 1 ] && [ $performance_tests -eq 0 ]; then
options+=" -m 'not performance' "
elif [ $nailgun_tests -eq 0 ] && [ $performance_tests -eq 1 ]; then
options+=" -m performance "
fi
prepare_artifacts $artifacts $config
if [ $# -ne 0 ]; then
TESTS="$@"
fi
if [ ! $TEST_WORKERS -eq 0 ]; then
options+=" -n $TEST_WORKERS"
fi
pushd $ROOT/nailgun >> /dev/null
# # run tests
NAILGUN_CONFIG=$config \
tox -epy26 -- $options $TESTS || result=1
popd >> /dev/null
return $result
}
# Run webui tests.
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
function run_webui_tests {
local SERVER_PORT=$UI_SERVER_PORT
local TESTS_DIR=$ROOT/nailgun/ui_tests
local TESTS=$TESTS_DIR/test_*.js
local artifacts=$ARTIFACTS/webui
local config=$artifacts/test.yaml
prepare_artifacts $artifacts $config
local COMPRESSED_STATIC_DIR=$artifacts/static_compressed
if [ $# -ne 0 ]; then
TESTS=$@
fi
pushd $ROOT/nailgun >> /dev/null
# test compression
echo -n "Compressing UI... "
local output=$(${GULP} build --static-dir=$COMPRESSED_STATIC_DIR 2>&1)
if [ $? -ne 0 ]; then
echo "$output"
popd >> /dev/null
exit 1
fi
echo "done"
# run js testcases
local server_log=`mktemp /tmp/test_nailgun_ui_server.XXXX`
local result=0
local pid
for testcase in $TESTS; do
dropdb $config
syncdb $config true
pid=`run_server $SERVER_PORT $server_log $config` || \
{ echo 'Failed to start Nailgun'; return 1; }
if [ "$pid" -ne "0" ]; then
SERVER_PORT=$SERVER_PORT \
${CASPERJS} test --includes="$TESTS_DIR/helpers.js" --fail-fast "$testcase"
if [ $? -ne 0 ]; then
result=1
break
fi
kill $pid
wait $pid 2> /dev/null
else
cat $server_log
result=1
break
fi
done
rm $server_log
popd >> /dev/null
return $result
}
# Run tests for fuel upgrade system
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
function run_upgrade_system_tests {
local UPGRADE_TESTS="$ROOT/fuel_upgrade_system/fuel_upgrade/fuel_upgrade/tests/"
local result=0
if [ $# -ne 0 ]; then
# run selected tests
TESTS="$@"
$TESTRTESTS -vv $testropts $TESTS || result=1
else
# run all tests
pushd $ROOT/fuel_upgrade_system/fuel_upgrade >> /dev/null
tox -epy26 -- -vv $testropts $UPGRADE_TESTS --xunit-file $FUELUPGRADE_XUNIT || result=1
popd >> /dev/null
fi
return $result
}
# Run shotgun tests
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
function run_shotgun_tests {
local result=0
pushd $ROOT/shotgun >> /dev/null
# run tests
tox -epy26 || result=1
popd >> /dev/null
return $result
}
function run_tasklib_tests {
local result=0
pushd $ROOT/tasklib >> /dev/null
# run tests
tox -epy26 || result=1
popd >> /dev/null
return $result
}
function run_flake8_subproject {
local DIRECTORY=$1
local result=0
pushd $ROOT/$DIRECTORY >> /dev/null
tox -epep8 || result=1
popd >> /dev/null
return $result
}
# Check python code with flake8 and pep8.
#
# Some settings description:
#
# * __init__.py --- excluded because it doesn't comply with pep8 standard
# * H302 --- "import only modules. does not import a module" requires to import
# only modules and not functions
# * H802 --- first line of git commit commentary should be less than 50 characters
function run_flake8 {
local result=0
run_flake8_subproject fuel_agent && \
run_flake8_subproject nailgun && \
run_flake8_subproject tasklib && \
run_flake8_subproject fuelmenu && \
run_flake8_subproject network_checker && \
run_flake8_subproject fuel_upgrade_system/fuel_upgrade && \
run_flake8_subproject fuel_upgrade_system/fuel_package_updates && \
run_flake8_subproject fuel_development && \
run_flake8_subproject shotgun || result=1
return $result
}
# Check javascript files with `jshint`. It's necessary to run it inside
# `nailgun` folder, so we temporary change current dir.
function run_lint_ui {
pushd $ROOT/nailgun >> /dev/null
local result=0
${LINTUI} || result=1
popd >> /dev/null
return $result
}
# Remove temporary files. No need to run manually, since it's
# called automatically in `run_tests` function.
function run_cleanup {
find . -type f -name "*.pyc" -delete
rm -f *.log
rm -f *.pid
}
# Arguments:
#
# $1 -- insert default data into database if true
function syncdb {
pushd $ROOT/nailgun >> /dev/null
local config=$1
local defaults=$2
NAILGUN_CONFIG=$config tox -evenv -- python manage.py syncdb > /dev/null
if [[ $# -ne 0 && $defaults = true ]]; then
NAILGUN_CONFIG=$config tox -evenv -- python manage.py loaddefault > /dev/null
fi
popd >> /dev/null
}
function dropdb {
pushd $ROOT/nailgun >> /dev/null
local config=$1
NAILGUN_CONFIG=$config tox -evenv -- python manage.py dropdb > /dev/null
popd >> /dev/null
}
# Arguments:
#
# $1 -- server port
# $2 -- path to log file
# $3 -- path to the server config
#
# Returns: a server pid, that you have to close manually
function run_server {
local SERVER_PORT=$1
local SERVER_LOG=$2
local SERVER_SETTINGS=$3
local RUN_SERVER="\
python manage.py run \
--port=$SERVER_PORT \
--config=$SERVER_SETTINGS \
--fake-tasks \
--fake-tasks-tick-count=80 \
--fake-tasks-tick-interval=1"
pushd $ROOT/nailgun >> /dev/null
# kill old server instance if exists
local pid=`lsof -ti tcp:$SERVER_PORT`
if [ -n "$pid" ]; then
kill $pid
sleep 5
fi
# run new server instance
tox -evenv -- $RUN_SERVER >> $SERVER_LOG 2>&1 &
# wait for server availability
which curl > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
local num_retries=$[$NAILGUN_START_MAX_WAIT_TIME * 10]
for i in $(seq 1 $num_retries); do
local http_code=`curl -s -w %{http_code} -o /dev/null $NAILGUN_CHECK_URL`
if [ "$http_code" == "200" ]; then break; fi
sleep 0.1
done
else
sleep 5
fi
popd >> /dev/null
pid=`lsof -ti tcp:$SERVER_PORT`
local nailgun_launched=$?
echo $pid
return $nailgun_launched
}
function prepare_artifacts {
local artifacts=$1
local config=$2
mkdir -p $artifacts
create_settings_yaml $config $artifacts
}
function create_settings_yaml {
local config_path=$1
local artifacts_path=$2
cat > $config_path <<EOL
DEVELOPMENT: 1
STATIC_DIR: ${artifacts_path}/static_compressed
TEMPLATE_DIR: ${artifacts_path}/static_compressed
DATABASE:
name: ${TEST_NAILGUN_DB}
engine: "postgresql"
host: "localhost"
port: "5432"
user: "nailgun"
passwd: "nailgun"
API_LOG: ${artifacts_path}/api.log
APP_LOG: ${artifacts_path}/app.log
EOL
}
# Detect test runner for a given testfile and then run the test with
# this runner.
#
# Arguments:
#
# $1 -- path to the test file
function guess_test_run {
if [[ $1 == *ui_tests* && $1 == *.js ]]; then
run_webui_tests $1 || echo "ERROR: $1"
elif [[ $1 == *fuel_upgrade_system* ]]; then
run_upgrade_system_tests $1 || echo "ERROR: $1"
elif [[ $1 == *shotgun* ]]; then
run_shotgun_tests $1 || echo "ERROR: $1"
else
run_nailgun_tests $1 || echo "ERROR: $1"
fi
}
# parse command line arguments and run the tests
process_options $@
run_tests