Move UI-related testing logic to a separate file

Implements: blueprint separate-fuel-ui-repo

Change-Id: I509b336785f39066f5673f17af3c97b61e7f3be2
This commit is contained in:
Vitaly Kramskikh 2016-02-05 19:28:22 +03:00
parent 401fbb860b
commit f49add8cba
6 changed files with 170 additions and 313 deletions

View File

@ -99,14 +99,10 @@ Setup for Nailgun Unit Tests
workon fuel #activate virtual environment created in the previous section
pip install tox
#. Run the Nailgun backend unit tests::
#. Run the Nailgun backend unit tests and flake8 test::
sudo apt-get install puppet-common #install missing package required by tasklib tests
./run_tests.sh --no-webui
#. Run the Nailgun flake8 test::
./run_tests.sh --flake8
./run_tests.sh
#. You can also run the same tests by hand, using tox itself::
@ -161,13 +157,14 @@ Setup for Web UI Tests
#. Run full Web UI test suite (this will wipe your Nailgun database in
PostgreSQL)::
cd fuel-web
./run_tests.sh --webui
cd nailgun
npm run lint
npm test
By default Firefox browser is used. You can specify the browser using
BROWSER environment variable::
BROWSER=chrome ./run_tests.sh --webui
BROWSER=chrome npm test
.. _running-parallel-tests-py:

View File

