forked from rucio/rucio
/
conftest.py
623 lines (472 loc) · 20.7 KB
/
conftest.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# -*- coding: utf-8 -*-
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import traceback
import re
import functools
from os import environ
from random import choice
from string import ascii_uppercase
import pytest
_del_test_prefix = functools.partial(re.compile(r'^[Tt][Ee][Ss][Tt]_?').sub, '')
# local imports in the fixtures to make this file loadable in e.g. client tests
pytest_plugins = ('tests.ruciopytest.artifacts_plugin', )
def pytest_configure(config):
config.addinivalue_line('markers', 'dirty: marks test as dirty, i.e. tests are leaving structures behind')
config.addinivalue_line('markers', 'noparallel(reason, groups): marks test being unable to run in parallel to other tests')
if config.pluginmanager.hasplugin("xdist"):
from .ruciopytest import xdist_noparallel_scheduler
config.pluginmanager.register(xdist_noparallel_scheduler)
def pytest_make_parametrize_id(config, val, argname):
if argname == 'file_config_mock':
cfg = {}
for section, option, value in val['overrides']:
cfg.setdefault(section, {})[option] = value
return argname + str(cfg)
if argname == 'core_config_mock':
cfg = {}
for section, option, value in val['table_content']:
cfg.setdefault(section, {})[option] = value
return argname + str(cfg)
# return None to let pytest handle the formatting
return None
@pytest.fixture(scope='session')
def session_scope_prefix():
"""
Generate a name prefix to be shared by objects created during this pytest session
"""
return ''.join(choice(ascii_uppercase) for _ in range(6)) + '-'
@pytest.fixture(scope='module')
def module_scope_prefix(request, session_scope_prefix):
"""
Generate a name prefix to be shared by objects created during this pytest module
Relies on pytest's builtin fixture "request"
https://docs.pytest.org/en/6.2.x/reference.html#std-fixture-request
"""
return session_scope_prefix + _del_test_prefix(request.module.__name__.split('.')[-1]) + '-'
@pytest.fixture(scope='class')
def class_scope_prefix(request, module_scope_prefix):
if not request.cls:
return module_scope_prefix
return module_scope_prefix + _del_test_prefix(request.cls.__name__) + '-'
@pytest.fixture(scope='function')
def function_scope_prefix(request, class_scope_prefix):
return class_scope_prefix + _del_test_prefix(request.node.originalname) + '-'
@pytest.fixture(scope='session')
def vo():
if environ.get('SUITE', 'remote_dbs') != 'client':
# Server test, we can use short VO via DB for internal tests
from rucio.tests.common_server import get_vo
return get_vo()
else:
# Client-only test, only use config with no DB config
from rucio.tests.common import get_long_vo
return get_long_vo()
@pytest.fixture(scope='session')
def second_vo():
from rucio.common.config import config_get_bool
from rucio.core.vo import vo_exists, add_vo
multi_vo = config_get_bool('common', 'multi_vo', raise_exception=False, default=False)
if not multi_vo:
pytest.skip('multi_vo mode is not enabled. Running multi_vo tests in single_vo mode would result in failures.')
new_vo = 'new'
if not vo_exists(vo=new_vo):
add_vo(vo=new_vo, description='Test', email='rucio@email.com')
return new_vo
@pytest.fixture(scope='session')
def long_vo():
from rucio.tests.common import get_long_vo
return get_long_vo()
@pytest.fixture(scope='module')
def account_client():
from rucio.client.accountclient import AccountClient
return AccountClient()
@pytest.fixture(scope='module')
def replica_client():
from rucio.client.replicaclient import ReplicaClient
return ReplicaClient()
@pytest.fixture(scope='module')
def rucio_client():
from rucio.client import Client
return Client()
@pytest.fixture(scope='module')
def did_client():
from rucio.client.didclient import DIDClient
return DIDClient()
@pytest.fixture(scope='module')
def rse_client():
from rucio.client.rseclient import RSEClient
return RSEClient()
@pytest.fixture(scope='module')
def scope_client():
from rucio.client.scopeclient import ScopeClient
return ScopeClient()
@pytest.fixture(scope='module')
def dirac_client():
from rucio.client.diracclient import DiracClient
return DiracClient()
@pytest.fixture
def download_client():
from rucio.client.downloadclient import DownloadClient
return DownloadClient()
@pytest.fixture
def rest_client():
from rucio.tests.common import print_response
from flask.testing import FlaskClient
from rucio.web.rest.flaskapi.v1.main import application
class WrappedFlaskClient(FlaskClient):
def __init__(self, *args, **kwargs):
super(WrappedFlaskClient, self).__init__(*args, **kwargs)
def open(self, path='/', *args, **kwargs):
print(kwargs.get('method', 'GET'), path)
response = super(WrappedFlaskClient, self).open(path, *args, **kwargs)
try:
print_response(response)
except Exception:
traceback.print_exc()
return response
_testing = application.testing
application.testing = True
application.test_client_class = WrappedFlaskClient
with application.test_client() as client:
yield client
application.test_client_class = None
application.testing = _testing
@pytest.fixture
def auth_token(rest_client, long_vo):
from rucio.tests.common import vohdr, headers, loginhdr
auth_response = rest_client.get('/auth/userpass', headers=headers(loginhdr('root', 'ddmlab', 'secret'), vohdr(long_vo)))
assert auth_response.status_code == 200
token = auth_response.headers.get('X-Rucio-Auth-Token')
assert token
return str(token)
@pytest.fixture(scope='module')
def mock_scope(vo):
from rucio.common.types import InternalScope
return InternalScope('mock', vo=vo)
@pytest.fixture(scope='module')
def test_scope(vo):
from rucio.common.types import InternalScope
return InternalScope('test', vo=vo)
@pytest.fixture(scope='module')
def root_account(vo):
from rucio.common.types import InternalAccount
return InternalAccount('root', vo=vo)
@pytest.fixture(scope='module')
def jdoe_account(vo):
from rucio.common.types import InternalAccount
return InternalAccount('jdoe', vo=vo)
@pytest.fixture
def random_account(vo):
import random
import string
from rucio.common.types import InternalAccount
from rucio.core.account import add_account, del_account
from rucio.db.sqla import models
from rucio.db.sqla.constants import AccountType
from rucio.tests.common_server import cleanup_db_deps
account = InternalAccount(''.join(random.choice(string.ascii_uppercase) for _ in range(10)), vo=vo)
add_account(account=account, type_=AccountType.USER, email=f'{account.external}@email.com')
yield account
cleanup_db_deps(model=models.Account, select_rows_stmt=models.Account.account == account)
del_account(account)
@pytest.fixture(scope="module")
def containerized_rses(rucio_client):
"""
Detects if containerized rses for xrootd & ssh are available in the testing environment.
:return: A list of (rse_name, rse_id) tuples.
"""
from rucio.common.exception import InvalidRSEExpression
rses = []
try:
xrd_rses = [x['rse'] for x in rucio_client.list_rses(rse_expression='test_container_xrd=True')]
xrd_rses = [rucio_client.get_rse(rse) for rse in xrd_rses]
xrd_containerized_rses = [(rse_obj['rse'], rse_obj['id']) for rse_obj in xrd_rses if "xrd" in rse_obj['rse'].lower()]
xrd_containerized_rses.sort()
rses.extend(xrd_containerized_rses)
ssh_rses = [x['rse'] for x in rucio_client.list_rses(rse_expression='test_container_ssh=True')]
ssh_rses = [rucio_client.get_rse(rse) for rse in ssh_rses]
ssh_containerized_rses = [(rse_obj['rse'], rse_obj['id']) for rse_obj in ssh_rses if "ssh" in rse_obj['rse'].lower()]
ssh_containerized_rses.sort()
rses.extend(ssh_containerized_rses)
except InvalidRSEExpression as invalid_rse_expression:
print("{ex}. Note that containerized RSEs will not be available in non-containerized test environments"
.format(ex=invalid_rse_expression))
traceback.print_exc()
return rses
@pytest.fixture
def rse_factory(request, vo, function_scope_prefix):
from .temp_factories import TemporaryRSEFactory
session = None
if 'db_session' in request.fixturenames:
session = request.getfixturevalue('db_session')
with TemporaryRSEFactory(vo=vo, name_prefix=function_scope_prefix, db_session=session) as factory:
yield factory
@pytest.fixture(scope="class")
def rse_factory_unittest(request, vo, class_scope_prefix):
"""
unittest classes can get access to rse_factory fixture via this fixture
"""
from .temp_factories import TemporaryRSEFactory
with TemporaryRSEFactory(vo=vo, name_prefix=class_scope_prefix) as factory:
request.cls.rse_factory = factory
yield factory
@pytest.fixture
def did_factory(request, vo, mock_scope, function_scope_prefix, file_factory, root_account):
from .temp_factories import TemporaryDidFactory
session = None
if 'db_session' in request.fixturenames:
session = request.getfixturevalue('db_session')
with TemporaryDidFactory(vo=vo, default_scope=mock_scope, name_prefix=function_scope_prefix, file_factory=file_factory,
default_account=root_account, db_session=session) as factory:
yield factory
@pytest.fixture
def file_factory(tmp_path_factory):
from .temp_factories import TemporaryFileFactory
with TemporaryFileFactory(pytest_path_factory=tmp_path_factory) as factory:
yield factory
@pytest.fixture
def scope_factory():
from rucio.common.utils import generate_uuid
from rucio.core.scope import add_scope
from rucio.common.types import InternalAccount, InternalScope
def create_scopes(vos, account_name=None):
scope_uuid = str(generate_uuid()).lower()[:16]
scope_name = 'shr_%s' % scope_uuid
created_scopes = []
for vo in vos:
scope = InternalScope(scope_name, vo=vo)
add_scope(scope, InternalAccount(account_name if account_name else 'root', vo=vo))
created_scopes.append(scope)
return scope_name, created_scopes
return create_scopes
class _TagFactory:
def __init__(self, prefix):
self.prefix = prefix
self.index = 0
def new_tag(self):
self.index += 1
return f'{self.prefix}-{self.index}'
@pytest.fixture
def tag_factory(function_scope_prefix):
return _TagFactory(prefix=f'{function_scope_prefix}{"".join(choice(ascii_uppercase) for _ in range(6))}'.replace('_', '-'))
@pytest.fixture(scope='class')
def tag_factory_class(class_scope_prefix):
return _TagFactory(prefix=f'{class_scope_prefix}{"".join(choice(ascii_uppercase) for _ in range(6))}'.replace('_', '-'))
@pytest.fixture
def db_session():
from rucio.db.sqla import session
db_session = session.get_session()
yield db_session
db_session.commit()
db_session.close()
def __get_fixture_param(request):
fixture_param = getattr(request, "param", None)
if not fixture_param and request.instance:
# Parametrize support is incomplete for legacy unittest test cases
# Manually retrieve the parameters from the list of marks:
mark = next(iter(filter(lambda m: m.name == 'parametrize', request.instance.pytestmark)), None)
if mark:
fixture_param = mark.args[1][0]
return fixture_param
def __create_in_memory_db_table(name, *columns, **kwargs):
"""
Create an in-memory temporary table using the sqlite memory driver.
Make sqlalchemy aware of that table by registering it via a
declarative base.
"""
import datetime
from sqlalchemy import Column, DateTime, CheckConstraint
from sqlalchemy.pool import StaticPool
from sqlalchemy.schema import Table
from sqlalchemy.orm import registry
from rucio.db.sqla.models import ModelBase
from rucio.db.sqla.session import get_maker, create_engine
engine = create_engine('sqlite://', connect_args={'check_same_thread': False}, poolclass=StaticPool)
# Create a class which inherits from ModelBase. This will allow us to use the rucio-specific methods like .save()
DeclarativeObj = type('DeclarativeObj{}'.format(name), (ModelBase,), {})
# Create a new declarative base and map the previously created object into the base
mapper_registry = registry()
InMemoryBase = mapper_registry.generate_base(name='InMemoryBase{}'.format(name))
table_args = tuple(columns) + tuple(kwargs.get('table_args', ())) + (
Column("created_at", DateTime, default=datetime.datetime.utcnow),
Column("updated_at", DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow),
CheckConstraint('CREATED_AT IS NOT NULL', name=name.upper() + '_CREATED_NN'),
CheckConstraint('UPDATED_AT IS NOT NULL', name=name.upper() + '_UPDATED_NN'),
)
table = Table(
name,
InMemoryBase.metadata,
*table_args
)
mapper_registry.map_imperatively(DeclarativeObj, table)
# Performa actual creation of the in-memory table
InMemoryBase.metadata.create_all(engine)
# Register the new table with the associated engine into the sqlalchemy sessionmaker
# In theory, this code must be protected by rucio.db.scla.session._LOCK, but this code will be executed
# during test case initialization, so there is no risk here to have concurrent calls from within the
# same process
senssionmaker = get_maker()
senssionmaker.kw.setdefault('binds', {}).update({DeclarativeObj: engine})
return DeclarativeObj
@pytest.fixture
def message_mock():
"""
Fixture which overrides the Message table with a private instance
"""
from unittest import mock
from sqlalchemy import Column
from rucio.common.utils import generate_uuid
from rucio.db.sqla.models import String, PrimaryKeyConstraint, CheckConstraint, Text, Index, GUID
InMemoryMessage = __create_in_memory_db_table(
'message_' + generate_uuid(),
Column('id', GUID(), default=generate_uuid),
Column('event_type', String(256)),
Column('payload', String(4000)),
Column('payload_nolimit', Text),
Column('services', String(256)),
table_args=(PrimaryKeyConstraint('id', name='MESSAGES_ID_PK'),
CheckConstraint('EVENT_TYPE IS NOT NULL', name='MESSAGES_EVENT_TYPE_NN'),
CheckConstraint('PAYLOAD IS NOT NULL', name='MESSAGES_PAYLOAD_NN'),
Index('MESSAGES_SERVICES_IDX', 'services', 'event_type'))
)
with mock.patch('rucio.core.message.Message', new=InMemoryMessage):
yield
@pytest.fixture
def core_config_mock(request):
"""
Fixture to allow having per-test core.config tables without affecting the other parallel tests.
This override works only in tests which use core function calls directly, not in the ones working
via the API, because the normal config table is not touched and the rucio instance answering API
calls is not aware of this mock.
This fixture acts by creating a new copy of the "config" sql table using the :memory: sqlite engine.
Accesses to the "models.Config" table are then redirected to this temporary table via mock.patch().
"""
from unittest import mock
from sqlalchemy import Column
from rucio.common.utils import generate_uuid
from rucio.db.sqla.models import String, PrimaryKeyConstraint
from rucio.db.sqla.session import get_session
# Get the fixture parameters
table_content = []
params = __get_fixture_param(request)
if params:
table_content = params.get("table_content", table_content)
InMemoryConfig = __create_in_memory_db_table(
'configs_' + generate_uuid(),
Column('section', String(128)),
Column('opt', String(128)),
Column('value', String(4000)),
table_args=(PrimaryKeyConstraint('section', 'opt', name='CONFIGS_PK'),),
)
# Fill the table with the requested mock data
session = get_session()()
for section, option, value in (table_content or []):
InMemoryConfig(section=section, opt=option, value=value).save(flush=True, session=session)
session.commit()
with mock.patch('rucio.core.config.models.Config', new=InMemoryConfig):
yield
@pytest.fixture
def file_config_mock(request):
"""
Fixture which allows to have an isolated in-memory configuration file instance which
is not persisted after exiting the fixture.
This override works only in tests which use config calls directly, not in the ones working
via the API, as the server config is not changed.
"""
from unittest import mock
from rucio.common.config import Config, config_set, config_has_section, config_add_section
# Get the fixture parameters
overrides = []
params = __get_fixture_param(request)
if params:
overrides = params.get("overrides", overrides)
parser = Config().parser
with mock.patch('rucio.common.config.get_config', side_effect=lambda: parser):
for section, option, value in (overrides or []):
if not config_has_section(section):
config_add_section(section)
config_set(section, option, value)
yield parser
@pytest.fixture
def caches_mock(request):
"""
Fixture which overrides the different internal caches with in-memory ones for the duration
of a particular test.
This override works only in tests which use core function calls directly, not in the ones
working via API.
The fixture acts by by mock.patch the REGION object in the provided list of modules to mock.
"""
from unittest import mock
from contextlib import ExitStack
from dogpile.cache import make_region
caches_to_mock = []
expiration_time = 600
params = __get_fixture_param(request)
if params:
caches_to_mock = params.get("caches_to_mock", caches_to_mock)
expiration_time = params.get("expiration_time", expiration_time)
with ExitStack() as stack:
mocked_caches = []
for module in caches_to_mock:
region = make_region().configure('dogpile.cache.memory', expiration_time=expiration_time)
stack.enter_context(mock.patch(module, new=region))
mocked_caches.append(region)
yield mocked_caches
@pytest.fixture
def metrics_mock():
"""
Overrides the prometheus metric registry and allows to verify if the desired
prometheus metrics were correctly recorded.
"""
from unittest import mock
from prometheus_client import CollectorRegistry, values
with mock.patch('rucio.core.monitor.REGISTRY', new=CollectorRegistry()) as registry, \
mock.patch('rucio.core.monitor.COUNTERS', new={}), \
mock.patch('rucio.core.monitor.GAUGES', new={}), \
mock.patch('rucio.core.monitor.TIMINGS', new={}), \
mock.patch('prometheus_client.values.ValueClass', new=values.MutexValue):
yield registry
@pytest.fixture
def upload_success_str():
return lambda x: f'Successfully uploaded file {x}'
@pytest.fixture
def account_name_generator():
from rucio.common.utils import generate_uuid
return lambda: 'jdoe-' + str(generate_uuid()).lower()[:16]
@pytest.fixture
def scope_name_generator():
from rucio.common.utils import generate_uuid
return lambda: 'mock_' + str(generate_uuid()).lower()[:16]
@pytest.fixture
def rse_name_generator():
def generator(size=10):
return 'MOCK-' + ''.join(choice(ascii_uppercase) for _ in range(size))
return generator
@pytest.fixture
def client_rse_factory(rse_client, rse_factory):
"""
Makes an rse factory that does not require a new session
"""
if environ.get('SUITE', 'remote_dbs') != 'client':
yield rse_factory
else:
# MOCK4 always exists and is defined as a posix
class MockRSEFactory:
@staticmethod
def make_posix_rse():
def_rse_name = 'MOCK4'
def_rse_id = rse_client.get_rse(rse=def_rse_name)['id']
return def_rse_name, def_rse_id
yield MockRSEFactory