2Service main and monitor clients.
12from testsuite.daemons
import service_client
as base_service_client
13from testsuite.daemons.pytest_plugin
import DaemonInstance
14from testsuite.utils
import compat
16from pytest_userver
import client
19logger = logging.getLogger(__name__)
25 Service client dependencies hook. Feel free to override, e.g.:
29 def extra_client_deps(some_fixtures_to_wait_before_service_start):
32 @ingroup userver_testsuite_fixtures
39 Service client dependencies hook that knows about pgsql, mongodb,
40 clickhouse, rabbitmq, redis_store, ydb, and mysql dependencies.
41 To add some other dependencies prefer overriding the
42 extra_client_deps() fixture.
44 @ingroup userver_testsuite_fixtures
57 fixture_lookup_error = pytest.FixtureLookupError
58 except AttributeError:
60 import _pytest.fixtures
61 fixture_lookup_error = _pytest.fixtures.FixtureLookupError
64 for dep
in known_deps:
66 request.getfixturevalue(dep)
67 resolved_deps.append(dep)
68 except fixture_lookup_error:
72 'userver fixture "auto_client_deps" resolved dependencies %s',
78async def service_client(
79 ensure_daemon_started,
83 cleanup_userver_dumps,
86 _config_service_defaults_updated,
87 _testsuite_client_config: client.TestsuiteClientConfig,
89 _service_client_testsuite,
92 Main fixture that provides access to userver based service.
94 @snippet samples/testsuite-support/tests/test_ping.py service_client
95 @anchor service_client
96 @ingroup userver_testsuite_fixtures
100 daemon = await ensure_daemon_started(service_daemon)
102 if not _testsuite_client_config.testsuite_action_path:
103 return _service_client_base
105 service_client = _service_client_testsuite(daemon)
106 await _config_service_defaults_updated.update(
107 service_client, dynamic_config,
109 return service_client
115 Fixture that provides access to userver based websocket service.
117 @anchor websocket_client
118 @ingroup userver_testsuite_fixtures
122 @compat.asynccontextmanager
123 async def get(self, path):
124 update_server_state = getattr(
125 service_client,
'update_server_state',
None,
127 if update_server_state:
128 await update_server_state()
129 ws_context = websockets.connect(
130 f
'ws://localhost:{service_port}/{path}',
132 async with ws_context
as socket:
141 service_client_options,
143 monitor_baseurl: str,
147 Main fixture that provides access to userver monitor listener.
149 @snippet samples/testsuite-support/tests/test_metrics.py metrics labels
150 @ingroup userver_testsuite_fixtures
152 aiohttp_client = client.AiohttpClientMonitor(
154 config=_testsuite_client_config,
155 headers={
'x-yatraceid': mockserver.trace_id},
156 **service_client_options,
162async def _service_client_base(service_baseurl, service_client_options):
163 class _ClientDiagnose(base_service_client.Client):
164 def __getattr__(self, name: str) ->
None:
165 raise AttributeError(
166 f
'"Client" object has no attribute "{name}". '
167 'Note that "service_client" fixture returned the basic '
168 '"testsuite.daemons.service_client.Client" client rather than '
169 'a "pytest_userver.client.Client" client with userver '
170 'extensions. That happened because the service '
171 'static configuration file contains no "tests-control" '
172 'component with "action" field.',
175 return _ClientDiagnose(service_baseurl, **service_client_options)
179def _service_client_testsuite(
181 service_client_options,
183 userver_cache_control,
187 cache_invalidation_state,
188 _testsuite_client_config: client.TestsuiteClientConfig,
190 def create_client(daemon):
191 aiohttp_client = client.AiohttpClient(
193 config=_testsuite_client_config,
195 testpoint_control=testpoint_control,
196 log_capture_fixture=userver_log_capture,
197 mocked_time=mocked_time,
198 cache_invalidation_state=cache_invalidation_state,
199 cache_control=userver_cache_control(daemon),
200 **service_client_options,
207@pytest.fixture(scope='session')
210 Returns the main listener URL of the service.
212 Override this fixture to change the main listener URL that the testsuite
215 @ingroup userver_testsuite_fixtures
217 return f
'http://localhost:{service_port}/'
220@pytest.fixture(scope='session')
223 Returns the main monitor URL of the service.
225 Override this fixture to change the main monitor URL that the testsuite
228 @ingroup userver_testsuite_fixtures
230 return f
'http://localhost:{monitor_port}/'
233@pytest.fixture(scope='session')
234def _testsuite_client_config(
235 pytestconfig, service_config,
236) -> client.TestsuiteClientConfig:
237 components = service_config[
'components_manager'][
'components']
239 def get_component_path(name, argname=None):
240 if name
in components:
241 path = components[name][
'path']
242 path = path.rstrip(
'*')
244 if argname
and f
'{{{argname}}}' not in path:
246 f
'Component {name} must provide path argument {argname}',
252 server_monitor_path=get_component_path(
'handler-server-monitor'),
253 testsuite_action_path=get_component_path(
'tests-control',
'action'),