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