Skip to content

Commit 07f0154

Browse files
author
Pedro Crespo
committed
Fixes pytest-asyncio loop issue in storage
Copied changes in storage files from PR ITISFoundation#355 - commit ff814d9
1 parent ad23fdb commit 07f0154

File tree

4 files changed

+88
-81
lines changed

4 files changed

+88
-81
lines changed

services/storage/src/simcore_service_storage/handlers.py

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ async def check_action(request: web.Request):
6868
}
6969
return output
7070

71+
7172
async def get_storage_locations(request: web.Request):
7273
log.info("CHECK LOCATION PATH %s %s",request.path, request.url)
7374

services/storage/tests/conftest.py

+75-62
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# TODO: W0611:Unused import ...
2-
# pylint: disable=W0611
3-
# TODO: W0613:Unused argument ...
4-
# pylint: disable=W0613
5-
#
6-
# pylint: disable=W0621
1+
# pylint:disable=wildcard-import
2+
# pylint:disable=unused-import
3+
# pylint:disable=unused-variable
4+
# pylint:disable=unused-argument
5+
# pylint:disable=redefined-outer-name
6+
77
import asyncio
88
import os
99
import subprocess
@@ -26,24 +26,27 @@
2626
SIMCORE_S3_STR)
2727
from utils import ACCESS_KEY, BUCKET_NAME, DATABASE, PASS, SECRET_KEY, USER
2828

29-
# fixtures -------------------------------------------------------
29+
3030

3131
@pytest.fixture(scope='session')
3232
def here():
3333
return Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent
3434

35+
3536
@pytest.fixture(scope='session')
3637
def package_dir(here):
3738
dirpath = Path(simcore_service_storage.__file__).parent
3839
assert dirpath.exists()
3940
return dirpath
4041

42+
4143
@pytest.fixture(scope='session')
4244
def osparc_simcore_root_dir(here):
4345
root_dir = here.parent.parent.parent
4446
assert root_dir.exists(), "Is this service within osparc-simcore repo?"
4547
return root_dir
4648

49+
4750
@pytest.fixture(scope='session')
4851
def python27_exec(osparc_simcore_root_dir, tmpdir_factory, here):
4952
# Assumes already created with make .venv27
@@ -52,16 +55,18 @@ def python27_exec(osparc_simcore_root_dir, tmpdir_factory, here):
5255
if not venv27.exists():
5356
# create its own virtualenv
5457
venv27 = tmpdir_factory.mktemp("virtualenv") / ".venv27"
55-
cmd = "virtualenv --python=python2 %s"%(venv27) # TODO: how to split in command safely?
56-
assert subprocess.check_call(cmd.split()) == 0, "Unable to run %s" %cmd
58+
# TODO: how to split in command safely?
59+
cmd = "virtualenv --python=python2 %s" % (venv27)
60+
assert subprocess.check_call(
61+
cmd.split()) == 0, "Unable to run %s" % cmd
5762

5863
# installs python2 requirements
5964
pip_exec = venv27 / "bin" / "pip"
6065
assert pip_exec.exists()
6166
requirements_py2 = here.parent / "requirements/py27.txt"
6267
cmd = "{} install -r {}".format(pip_exec, requirements_py2)
63-
assert subprocess.check_call(cmd.split()) == 0, "Unable to run %s" %cmd
64-
68+
assert subprocess.check_call(
69+
cmd.split()) == 0, "Unable to run %s" % cmd
6570

6671
python27_exec = venv27 / "bin" / "python2.7"
6772
assert python27_exec.exists()
@@ -73,19 +78,20 @@ def python27_path(python27_exec):
7378
return Path(python27_exec).parent.parent
7479
# Assumes already created with make .venv27
7580

81+
7682
@pytest.fixture(scope='session')
7783
def docker_compose_file(here):
7884
""" Overrides pytest-docker fixture
7985
"""
8086
old = os.environ.copy()
8187

