Extended run_tests.sh to run Python tests

run_test.sh by default runs all available tests and checks:

 * flake8
 * unit
 * integration
 * selected tests

Usage info is available by running command `run_tests.sh -h`.
It is possible to run just selected tests by running
  `run_tests.sh -t selected_tests.py`.
Integration tests require correctly set up.
Removed fabfile and fabric requirement.

DocImpact
Closes-Bug: #1404892
Change-Id: I8dbe2561aba655e698932bb7bb7c2f6f9887f9d7
This commit is contained in:
Sebastian Kalinowski 2015-01-07 11:42:12 +01:00
parent 95054878ef
commit c910026314
4 changed files with 192 additions and 146 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ dist
.coverage
.ropeproject
*.swp
test_run

121
fabfile.py vendored
View File

@ -1,121 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2015 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.
from fabric.api import local
def createrole(user='ostf', password='ostf'):
local(('psql -U postgres -c "CREATE ROLE {0} WITH PASSWORD'
'\'{1}\' SUPERUSER CREATEDB LOGIN;"').format(user, password))
def createdb(user='ostf', database='ostf'):
local(
'psql -U postgres -c "CREATE DATABASE {0} WITH OWNER={1};"'
.format(database, user)
)
def dropdb(database='ostf'):
local('psql -U postgres -c "DROP DATABASE {0};"'.format(database))
def deps():
local('python setup.py egg_info && pip install -r *.egg-info/requires.txt')
def devlink():
local('python setup.py develop')
def testdeps():
local('pip install -r test-requires')
def startserver():
local(('ostf-server '
'--dbpath postgresql+psycopg2://ostf:ostf@localhost/ostf '))
def startdebugserver():
local(('ostf-server '
'--debug '
'--debug_tests=fuel_plugin/testing/fixture/dummy_tests'))
def startnailgunmimic():
path = 'fuel_plugin/testing/test_utils/nailgun_mimic.py'
local('python {0}'.format(path))
def createmigration(comment):
"""Supply comment for new alembic revision as a value
for comment argument
"""
config_path = 'fuel_plugin/ostf_adapter/storage/alembic.ini'
local(
'alembic --config {0} revision --autogenerate -m \"{1}\"'
.format(config_path, comment)
)
def migrate(database='ostf'):
local(
'ostf-server --after-initialization-environment-hook'
)
def auth(method='trust', os='ubuntu'):
"""By default postgres doesn't allow auth without password
development without password is more fun
"""
if os == 'centos':
path = '/var/lib/pgsql/data/pg_hba.conf'
elif os == 'ubuntu':
path = '/etc/postgresql/9.1/main/pg_hba.conf'
wrong = '^local.*all.*postgres.*'
right = 'local all postgres {0}'.format(method)
local("sudo sed -i 's/{0}/{1}/' {2}".format(wrong, right, path))
local("sudo service postgresql restart")
def remakedb(database='ostf'):
dropdb(database=database)
createdb(database=database)
migrate(database=database)
def installapp():
deps()
devlink()
testdeps()
def testall():
unit()
integration()
def integration():
local(
('nosetests fuel_plugin/testing/'
'tests/functional/tests.py:AdapterTests -vs')
)
def unit():
local('nosetests fuel_plugin/testing/tests/unit -v')

View File

