Skip to content

Commit

Permalink
added test_batch.py for integration testing /batch endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrussimon committed Mar 20, 2017
1 parent 8ebcddd commit 8c64ada
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 42 deletions.
86 changes: 57 additions & 29 deletions test/integration_tests/python/states.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Various wholesale app states that might be useful in your tests

import copy
import time

import pytest


@pytest.fixture
@pytest.fixture(scope='module')
def with_hierarchy(api_as_admin, bunch, request, data_builder):
group = data_builder.create_group('test_prop_' + str(int(time.time() * 1000)))
project = data_builder.create_project(group)
Expand Down Expand Up @@ -44,36 +46,62 @@ def with_hierarchy_and_file_data(with_hierarchy):
return fixture_data


@pytest.fixture
test_gear = {
'category': 'converter',
'gear': {
'inputs': {
'any text file <= 100 KB': {
'base': 'file',
'name': { 'pattern': '^.*.txt$' },
'size': { 'maximum': 100000 }
}
},
'maintainer': 'Example',
'description': 'Example',
'license': 'BSD-2-Clause',
'author': 'Example',
'url': 'https://example.example',
'label': 'wat',
'flywheel': '0',
'source': 'https://example.example',
'version': '0.0.1',
'config': {},
},
'exchange': {
'git-commit': 'aex',
'rootfs-hash': 'sha384:oy',
'rootfs-url': 'https://example.example'
}
}


@pytest.fixture(scope='module')
def with_gear(request, as_admin):
gear_name = 'test-gear'
r = as_admin.post('/gears/' + gear_name, json={
'category': 'converter',
'gear': {
'inputs': {
'wat': {
'base': 'file',
'type': { 'enum': [ 'wat' ] }
}
},
'maintainer': 'Example',
'description': 'Example',
'license': 'BSD-2-Clause',
'author': 'Example',
'url': 'https://example.example',
'label': 'wat',
'flywheel': '0',
'source': 'https://example.example',
'version': '0.0.1',
'config': {},
'name': gear_name
},
'exchange': {
'git-commit': 'aex',
'rootfs-hash': 'sha384:oy',
'rootfs-url': 'https://example.example'
}
})
gear_data = copy.deepcopy(test_gear)
gear_data['gear']['name'] = gear_name

r = as_admin.post('/gears/' + gear_name, json=gear_data)
assert r.ok
gear_id = r.json()['_id']

def teardown_db():
r = as_admin.delete('/gears/' + gear_id)
assert r.ok

request.addfinalizer(teardown_db)

return gear_id


@pytest.fixture(scope='module')
def with_invalid_gear(request, as_admin):
gear_name = 'invalid-test-gear'
gear_data = copy.deepcopy(test_gear)
gear_data['gear']['name'] = gear_name
gear_data['gear']['custom'] = {'flywheel': {'invalid': True}}

r = as_admin.post('/gears/' + gear_name, json=gear_data)
assert r.ok
gear_id = r.json()['_id']

Expand Down
83 changes: 83 additions & 0 deletions test/integration_tests/python/test_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import json
import logging


log = logging.getLogger(__name__)
sh = logging.StreamHandler()
log.addHandler(sh)


def test_batch(with_hierarchy, with_gear, with_invalid_gear, as_user, as_admin):
data = with_hierarchy
gear = with_gear
invalid_gear = with_invalid_gear

# get all
r = as_user.get('/batch')
assert r.ok

# get all w/o enforcing permissions
r = as_admin.get('/batch')
assert r.ok

# try to create batch without gear_id/targets
r = as_admin.post('/batch', json={})
assert r.status_code == 400

# try to create batch with different target container types
r = as_admin.post('/batch', json={
'gear_id': gear,
'targets': [
{'type': 'session', 'id': 'test-session-id'},
{'type': 'acquisition', 'id': 'test-acquisition-id'},
],
})
assert r.status_code == 400

# try to create batch using an invalid gear
r = as_admin.post('/batch', json={
'gear_id': invalid_gear,
'targets': [{'type': 'session', 'id': 'test-session-id'}],
})
assert r.status_code == 400

# create a batch
r = as_admin.post('/acquisitions/' + data.acquisition + '/files', files={
'file': ('test.txt', 'test\ncontent\n')
})
assert r.ok

r = as_admin.post('/batch', json={
'gear_id': gear,
'targets': [
{'type': 'acquisition', 'id': data.acquisition},
],
'target_context': {'type': 'session', 'id': data.session}
})
assert r.ok
batch_id = r.json()['_id']

# get batch
r = as_admin.get('/batch/' + batch_id)
assert r.ok
assert r.json()['state'] == 'pending'

# try to cancel non-launched batch
r = as_admin.post('/batch/' + batch_id + '/cancel')
assert r.status_code == 400

# run batch
r = as_admin.post('/batch/' + batch_id + '/run')
assert r.ok
r = as_admin.get('/batch/' + batch_id)
assert r.json()['state'] == 'launched'

# try to run non-pending batch
r = as_admin.post('/batch/' + batch_id + '/run')
assert r.status_code == 400

# cancel batch
r = as_admin.post('/batch/' + batch_id + '/cancel')
assert r.ok
r = as_admin.get('/batch/' + batch_id)
assert r.json()['state'] == 'cancelled'
17 changes: 4 additions & 13 deletions test/integration_tests/python/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ def test_jobs_access(as_user):
assert r.status_code == 403


def test_jobs(with_gear, with_hierarchy, as_user, as_admin):
gear = with_gear
def test_jobs(with_hierarchy, with_gear, with_invalid_gear, as_user, as_admin):
data = with_hierarchy
gear = with_gear
invalid_gear = with_invalid_gear

job1 = {
'gear_id': gear,
Expand Down Expand Up @@ -73,22 +74,12 @@ def test_jobs(with_gear, with_hierarchy, as_user, as_admin):
assert r.ok

# add job with invalid gear
r = as_admin.get('/gears/' + gear)
assert r.ok
invalid_gear = r.json()
del invalid_gear['_id']
invalid_gear['gear']['name'] = 'invalid'
invalid_gear['gear']['custom'] = {'flywheel': {'invalid': True}}
invalid_gear_id = as_admin.post('/gears/invalid', json=invalid_gear).json()['_id']

job3 = deepcopy(job2)
job3['gear_id'] = invalid_gear_id
job3['gear_id'] = invalid_gear

r = as_user.post('/jobs/add', json=job3)
assert r.status_code == 400

as_admin.delete('/gears/' + invalid_gear_id)

# get next job - with nonexistent tag
r = as_admin.get('/jobs/next?tags=fake-tag')
assert r.status_code == 400
Expand Down

0 comments on commit 8c64ada

Please sign in to comment.