Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixtures: shared volume path through env variable #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changes
Version master (UNRELEASED)
---------------------------

- Makes the `tmp_shared_volume_path` configurable through environment variable.
- Creates empty workflow workspaces for sample workflows by default.

Version 0.7.0 (UNRELEASED)
---------------------------

- Fixes `bug related to duplicated database session <https://github.com/reanahub/pytest-reana/issues/33>`_.
- Add Black formatter support.
- Create ``__reana`` database schema for ``db`` fixture.
Expand Down
55 changes: 36 additions & 19 deletions pytest_reana/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from kubernetes import client
from mock import Mock, patch
from reana_commons.consumer import BaseConsumer
from reana_db.models import Base, User, Workflow
from reana_db.utils import build_workspace_path
from sqlalchemy import create_engine
from sqlalchemy.schema import CreateSchema
from sqlalchemy_utils import create_database, database_exists
Expand All @@ -48,9 +48,13 @@ def test_dir_exists(tmp_shared_volume_path):
assert os.path.exists(path)

"""
temp_path = str(tmpdir_factory.mktemp("reana"))
yield temp_path
shutil.rmtree(temp_path)
shared_volume_path = os.getenv("SHARED_VOLUME_PATH", "")
Copy link
Member Author

@diegodelemos diegodelemos Oct 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See how this is being used in reanahub/reana-workflow-controller#338 (this line).

temp_path = None
if not os.path.exists(shared_volume_path):
temp_path = str(tmpdir_factory.mktemp("reana"))
yield temp_path or shared_volume_path
if temp_path:
shutil.rmtree(temp_path)


@pytest.fixture()
Expand Down Expand Up @@ -99,6 +103,7 @@ def create_ninja_turtle()

"""
from reana_db.database import Session
from reana_db.models import Base

engine = create_engine(base_app.config["SQLALCHEMY_DATABASE_URI"])
base_app.session.bind = engine
Expand Down Expand Up @@ -134,6 +139,8 @@ def test_default_user_exists(default)


"""
from reana_db.models import User

default_user_id = "00000000-0000-0000-0000-000000000000"
user = User.query.filter_by(id_=default_user_id).first()
if not user:
Expand Down Expand Up @@ -432,37 +439,39 @@ def sample_workflow_workspace(tmp_shared_volume_path):

"""

def _create_sample_workflow_workspace(workflow_id):
test_workspace_path = pkg_resources.resource_filename(
"pytest_reana", "test_workspace"
)
sample_workspace_path = os.path.join(tmp_shared_volume_path, str(workflow_id))
if not os.path.exists(sample_workspace_path):
shutil.copytree(test_workspace_path, sample_workspace_path)
yield sample_workspace_path
shutil.rmtree(test_workspace_path, sample_workspace_path)
else:
yield sample_workspace_path
def _create_sample_workflow_workspace(relative_workspace_path):
empty_workspace = os.path.join(tmp_shared_volume_path, relative_workspace_path)
if not os.path.exists(empty_workspace):
os.makedirs(empty_workspace)
yield empty_workspace

return _create_sample_workflow_workspace


@pytest.fixture()
def sample_yadage_workflow_in_db(app, default_user, session, yadage_workflow_with_name):
def sample_yadage_workflow_in_db(
app, default_user, session, yadage_workflow_with_name, sample_workflow_workspace
):
"""Create a sample workflow in the database.

Scope: function

Adds a sample yadage workflow in the DB.
"""
from reana_db.models import Workflow

workflow_id = uuid4()
relative_workspace_path = build_workspace_path(default_user.id_, workflow_id)
next(sample_workflow_workspace(relative_workspace_path))
workflow = Workflow(
id_=uuid4(),
id_=workflow_id,
name="sample_serial_workflow_1",
owner_id=default_user.id_,
reana_specification=yadage_workflow_with_name["reana_specification"],
operational_options={},
type_=yadage_workflow_with_name["reana_specification"]["workflow"]["type"],
logs="",
workspace_path=relative_workspace_path,
)
session.add(workflow)
session.commit()
Expand All @@ -472,21 +481,29 @@ def sample_yadage_workflow_in_db(app, default_user, session, yadage_workflow_wit


@pytest.fixture()
def sample_serial_workflow_in_db(app, default_user, session, serial_workflow):
def sample_serial_workflow_in_db(
app, default_user, session, serial_workflow, sample_workflow_workspace
):
"""Create a sample workflow in the database.

Scope: function

Adds a sample serial workflow in the DB.
"""
from reana_db.models import Workflow
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved REANA-DB imports form global scope to the fixtures to make REANA-Client pass on Python2.7 builds, currently failing because of this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're missing Base import in app fixture.


workflow_id = uuid4()
relative_workspace_path = build_workspace_path(default_user.id_, workflow_id)
next(sample_workflow_workspace(relative_workspace_path))
workflow = Workflow(
id_=uuid4(),
id_=workflow_id,
name="sample_serial_workflow_1",
owner_id=default_user.id_,
reana_specification=serial_workflow["reana_specification"],
operational_options={},
type_=serial_workflow["reana_specification"]["workflow"]["type"],
logs="",
workspace_path=relative_workspace_path,
)
session.add(workflow)
session.commit()
Expand Down
2 changes: 1 addition & 1 deletion pytest_reana/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

from __future__ import absolute_import, print_function

__version__ = "0.7.0a1"
__version__ = "0.8.0a1"