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