From 22994a96122f988617781633d419456ef163b78c Mon Sep 17 00:00:00 2001 From: Vladimir Kozhukalov Date: Tue, 22 Dec 2015 10:40:20 +0300 Subject: [PATCH] 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 --- nailgun/tools/env.sh | 197 +++++++++++++++++++++++++ nailgun/tools/prepare_database.sh | 22 --- nailgun/tools/prepare_settings_yaml.sh | 16 -- tox.ini | 72 +++++++-- 4 files changed, 257 insertions(+), 50 deletions(-) create mode 100755 nailgun/tools/env.sh delete mode 100755 nailgun/tools/prepare_database.sh delete mode 100755 nailgun/tools/prepare_settings_yaml.sh diff --git a/nailgun/tools/env.sh b/nailgun/tools/env.sh new file mode 100755 index 0000000000..2dfb94ab55 --- /dev/null +++ b/nailgun/tools/env.sh @@ -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}" < /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 diff --git a/nailgun/tools/prepare_database.sh b/nailgun/tools/prepare_database.sh deleted file mode 100755 index 20daf9775f..0000000000 --- a/nailgun/tools/prepare_database.sh +++ /dev/null @@ -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}" diff --git a/nailgun/tools/prepare_settings_yaml.sh b/nailgun/tools/prepare_settings_yaml.sh deleted file mode 100755 index cda930b994..0000000000 --- a/nailgun/tools/prepare_settings_yaml.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -cat > ${NAILGUN_CONFIG} <