@ -14,64 +14,230 @@
# License for the specific language governing permissions and limitations
# under the License.
set -eu
function usage {
echo "Usage: $0 [OPTION]..."
echo "Run fuel-ostf test suite(s)"
echo ""
echo " -p, --pep8 Just run pep8"
echo " -P, --no-pep8 Do not run pep8"
echo " -h, --help Print this usage message"
echo " -p, --flake8 Run FLAKE8 checks"
echo " -P, --no-flake8 Don't run FLAKE8 checks"
echo " -u, --unit Run unit tests"
echo " -U, --no-unit Don't run unit tests"
echo " -i, --integration Run integarion tests"
echo " -I, --no-integration Don't run inteagration tests"
echo " -t, --tests Run a given test files"
echo " -h, --help Print this usage message"
echo ""
echo "Note: with no options specified, the script will try to run all available"
echo " tests with all available checks."
exit
}
function process_option {
case "$1" in
-h|--help) usage;;
-p|--pep8) just_pep8=1;;
-P|--no-pep8) no_just_pep8=1;;
esac
function process_options {
for arg in $@; do
case "$arg" in
-h|--help) usage;;
-p|--flake8) flake8_checks=1;;
-P|--no-flake8) no_flake8_checks=1;;
-u|--unit) unit_tests=1;;
-U|--no-unit) no_unit_tests=1;;
-i|--integration) integration_tests=1;;
-I|--no-integration) no_integration_tests=1;;
-t|--tests) certain_tests=1;;
-*) testropts="$testropts $arg";;
*) testrargs="$testrargs $arg"
esac
done
}
# settings
ROOT=$(dirname `readlink -f $0`)
just_pep8=0
no_just_pep8=0
# test options
testrargs=
testropts="--with-timer --timer-warning=10 --timer-ok=2 --timer-top-n=10"
# customizable options
ARTIFACTS=${ARTIFACTS:-`pwd`/test_run}
INTEGRATION_XUNIT=${INTEGRATION_XUNIT:-"$ROOT/integration.xml"}
OSTF_SERVER_PORT=${OSRF_SERVER_PORT:-8777}
UNIT_XUNIT=${UNIT_XUNIT:-"$ROOT/unittests.xml"}
mkdir -p $ARTIFACTS
# disabled/enabled flags that are setted from the cli.
# used for manipulating run logic.
flake8_checks=0
no_flake8_checks=0
unit_tests=0
no_unit_tests=0
integration_tests=0
no_integration_tests=0
certain_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
local result=0
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 || result=1
done
exit $result
fi
# Enable all tests if none was specified skipping all explicitly disabled tests.
if [[ $just_pep8 -eq 0 ]]; then
if [ $no_just_pep8 -ne 1 ]; then just_pep8=1; fi
if [[ $flake8_checks -eq 0 && \
$integration_tests -eq 0 && \
$unit_tests -eq 0 ]]; then
if [[ $no_flake8_checks -ne 1 ]]; then flake8_checks=1; fi
if [[ $no_unit_tests -ne 1 ]]; then unit_tests=1; fi
if [[ $no_integration_tests -ne 1 ]]; then integration_tests=1; fi
fi
# Run all enabled tests
if [ $just_pep8 -eq 1 ]; then
run_pep8 || errors+=" pep8_checks"
if [[ $flake8_checks -eq 1 ]]; then
run_flake8 || errors+=" flake8_checks"
fi
if [[ $unit_tests -eq 1 ]]; then
run_unit_tests || errors+=" unit_tests"
fi
if [[ $integration_tests -eq 1 ]]; then
run_integration_tests || errors+=" integration_tests"
fi
# print failed tests
if [ -n "$errors" ]; then
if [[ -n "$errors" ]]; then
echo Failed tests: $errors
exit 1
fi
exit
exit 0
}
function run_pep8 {
echo "Running pep8 ..."
tox -epep8 -v
function guess_test_run {
local errors=""
if [[ $1 == *integration* ]]; then
run_integration_tests $1 || errors=$1
else
run_unit_tests $1 || errors=$1
fi
if [[ -n "${errors}" ]]; then
echo "ERROR: ${errors}"
return 1
fi
}
for arg in "$@"; do
process_option $arg
done
# 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
}
function run_flake8 {
echo "Starting flake8 checks"
local result=0
tox -e pep8 || result=1
return $result
}
function run_unit_tests {
echo "Starting unit tests"
local TESTS="$ROOT/fuel_plugin/testing/tests/unit"
local options="-vv $testropts --xunit-file $UNIT_XUNIT"
local result=0
if [[ $# -ne 0 ]]; then
TESTS=$@
fi
# run tests
tox -epy26 -- $options $TESTS || result=1
return $result
}
function create_ostf_conf {
local config_path=$1
local artifacts_path=$2
local SERVER_PORT=${3:-$OSTF_SERVER_PORT}
cat > $config_path <<EOL
[adapter]
server_port = $SERVER_PORT
dbpath = postgresql+psycopg2://ostf:ostf@localhost/ostf
log_file = $artifacts_path/ostf.log
EOL
}
function syncdb {
local SERVER_SETTINGS=$1
local RUN_SYNCDB="\
ostf-server \
--debug
--after-initialization-environment-hook
--config-file $SERVER_SETTINGS"
tox -evenv -- $RUN_SYNCDB > /dev/null
}
function run_integration_tests {
echo "Starting integration tests"
local TESTS="$ROOT/fuel_plugin/testing/tests/integration"
local options="-vv $testropts --xunit-file $INTEGRATION_XUNIT"
local result=0
local artifacts=$ARTIFACTS/integration
local config=$artifacts/ostf.conf
mkdir -p $artifacts
if [[ $# -ne 0 ]]; then
TESTS=$@
fi
create_ostf_conf $config $artifacts
syncdb $config
# run tests
tox -epy26 -- $options $TESTS || result=1
return $result
}
# parse command line arguments and run the tests
process_options $@
run_tests

View File

@ -1,7 +1,7 @@
-r requirements.txt
WebTest>=2.0.17
mock==1.0.1
nose-timer>=0.4.3
requests-mock>=0.5.1
tox>=1.7.1
coverage==3.6
fabric