Skip to content

Commit

Permalink
tests: move to containerised postgres DB
Browse files Browse the repository at this point in the history
partially addresses reanahub/reana#348
  • Loading branch information
mvidalgarcia committed Jul 10, 2020
1 parent 83465dc commit 6c9bc74
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
1 change: 1 addition & 0 deletions reana_job_controller/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os

from reana_commons.config import REANA_COMPONENT_PREFIX

from werkzeug.utils import import_string

SHARED_VOLUME_PATH_ROOT = os.getenv("SHARED_VOLUME_PATH_ROOT", "/var/reana")
Expand Down
67 changes: 62 additions & 5 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,83 @@
#!/bin/bash
#
# This file is part of REANA.
# Copyright (C) 2017, 2018 CERN.
# Copyright (C) 2017, 2018, 2019, 2020 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

# Quit on errors
set -o errexit

# Quit on unbound symbols
set -o nounset

# Verify that db container is running before continuing
_check_ready() {
RETRIES=40
while ! $2
do
echo "==> [INFO] Waiting for $1, $((RETRIES--)) remaining attempts..."
sleep 2
if [ $RETRIES -eq 0 ]
then
echo "==> [ERROR] Couldn't reach $1"
exit 1
fi
done
}

_db_check() {
docker exec --user postgres postgres__reana-job-controller bash -c "pg_isready" &>/dev/null;
}

clean_old_db_container() {
OLD="$(docker ps --all --quiet --filter=name=postgres__reana-job-controller)"
if [ -n "$OLD" ]; then
echo '==> [INFO] Cleaning old DB container...'
docker stop postgres__reana-job-controller
fi
}

start_db_container() {
echo '==> [INFO] Starting DB container...'
docker run --rm --name postgres__reana-job-controller -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:9.6.2
_check_ready "Postgres" _db_check
}

stop_db_container() {
echo '==> [INFO] Stopping DB container...'
docker stop postgres__reana-job-controller
}

check_black() {
echo "echo '==> [INFO] Checking Black compliance...'"
echo "black --check ."
}

COMPONENT_NAME=reana-job-controller
DOCKER_IMAGE_NAME=reanahub/$COMPONENT_NAME
PLATFORM="$(python -c 'import platform; print(platform.system())')"

clean_old_db_container
start_db_container
db_container_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' postgres__reana-job-controller)
SQLALCHEMY_URI=postgresql+psycopg2://postgres:mysecretpassword@$db_container_ip/postgres

RUN_TESTS="pydocstyle reana_job_controller &&
black --check . &&
$(check_black) &&
check-manifest --ignore '.travis-*' &&
FLASK_APP=reana_job_controller/app.py flask openapi create openapi.json &&
diff -q openapi.json docs/openapi.json &&
sphinx-build -qnN docs docs/_build/html &&
python setup.py test &&
REANA_SQLALCHEMY_DATABASE_URI=$SQLALCHEMY_URI python setup.py test &&
rm openapi.json || exit 1"

case $PLATFORM in
Darwin*)
# Tests are run inside the docker container because there is
# no HTCondor Python package for MacOS
echo "==> Running tests inside $DOCKER_IMAGE_NAME Docker image ..."
echo "==> [INFO] Running tests inside $DOCKER_IMAGE_NAME Docker image ..."
docker build -t $DOCKER_IMAGE_NAME .
RUN_TESTS_INSIDE_DOCKER="
cd $COMPONENT_NAME &&
Expand All @@ -35,7 +89,7 @@ Darwin*)
docker run -v $(pwd)/..:/code -ti $DOCKER_IMAGE_NAME bash -c "eval $RUN_TESTS_INSIDE_DOCKER"
;;
*)
echo "==> Running tests locally ..."
echo "==> [INFO] Running tests locally ..."
eval $RUN_TESTS
# Test Docker build?
if [[ ! "$@" = *"--include-docker-tests"* ]]; then
Expand All @@ -44,3 +98,6 @@ Darwin*)
docker build -t $DOCKER_IMAGE_NAME .
;;
esac

stop_db_container
echo '==> [INFO] All tests passed! ✅'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
history = open("CHANGES.rst").read()

tests_require = [
"pytest-reana>=0.7.0.dev20191219,<0.8.0",
"pytest-reana>=0.7.0.dev20200707,<0.8.0",
]

extras_require = {
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from __future__ import absolute_import, print_function


import os
import uuid

import mock
Expand Down Expand Up @@ -60,7 +62,7 @@ def base_app(tmp_shared_volume_path):
"SECRET_KEY": "SECRET_KEY",
"TESTING": True,
"SHARED_VOLUME_PATH": tmp_shared_volume_path,
"SQLALCHEMY_DATABASE_URI": "sqlite://",
"SQLALCHEMY_DATABASE_URI": os.getenv("REANA_SQLALCHEMY_DATABASE_URI"),
"SQLALCHEMY_TRACK_MODIFICATIONS": False,
"ORGANIZATIONS": ["default"],
}
Expand Down

0 comments on commit 6c9bc74

Please sign in to comment.