2Start the service in testsuite.
13from testsuite.logging
import logger
14from testsuite.utils
import url_util
16from ..utils
import colorize
17from ..utils
import net
20logger_testsuite = logging.getLogger(__name__)
25 self, *, writer: logger.LineLogger, verbose, colors_enabled,
27 super().__init__(writer)
29 verbose=verbose, colors_enabled=colors_enabled,
32 def log_service_line(self, line) -> None:
37 def log_entry(self, entry: dict) ->
None:
43def pytest_addoption(parser) -> None:
44 group = parser.getgroup(
'userver')
46 '--service-logs-file',
48 help=
'Write service output to specified file',
51 '--service-logs-pretty',
53 help=
'Enable pretty print and colorize service logs',
56 '--service-logs-pretty-verbose',
57 dest=
'service_logs_pretty',
60 help=
'Enable pretty print and colorize service logs in verbose mode',
63 '--service-logs-pretty-disable',
65 dest=
'service_logs_pretty',
66 help=
'Disable pretty print and colorize service logs',
70def pytest_override_testsuite_logger(
71 config, line_logger: logger.LineLogger, colors_enabled: bool,
72) -> typing.Optional[logger.Logger]:
73 pretty_logs = config.option.service_logs_pretty
78 verbose=pretty_logs ==
'verbose',
79 colors_enabled=colors_enabled,
83@pytest.fixture(scope='session')
86 Override this to pass extra environment variables to the service.
88 @snippet samples/redis_service/tests/conftest.py service_env
89 @ingroup userver_testsuite_fixtures
94@pytest.fixture(scope='session')
96 service_config_yaml, service_baseurl,
97) -> typing.Optional[str]:
99 Returns the service HTTP ping URL that is used by the testsuite to detect
100 that the service is ready to work. Returns None if there's no such URL.
102 By default attempts to find server::handlers::Ping component by
103 "handler-ping" name in static config. Override this fixture to change the
106 @ingroup userver_testsuite_fixtures
108 components = service_config_yaml[
'components_manager'][
'components']
109 ping_handler = components.get(
'handler-ping')
111 return url_util.join(service_baseurl, ping_handler[
'path'])
115@pytest.fixture(scope='session')
118 Returns a health checks info.
120 By default returns pytest_userver.utils.net.get_health_checks_info().
122 Override this fixture to change the way testsuite detects the tested
125 @ingroup userver_testsuite_fixtures
128 return net.get_health_checks_info(service_config_yaml)
131@pytest.fixture(scope='session')
136 service_http_ping_url,
137 service_config_path_temp,
140 service_non_http_health_checks,
142 _userver_log_handler,
145 Configures the health checking to use service_http_ping_url fixture value
146 if it is not None; otherwise uses the service_non_http_health_checks info.
147 Starts the service daemon.
149 @ingroup userver_testsuite_fixtures
151 assert service_http_ping_url
or service_non_http_health_checks.tcp, (
152 '"service_http_ping_url" and "create_health_checker" fixtures '
153 'returned None. Testsuite is unable to detect if the service is ready '
154 'to accept requests.',
157 logger_testsuite.debug(
158 'userver fixture "service_daemon" would check for "%s"',
159 service_non_http_health_checks,
162 async def _checker(*, session, process) -> bool:
163 logger_testsuite.debug(
164 'userver fixture "service_daemon" is about to start "%s" checks',
165 service_non_http_health_checks,
167 result = await net.check_availability(service_non_http_health_checks)
168 logger_testsuite.debug(
169 'userver fixture "service_daemon" checked "%s" and got "%s"',
170 service_non_http_health_checks,
175 health_check = _checker
176 if service_http_ping_url:
179 async with create_daemon_scope(
183 str(service_config_path_temp),
185 ping_url=service_http_ping_url,
186 health_check=health_check,
188 stderr_handler=_userver_log_handler,
193@pytest.fixture(scope='session')
194def _userver_log_handler(pytestconfig, testsuite_logger, _uservice_logfile):
195 service_logs_pretty = pytestconfig.option.service_logs_pretty
196 if not service_logs_pretty
and not bool(_uservice_logfile):
199 if service_logs_pretty:
200 logger_plugin = pytestconfig.pluginmanager.getplugin(
203 logger_plugin.enable_logs_suspension()
205 def log_handler(line_binary):
206 if _uservice_logfile:
207 _uservice_logfile.write(line_binary)
209 line = line_binary.decode(
'utf-8').rstrip(
'\r\n')
210 testsuite_logger.log_service_line(line)
213 traceback.print_exc(file=sys.stderr)
218@pytest.fixture(scope='session')
219def _uservice_logfile(pytestconfig):
220 path = pytestconfig.option.service_logs_file
224 with path.open(
'wb')
as fp: