userver: /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/mysql.py Source File
Loading...
Searching...
No Matches
mysql.py
1"""
2Plugin that starts MySQL/MariaDB and redirects the default local secdist endpoint
3to the database selected by testsuite.
4"""
5
6import json
7
8import pytest
9
11
12pytest_plugins = [
13 'testsuite.databases.mysql.pytest_plugin',
14 'pytest_userver.plugins.core',
15]
16
17_DEFAULT_MYSQL_PORT = 13307
18_LOCAL_MYSQL_HOSTS = (['localhost'], ['127.0.0.1'], ['::1'])
19
20
21@pytest.fixture(scope='session')
22@pytest_userver.config.patch
23def userver_config_mysql(mysql_conninfo, service_env):
24 """
25 Redirect the default local MySQL endpoint in environment-backed secdist
26 to `mysql_conninfo`. Preserve explicit endpoints, including chaos proxies.
27
28 Only single loopback hosts on port 13307 are remapped. Override this fixture
29 to customize or disable the patch. File-backed secdist is not modified.
30
31 @ingroup userver_testsuite_fixtures
32 """
33
34 def _patch_config(config_yaml, config_vars):
35 components = config_yaml['components_manager']['components']
36 provider = components.get('default-secdist-provider') or {}
37 env_key = provider.get('environment-secrets-key')
38 if not env_key or not service_env or env_key not in service_env:
39 return
40
41 secdist = json.loads(service_env[env_key])
42 if _patch_mysql_secdist(secdist, mysql_conninfo):
43 service_env[env_key] = json.dumps(secdist)
44
45 return _patch_config
46
47
48def _patch_mysql_secdist(secdist, conninfo):
49 changed = False
50 for settings in secdist.get('mysql_settings', {}).values():
51 if settings.get('hosts') not in _LOCAL_MYSQL_HOSTS or settings.get('port') != _DEFAULT_MYSQL_PORT:
52 continue
53 settings.update(
54 hosts=[conninfo.hostname],
55 port=conninfo.port,
56 user=conninfo.user,
57 password=conninfo.password or '',
58 )
59 changed = True
60 return changed