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 82e0f9f commit 2656ee5
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
2 changes: 2 additions & 0 deletions reana_workflow_controller/config.py
Expand Up @@ -15,6 +15,8 @@

from reana_workflow_controller.version import __version__

from reana_db.config import SQLALCHEMY_DATABASE_URI

SQLALCHEMY_TRACK_MODIFICATIONS = False
"""Track modifications flag."""

Expand Down
2 changes: 1 addition & 1 deletion reana_workflow_controller/rest/workflows.py
Expand Up @@ -169,7 +169,7 @@ def get_workflows(): # noqa
verbose = json.loads(request.args.get("verbose", "false").lower())
block_size = request.args.get("block_size")
if not user:
return jsonify({"message": "User {} does not exist".format(user)}), 404
return jsonify({"message": "User {} does not exist".format(user_uuid)}), 404
workflows = []
for workflow in user.workflows:
workflow_response = {
Expand Down
74 changes: 65 additions & 9 deletions run-tests.sh
@@ -1,16 +1,72 @@
#!/bin/sh
#!/bin/bash
#
# This file is part of REANA.
# Copyright (C) 2017, 2018, 2019 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.

pydocstyle reana_workflow_controller && \
black --check . && \
FLASK_APP=reana_workflow_controller/app.py python ./scripts/generate_openapi_spec.py && \
diff -q -w temp_openapi.json docs/openapi.json && \
check-manifest --ignore ".travis-*" && \
sphinx-build -qnN docs docs/_build/html && \
python setup.py test && \
# Quit on errors
set -o errexit

# Quit on unbound symbols
set -o nounset

export REANA_SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://postgres:mysecretpassword@localhost/postgres

# 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 reana-postgres bash -c "pg_isready" &>/dev/null;
}

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

start_db_container() {
echo '==> [INFO] Starting DB container...'
docker run --rm --name reana-postgres -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 reana-postgres
}

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

pydocstyle reana_workflow_controller
check_black
FLASK_APP=reana_workflow_controller/app.py python ./scripts/generate_openapi_spec.py
diff -q -w temp_openapi.json docs/openapi.json
check-manifest --ignore ".travis-*"
sphinx-build -qnNW docs docs/_build/html
clean_old_db_container
start_db_container
python setup.py test
stop_db_container
sphinx-build -qnNW -b doctest docs docs/_build/doctest
docker build -t reanahub/reana-workflow-controller .
echo '==> [INFO] All tests passed! ✅'
2 changes: 1 addition & 1 deletion setup.py
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: 2 additions & 2 deletions tests/conftest.py
Expand Up @@ -29,11 +29,11 @@ def base_app(tmp_shared_volume_path):
"SECRET_KEY": "SECRET_KEY",
"TESTING": True,
"SHARED_VOLUME_PATH": tmp_shared_volume_path,
"SQLALCHEMY_DATABASE_URI": "sqlite:///testdb.db",
"SQLALCHEMY_TRACK_MODIFICATIONS": False,
"FLASK_ENV": "development",
"ORGANIZATIONS": ["default"],
}
app_ = create_app(config_mapping)
app_ = create_app(config_mapping=config_mapping)
return app_


Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Expand Up @@ -119,6 +119,7 @@ def test_workspace_deletion(
workflow_job = Job(id_=uuid.uuid4(), workflow_uuid=workflow.id_)
job_cache_entry = JobCache(job_id=workflow_job.id_)
session.add(workflow_job)
session.commit()
session.add(job_cache_entry)
session.commit()

Expand Down
1 change: 1 addition & 0 deletions tests/test_views.py
Expand Up @@ -1097,6 +1097,7 @@ def test_workspace_deletion(
workflow_job = Job(id_=uuid.uuid4(), workflow_uuid=workflow.id_)
job_cache_entry = JobCache(job_id=workflow_job.id_)
session.add(workflow_job)
session.commit()
session.add(job_cache_entry)
session.commit()

Expand Down

0 comments on commit 2656ee5

Please sign in to comment.