[#132] Add steps logger and refactor reporter usage

Signed-off-by: Andrey Berezin <a.berezin@yadro.com>
This commit is contained in:
Andrey Berezin 2023-11-28 12:28:44 +03:00 committed by Andrey Berezin
parent 47414eb866
commit 39a17f3634
9 changed files with 163 additions and 16 deletions

View file

@ -7,6 +7,9 @@ from typing import Any
from _pytest.outcomes import Failed
from pytest import fail
from frostfs_testlib import reporter
from frostfs_testlib.utils.func_utils import format_by_args
logger = logging.getLogger("NeoLogger")
# TODO: we may consider deprecating some methods here and use tenacity instead
@ -50,7 +53,7 @@ class expect_not_raises:
return impl
def retry(max_attempts: int, sleep_interval: int = 1, expected_result: Any = None):
def retry(max_attempts: int, sleep_interval: int = 1, expected_result: Any = None, title: str = None):
"""
Decorator to wait for some conditions/functions to pass successfully.
This is useful if you don't know exact time when something should pass successfully and do not
@ -62,8 +65,7 @@ def retry(max_attempts: int, sleep_interval: int = 1, expected_result: Any = Non
assert max_attempts >= 1, "Cannot apply retry decorator with max_attempts < 1"
def wrapper(func):
@wraps(func)
def impl(*a, **kw):
def call(func, *a, **kw):
last_exception = None
for _ in range(max_attempts):
try:
@ -84,6 +86,14 @@ def retry(max_attempts: int, sleep_interval: int = 1, expected_result: Any = Non
if last_exception is not None:
raise last_exception
@wraps(func)
def impl(*a, **kw):
if title is not None:
with reporter.step(format_by_args(func, title, *a, **kw)):
return call(func, *a, **kw)
return call(func, *a, **kw)
return impl
return wrapper
@ -124,6 +134,7 @@ def wait_for_success(
expected_result: Any = None,
fail_testcase: bool = False,
fail_message: str = "",
title: str = None,
):
"""
Decorator to wait for some conditions/functions to pass successfully.
@ -134,8 +145,7 @@ def wait_for_success(
"""
def wrapper(func):
@wraps(func)
def impl(*a, **kw):
def call(func, *a, **kw):
start = int(round(time()))
last_exception = None
while start + max_wait_time >= int(round(time())):
@ -160,6 +170,14 @@ def wait_for_success(
if last_exception is not None:
raise last_exception
@wraps(func)
def impl(*a, **kw):
if title is not None:
with reporter.step(format_by_args(func, title, *a, **kw)):
return call(func, *a, **kw)
return call(func, *a, **kw)
return impl
return wrapper