userver: /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/mongo.py Source File
Loading...
Searching...
No Matches
mongo.py
1"""
2Plugin that imports the required fixtures to start the database
3and adjusts the mongo "dbconnection" static config value.
4"""
5
6import re
7
8import pytest
9
10pytest_plugins = [
11 'testsuite.databases.mongo.pytest_plugin',
12 'pytest_userver.plugins.core',
13]
14
15
16USERVER_CONFIG_HOOKS = ['userver_mongo_config']
17
18
19@pytest.fixture(scope='session')
20def userver_mongo_config(mongo_connection_info):
21 """
22 Returns a function that adjusts the static configuration file for
23 the testsuite.
24 Sets the `dbconnection` to the testsuite started MongoDB credentials if
25 the `dbconnection` starts with `mongodb://`. Additionally
26 increases MongoDB connection timeouts to 30 seconds.
27
28 @ingroup userver_testsuite_fixtures
29 """
30
31 port = mongo_connection_info.port
32 host = mongo_connection_info.host
33 new_uri = f'mongodb://{host}:{port}/'
34
35 def _patch_config(config_yaml, config_vars):
36 components = config_yaml['components_manager']['components']
37 for _, params in components.items():
38 uri = ''
39
40 if params and 'dbconnection#fallback' in params:
41 uri = params['dbconnection#fallback']
42
43 if params and 'dbconnection' in params:
44 uri = params['dbconnection']
45
46 if not uri or not uri.startswith('mongodb://'):
47 continue
48
49 uri = re.sub('mongodb://[^:]+:\\d+/', new_uri, uri)
50 uri = re.sub('mongodb://[^:]+/', new_uri, uri)
51
52 params['dbconnection'] = uri
53 params['conn_timeout'] = '30s'
54 params['so_timeout'] = '30s'
55 params.pop('dbalias', None)
56
57 return _patch_config