8288
# docker-compose reads these environs
83-
os.environ['POSTGRES_DB']=DATABASE
84-
os.environ['POSTGRES_USER']=USER
85-
os.environ['POSTGRES_PASSWORD']=PASS
86-
os.environ['POSTGRES_ENDPOINT']="FOO" # TODO: update config schema!!
87-
os.environ['MINIO_ACCESS_KEY']=ACCESS_KEY
88-
os.environ['MINIO_SECRET_KEY']=SECRET_KEY
89+
os.environ['POSTGRES_DB'] = DATABASE
90+
os.environ['POSTGRES_USER'] = USER
91+
os.environ['POSTGRES_PASSWORD'] = PASS
92+
os.environ['POSTGRES_ENDPOINT'] = "FOO" # TODO: update config schema!!
93+
os.environ['MINIO_ACCESS_KEY'] = ACCESS_KEY
94+
os.environ['MINIO_SECRET_KEY'] = SECRET_KEY
8995

9096
dc_path = here / 'docker-compose.yml'
9197

@@ -94,12 +100,13 @@ def docker_compose_file(here):
94100

95101
os.environ = old
96102

103+
97104
@pytest.fixture(scope='session')
98105
def postgres_service(docker_services, docker_ip):
99106
url = 'postgresql://{user}:{password}@{host}:{port}/{database}'.format(
100-
user = USER,
101-
password = PASS,
102-
database = DATABASE,
107+
user=USER,
108+
password=PASS,
109+
database=DATABASE,
103110
host=docker_ip,
104111
port=docker_services.port_for('postgres', 5432),
105112
)
@@ -112,27 +119,29 @@ def postgres_service(docker_services, docker_ip):
112119
)
113120

114121
postgres_service = {
115-
'user' : USER,
116-
'password' : PASS,
117-
'database' : DATABASE,
118-
'host' : docker_ip,
119-
'port' : docker_services.port_for('postgres', 5432)
122+
'user': USER,
123+
'password': PASS,
124+
'database': DATABASE,
125+
'host': docker_ip,
126+
'port': docker_services.port_for('postgres', 5432)
120127
}
121128

122129
return postgres_service
123130

131+
124132
@pytest.fixture(scope='session')
125133
def postgres_service_url(postgres_service, docker_services, docker_ip):
126134
postgres_service_url = 'postgresql://{user}:{password}@{host}:{port}/{database}'.format(
127-
user = USER,
128-
password = PASS,
129-
database = DATABASE,
135+
user=USER,
136+
password=PASS,
137+
database=DATABASE,
130138
host=docker_ip,
131139
port=docker_services.port_for('postgres', 5432),
132140
)
133141

134142
return postgres_service_url
135143

144+
136145
@pytest.fixture(scope='function')
137146
async def postgres_engine(loop, postgres_service_url):
138147
postgres_engine = await create_engine(postgres_service_url)
@@ -163,24 +172,28 @@ def minio_service(docker_services, docker_ip):
163172
return {
164173
'endpoint': '{ip}:{port}'.format(ip=docker_ip, port=docker_services.port_for('minio', 9000)),
165174
'access_key': ACCESS_KEY,
166-
'secret_key' : SECRET_KEY,
167-
'bucket_name' : BUCKET_NAME,
168-
}
175+
'secret_key': SECRET_KEY,
176+
'bucket_name': BUCKET_NAME,
177+
}
178+
169179

170180
@pytest.fixture(scope="module")
171181
def s3_client(minio_service):
172182
from s3wrapper.s3_client import S3Client
173183

174-
s3_client = S3Client(endpoint=minio_service['endpoint'],access_key=minio_service["access_key"], secret_key=minio_service["secret_key"])
184+
s3_client = S3Client(
185+
endpoint=minio_service['endpoint'], access_key=minio_service["access_key"], secret_key=minio_service["secret_key"])
175186
return s3_client
176187

188+
177189
@pytest.fixture(scope="function")
178190
def mock_files_factory(tmpdir_factory):
179191
def _create_files(count):
180192
filepaths = []
181193
for _i in range(count):
182194
name = str(uuid.uuid4())
183-
filepath = os.path.normpath(str(tmpdir_factory.mktemp('data').join(name + ".txt")))
195+
filepath = os.path.normpath(
196+
str(tmpdir_factory.mktemp('data').join(name + ".txt")))
184197
with open(filepath, 'w') as fout:
185198
fout.write("Hello world\n")
186199
filepaths.append(filepath)
@@ -198,10 +211,11 @@ def dsm_mockup_db(postgres_service_url, s3_client, mock_files_factory):
198211
bucket_name = BUCKET_NAME
199212
s3_client.create_bucket(bucket_name, delete_contents_if_exists=True)
200213

