-
Notifications
You must be signed in to change notification settings - Fork 3
/
fixtures_server.py
448 lines (358 loc) · 11.9 KB
/
fixtures_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""
Copyright 2022 (C) Friedrich Miescher Institute for Biomedical Research and
University of Zurich
Original authors:
Jacopo Nespolo <jacopo.nespolo@exact-lab.it>
This file is part of Fractal and was originally developed by eXact lab S.r.l.
<exact-lab.it> under contract with Liberali Lab from the Friedrich Miescher
Institute for Biomedical Research and Pelkmans Lab from the University of
Zurich.
"""
import logging
import shutil
from dataclasses import dataclass
from dataclasses import field
from pathlib import Path
from typing import Any
from typing import AsyncGenerator
from typing import Dict
from typing import List
from typing import Optional
from uuid import uuid4
import pytest
from asgi_lifespan import LifespanManager
from devtools import debug
from fastapi import FastAPI
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from fractal_server.config import get_settings
from fractal_server.config import Settings
from fractal_server.syringe import Inject
try:
import asyncpg # noqa: F401
DB_ENGINE = "postgres"
except ModuleNotFoundError:
DB_ENGINE = "sqlite"
HAS_LOCAL_SBATCH = bool(shutil.which("sbatch"))
def check_python_has_venv(python_path: str, temp_path: Path):
"""
This function checks that we can safely use a certain python interpreter,
namely
1. It exists;
2. It has the venv module installed.
"""
import subprocess
import shlex
temp_path.parent.mkdir(parents=True, exist_ok=True)
temp_path.parent.chmod(0o777)
temp_path.mkdir(parents=True, exist_ok=True)
temp_path.chmod(0o777)
cmd = f"{python_path} -m venv {temp_path.as_posix()}"
p = subprocess.run(
shlex.split(cmd),
capture_output=True,
)
if p.returncode != 0:
debug(cmd)
debug(p.stdout.decode("UTF-8"))
debug(p.stderr.decode("UTF-8"))
logging.warning(
"check_python_has_venv({python_path=}, {temp_path=}) failed."
)
raise RuntimeError(
p.stderr.decode("UTF-8"),
f"Hint: is the venv module installed for {python_path}? "
f'Try running "{cmd}".',
)
def get_patched_settings(temp_path: Path):
settings = Settings()
settings.JWT_SECRET_KEY = "secret_key"
settings.DEPLOYMENT_TYPE = "development"
settings.DB_ENGINE = DB_ENGINE
if DB_ENGINE == "sqlite":
settings.SQLITE_PATH = (
f"{temp_path.as_posix()}/_test.db?mode=memory&cache=shared"
)
elif DB_ENGINE == "postgres":
settings.DB_ENGINE = "postgres"
settings.POSTGRES_USER = "postgres"
settings.POSTGRES_PASSWORD = "postgres"
settings.POSTGRES_DB = "fractal"
else:
raise ValueError
settings.FRACTAL_TASKS_DIR = temp_path / "fractal_root"
settings.FRACTAL_TASKS_DIR.mkdir(parents=True, exist_ok=True)
debug(settings.FRACTAL_TASKS_DIR)
settings.FRACTAL_TASKS_DIR.chmod(0o777)
settings.FRACTAL_RUNNER_WORKING_BASE_DIR = temp_path / "artifacts"
settings.FRACTAL_RUNNER_WORKING_BASE_DIR.mkdir(parents=True, exist_ok=True)
settings.FRACTAL_RUNNER_WORKING_BASE_DIR.chmod(0o777)
# NOTE:
# This variable is set to work with the system interpreter within a docker
# container. If left unset it defaults to `sys.executable`
if not HAS_LOCAL_SBATCH:
settings.FRACTAL_SLURM_WORKER_PYTHON = "/usr/bin/python3"
check_python_has_venv(
"/usr/bin/python3", temp_path / "check_python_has_venv"
)
settings.FRACTAL_SLURM_CONFIG_FILE = temp_path / "slurm_config.json"
settings.FRACTAL_SLURM_POLL_INTERVAL = 4
settings.FRACTAL_SLURM_KILLWAIT_INTERVAL = 4
settings.FRACTAL_LOGGING_LEVEL = logging.DEBUG
return settings
@pytest.fixture(scope="session", autouse=True)
def override_settings(tmp777_session_path):
tmp_path = tmp777_session_path("server_folder")
settings = get_patched_settings(tmp_path)
def _get_settings():
return settings
Inject.override(get_settings, _get_settings)
try:
yield settings
finally:
Inject.pop(get_settings)
@pytest.fixture(scope="function")
def override_settings_factory():
from fractal_server.config import Settings
# NOTE: using a mutable variable so that we can modify it from within the
# inner function
get_settings_orig = []
def _overrride_settings_factory(**kwargs):
# NOTE: extract patched settings *before* popping out the patch!
settings = Settings(**Inject(get_settings).dict())
get_settings_orig.append(Inject.pop(get_settings))
for k, v in kwargs.items():
setattr(settings, k, v)
def _get_settings():
return settings
Inject.override(get_settings, _get_settings)
try:
yield _overrride_settings_factory
finally:
if get_settings_orig:
Inject.override(get_settings, get_settings_orig[0])
@pytest.fixture
def unset_deployment_type():
"""
Temporarily override the seetings with a version that would fail
`settings.check()`
Afterwards, restore any previous injection, if any.
"""
settings = Settings()
settings.DEPLOYMENT_TYPE = None
def _get_settings():
return settings
try:
previous = Inject.pop(get_settings)
except RuntimeError:
previous = None
Inject.override(get_settings, _get_settings)
try:
yield
finally:
Inject.pop(get_settings)
if previous:
Inject.override(get_settings, previous)
@pytest.fixture
async def db_create_tables(override_settings):
from fractal_server.app.db import DB
from fractal_server.app.models import SQLModel
engine = DB.engine_sync()
metadata = SQLModel.metadata
metadata.create_all(engine)
yield
metadata.drop_all(engine)
@pytest.fixture
async def db(db_create_tables):
from fractal_server.app.db import get_db
async for session in get_db():
yield session
@pytest.fixture
async def db_sync(db_create_tables):
from fractal_server.app.db import get_sync_db
for session in get_sync_db():
yield session
@pytest.fixture
async def app(override_settings) -> AsyncGenerator[FastAPI, Any]:
app = FastAPI()
yield app
@pytest.fixture
async def register_routers(app, override_settings):
from fractal_server.main import collect_routers
collect_routers(app)
@pytest.fixture
async def client(
app: FastAPI, register_routers, db
) -> AsyncGenerator[AsyncClient, Any]:
async with AsyncClient(
app=app, base_url="http://test"
) as client, LifespanManager(app):
yield client
@pytest.fixture
async def MockCurrentUser(app, db):
from fractal_server.app.security import current_active_user
from fractal_server.app.security import User
@dataclass
class _MockCurrentUser:
"""
Context managed user override
"""
name: str = "User Name"
user_kwargs: Optional[Dict[str, Any]] = None
scopes: Optional[List[str]] = field(
default_factory=lambda: ["project"]
)
email: Optional[str] = field(
default_factory=lambda: f"{uuid4()}@exact-lab.it"
)
persist: Optional[bool] = True
def _create_user(self):
defaults = dict(
email=self.email,
hashed_password="fake_hashed_password",
slurm_user="test01",
)
if self.user_kwargs:
defaults.update(self.user_kwargs)
self.user = User(name=self.name, **defaults)
def current_active_user_override(self):
def __current_active_user_override():
return self.user
return __current_active_user_override
async def __aenter__(self):
self._create_user()
if self.persist:
db.add(self.user)
await db.commit()
await db.refresh(self.user)
# Removing object from test db session, so that we can operate
# on user from other sessions
db.expunge(self.user)
self.previous_user = app.dependency_overrides.get(
current_active_user, None
)
app.dependency_overrides[
current_active_user
] = self.current_active_user_override()
return self.user
async def __aexit__(self, *args, **kwargs):
if self.previous_user:
app.dependency_overrides[
current_active_user
] = self.previous_user()
return _MockCurrentUser
@pytest.fixture
async def project_factory(db):
"""
Factory that adds a project to the database
"""
from fractal_server.app.models import Project
async def __project_factory(user, **kwargs):
defaults = dict(
name="project",
project_dir="/tmp/",
)
defaults.update(kwargs)
project = Project(**defaults)
project.user_member_list.append(user)
db.add(project)
await db.commit()
await db.refresh(project)
return project
return __project_factory
@pytest.fixture
async def dataset_factory(db):
from fractal_server.app.models import Project, Dataset
async def __dataset_factory(project: Project, **kwargs):
defaults = dict(name="test dataset")
defaults.update(kwargs)
project.dataset_list.append(Dataset(**defaults))
db.add(project)
await db.commit()
await db.refresh(project)
return project.dataset_list[-1]
return __dataset_factory
@pytest.fixture
async def resource_factory(db, testdata_path):
from fractal_server.app.models import Dataset, Resource
async def __resource_factory(dataset: Dataset, **kwargs):
"""
Add a new resorce to dataset
"""
defaults = dict(
path=(testdata_path / "png").as_posix(), glob_pattern="*.png"
)
defaults.update(kwargs)
resource = Resource(dataset_id=dataset.id, **defaults)
db.add(resource)
await db.commit()
await db.refresh(dataset)
return dataset.resource_list[-1]
return __resource_factory
@pytest.fixture
async def task_factory(db: AsyncSession):
"""
Insert task in db
"""
from fractal_server.app.models import Task
async def __task_factory(db: AsyncSession = db, index: int = 0, **kwargs):
defaults = dict(
name=f"task{index}",
input_type="zarr",
output_type="zarr",
command="cmd",
source="source",
)
args = dict(**defaults)
args.update(kwargs)
t = Task(**args)
db.add(t)
await db.commit()
await db.refresh(t)
return t
return __task_factory
@pytest.fixture
async def job_factory(db: AsyncSession):
"""
Insert job in db
"""
from fractal_server.app.models import ApplyWorkflow
async def __job_factory(
working_dir: Path, db: AsyncSession = db, **kwargs
):
defaults = dict(
project_id=1,
input_dataset_id=1,
output_dataset_id=2,
workflow_id=1,
overwrite_input=False,
worker_init="WORKER_INIT string",
working_dir=working_dir,
)
args = dict(**defaults)
args.update(kwargs)
j = ApplyWorkflow(**args)
db.add(j)
await db.commit()
await db.refresh(j)
return j
return __job_factory
@pytest.fixture
async def workflow_factory(db: AsyncSession):
"""
Insert workflow in db
"""
from fractal_server.app.models import Workflow
async def __workflow_factory(db: AsyncSession = db, **kwargs):
defaults = dict(
name="my workflow",
project_id=1,
)
args = dict(**defaults)
args.update(kwargs)
w = Workflow(**args)
db.add(w)
await db.commit()
await db.refresh(w)
return w
return __workflow_factory