test_api.py 6.99 KB
Newer Older
1
2
3
4
5
6
7
8
import pytest
from threading import Thread
import subprocess
import shlex
import time
import json
from mongoengine import connect
from mongoengine.connection import disconnect
9
from datetime import datetime, timedelta
10

11
12
13
14
15
16
from nomad import config
# for convinience we test the api without path prefix
services_config = config.services._asdict()
services_config.update(api_base_path='')
config.services = config.NomadServicesConfig(**services_config)

Markus Scheidgen's avatar
Markus Scheidgen committed
17
18
from nomad import api, files  # noqa
from nomad.processing import Upload, handle_uploads_thread  # noqa
19

Markus Scheidgen's avatar
Markus Scheidgen committed
20
from tests.processing.test_data import example_files  # noqa
21
from tests.test_files import assert_exists  # noqa
22

23
# import fixtures
24
25
26
from tests.test_files import clear_files, archive_id  # noqa pylint: disable=unused-import
from tests.test_normalizing import normalized_vasp_example  # noqa pylint: disable=unused-import
from tests.test_parsing import parsed_vasp_example  # noqa pylint: disable=unused-import
Markus Scheidgen's avatar
Markus Scheidgen committed
27
from tests.test_repo import example_elastic_calc  # noqa pylint: disable=unused-import
28

29

30
@pytest.fixture(scope='function')
31
32
33
34
35
36
37
38
def client():
    disconnect()
    connect('users_test', host=config.mongo.host, is_mock=True)

    api.app.config['TESTING'] = True
    client = api.app.test_client()

    yield client
39
    Upload._get_collection().drop()
40
41
42
43
44
45
46
47
48
49
50


def assert_uploads(upload_json_str, count=0, **kwargs):
    data = json.loads(upload_json_str)
    assert isinstance(data, list)
    assert len(data) == count

    if count > 0:
        assert_upload(json.dumps(data[0]), **kwargs)


51
def assert_upload(upload_json_str, id=None, **kwargs):
52
    data = json.loads(upload_json_str)
Markus Scheidgen's avatar
Markus Scheidgen committed
53
    assert 'upload_id' in data
54
    if id is not None:
Markus Scheidgen's avatar
Markus Scheidgen committed
55
        assert id == data['upload_id']
56
57
    assert 'create_time' in data
    assert 'presigned_url' in data
58
    assert 'upload_command' in data
59

60
61
62
    for key, value in kwargs.items():
        assert data.get(key, None) == value

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    return data


def test_no_uploads(client):
    rv = client.get('/uploads')

    assert rv.status_code == 200
    assert_uploads(rv.data, count=0)


def test_not_existing_upload(client):
    rv = client.get('/uploads/123456789012123456789012')
    assert rv.status_code == 404


78
79
80
81
82
83
84
85
def test_stale_upload(client):
    rv = client.post(
        '/uploads',
        data=json.dumps(dict(name='test_name')),
        content_type='application/json')
    assert rv.status_code == 200
    upload_id = assert_upload(rv.data)['upload_id']

Markus Scheidgen's avatar
Markus Scheidgen committed
86
    upload = Upload.get(upload_id)
87
88
89
90
91
92
93
94
    upload.create_time = datetime.now() - timedelta(days=2)
    upload.save()

    rv = client.get('/uploads/%s' % upload_id)
    assert rv.status_code == 200
    assert_upload(rv.data, is_stale=True)


95
96
97
98
def test_create_upload(client):
    rv = client.post('/uploads')

    assert rv.status_code == 200
Markus Scheidgen's avatar
Markus Scheidgen committed
99
    upload_id = assert_upload(rv.data)['upload_id']
100
101
102

    rv = client.get('/uploads/%s' % upload_id)
    assert rv.status_code == 200
103
    assert_upload(rv.data, id=upload_id, is_stale=False)
104
105
106
107
108
109

    rv = client.get('/uploads')
    assert rv.status_code == 200
    assert_uploads(rv.data, count=1, id=upload_id)


110
111
112
113
114
115
116
117
def test_create_upload_with_name(client):
    rv = client.post(
        '/uploads', data=json.dumps(dict(name='test_name')), content_type='application/json')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)
    assert upload['name'] == 'test_name'


Markus Scheidgen's avatar
Markus Scheidgen committed
118
119
120
121
122
123
124
125
126
127
128
129
130
def test_delete_empty_upload(client):
    rv = client.post('/uploads')

    assert rv.status_code == 200
    upload_id = assert_upload(rv.data)['upload_id']

    rv = client.delete('/uploads/%s' % upload_id)
    assert rv.status_code == 200

    rv = client.get('/uploads/%s' % upload_id)
    assert rv.status_code == 404


