Functional testing interface
This patch introduces a single entry point for starting/stopping nailgun server when needed. This could be used while running functional tests. For example, python-fuelclient needs nailgun to be started before running tests that send requests to nailgun REST API. Change-Id: I4d9954e257aec3d66b508d13500aa9204059026b Related-Bug: #1517408
This commit is contained in:
parent
767b44a0ef
commit
22994a9612
|
@ -0,0 +1,197 @@
|
|||
#!/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
|
||||
|
||||
cleanup_server() {
|
||||
echo "Stopping Nailgun and waiting $NAILGUN_START_MAX_WAIT_TIME seconds."
|
||||
local pid="$(lsof -ti tcp:${NAILGUN_PORT})"
|
||||
local kill9=0
|
||||
if [[ -z "$pid" ]]; then
|
||||
return 0
|
||||
fi
|
||||
kill ${pid} >/dev/null 2>&1
|
||||
for i in $(seq 1 $NAILGUN_START_MAX_WAIT_TIME); do
|
||||
if kill -0 ${pid} >/dev/null 2>&1; then
|
||||
kill9=1
|
||||
sleep 1
|
||||
else
|
||||
kill9=0
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ ${kill9} -ne 0 ]]; then
|
||||
kill -9 ${pid} >/dev/null 2>&1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
cleanup_nailgun_env() {
|
||||
rm -f "$NAILGUN_LOGS/nailgun.log"
|
||||
rm -f "$NAILGUN_LOGS/app.log"
|
||||
rm -f "$NAILGUN_LOGS/api.log"
|
||||
rm -f "${NAILGUN_CONFIG}"
|
||||
}
|
||||
|
||||
prepare_nailgun_env() {
|
||||
mkdir -p $(dirname "${NAILGUN_CONFIG}")
|
||||
mkdir -p $(dirname "${NAILGUN_LOGS}")
|
||||
cat > "${NAILGUN_CONFIG}" <<EOL
|
||||
DEVELOPMENT: 1
|
||||
STATIC_DIR: "${NAILGUN_STATIC}"
|
||||
TEMPLATE_DIR: "${NAILGUN_TEMPLATES}"
|
||||
DATABASE:
|
||||
name: "${NAILGUN_DB}"
|
||||
engine: "postgresql"
|
||||
host: "${NAILGUN_DB_HOST}"
|
||||
port: "${NAILGUN_DB_PORT}"
|
||||
user: "${NAILGUN_DB_USER}"
|
||||
passwd: "${NAILGUN_DB_PW}"
|
||||
API_LOG: "${NAILGUN_LOGS}/api.log"
|
||||
APP_LOG: "${NAILGUN_LOGS}/app.log"
|
||||
EOL
|
||||
}
|
||||
|
||||
prepare_server() {
|
||||
python ${NAILGUN_ROOT}/manage.py syncdb > /dev/null
|
||||
python ${NAILGUN_ROOT}/manage.py loaddefault > /dev/null
|
||||
if test "$NAILGUN_SAMPLE_LOAD" = "yes"; then
|
||||
python ${NAILGUN_ROOT}/manage.py loaddata $NAILGUN_SAMPLE_FILE > /dev/null
|
||||
fi
|
||||
|
||||
python ${NAILGUN_ROOT}/manage.py run \
|
||||
--port=$NAILGUN_PORT \
|
||||
--config="$NAILGUN_CONFIG" \
|
||||
--fake-tasks \
|
||||
--fake-tasks-tick-count=80 \
|
||||
--fake-tasks-tick-interval=1 >> "$NAILGUN_LOGS/nailgun.log" 2>&1 &
|
||||
|
||||
which curl >/dev/null 2>&1
|
||||
if [[ $? -ne 0 ]]; then
|
||||
echo "WARNING: Cannot check whether Nailgun is running bacause curl is not available."
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo "INFO: Waiting $NAILGUN_START_MAX_WAIT_TIME in order to let it start properly."
|
||||
|
||||
local check_url="http://127.0.0.1:${NAILGUN_PORT}${NAILGUN_CHECK_URL}"
|
||||
local nailgun_status=1
|
||||
for i in $(seq 1 $NAILGUN_START_MAX_WAIT_TIME); do
|
||||
echo "Trying to send a request: curl -s -w %{http_code} -o /dev/null ${check_url}"
|
||||
local http_code=$(curl -s -w %{http_code} -o /dev/null ${check_url})
|
||||
if [[ "$http_code" = "200" ]]; then
|
||||
echo "OK: Nailgun server seems working and ready to use."
|
||||
nailgun_status=0
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if test $nailgun_status -ne 0; then
|
||||
echo "CRITICAL: Nailgun failed to start before the timeout exceeded."
|
||||
echo " It's possible to increase waiting time setting the required"
|
||||
echo " number of seconds in NAILGUN_START_MAX_WAIT_TIME environment variable."
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
cleanup_pgpass() {
|
||||
rm -f ${DB_ROOTPGPASS}
|
||||
}
|
||||
|
||||
prepare_pgpass() {
|
||||
echo "Preparing pgpass file ${DB_ROOTPGPASS}"
|
||||
mkdir -p $(dirname ${DB_ROOTPGPASS})
|
||||
echo "*:*:*:${DB_ROOT}:${DB_ROOTPW}" > ${DB_ROOTPGPASS}
|
||||
chmod 600 ${DB_ROOTPGPASS}
|
||||
export PGPASSFILE=${DB_ROOTPGPASS}
|
||||
}
|
||||
|
||||
cleanup_database_role() {
|
||||
# requires pgpass
|
||||
echo "Dropping role ${NAILGUN_DB_USER} if exists"
|
||||
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "DROP ROLE IF EXISTS ${NAILGUN_DB_USER}"
|
||||
}
|
||||
|
||||
cleanup_database() {
|
||||
# requires pgpass
|
||||
echo "Terminating database activities for ${NAILGUN_DB} if there are any"
|
||||
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} \
|
||||
-c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
|
||||
WHERE pg_stat_activity.datname = '${NAILGUN_DB}'
|
||||
AND pid <> pg_backend_pid()"
|
||||
echo "Dropping database ${NAILGUN_DB} if exists"
|
||||
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "DROP DATABASE IF EXISTS ${NAILGUN_DB}"
|
||||
}
|
||||
|
||||
prepare_database_role() {
|
||||
# requires pgpass
|
||||
echo "Trying to find out if role ${NAILGUN_DB_USER} exists"
|
||||
local roles=$(psql -h 127.0.0.1 -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}'"
|
||||
else
|
||||
echo "Creating role ${NAILGUN_DB_USER} with password ${NAILGUN_DB_PASSWD}"
|
||||
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}'"
|
||||
fi
|
||||
}
|
||||
|
||||
prepare_database() {
|
||||
# requires pgpass
|
||||
echo "Trying to find out if database ${NAILGUN_DB} exists"
|
||||
local databases=$(psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -t -c "SELECT 'HERE' from pg_database where datname='${NAILGUN_DB}'")
|
||||
if [[ ${databases} == *HERE ]]; then
|
||||
echo "Database ${NAILGUN_DB} exists"
|
||||
else
|
||||
echo "Creating database ${NAILGUN_DB}"
|
||||
psql -h ${NAILGUN_DB_HOST} -p ${NAILGUN_DB_PORT} -U ${DB_ROOT} -c "CREATE DATABASE ${NAILGUN_DB} OWNER ${NAILGUN_DB_USER}"
|
||||
fi
|
||||
}
|
||||
|
||||
case $1 in
|
||||
prepare_nailgun_env)
|
||||
prepare_nailgun_env
|
||||
;;
|
||||
cleanup_nailgun_env)
|
||||
cleanup_nailgun_env
|
||||
;;
|
||||
prepare_nailgun_database)
|
||||
prepare_pgpass
|
||||
prepare_database_role
|
||||
prepare_database
|
||||
;;
|
||||
cleanup_nailgun_database)
|
||||
prepare_pgpass
|
||||
cleanup_database
|
||||
cleanup_pgpass
|
||||
;;
|
||||
cleanup_nailgun_server)
|
||||
cleanup_server
|
||||
;;
|
||||
prepare_nailgun_server)
|
||||
prepare_server
|
||||
;;
|
||||
*)
|
||||
echo "Not supported subcommand. Available subcommands: "
|
||||
echo "cleanup_nailgun_env"
|
||||
echo "prepare_nailgun_env"
|
||||
echo "cleanup_nailgun_database"
|
||||
echo "prepare_nailgun_database"
|
||||
echo "cleanup_nailgun_server"
|
||||
echo "prepare_nailgun_server"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
|
@ -1,22 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
echo "Preparing pgpass file ${NAILGUN_DB_ROOTPGPASS}"
|
||||
echo "*:*:*:${NAILGUN_DB_ROOT}:${NAILGUN_DB_ROOTPW}" > ${NAILGUN_DB_ROOTPGPASS}
|
||||
chmod 600 ${NAILGUN_DB_ROOTPGPASS}
|
||||
|
||||
export PGPASSFILE=${NAILGUN_DB_ROOTPGPASS}
|
||||
|
||||
echo "Trying to find out if role ${NAILGUN_DB_USER} exists"
|
||||
root_roles=$(psql -h 127.0.0.1 -U ${NAILGUN_DB_ROOT} -t -c "SELECT 'HERE' from pg_roles where rolname='${NAILGUN_DB_USER}'")
|
||||
if [[ ${root_roles} == *HERE ]];then
|
||||
echo "Role ${NAILGUN_DB_USER} exists. Setting password ${NAILGUN_DB_PW}"
|
||||
psql -h 127.0.0.1 -U ${NAILGUN_DB_ROOT} -c "ALTER ROLE ${NAILGUN_DB_USER} WITH SUPERUSER LOGIN PASSWORD '${NAILGUN_DB_PW}'"
|
||||
else
|
||||
echo "Creating role ${NAILGUN_DB_USER} with password ${NAILGUN_DB_PASSWD}"
|
||||
psql -h 127.0.0.1 -U ${NAILGUN_DB_ROOT} -c "CREATE ROLE ${NAILGUN_DB_USER} WITH SUPERUSER LOGIN PASSWORD '${NAILGUN_DB_PW}'"
|
||||
fi
|
||||
|
||||
echo "Dropping database ${NAILGUN_DB} if exists"
|
||||
psql -h 127.0.0.1 -U ${NAILGUN_DB_ROOT} -c "DROP DATABASE IF EXISTS ${NAILGUN_DB}"
|
||||
echo "Creating database ${NAILGUN_DB}"
|
||||
psql -h 127.0.0.1 -U ${NAILGUN_DB_ROOT} -c "CREATE DATABASE ${NAILGUN_DB} OWNER ${NAILGUN_DB_USER}"
|
|
@ -1,16 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
cat > ${NAILGUN_CONFIG} <<EOL
|
||||
DEVELOPMENT: 1
|
||||
STATIC_DIR: ${NAILGUN_STATIC}
|
||||
TEMPLATE_DIR: ${NAILGUN_TEMPLATES}
|
||||
DATABASE:
|
||||
name: "${NAILGUN_DB}"
|
||||
engine: "postgresql"
|
||||
host: "localhost"
|
||||
port: "5432"
|
||||
user: "${NAILGUN_DB_USER}"
|
||||
passwd: "${NAILGUN_DB_PW}"
|
||||
API_LOG: ${NAILGUN_LOGS}/api.log
|
||||
APP_LOG: ${NAILGUN_LOGS}/app.log
|
||||
EOL
|
72
tox.ini
72
tox.ini
|
@ -4,6 +4,39 @@ skipsdist = True
|
|||
setupdir = {toxinidir}/nailgun
|
||||
envlist = py27,py34,pep8
|
||||
|
||||
[base]
|
||||
ARTS = {toxinidir}/artifacts
|
||||
TOOLS = {toxinidir}/nailgun/tools
|
||||
|
||||
[common]
|
||||
setenv =
|
||||
DB_ROOT={env:NAILGUN_DB_ROOT:postgres}
|
||||
DB_ROOTPW={env:NAILGUN_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_ROOT={toxinidir}/nailgun
|
||||
NAILGUN_STATIC={toxinidir}/nailgun/static
|
||||
NAILGUN_TEMPLATES={toxinidir}/nailgun/static
|
||||
NAILGUN_SAMPLE_FILE={toxinidir}/nailgun/nailgun/fixtures/sample_environment.json
|
||||
NAILGUN_SAMPLE_LOAD=no
|
||||
|
||||
NAILGUN_CONFIG={[base]ARTS}/test.yaml
|
||||
NAILGUN_LOGS={[base]ARTS}
|
||||
NAILGUN_PORT={env:NAILGUN_PORT:8000}
|
||||
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}
|
||||
|
@ -11,24 +44,39 @@ whitelist_externals = bash
|
|||
python
|
||||
changedir={toxinidir}/nailgun
|
||||
setenv = VIRTUAL_ENV={envdir}
|
||||
NAILGUN_CONFIG={toxinidir}/nailgun/test.yaml
|
||||
NAILGUN_LOGS={toxinidir}/nailgun
|
||||
NAILGUN_STATIC={toxinidir}/nailgun/static
|
||||
NAILGUN_TEMPLATES={toxinidir}/nailgun/static
|
||||
NAILGUN_DB=openstack_citest
|
||||
NAILGUN_DB_USER=openstack_citest
|
||||
NAILGUN_DB_PW=openstack_citest
|
||||
NAILGUN_DB_ROOT=postgres
|
||||
NAILGUN_DB_ROOTPW=insecure_slave
|
||||
NAILGUN_DB_ROOTPGPASS={toxinidir}/nailgun/pgpass
|
||||
{[common]setenv}
|
||||
passenv = http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
|
||||
deps = -r{toxinidir}/nailgun/test-requirements.txt
|
||||
commands =
|
||||
bash "{toxinidir}/nailgun/tools/prepare_settings_yaml.sh"
|
||||
bash "{toxinidir}/nailgun/tools/prepare_database.sh"
|
||||
{[prepare]commands}
|
||||
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}
|
||||
|
||||
[testenv:cleanup]
|
||||
usedevelop = False
|
||||
setenv = VIRTUAL_ENV={envdir}
|
||||
{[common]setenv}
|
||||
deps =
|
||||
commands =
|
||||
bash -c "{[base]TOOLS}/env.sh cleanup_nailgun_database"
|
||||
bash -c "{[base]TOOLS}/env.sh cleanup_nailgun_env"
|
||||
|
||||
[testenv:start]
|
||||
setenv = VIRTUAL_ENV={envdir}
|
||||
{[common]setenv}
|
||||
deps = -r{toxinidir}/nailgun/requirements.txt
|
||||
commands =
|
||||
{[prepare]commands}
|
||||
bash -c "{[base]TOOLS}/env.sh prepare_nailgun_server"
|
||||
|
||||
[testenv:stop]
|
||||
usedevelop = False
|
||||
setenv = VIRTUAL_ENV={envdir}
|
||||
{[common]setenv}
|
||||
deps =
|
||||
commands =
|
||||
bash -c "{[base]TOOLS}/env.sh cleanup_nailgun_server"
|
||||
|
||||
[testenv:cover]
|
||||
setenv = NOSE_WITH_COVERAGE=1
|
||||
|
||||
|
|
Loading…
Reference in New Issue