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 6, 2020
1 parent 8a9b081 commit a3b5eef
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -14,6 +14,9 @@ language: python
cache:
- pip

services:
- docker

matrix:
fast_finish: true
include:
Expand Down
57 changes: 53 additions & 4 deletions run-tests.sh
@@ -1,14 +1,63 @@
#!/bin/sh
#!/bin/bash
#
# This file is part of REANA.
# Copyright (C) 2018 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.

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 && \
_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_db && \
black --check . && \
check_black && \
check-manifest --ignore ".travis-*" && \
sphinx-build -qnNW docs docs/_build/html && \
REANA_SQLALCHEMY_DATABASE_URI=sqlite:// python setup.py test && \
sphinx-build -qnNW -b doctest docs docs/_build/doctest
clean_old_db_container && \
start_db_container && \
python setup.py test && \
stop_db_container && \
sphinx-build -qnNW -b doctest docs docs/_build/doctest && \
echo '==> [INFO] All tests passed! ✅'
6 changes: 1 addition & 5 deletions tests/conftest.py
Expand Up @@ -12,12 +12,8 @@
from uuid import uuid4

import pytest
from mock import patch
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy_utils import create_database, database_exists, drop_database

from reana_db.models import Base, User
from reana_db.models import User


@pytest.fixture(scope="module")
Expand Down
23 changes: 9 additions & 14 deletions tests/test_models.py
Expand Up @@ -11,29 +11,25 @@
from uuid import uuid4

import pytest
import sqlalchemy
from mock import patch

from reana_db.models import (
ALLOWED_WORKFLOW_STATUS_TRANSITIONS,
AuditLog,
AuditLogAction,
User,
UserTokenStatus,
UserTokenType,
Workflow,
WorkflowStatus,
)


def test_workflow_run_number_assignment(db, session):
def test_workflow_run_number_assignment(db, session, new_user):
"""Test workflow run number assignment."""
workflow_name = "workflow"
owner_id = str(uuid4())

first_workflow = Workflow(
id_=str(uuid4()),
name=workflow_name,
owner_id=owner_id,
owner_id=new_user.id_,
reana_specification=[],
type_="serial",
logs="",
Expand All @@ -44,7 +40,7 @@ def test_workflow_run_number_assignment(db, session):
second_workflow = Workflow(
id_=str(uuid4()),
name=workflow_name,
owner_id=owner_id,
owner_id=new_user.id_,
reana_specification=[],
type_="serial",
logs="",
Expand All @@ -55,7 +51,7 @@ def test_workflow_run_number_assignment(db, session):
first_workflow_restart = Workflow(
id_=str(uuid4()),
name=workflow_name,
owner_id=owner_id,
owner_id=new_user.id_,
reana_specification=[],
type_="serial",
logs="",
Expand All @@ -68,7 +64,7 @@ def test_workflow_run_number_assignment(db, session):
first_workflow_second_restart = Workflow(
id_=str(uuid4()),
name=workflow_name,
owner_id=owner_id,
owner_id=new_user.id_,
reana_specification=[],
type_="serial",
logs="",
Expand Down Expand Up @@ -106,15 +102,14 @@ def test_workflow_run_number_assignment(db, session):
+ [tuple + (True,) for tuple in ALLOWED_WORKFLOW_STATUS_TRANSITIONS],
)
def test_workflow_can_transition_to(
db, session, from_status, to_status, can_transition
db, session, from_status, to_status, can_transition, new_user
):
"""Test workflow run number assignment."""
workflow_name = "test-workflow"
owner_id = str(uuid4())
workflow = Workflow(
id_=str(uuid4()),
name=workflow_name,
owner_id=owner_id,
owner_id=new_user.id_,
reana_specification=[],
type_="serial",
logs="",
Expand Down Expand Up @@ -148,7 +143,7 @@ def _audit_action():
)
assert audited_action.details == details
else:
with pytest.raises(sqlalchemy.exc.IntegrityError):
with pytest.raises(Exception):
_audit_action()


Expand Down

0 comments on commit a3b5eef

Please sign in to comment.