forked from TrueCloudLab/frostfs-testlib
[#319] Add cached fixture feature
Signed-off-by: a.berezin <a.berezin@yadro.com>
This commit is contained in:
parent
47bc11835b
commit
f24bfc06fd
2 changed files with 44 additions and 6 deletions
|
@ -16,11 +16,10 @@ OPTIONAL_NODE_UNDER_LOAD = os.getenv("OPTIONAL_NODE_UNDER_LOAD")
|
||||||
OPTIONAL_FAILOVER_ENABLED = str_to_bool(os.getenv("OPTIONAL_FAILOVER_ENABLED", "true"))
|
OPTIONAL_FAILOVER_ENABLED = str_to_bool(os.getenv("OPTIONAL_FAILOVER_ENABLED", "true"))
|
||||||
|
|
||||||
# Set this to True to disable background load. I.E. node which supposed to be stopped will not be actually stopped.
|
# Set this to True to disable background load. I.E. node which supposed to be stopped will not be actually stopped.
|
||||||
OPTIONAL_BACKGROUND_LOAD_ENABLED = str_to_bool(
|
OPTIONAL_BACKGROUND_LOAD_ENABLED = str_to_bool(os.getenv("OPTIONAL_BACKGROUND_LOAD_ENABLED", "true"))
|
||||||
os.getenv("OPTIONAL_BACKGROUND_LOAD_ENABLED", "true")
|
|
||||||
)
|
|
||||||
|
|
||||||
# Set this to False for disable autouse fixture like node healthcheck during developing time.
|
# Set this to False for disable autouse fixture like node healthcheck during developing time.
|
||||||
OPTIONAL_AUTOUSE_FIXTURES_ENABLED = str_to_bool(
|
OPTIONAL_AUTOUSE_FIXTURES_ENABLED = str_to_bool(os.getenv("OPTIONAL_AUTOUSE_FIXTURES_ENABLED", "true"))
|
||||||
os.getenv("OPTIONAL_AUTOUSE_FIXTURES_ENABLED", "true")
|
|
||||||
)
|
# Use cache for fixtures with @cachec_fixture decorator
|
||||||
|
OPTIONAL_CACHE_FIXTURES = str_to_bool(os.getenv("OPTIONAL_CACHE_FIXTURES", "false"))
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
import yaml
|
||||||
from _pytest.outcomes import Failed
|
from _pytest.outcomes import Failed
|
||||||
from pytest import fail
|
from pytest import fail
|
||||||
|
|
||||||
from frostfs_testlib import reporter
|
from frostfs_testlib import reporter
|
||||||
|
from frostfs_testlib.resources.common import ASSETS_DIR
|
||||||
from frostfs_testlib.utils.func_utils import format_by_args
|
from frostfs_testlib.utils.func_utils import format_by_args
|
||||||
|
|
||||||
logger = logging.getLogger("NeoLogger")
|
logger = logging.getLogger("NeoLogger")
|
||||||
|
@ -128,6 +131,42 @@ def run_optionally(enabled: bool, mock_value: Any = True):
|
||||||
return deco
|
return deco
|
||||||
|
|
||||||
|
|
||||||
|
def cached_fixture(enabled: bool):
|
||||||
|
"""
|
||||||
|
Decorator to cache fixtures.
|
||||||
|
MUST be placed after @pytest.fixture and before @allure decorators.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
enabled: if true, decorated func will be cached.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def deco(func):
|
||||||
|
@wraps(func)
|
||||||
|
def func_impl(*a, **kw):
|
||||||
|
# TODO: *a and *kw should be parsed to some kind of hashsum and used in filename to prevent cache load from different parameters
|
||||||
|
cache_file = os.path.join(ASSETS_DIR, f"fixture_cache_{func.__name__}.yml")
|
||||||
|
|
||||||
|
if enabled and os.path.exists(cache_file):
|
||||||
|
with open(cache_file, "r") as cache_input:
|
||||||
|
return yaml.load(cache_input, Loader=yaml.Loader)
|
||||||
|
|
||||||
|
result = func(*a, **kw)
|
||||||
|
|
||||||
|
if enabled:
|
||||||
|
with open(cache_file, "w") as cache_output:
|
||||||
|
yaml.dump(result, cache_output)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# TODO: cache yielding fixtures
|
||||||
|
@wraps(func)
|
||||||
|
def gen_impl(*a, **kw):
|
||||||
|
raise NotImplementedError("Not implemented for yielding fixtures")
|
||||||
|
|
||||||
|
return gen_impl if inspect.isgeneratorfunction(func) else func_impl
|
||||||
|
|
||||||
|
return deco
|
||||||
|
|
||||||
|
|
||||||
def wait_for_success(
|
def wait_for_success(
|
||||||
max_wait_time: int = 60,
|
max_wait_time: int = 60,
|
||||||
interval: int = 1,
|
interval: int = 1,
|
||||||
|
|
Loading…
Reference in a new issue