131
@pytest.mark.parametrize("file", example_files)
Markus Scheidgen's avatar
Markus Scheidgen committed
132
@pytest.mark.timeout(30)
133
def test_upload_to_upload(client, file):
134
135
136
137
138
139
    rv = client.post('/uploads')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    @files.upload_put_handler
    def handle_upload_put(received_upload_id: str):
Markus Scheidgen's avatar
Markus Scheidgen committed
140
        assert upload['upload_id'] == received_upload_id
141
142
143
144
145
146
147
148
149
150
        raise StopIteration

    def handle_uploads():
        handle_upload_put(received_upload_id='provided by decorator')

    handle_uploads_thread = Thread(target=handle_uploads)
    handle_uploads_thread.start()

    time.sleep(1)
    upload_url = upload['presigned_url']
151
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
152
153
154
155
    subprocess.call(shlex.split(cmd))

    handle_uploads_thread.join()

156
157
    upload_id = upload['upload_id']
    assert_exists(config.files.uploads_bucket, upload_id)
158
159
160


@pytest.mark.parametrize("file", example_files)
Markus Scheidgen's avatar
Markus Scheidgen committed
161
@pytest.mark.timeout(10)
162
def test_processing(client, file, worker, mocksearch):
Markus Scheidgen's avatar
Markus Scheidgen committed
163
    handler = handle_uploads_thread(quit=True)
164
165
166
167
168
169
170
171
172
173

    rv = client.post('/uploads')
    assert rv.status_code == 200
    upload = assert_upload(rv.data)

    time.sleep(1)
    upload_url = upload['presigned_url']
    cmd = files.create_curl_upload_cmd(upload_url).replace('<ZIPFILE>', file)
    subprocess.call(shlex.split(cmd))

Markus Scheidgen's avatar
Markus Scheidgen committed
174
    handler.join()
175
176
177
178

    while True:
        time.sleep(1)

Markus Scheidgen's avatar
Markus Scheidgen committed
179
        rv = client.get('/uploads/%s' % upload['upload_id'])
180
181
182
        assert rv.status_code == 200
        upload = assert_upload(rv.data)
        assert 'upload_time' in upload
Markus Scheidgen's avatar
Markus Scheidgen committed
183
        if upload['completed']:
Markus Scheidgen's avatar
Markus Scheidgen committed
184
            break
185

Markus Scheidgen's avatar
Markus Scheidgen committed
186
187
188
    assert len(upload['tasks']) == 4
    assert upload['status'] == 'SUCCESS'
    assert upload['current_task'] == 'cleanup'
189
    calcs = upload['calcs']['results']
Markus Scheidgen's avatar
Markus Scheidgen committed
190
191
192
193
194
195
    for calc in calcs:
        assert calc['status'] == 'SUCCESS'
        assert calc['current_task'] == 'archiving'
        assert len(calc['tasks']) == 3
        assert_exists(config.files.uploads_bucket, upload['upload_id'])

196
197
198
199
200
201
    if upload['calcs']['pagination']['total'] > 1:
        rv = client.get('/uploads/%s?page=2&per_page=1&order_by=status' % upload['upload_id'])
        assert rv.status_code == 200
        upload = assert_upload(rv.data)
        assert len(upload['calcs']['results']) == 1

Markus Scheidgen's avatar
Markus Scheidgen committed
202
    time.sleep(1)
203
204


Markus Scheidgen's avatar
Markus Scheidgen committed
205
206
207
def test_repo_calc(client, example_elastic_calc):
    rv = client.get(
        '/repo/%s/%s' % (example_elastic_calc.upload_hash, example_elastic_calc.calc_hash))
208
209
210
    assert rv.status_code == 200


211
def test_non_existing_repo_cals(client):
212
213
214
215
    rv = client.get('/repo/doesnt/exist')
    assert rv.status_code == 404


Markus Scheidgen's avatar
Markus Scheidgen committed
216
def test_repo_calcs(client, example_elastic_calc):
217
218
219
220
221
222
223
224
225
    rv = client.get('/repo')
    assert rv.status_code == 200
    data = json.loads(rv.data)
    results = data.get('results', None)
    assert results is not None
    assert isinstance(results, list)
    assert len(results) >= 1


Markus Scheidgen's avatar
Markus Scheidgen committed
226
def test_repo_calcs_pagination(client, example_elastic_calc):
227
228
229
230
231
232
233
234
235
    rv = client.get('/repo?page=1&per_page=1')
    assert rv.status_code == 200
    data = json.loads(rv.data)
    results = data.get('results', None)
    assert results is not None
    assert isinstance(results, list)
    assert len(results) == 1


236
237
def test_get_archive(client, archive_id):
    rv = client.get('/archive/%s' % archive_id)
238
    assert rv.status_code == 302
239
240
241
242
243


def test_get_non_existing_archive(client):
    rv = client.get('/archive/%s' % 'doesnt/exist')
    assert rv.status_code == 404