Skip to content

Commit

Permalink
feat u/testsuite: better logs report handling
Browse files Browse the repository at this point in the history
* set asyncio mode to auto by default
19eb1bb1159812e3cd54b83ed58c826104c2c71a
  • Loading branch information
vitja committed May 23, 2024
1 parent a78f487 commit 76dc7ef
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# pytest.ini file for all the functional tests of the userver drivers.

[pytest]
asyncio_mode = auto
mockserver-tracing-enabled = true
log_level = DEBUG
40 changes: 14 additions & 26 deletions testsuite/pytest_plugins/pytest_userver/plugins/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,16 @@ def pytest_sessionfinish(self, session):
if self._live_logs:
self._live_logs.join()

@pytest.hookimpl(wrapper=True)
def pytest_runtest_setup(self, item):
self._flushers.clear()
self.update_position()
yield from self._userver_log_dump(item, 'setup')

@pytest.hookimpl(wrapper=True)
def pytest_runtest_call(self, item):
yield from self._userver_log_dump(item, 'call')

@pytest.hookimpl(wrapper=True)
def pytest_runtest_teardown(self, item):
yield from self._userver_log_dump(item, 'teardown')
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(self, item, call):
report = yield
if report.failed:
self._userver_report_attach(report)
return report

def update_position(self):
for logfile in self._logs.values():
Expand All @@ -133,32 +130,23 @@ def register_logfile(self, path: pathlib.Path, title: str):
if self._live_logs:
self._live_logs.register_logfile(path)

def _userver_log_dump(self, item, when):
try:
yield
except Exception:
self._userver_report_attach(item, when)
raise

def _userver_report_attach(self, item, when):
def _userver_report_attach(self, report):
self._run_flushers()
for (_, title), logfile in self._logs.items():
self._userver_report_attach_log(logfile, item, when, title)
self._userver_report_attach_log(logfile, report, title)

def _userver_report_attach_log(self, logfile: LogFile, item, when, title):
report = io.StringIO()
def _userver_report_attach_log(self, logfile: LogFile, report, title):
log = io.StringIO()
colorizer = self._colorize_factory()
for line in logfile.readlines():
line = line.rstrip('\r\n')
line = colorizer(line)
if line:
report.write(line)
report.write('\n')
value = report.getvalue()
log.write(line)
log.write('\n')
value = log.getvalue()
if value:
item.add_report_section(when, title, value)

self._run_flushers()
report.sections.append((f'Captured {title} {report.when}', value))

def _run_flushers(self):
loop = asyncio.get_event_loop()
Expand Down
1 change: 0 additions & 1 deletion testsuite/pytest_plugins/pytest_userver/plugins/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ async def service_daemon(
service_config,
service_binary,
service_non_http_health_checks,
testsuite_logger,
):
"""
Configures the health checking to use service_http_ping_url fixture value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ def userver_client_cleanup(request, _userver_logging_plugin):
tasks_to_suspend = ()

@compat.asynccontextmanager
async def cleanup_manager(client: client.AiohttpClient):
async def cleanup_manager(client: client.Client):
@_userver_logging_plugin.register_flusher
async def do_flush():
try:
await client.log_flush()
except aiohttp.client_exceptions.ClientResponseError:
except aiohttp.client_exceptions.ClientError:
pass
except RuntimeError:
# TODO: find a better way to handle closed aiohttp session
pass

# Service is already started we don't want startup logs to be shown
Expand Down

0 comments on commit 76dc7ef

Please sign in to comment.