Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

4.0 KB
Raw Blame
from collections.abc import Callable
from typing import TypeVar

import pytest

from fixtures.logger import setup_logger

logger = setup_logger(__name__)

T = TypeVar("T")


def reuse(request: pytest.FixtureRequest) -> bool:
    return request.config.getoption("--reuse")


def teardown(request: pytest.FixtureRequest) -> bool:
    return request.config.getoption("--teardown")


def get_cached_resource(pytestconfig: pytest.Config, key: str):
    return pytestconfig.cache.get(key, None)


def set_cached_resource(pytestconfig: pytest.Config, key: str, value):
    pytestconfig.cache.set(key, value)


def remove_cached_resource(pytestconfig: pytest.Config, key: str):
    pytestconfig.cache.set(key, None)


def wrap(  # pylint: disable=too-many-arguments,too-many-positional-arguments
    request: pytest.FixtureRequest,
    pytestconfig: pytest.Config,
    key: str,
    empty: Callable[[], T],
    create: Callable[[], T],
    delete: Callable[[T], None],
    restore: Callable[[dict], T],
    rebuild: bool = False,
    stale: Callable[[T], bool] | None = None,
) -> T:
    """
    Wraps a resource creation and cleanup process with reuse and teardown options.
    - request: pytest.FixtureRequest
    - pytestconfig: pytest.Config
    - key: cache key for the resource
    - empty: function to create an empty resource
    - create: function to create the resource
    - delete: function to delete the resource
    - restore: function to restore resource from cache
    - rebuild: under --reuse, delete the cached resource and recreate it instead of restoring it
    - stale: under --reuse, decides whether a restored resource is still usable; a stale resource is deleted and recreated
    """
    resource = empty()

    if reuse(request):
        existing_resource = pytestconfig.cache.get(key, None)
        if existing_resource:
            assert isinstance(existing_resource, dict)
            if rebuild:
                logger.info("Rebuilding %s(%s), removing the existing one", key, existing_resource)
                delete(restore(existing_resource))
                pytestconfig.cache.set(key, None)
            else:
                restored = restore(existing_resource)
                if stale is not None and stale(restored):
                    logger.info("Recreating stale %s(%s)", key, existing_resource)
                    delete(restored)
                    pytestconfig.cache.set(key, None)
                else:
                    logger.info("Reusing existing %s(%s)", key, existing_resource)
                    return restored

    if not teardown(request):
        resource = create()

    def finalizer():
        nonlocal resource
        if reuse(request):
            logger.info(
                "Skipping removal of %s",
                resource.__log__() if hasattr(resource, "__log__") else resource,
            )
            return

        if teardown(request):
            existing_resource = pytestconfig.cache.get(key, None)
            if not existing_resource:
                logger.info(
                    "Skipping removal of %s, no existing %s found. Maybe you ran teardown without reuse?",
                    key,
                    key,
                )
                return

            resource = restore(existing_resource)
            logger.info(
                "Removing %s",
                resource.__log__() if hasattr(resource, "__log__") else resource,
            )
            delete(resource)
            pytestconfig.cache.set(key, None)
            return

        # A run without --reuse owns only what it created this session: the
        # cache key (and whatever a parked --reuse stack has under it) is left
        # untouched.
        logger.info(
            "Removing %s",
            resource.__log__() if hasattr(resource, "__log__") else resource,
        )
        delete(resource)

    request.addfinalizer(finalizer)

    if reuse(request):
        pytestconfig.cache.set(key, resource.__cache__() if hasattr(resource, "__cache__") else resource)

    return resource