@ -14,7 +14,7 @@
"lint": "gulp lint",
"test": "npm run unit-tests && npm run func-tests",
"unit-tests": "gulp unit-tests",
"func-tests": "../run_tests.sh --ui-func",
"func-tests": "./run_ui_func_tests.sh",
"prepublish": "gulp build"
},
"dependencies": {

145
nailgun/run_ui_func_tests.sh Executable file
View File

@ -0,0 +1,145 @@
#!/bin/bash
# Copyright 2016 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 UI functional tests"
echo ""
echo " -h, --help Print this usage message"
echo " --no-ui-compression Skip UI compression"
echo " --no-nailgun-start Skip Nailgun start"
exit
}
no_ui_compression=0
no_nailgun_start=0
tests=
function process_options {
for arg in $@; do
case "$arg" in
-h|--help) usage;;
--no-ui-compression) no_ui_compression=1;;
--no-nailgun-start) no_nailgun_start=1;;
-*);;
*) tests="$tests $arg"
esac
done
}
FUEL_WEB_ROOT=$(readlink -f $(dirname $0)/..)
NAILGUN_ROOT=$FUEL_WEB_ROOT/nailgun
ARTIFACTS=${ARTIFACTS:-`pwd`/test_run}
mkdir -p $ARTIFACTS
export NAILGUN_STATIC=$ARTIFACTS/static
export NAILGUN_TEMPLATES=$NAILGUN_STATIC
export NAILGUN_PORT=${NAILGUN_PORT:-5544}
export NAILGUN_START_MAX_WAIT_TIME=${NAILGUN_START_MAX_WAIT_TIME:-30}
export NAILGUN_DB_HOST=${NAILGUN_DB_HOST:-/var/run/postgresql}
export NAILGUN_DB=${NAILGUN_DB:-nailgun}
export NAILGUN_DB_USER=${NAILGUN_DB_USER:-nailgun}
export NAILGUN_DB_USERPW=${NAILGUN_DB_USERPW:-nailgun}
export DB_ROOT=${DB_ROOT:-postgres}
export NAILGUN_FIXTURE_FILES="${NAILGUN_ROOT}/nailgun/fixtures/sample_environment.json ${NAILGUN_ROOT}/nailgun/fixtures/sample_plugins.json"
export NAILGUN_CHECK_URL='/api/version'
# Run UI functional tests.
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
function run_ui_func_tests {
local GULP="./node_modules/.bin/gulp"
local TESTS_DIR=static/tests/functional # FIXME(vkramskikh): absolute path should be used
local TESTS=$TESTS_DIR/test_*.js
pushd "$FUEL_WEB_ROOT" > /dev/null
tox -e cleanup
popd > /dev/null
if [ $# -ne 0 ]; then
TESTS=$@
fi
if [ $no_ui_compression -ne 1 ]; then
echo "Compressing UI... "
${GULP} build --no-sourcemaps --extra-entries=sinon --static-dir=$NAILGUN_STATIC
if [ $? -ne 0 ]; then
return 1
fi
else
echo "Using compressed UI from $NAILGUN_STATIC"
if [ ! -f "$NAILGUN_STATIC/index.html" ]; then
echo "Cannot find compressed UI. Don't use --no-ui-compression key"
return 1
fi
fi
if [ $no_nailgun_start -ne 1 ]; then
pushd "$FUEL_WEB_ROOT" > /dev/null
tox -e stop
popd > /dev/null
fi
local result=0
for testcase in $TESTS; do
pushd "$FUEL_WEB_ROOT" > /dev/null
tox -e cleanup
popd > /dev/null
local server_log=`mktemp /tmp/test_nailgun_ui_server.XXXX`
if [ $no_nailgun_start -ne 1 ]; then
pushd "$FUEL_WEB_ROOT" > /dev/null
tox -e start
popd > /dev/null
fi
SERVER_PORT=$NAILGUN_PORT \
ARTIFACTS=$ARTIFACTS \
${GULP} functional-tests --suites=$testcase || result=1
if [ $no_nailgun_start -ne 1 ]; then
pushd "$FUEL_WEB_ROOT" > /dev/null
tox -e stop
popd > /dev/null
fi
if [ $result -ne 0 ]; then
mv $server_log $ARTIFACTS/app.log
break
fi
rm $server_log
done
return $result
}
# parse command line arguments and run the tests
process_options $@
run_ui_func_tests $tests

View File

@ -55,7 +55,7 @@ DATABASE:
host: "${NAILGUN_DB_HOST}"
port: "${NAILGUN_DB_PORT}"
user: "${NAILGUN_DB_USER}"
passwd: "${NAILGUN_DB_PW}"
passwd: "${NAILGUN_DB_USERPW}"
API_LOG: "${NAILGUN_LOGS}/api.log"
APP_LOG: "${NAILGUN_LOGS}/app.log"
RPC_CONSUMER_LOG_PATH: "${NAILGUN_LOGS}/receiverd.log"
@ -143,11 +143,11 @@ prepare_database_role() {
echo "Trying to find out if role ${NAILGUN_DB_USER} exists"
local roles=$(psql -h ${NAILGUN_DB_HOST} -U ${DB_ROOT} -t -c "SELECT 'HERE' from pg_roles where rolname='${NAILGUN_DB_USER}'")
if [[ ${roles} == *HERE ]]; then
echo "Role ${NAILGUN_DB_USER} exists. Setting password ${NAILGUN_DB_PW}"
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "ALTER ROLE ${NAILGUN_DB_USER} WITH SUPERUSER LOGIN PASSWORD '${NAILGUN_DB_PW}'"
echo "Role ${NAILGUN_DB_USER} exists. Setting password ${NAILGUN_DB_USERPW}"
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "ALTER ROLE ${NAILGUN_DB_USER} WITH SUPERUSER LOGIN PASSWORD '${NAILGUN_DB_USERPW}'"
else
echo "Creating role ${NAILGUN_DB_USER} with password ${NAILGUN_DB_PW}"
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "CREATE ROLE ${NAILGUN_DB_USER} WITH SUPERUSER LOGIN PASSWORD '${NAILGUN_DB_PW}'"
echo "Creating role ${NAILGUN_DB_USER} with password ${NAILGUN_DB_USERPW}"
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "CREATE ROLE ${NAILGUN_DB_USER} WITH SUPERUSER LOGIN PASSWORD '${NAILGUN_DB_USERPW}'"
fi
}

View File

@ -27,18 +27,8 @@ function usage {
echo " -p, --flake8 Run FLAKE8 and HACKING compliance check"
echo " -P, --no-flake8 Don't run static code checks"
echo " -t, --tests Run a given test files"
echo " -w, --webui Run all UI tests"
echo " -W, --no-webui Don't run all UI tests"
echo " -e, --extensions Run EXTENSIONS unit/integration tests"
echo " -E, --no-extensions Don't run EXTENSIONS unit/integration tests"
echo " --ui-lint Run UI linting tasks"
echo " --no-ui-lint Don't run UI linting tasks"
echo " --ui-unit Run UI unit tests"
echo " --no-ui-unit Don't run UI unit tests"
echo " --ui-func Run UI functional tests"
echo " --no-ui-func Don't run UI functional tests"
echo " --no-ui-compression Skip UI compression for UI functional tests"
echo " --no-nailgun-start Skip Nailgun start for UI functional tests"
echo ""
echo "Note: with no options specified, the script will try to run all available"
echo " tests with all available checks."
@ -55,18 +45,8 @@ function process_options {
-x|--performance) performance_tests=1;;
-p|--flake8) flake8_checks=1;;
-P|--no-flake8) no_flake8_checks=1;;
-w|--webui) ui_lint_checks=1; ui_unit_tests=1; ui_func_tests=1;;
-W|--no-webui) no_ui_lint_checks=1; no_ui_unit_tests=1; no_ui_func_tests=1;;
-e|--extensions) extensions_tests=1;;
-E|--no-extensions) no_extensions_tests=1;;
--ui-lint) ui_lint_checks=1;;
--no-ui-lint) no_ui_lint_checks=1;;
--ui-unit) ui_unit_tests=1;;
--no-ui-unit) no_ui_unit_tests=1;;
--ui-func) ui_func_tests=1;;
--no-ui-func) no_ui_func_tests=1;;
--no-ui-compression) no_ui_compression=1;;
--no-nailgun-start) no_nailgun_start=1;;
-t|--tests) certain_tests=1;;
-*) testropts="$testropts $arg";;
*) testrargs="$testrargs $arg"
@ -80,7 +60,6 @@ NAILGUN_ROOT=$ROOT/nailgun
TESTRTESTS="nosetests"
FLAKE8="flake8"
PEP8="pep8"
GULP="./node_modules/.bin/gulp"
TOXENV=${TOXENV:-py27}
# test options
@ -90,11 +69,7 @@ testropts="--with-timer --timer-warning=10 --timer-ok=2 --timer-top-n=10"
# nosetest xunit options
NAILGUN_XUNIT=${NAILGUN_XUNIT:-"$ROOT/nailgun.xml"}
EXTENSIONS_XUNIT=${EXTENSIONS_XUNIT:-"$ROOT/extensions.xml"}
NAILGUN_PORT=${NAILGUN_PORT:-5544}
TEST_NAILGUN_DB=${TEST_NAILGUN_DB:-nailgun}
NAILGUN_CHECK_PATH=${NAILGUN_CHECK_PATH:-"/api/version"}
NAILGUN_STARTUP_TIMEOUT=${NAILGUN_STARTUP_TIMEOUT:-10}
NAILGUN_SHUTDOWN_TIMEOUT=${NAILGUN_SHUTDOWN_TIMEOUT:-3}
ARTIFACTS=${ARTIFACTS:-`pwd`/test_run}
TEST_WORKERS=${TEST_WORKERS:-0}
mkdir -p $ARTIFACTS
@ -106,17 +81,9 @@ no_nailgun_tests=0
performance_tests=0
flake8_checks=0
no_flake8_checks=0
ui_lint_checks=0
no_ui_lint_checks=0
ui_unit_tests=0
no_ui_unit_tests=0
ui_func_tests=0
no_ui_func_tests=0
extensions_tests=0
no_extensions_tests=0
certain_tests=0
no_ui_compression=0
no_nailgun_start=0
function run_tests {
run_cleanup
@ -134,7 +101,7 @@ function run_tests {
echo "ERROR: File or directory $tf not found"
exit 1
fi
guess_test_run $testfile
run_nailgun_tests $testfile
done
exit
fi
@ -142,16 +109,10 @@ function run_tests {
# Enable all tests if none was specified skipping all explicitly disabled tests.
if [[ $nailgun_tests -eq 0 && \
$performance_tests -eq 0 && \
$ui_lint_checks -eq 0 && \
$ui_unit_tests -eq 0 && \
$ui_func_tests -eq 0 && \
$extensions_tests -eq 0 && \
$flake8_checks -eq 0 ]]; then
if [ $no_nailgun_tests -ne 1 ]; then nailgun_tests=1; fi
if [ $no_ui_lint_checks -ne 1 ]; then ui_lint_checks=1; fi
if [ $no_ui_unit_tests -ne 1 ]; then ui_unit_tests=1; fi
if [ $no_ui_func_tests -ne 1 ]; then ui_func_tests=1; fi
if [ $no_flake8_checks -ne 1 ]; then flake8_checks=1; fi
if [ $no_extensions_tests -ne 1 ]; then extensions_tests=1; fi
@ -168,21 +129,6 @@ function run_tests {
run_nailgun_tests || errors+=" nailgun_tests"
fi
if [ $ui_lint_checks -eq 1 ]; then
echo "Starting UI lint checks..."
run_lint_ui || errors+=" ui_lint_checks"
fi
if [ $ui_unit_tests -eq 1 ]; then
echo "Starting UI unit tests..."
run_ui_unit_tests || errors+=" ui_unit_tests"
fi
if [ $ui_func_tests -eq 1 ]; then
echo "Starting UI functional tests..."
run_ui_func_tests || errors+=" ui_func_tests"
fi
if [ $extensions_tests -eq 1 ]; then
echo "Starting Extensions tests..."
run_extensions_tests || errors+=" extensions_tests"
@ -236,87 +182,6 @@ function run_nailgun_tests {
return $result
}
# Run UI unit tests.
#
function run_ui_unit_tests {
local result=0
pushd $ROOT/nailgun >> /dev/null
npm run unit-tests || result=1
popd >> /dev/null
return $result
}
# Run UI functional tests.
#
# Arguments:
#
# $@ -- tests to be run; with no arguments all tests will be run
function run_ui_func_tests {
local TESTS_DIR=$ROOT/nailgun/static/tests/functional
local TESTS=$TESTS_DIR/test_*.js
local artifacts=$ARTIFACTS/ui_func
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
if [ $no_ui_compression -ne 1 ]; then
echo "Compressing UI... "
${GULP} build --no-sourcemaps --extra-entries=sinon --static-dir=$COMPRESSED_STATIC_DIR
if [ $? -ne 0 ]; then
popd >> /dev/null
return 1
fi
else
echo "Using compressed UI from $COMPRESSED_STATIC_DIR"
if [ ! -f "$COMPRESSED_STATIC_DIR/index.html" ]; then
echo "Cannot find compressed UI. Don't use --no-ui-compression key"
return 1
fi
fi
# run js testcases
local result=0
for testcase in $TESTS; do
local server_log=`mktemp /tmp/test_nailgun_ui_server.XXXX`
if [ $no_nailgun_start -ne 1 ]; then
dropdb $config
syncdb $config true
run_server $NAILGUN_PORT $server_log $config || \
{ echo 'Failed to start Nailgun'; return 1; }
fi
SERVER_PORT=$NAILGUN_PORT \
ARTIFACTS=$artifacts \
${GULP} functional-tests --suites=$testcase || result=1
if [ $no_nailgun_start -ne 1 ]; then
kill_server $NAILGUN_PORT
fi
if [ $result -ne 0 ]; then
mv $server_log $artifacts/app.log
break
fi
rm $server_log
done
popd >> /dev/null
return $result
}
function run_flake8_subproject {
local DIRECTORY=$1
@ -358,19 +223,6 @@ function run_flake8 {
}
# 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
npm run lint || 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 {
@ -380,126 +232,6 @@ function run_cleanup {
}
# 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
NAILGUN_CONFIG=$config tox -evenv -- python manage.py loaddata nailgun/fixtures/sample_environment.json > /dev/null
NAILGUN_CONFIG=$config tox -evenv -- python manage.py loaddata nailgun/fixtures/sample_plugins.json > /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
#
# Sends SIGING to running Nailgun
function kill_server() {
local server_port=$1
# kill old server instance if exists
local pid=$(lsof -ti tcp:$server_port)
if [[ -n "$pid" ]]; then
kill $pid
sleep $NAILGUN_SHUTDOWN_TIMEOUT
fi
}
# Arguments:
#
# $1 -- server port
# $1 -- path to log file
# $2 -- 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_config=$3
local run_server_cmd="\
python manage.py run \
--port=$server_port \
--config=$server_config \
--fake-tasks \
--fake-tasks-tick-count=80 \
--fake-tasks-tick-interval=1"
kill_server $server_port
# run new server instance
pushd $NAILGUN_ROOT > /dev/null
tox -evenv -- $run_server_cmd >> $server_log 2>&1 &
if [[ $? -ne 0 ]]; then
echo "CRITICAL: Nailgun failed to start."
echo " Server log is stored in $server_log"
return 1
fi
popd > /dev/null
# wait for server availability
which curl > /dev/null
if [[ $? -eq 0 ]]; then
local num_retries=$((NAILGUN_STARTUP_TIMEOUT * 10))
local check_url="http://0.0.0.0:${server_port}${NAILGUN_CHECK_PATH}"
local i=0
while true; do
# Fail if number of retries exeeded
if [[ $i -gt $((num_retries + 1)) ]]; then
# Report, if Nailgun failed to start before the timeout.
echo "CRITICAL: Nailgun failed to start before the timeout."
echo " It's possible to increase waiting time by settings the required"
echo " number of seconds in NAILGUN_STARTUP_TIMEOUT environment variable."
return 1
fi
local http_code=$(curl -s -w %{http_code} -o /dev/null $check_url)
if [[ "$http_code" = "200" ]]; then return 0; fi
sleep 0.1
i=$((i + 1))
done
kill_server $server_port
return 1;
else
echo "WARNING: Cannot check whether Nailgun is running bacause curl is not available."
echo " Waiting $NAILGUN_STARTUP_TIMEOUT in order to let it start properly."
echo " It's possible to increase waiting time by settings the required number of"
echo " seconds in NAILGUN_STARTUP_TIMEOUT environment variable."
sleep $NAILGUN_STARTUP_TIMEOUT
lsof -ti tcp:$server_port
return $?
fi
}
function prepare_artifacts {
local artifacts=$1
local config=$2
@ -507,6 +239,7 @@ function prepare_artifacts {
create_settings_yaml $config $artifacts
}
function create_settings_yaml {
local config_path=$1
local artifacts_path=$2
@ -526,22 +259,6 @@ 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 == *functional* && $1 == *.js ]]; then
run_ui_func_tests $1
else
run_nailgun_tests $1
fi
}
# parse command line arguments and run the tests
process_options $@
run_tests

24
tox.ini
View File

@ -5,24 +5,25 @@ setupdir = {toxinidir}/nailgun
envlist = py27,py34,pep8
[base]
ARTS = {toxinidir}/artifacts
ARTS = {toxinidir}/test_run
TOOLS = {toxinidir}/nailgun/tools
[common]
changedir={toxinidir}/nailgun
setenv =
DB_ROOT={env:NAILGUN_DB_ROOT:postgres}
DB_ROOTPW={env:NAILGUN_DB_ROOTPW:insecure_slave}
DB_ROOT={env:DB_ROOT:postgres}
DB_ROOTPW={env:DB_ROOTPW:insecure_slave}
DB_ROOTPGPASS={[base]ARTS}/pgpass
NAILGUN_DB={env:NAILGUN_DB:openstack_citest}
NAILGUN_DB_HOST={env:NAILGUN_DB_HOST:127.0.0.1}
NAILGUN_DB_PORT={env:NAILGUN_DB_PORT:5432}
NAILGUN_DB_USER={env:NAILGUN_DB_USER:openstack_citest}
NAILGUN_DB_PW={env:NAILGUN_DB_PW:openstack_citest}
NAILGUN_DB_USERPW={env:NAILGUN_DB_USERPW:openstack_citest}
NAILGUN_ROOT={toxinidir}/nailgun
NAILGUN_STATIC={toxinidir}/nailgun/static
NAILGUN_TEMPLATES={toxinidir}/nailgun/static
NAILGUN_STATIC={env:NAILGUN_STATIC:static}
NAILGUN_TEMPLATES={env:NAILGUN_TEMPLATES:static}
NAILGUN_FIXTURE_FILES={env:NAILGUN_FIXTURE_FILES:}
NAILGUN_CONFIG={[base]ARTS}/test.yaml
@ -31,11 +32,6 @@ setenv =
NAILGUN_START_MAX_WAIT_TIME=20
NAILGUN_CHECK_URL=/api/version
[prepare]
commands =
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_env"
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_database"
[testenv]
usedevelop = True
install_command = pip install --allow-external -U {opts} {packages}
@ -47,7 +43,8 @@ setenv = VIRTUAL_ENV={envdir}
passenv = http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
deps = -r{toxinidir}/nailgun/test-requirements.txt
commands =
{[prepare]commands}
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_env"
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_database"
py.test -vv --cleandb --junit-xml {toxinidir}/nailgun/nailgun.xml -m 'not performance' -n 4 {posargs:nailgun/test}
py.test -vv --junit-xml {toxinidir}/nailgun/extensions.xml {posargs:nailgun/extensions}
@ -65,7 +62,8 @@ setenv = VIRTUAL_ENV={envdir}
{[common]setenv}
deps = -r{toxinidir}/nailgun/requirements.txt
commands =
{[prepare]commands}
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_env"
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_database"
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_server"
[testenv:stop]