frostfs-testlib/src/frostfs_testlib/reporter/reporter.py
Andrey Berezin 39a17f3634 [#132] Add steps logger and refactor reporter usage
Signed-off-by: Andrey Berezin <a.berezin@yadro.com>
2023-11-28 09:29:07 +00:00

135 lines
4.3 KiB
Python

from contextlib import AbstractContextManager, contextmanager
from functools import wraps
from types import TracebackType
from typing import Any, Callable, Optional
from frostfs_testlib.plugins import load_plugin
from frostfs_testlib.reporter.interfaces import ReporterHandler
from frostfs_testlib.utils.func_utils import format_by_args
@contextmanager
def _empty_step():
yield
class Reporter:
"""Root reporter that sends artifacts to handlers."""
handlers: list[ReporterHandler]
def __init__(self) -> None:
super().__init__()
self.handlers = []
def register_handler(self, handler: ReporterHandler) -> None:
"""Register a new handler for the reporter.
Args:
handler: Handler instance to add to the reporter.
"""
self.handlers.append(handler)
def configure(self, config: dict[str, Any]) -> None:
"""Configure handlers in the reporter from specified config.
All existing handlers will be removed from the reporter.
Args:
config: Dictionary with reporter configuration.
"""
# Reset current configuration
self.handlers = []
# Setup handlers from the specified config
handler_configs = config.get("handlers", [])
for handler_config in handler_configs:
handler_class = load_plugin("frostfs.testlib.reporter", handler_config["plugin_name"])
self.register_handler(handler_class())
def step_deco(self, name: str) -> Callable:
"""Register a new step in test execution in a decorator fashion.
To note: the actual decoration with handlers is happening during target function call time.
Args:
name: Name of the step.
Returns:
decorated function
"""
def deco(func):
@wraps(func)
def wrapper(*a, **kw):
resulting_func = func
for handler in self.handlers:
parsed_name = format_by_args(func, name, *a, **kw)
decorator = handler.step_decorator(parsed_name)
resulting_func = decorator(resulting_func)
return resulting_func(*a, **kw)
return wrapper
return deco
def step(self, name: str) -> AbstractContextManager:
"""Register a new step in test execution.
Args:
name: Name of the step.
Returns:
Step context.
"""
step_contexts = [handler.step(name) for handler in self.handlers]
if not step_contexts:
step_contexts = [_empty_step()]
decorated_wrapper = self.step_deco(name)
return AggregateContextManager(step_contexts, decorated_wrapper)
def attach(self, content: Any, file_name: str) -> None:
"""Attach specified content with given file name to the test report.
Args:
content: Content to attach. If content value is not a string, it will be
converted to a string.
file_name: File name of attachment.
"""
for handler in self.handlers:
handler.attach(content, file_name)
class AggregateContextManager(AbstractContextManager):
"""Aggregates multiple context managers in a single context."""
contexts: list[AbstractContextManager]
def __init__(self, contexts: list[AbstractContextManager], decorated_wrapper: Callable) -> None:
super().__init__()
self.contexts = contexts
self.wrapper = decorated_wrapper
def __enter__(self):
for context in self.contexts:
context.__enter__()
return self
def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
suppress_decisions = []
for context in self.contexts:
suppress_decision = context.__exit__(exc_type, exc_value, traceback)
suppress_decisions.append(suppress_decision)
# If all context agreed to suppress exception, then suppress it;
# otherwise return None to reraise
return True if all(suppress_decisions) else None
def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.wrapper(*args, **kwds)