Skip to content

Commit

Permalink
test: add test for /workflows endpoint
Browse files Browse the repository at this point in the history
* Bumps `isort` version and amends import sort accordingly.
  • Loading branch information
Diego Rodriguez committed Jun 2, 2017
1 parent f80b8fe commit 3ff2a4e
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 28 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ recursive-include docs *.rst
recursive-include docs *.txt
recursive-include reana_workflow_controller *.html
recursive-include tests *.py
recursive-include tests *.finished
recursive-include tests *.running
recursive-include tests *.waiting
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from __future__ import print_function

import os
import sphinx.environment

import sphinx.environment

# -- General configuration ------------------------------------------------

Expand Down
22 changes: 19 additions & 3 deletions reana_workflow_controller/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,29 @@ def get_workflows():
:resheader Content-Type: application/json
:statuscode 200: no error - the list has been returned.
.. sourcecode:: http
HTTP/1.1 500 Internal Error
Content-Length: 22
Content-Type: application/json
{
"msg": "Either organization or tenant doesn't exist."
}
:resheader Content-Type: application/json
:statuscode 500: error - the list couldn't be returned.
"""
workflows = []
tenant = 'default_tenant'
for org in Organization:
workflows.extend(get_all_workflows(org, tenant))
try:
for org in Organization:
workflows.extend(get_all_workflows(org, tenant))

return jsonify({"workflows": workflows}), 200
return jsonify({"workflows": workflows}), 200
except Exception as e:
return jsonify({"msg": str(e)}), 500


@app.route('/yadage', methods=['POST'])
Expand Down
50 changes: 29 additions & 21 deletions reana_workflow_controller/fsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from flask import current_app as app
from fs import open_fs, path
from fs.errors import CreateFailed, ResourceNotFound


class REANAFS(object):
Expand Down Expand Up @@ -67,25 +68,32 @@ def get_all_workflows(org, tenant, status=None):
:param status: Filter workflows by status. If not provided no filter
is applyied.
:return: List of dictionaries containing the workflow data.
:raises fs.errors.CreateFailed: Probably the configured path doesn't exist.
:raises fs.errors.ResourceNotFound: Probably either org or tenant doesn't
exist.
"""
reana_fs = REANAFS()
workflows = []
tenant_analyses_dir = path.join(org.name, tenant, 'analyses')
if not reana_fs.exists(tenant_analyses_dir):
raise Exception('Tenant {0} does not exist.'.format(tenant))

for name in reana_fs.walk.files(
tenant_analyses_dir,
filter=['.status.{0}'.format(status.name)] if status else [],
exclude_dirs=[
'workspace',
]):
# /:org/:tenant/analyses/:workflow_uuid/.status.WorkflowStatus
# /atlas/default_tenant/analyses/256b25f4-4cfb-4684-b7a8-73872ef455a1/.status.waiting
path_data = name.split('/')
uuid = path_data[4]
status = path_data[-1].split('.')[-1]
workflows.append({'id': uuid, 'status': status,
'organization': org.name, 'tenant': tenant})

return workflows
try:
reana_fs = REANAFS()
workflows = []
tenant_analyses_dir = path.join(org.name, tenant, 'analyses')

for name in reana_fs.walk.files(
tenant_analyses_dir,
filter=['.status.{0}'.format(status.name)] if status else [],
exclude_dirs=[
'workspace',
]):
# /:org/:tenant/analyses/:workflow_uuid/.status.WorkflowStatus
# /atlas/default_tenant/analyses/256b25f4-4cfb-4684-b7a8-73872ef455a1/.status.waiting
path_data = name.split('/')
uuid = path_data[4]
status = path_data[-1].split('.')[-1]
workflows.append({'id': uuid, 'status': status,
'organization': org.name, 'tenant': tenant})

return workflows

except CreateFailed:
raise Exception("Couldn't load database.")
except ResourceNotFound:
raise Exception("Either org or tenant doesn't exist.")
3 changes: 1 addition & 2 deletions reana_workflow_controller/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@

import json
import os
import requests

import requests
from celery import Celery


celery = Celery('tasks',
broker='amqp://test:1234@'
'message-broker.default.svc.cluster.local//')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
tests_require = [
'check-manifest>=0.25',
'coverage>=4.0',
'isort>=4.2.2',
'isort>=4.2.9',
'pydocstyle>=1.0.0',
'pytest-cache>=1.0',
'pytest-cov>=1.8.0',
Expand Down
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,44 @@

from __future__ import absolute_import, print_function

import shutil
import tempfile
from os.path import dirname, join

import pytest

from reana_workflow_controller.app import app as reana_workflow_controller_app


@pytest.fixture
def tmp_fsdb_path(request):
"""Fixture data for XrootDPyFS."""
path = tempfile.mkdtemp()
shutil.copytree(join(dirname(__file__), "data"), join(path, "reana"))

def cleanup():
shutil.rmtree(path)

request.addfinalizer(cleanup)
return join(path, "reana")


@pytest.fixture()
def base_app(tmp_fsdb_path):
"""Flask application fixture."""
app_ = reana_workflow_controller_app
app_.config.from_object('reana_workflow_controller.config')
app_.config['SHARED_VOLUME_PATH'] = tmp_fsdb_path
app_.config.update(
SERVER_NAME='localhost:5000',
SECRET_KEY='SECRET_KEY',
TESTING=True,
)
return app_


@pytest.yield_fixture()
def app(base_app):
"""Flask application fixture."""
with base_app.app_context():
yield base_app
61 changes: 61 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2017 CERN.
#
# REANA is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# REANA is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# REANA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization or
# submit itself to any jurisdiction.
"""REANA-Workflow-Controller fsdb module tests."""

from __future__ import absolute_import, print_function

import json

from flask import url_for


def test_get_workflows(app):
"""Test listing all workflows."""
with app.test_client() as client:
res = client.get(url_for('get_workflows'))
assert res.status_code == 200
response_data = json.loads(res.get_data(as_text=True))
expected_data = {
"workflows": [{
"id": "6f6cd692-0eb2-439d-82bf-2f26ae992672",
"organization": "alice",
"status": "running",
"tenant": "default_tenant"
}, {
"id": "3c9b117c-d40a-49e3-a6de-5f89fcada5a3",
"organization": "atlas",
"status": "waiting",
"tenant": "default_tenant"
}, {
"id": "72e3ee4f-9cd3-4dc7-906c-24511d9f5ee3",
"organization": "cms",
"status": "finished",
"tenant": "default_tenant"
}, {
"id": "c4c0a1a6-beef-46c7-be04-bf4b3beca5a1",
"organization": "lhcb",
"status": "finished",
"tenant": "default_tenant"
}]
}

assert response_data == expected_data

0 comments on commit 3ff2a4e

Please sign in to comment.