1import pytest
2from pytest_userver import chaos
3
4from testsuite.databases.pgsql import connection
5from testsuite.databases.pgsql import discover
6
7pytest_plugins = ['pytest_userver.plugins.postgresql']
8
9
10@pytest.fixture(scope='session')
11def userver_testsuite_middleware_enabled():
12 return False
13
14
15
16@pytest.fixture(name='pgsql_local', scope='session')
17def _pgsql_local(service_source_dir, pgsql_local_create):
18 databases = discover.find_schemas(
19 'pg', [service_source_dir.joinpath('schemas/postgresql')],
20 )
21 return pgsql_local_create(list(databases.values()))
22
23
24@pytest.fixture(scope='session')
25async def _gate_started(loop, pgsql_local):
26 gate_config = chaos.GateRoute(
27 name='postgres proxy',
28 host_to_server=pgsql_local['key_value'].host,
29 port_to_server=pgsql_local['key_value'].port,
30 )
31 async with chaos.TcpGate(gate_config, loop) as proxy:
32 yield proxy
33
34
35@pytest.fixture
36def extra_client_deps(_gate_started):
37 pass
38
39
40@pytest.fixture(name='userver_config_testsuite', scope='session')
41def _userver_config_testsuite(userver_config_testsuite):
42 def patch_config(config_yaml, config_vars):
43 userver_config_testsuite(config_yaml, config_vars)
44
45 components: dict = config_yaml['components_manager']['components']
46 testsuite_support = components['testsuite-support']
47 testsuite_support['testsuite-pg-execute-timeout'] = '0ms'
48 testsuite_support['testsuite-pg-statement-timeout'] = '0ms'
49 testsuite_support.pop('testsuite-pg-readonly-master-expected')
50
51 return patch_config
52
53
54@pytest.fixture(scope='session')
55def userver_pg_config(pgsql_local, _gate_started):
56 def _hook_db_config(config_yaml, config_vars):
57 host, port = _gate_started.get_sockname_for_clients()
58
59 db_info = pgsql_local['key_value']
60 db_chaos_gate = connection.PgConnectionInfo(
61 host=host,
62 port=port,
63 user=db_info.user,
64 password=db_info.password,
65 options=db_info.options,
66 sslmode=db_info.sslmode,
67 dbname=db_info.dbname,
68 )
69
70 components = config_yaml['components_manager']['components']
71 db = components['key-value-database']
72 db['dbconnection'] = db_chaos_gate.get_uri()
73
74 return _hook_db_config
75
76
77
78
79@pytest.fixture(name='gate')
80async def _gate_ready(service_client, _gate_started):
81 _gate_started.to_server_pass()
82 _gate_started.to_client_pass()
83 _gate_started.start_accepting()
84
85 await _gate_started.wait_for_connections()
86 yield _gate_started
87
88
89
90@pytest.fixture(
91 autouse=True, params=[0, 1], ids=['pipeline_disabled', 'pipeline_enabled'],
92)
93async def pipeline_mode(request, service_client, dynamic_config):
94 dynamic_config.set_values(
95 {'POSTGRES_CONNECTION_PIPELINE_EXPERIMENT': request.param},
96 )
97 await service_client.update_server_state()