Debian Codegraph @

tests/benchmark/conftest.py

raw symbols SCIP
1 # Copyright (c) 2023-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """Fixtures for benchmarking ANTA."""
5
6 import logging
7 from collections import defaultdict
8
9 import pytest
10 import respx
11 from _pytest.terminal import TerminalReporter
12
13 from anta.catalog import AntaCatalog
14 from anta.result_manager import ResultManager
15
16 from .utils import AntaMockEnvironment
17
18 logger = logging.getLogger(__name__)
19
20 TEST_CASE_COUNT = None
21
22 # Used to globally configure the benchmarks by specifying parameters for inventories
23 BENCHMARK_PARAMETERS = [
24 pytest.param({"count": 1, "disable_cache": True, "reachable": True}, id="1-device"),
25 pytest.param({"count": 2, "disable_cache": True, "reachable": True}, id="2-devices"),
26 ]
27
28
29 @pytest.fixture(name="anta_mock_env", scope="session") # We want this fixture to have a scope set to session to avoid reparsing all the unit tests data.
30 def anta_mock_env_fixture() -> AntaMockEnvironment:
31 """Return an AntaMockEnvironment for this test session. Also configure respx to mock eAPI responses."""
32 global TEST_CASE_COUNT # noqa: PLW0603
33 eapi_route = respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"})
34 env = AntaMockEnvironment()
35 TEST_CASE_COUNT = env.tests_count
36 eapi_route.side_effect = env.eapi_response
37 return env
38
39
40 @pytest.fixture # This fixture should have a scope set to function as the indexing result is stored in this object
41 def catalog(anta_mock_env: AntaMockEnvironment) -> AntaCatalog:
42 """Fixture that return an ANTA catalog from the AntaMockEnvironment of this test session."""
43 return anta_mock_env.catalog
44
45
46 @pytest.fixture(name="session_results", scope="session") # We want this fixture to be reused across test modules within tests/benchmark
47 def session_results_fixture() -> defaultdict[str, ResultManager]:
48 """Return a dictionary of ResultManger objects for the benchmarks.
49
50 The key is the test id as defined in the pytest_generate_tests in this module.
51 Used to pass a populated ResultManager from one benchmark to another.
52 """
53 return defaultdict(ResultManager)
54
55
56 @pytest.fixture
57 def results(request: pytest.FixtureRequest, session_results: defaultdict[str, ResultManager]) -> ResultManager:
58 """Return the unique ResultManger object for the current benchmark parameter."""
59 return session_results[request.node.callspec.id]
60
61
62 def pytest_terminal_summary(terminalreporter: TerminalReporter) -> None:
63 """Display the total number of ANTA unit test cases used to benchmark."""
64 terminalreporter.write_sep("=", f"{TEST_CASE_COUNT} ANTA test cases")
65
66
67 def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
68 """Parametrize inventory for benchmark tests."""
69 if "inventory" in metafunc.fixturenames:
70 for marker in metafunc.definition.iter_markers(name="parametrize"):
71 if "inventory" in marker.args[0]:
72 # Do not override test function parametrize marker for inventory arg
73 return
74 metafunc.parametrize(
75 "inventory",
76 BENCHMARK_PARAMETERS,
77 indirect=True,
78 )
79 elif "results" in metafunc.fixturenames:
80 metafunc.parametrize(
81 "results",
82 BENCHMARK_PARAMETERS,
83 indirect=True,
84 )