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