201-
#TODO: use pip install Faker
202-
users = [ 'alice', 'bob', 'chuck', 'dennis']
214+
# TODO: use pip install Faker
215+
users = ['alice', 'bob', 'chuck', 'dennis']
203216

204-
projects = ['astronomy', 'biology', 'chemistry', 'dermatology', 'economics', 'futurology', 'geology']
217+
projects = ['astronomy', 'biology', 'chemistry',
218+
'dermatology', 'economics', 'futurology', 'geology']
205219
location = SIMCORE_S3_STR
206220

207221
nodes = ['alpha', 'beta', 'gamma', 'delta']
@@ -214,41 +228,43 @@ def dsm_mockup_db(postgres_service_url, s3_client, mock_files_factory):
214228
idx = randrange(len(users))
215229
user_name = users[idx]
216230
user_id = idx + 10
217-
idx = randrange(len(projects))
231+
idx = randrange(len(projects))
218232
project_name = projects[idx]
219233
project_id = idx + 100
220-
idx = randrange(len(nodes))
234+
idx = randrange(len(nodes))
221235
node = nodes[idx]
222236
node_id = idx + 10000
223237
file_name = str(counter)
224-
object_name = Path(str(project_id), str(node_id), str(counter)).as_posix()
238+
object_name = Path(str(project_id), str(
239+
node_id), str(counter)).as_posix()
225240
file_uuid = Path(object_name).as_posix()
226241

227242
assert s3_client.upload_file(bucket_name, object_name, _file)
228243

229-
d = { 'file_uuid' : file_uuid,
230-
'location_id' : "0",
231-
'location' : location,
232-
'bucket_name' : bucket_name,
233-
'object_name' : object_name,
234-
'project_id' : str(project_id),
235-
'project_name' : project_name,
236-
'node_id' : str(node_id),
237-
'node_name' : node,
238-
'file_name' : file_name,
239-
'user_id' : str(user_id),
240-
'user_name' : user_name
241-
}
244+
d = {'file_uuid': file_uuid,
245+
'location_id': "0",
246+
'location': location,
247+
'bucket_name': bucket_name,
248+
'object_name': object_name,
249+
'project_id': str(project_id),
250+
'project_name': project_name,
251+
'node_id': str(node_id),
252+
'node_name': node,
253+
'file_name': file_name,
254+
'user_id': str(user_id),
255+
'user_name': user_name
256+
}
242257

243258
counter = counter + 1
244259

245260
data[object_name] = FileMetaData(**d)
246261

247-
utils.insert_metadata(postgres_service_url, data[object_name]) #pylint: disable=no-member
248-
262+
# pylint: disable=no-member
263+
utils.insert_metadata(postgres_service_url,
264+
data[object_name])
249265

250266
total_count = 0
251-
for _obj in s3_client.list_objects_v2(bucket_name, recursive = True):
267+
for _obj in s3_client.list_objects_v2(bucket_name, recursive=True):
252268
total_count = total_count + 1
253269

254270
assert total_count == N
@@ -260,10 +276,6 @@ def dsm_mockup_db(postgres_service_url, s3_client, mock_files_factory):
260276
# db
261277
utils.drop_tables(url=postgres_service_url)
262278

263-
# This is weird, somehow the default loop gives problems with pytest asyncio, so lets override it
264-
@pytest.fixture
265-
def loop(event_loop):
266-
return event_loop
267279

268280
@pytest.fixture(scope="function")
269281
async def datcore_testbucket(loop, python27_exec, mock_files_factory):
@@ -282,19 +294,20 @@ async def datcore_testbucket(loop, python27_exec, mock_files_factory):
282294

283295
ready = False
284296
counter = 0
285-
while not ready and counter<5:
297+
while not ready and counter < 5:
286298
data = await dcw.list_files()
287299
ready = len(data) == 2
288300
await asyncio.sleep(10)
289301
counter = counter + 1
290302

291-
292303
yield BUCKET_NAME
293304

294305
await dcw.delete_test_dataset(BUCKET_NAME)
295306

307+
296308
@pytest.fixture(scope="function")
297309
def dsm_fixture(s3_client, python27_exec, postgres_engine, loop):
298310
pool = ThreadPoolExecutor(3)
299-
dsm_fixture = DataStorageManager(s3_client, python27_exec, postgres_engine, loop, pool, BUCKET_NAME)
311+
dsm_fixture = DataStorageManager(
312+
s3_client, python27_exec, postgres_engine, loop, pool, BUCKET_NAME)
300313
return dsm_fixture

