From 0d1bf05788cd193ac491b62063be3aa6b0ff4beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthieu=20T=C3=A2che?= Date: Mon, 30 Sep 2024 15:19:00 +0200 Subject: [PATCH] refactor: create AntaCatalog.clear_indexes() --- anta/catalog.py | 24 +++++++++++++++++------- anta/runner.py | 4 +++- tests/benchmark/test_anta.py | 8 +++++--- tests/conftest.py | 2 +- tests/units/test_catalog.py | 8 ++++---- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/anta/catalog.py b/anta/catalog.py index 1803a7176..9b752fa05 100644 --- a/anta/catalog.py +++ b/anta/catalog.py @@ -296,10 +296,16 @@ def __init__( else: self._filename = Path(filename) - # Default indexes for faster access - self.tag_to_tests: defaultdict[str | None, set[AntaTestDefinition]] = defaultdict(set) - self.tests_without_tags: set[AntaTestDefinition] = set() - self.indexes_built: bool = False + self.indexes_built: bool + self.tag_to_tests: defaultdict[str | None, set[AntaTestDefinition]] + self._tests_without_tags: set[AntaTestDefinition] + self._init_indexes() + + def _init_indexes(self) -> None: + """Init indexes related variables.""" + self.tag_to_tests = defaultdict(set) + self._tests_without_tags = set() + self.indexes_built = False @property def filename(self) -> Path | None: @@ -484,7 +490,7 @@ def build_indexes(self, filtered_tests: set[str] | None = None) -> None: - tag_to_tests: A dictionary mapping each tag to a set of tests that contain it. - - tests_without_tags: A set of tests that do not have any tags. + - _tests_without_tags: A set of tests that do not have any tags. Once the indexes are built, the `indexes_built` attribute is set to True. """ @@ -498,11 +504,15 @@ def build_indexes(self, filtered_tests: set[str] | None = None) -> None: for tag in test_tags: self.tag_to_tests[tag].add(test) else: - self.tests_without_tags.add(test) + self._tests_without_tags.add(test) - self.tag_to_tests[None] = self.tests_without_tags + self.tag_to_tests[None] = self._tests_without_tags self.indexes_built = True + def clear_indexes(self) -> None: + """Clear this AntaCatalog instance indexes.""" + self._init_indexes() + def get_tests_by_tags(self, tags: set[str], *, strict: bool = False) -> set[AntaTestDefinition]: """Return all tests that match a given set of tags, according to the specified strictness. diff --git a/anta/runner.py b/anta/runner.py index ea30a7a88..12f549daa 100644 --- a/anta/runner.py +++ b/anta/runner.py @@ -162,7 +162,7 @@ def prepare_tests( final_tests_count += len(device_to_tests[device]) - if final_tests_count == 0: + if len(device_to_tests.values()) == 0: msg = ( f"There are no tests{f' matching the tags {tags} ' if tags else ' '}to run in the current test catalog and device inventory, please verify your inputs." ) @@ -283,6 +283,8 @@ async def main( # noqa: PLR0913 if dry_run: logger.info("Dry-run mode, exiting before running the tests.") + for coro in coroutines: + coro.close() return if AntaTest.progress is not None: diff --git a/tests/benchmark/test_anta.py b/tests/benchmark/test_anta.py index a917edfd9..6885e2e8f 100644 --- a/tests/benchmark/test_anta.py +++ b/tests/benchmark/test_anta.py @@ -38,6 +38,7 @@ def test_anta_dry_run(benchmark: BenchmarkFixture, catalog: AntaCatalog, invento def bench() -> ResultManager: """Need to wrap the ANTA Runner to instantiate a new ResultManger for each benchmark run.""" manager = ResultManager() + catalog.clear_indexes() asyncio.run(main(manager, inventory, catalog, dry_run=True)) return manager @@ -45,8 +46,8 @@ def bench() -> ResultManager: logging.disable(logging.NOTSET) if len(manager.results) != len(inventory) * len(catalog.tests): - pytest.fail(f"Expected {len(inventory) * len(catalog.tests)} selected tests but got {len(manager.results)}", pytrace=False) - bench_info = "\n--- ANTA NRFU Dry-Run Benchmark Information ---\n" f"Selected tests: {len(manager.results)}\n" "-----------------------------------------------" + pytest.fail(f"Expected {len(inventory) * len(catalog.tests)} tests but got {len(manager.results)}", pytrace=False) + bench_info = "\n--- ANTA NRFU Dry-Run Benchmark Information ---\n" f"Test count: {len(manager.results)}\n" "-----------------------------------------------" logger.info(bench_info) @@ -69,6 +70,7 @@ def test_anta(benchmark: BenchmarkFixture, catalog: AntaCatalog, inventory: Anta def bench() -> ResultManager: """Need to wrap the ANTA Runner to instantiate a new ResultManger for each benchmark run.""" manager = ResultManager() + catalog.clear_indexes() asyncio.run(main(manager, inventory, catalog)) return manager @@ -90,7 +92,7 @@ def bench() -> ResultManager: for test in dupes: msg = f"Found duplicate in test catalog: {test}" logger.error(msg) - pytest.fail(f"Expected {len(catalog.tests) * len(inventory)} test results but got {len(manager.results)}", pytrace=False) + pytest.fail(f"Expected {len(catalog.tests) * len(inventory)} tests but got {len(manager.results)}", pytrace=False) bench_info = ( "\n--- ANTA NRFU Benchmark Information ---\n" f"Test results: {len(manager.results)}\n" diff --git a/tests/conftest.py b/tests/conftest.py index 7347d4430..dd535c104 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ DATA_DIR: Path = Path(__file__).parent.resolve() / "data" -@pytest.fixture(params=[{"count": 1}]) +@pytest.fixture(params=[{"count": 1}], ids=["1-reachable-device-without-cache"]) def inventory(request: pytest.FixtureRequest) -> Iterator[AntaInventory]: """Generate an ANTA inventory.""" user = "admin" diff --git a/tests/units/test_catalog.py b/tests/units/test_catalog.py index c2bb57c93..ca78a870c 100644 --- a/tests/units/test_catalog.py +++ b/tests/units/test_catalog.py @@ -260,10 +260,10 @@ def test_build_indexes_all(self) -> None: """Test AntaCatalog.build_indexes().""" catalog: AntaCatalog = AntaCatalog.parse(DATA_DIR / "test_catalog_with_tags.yml") catalog.build_indexes() - assert len(catalog.tests_without_tags) == 6 + assert len(catalog._tests_without_tags) == 6 assert "leaf" in catalog.tag_to_tests assert len(catalog.tag_to_tests["leaf"]) == 3 - all_unique_tests = catalog.tests_without_tags + all_unique_tests = catalog._tests_without_tags for tests in catalog.tag_to_tests.values(): all_unique_tests.update(tests) assert len(all_unique_tests) == 11 @@ -275,8 +275,8 @@ def test_build_indexes_filtered(self) -> None: catalog.build_indexes({"VerifyUptime", "VerifyCoredump", "VerifyL3MTU"}) assert "leaf" in catalog.tag_to_tests assert len(catalog.tag_to_tests["leaf"]) == 1 - assert len(catalog.tests_without_tags) == 1 - all_unique_tests = catalog.tests_without_tags + assert len(catalog._tests_without_tags) == 1 + all_unique_tests = catalog._tests_without_tags for tests in catalog.tag_to_tests.values(): all_unique_tests.update(tests) assert len(all_unique_tests) == 4