services/storage/tests/requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
coveralls
66
pytest
77
pytest-aiohttp
8-
pytest-asyncio
98
pytest-cov
109
pytest-docker
1110
openapi_spec_validator
1211
pyyaml
1312
virtualenv
13+
14+
# NOTE: pytest-aiohttp and pytest-asyncio incompatible
15+
# https://github.com/pytest-dev/pytest-asyncio/issues/76

services/storage/tests/test_dsm.py

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# TODO: W0611:Unused import ...
2-
# pylint: disable=W0611
3-
# TODO: W0613:Unused argument ...
4-
# pylint: disable=W0613
1+
# pylint:disable=wildcard-import
2+
# pylint:disable=unused-import
3+
# pylint:disable=unused-variable
4+
# pylint:disable=unused-argument
5+
# pylint:disable=redefined-outer-name
6+
# pylint: disable=too-many-arguments
57

68
import filecmp
79
import io
@@ -10,17 +12,17 @@
1012
import pdb
1113
import urllib
1214
import uuid
13-
from pprint import pprint
14-
1515
from pathlib import Path
16+
from pprint import pprint
1617

1718
import attr
1819
import pytest
1920

2021
import utils
2122
from simcore_service_storage.dsm import DataStorageManager
2223
from simcore_service_storage.models import FileMetaData
23-
from simcore_service_storage.s3 import DATCORE_STR, SIMCORE_S3_STR, SIMCORE_S3_ID
24+
from simcore_service_storage.s3 import (DATCORE_STR, SIMCORE_S3_ID,
25+
SIMCORE_S3_STR)
2426
from utils import BUCKET_NAME
2527

2628

@@ -176,7 +178,6 @@ async def test_copy_s3_s3(postgres_service_url, s3_client, mock_files_factory, d
176178
assert len(data) == 2
177179

178180
#NOTE: Below tests directly access the datcore platform, use with care!
179-
180181
@pytest.mark.travis
181182
def test_datcore_fixture(datcore_testbucket):
182183
print(datcore_testbucket)
@@ -198,8 +199,6 @@ async def test_dsm_datcore(postgres_service_url, dsm_fixture, datcore_testbucket
198199
data = await dsm.list_files(user_id=user_id, location=DATCORE_STR)
199200
assert len(data) == 1
200201

201-
# pylint: disable=R0913
202-
# Too many arguments
203202
@pytest.mark.travis
204203
async def test_dsm_s3_to_datcore(postgres_service_url, s3_client, mock_files_factory, dsm_fixture, datcore_testbucket):
205204
utils.create_tables(url=postgres_service_url)
@@ -230,8 +229,6 @@ async def test_dsm_s3_to_datcore(postgres_service_url, s3_client, mock_files_fac
230229
# there should now be 3 files
231230
assert len(data) == 3
232231

233-
# pylint: disable=R0913
234-
# Too many arguments
235232
@pytest.mark.travis
236233
async def test_dsm_datcore_to_local(postgres_service_url, dsm_fixture, mock_files_factory, datcore_testbucket):
237234
utils.create_tables(url=postgres_service_url)
@@ -250,8 +247,6 @@ async def test_dsm_datcore_to_local(postgres_service_url, dsm_fixture, mock_file
250247

251248
assert filecmp.cmp(tmp_file2, tmp_file)
252249

253-
# pylint: disable=R0913
254-
# Too many arguments
255250
@pytest.mark.travis
256251
async def test_dsm_datcore_to_S3(postgres_service_url, s3_client, dsm_fixture, mock_files_factory, datcore_testbucket):
257252
utils.create_tables(url=postgres_service_url)
@@ -287,10 +282,6 @@ async def test_dsm_datcore_to_S3(postgres_service_url, s3_client, dsm_fixture, m
287282

288283
assert filecmp.cmp(tmp_file1, tmp_file2)
289284

290-
291-
292-
# pylint: disable=R0913
293-
# Too many arguments
294285
@pytest.mark.travis
295286
async def test_copy_datcore(postgres_service_url, s3_client, dsm_fixture, mock_files_factory, datcore_testbucket):
296287
utils.create_tables(url=postgres_service_url)

0 commit comments

Comments
 (0)