From 898fdd3fe5b51f382009227b1059a1f348d22951 Mon Sep 17 00:00:00 2001 From: Carl Baillargeon Date: Wed, 30 Oct 2024 13:04:14 -0400 Subject: [PATCH] fix(anta)!: Fix tags behavior to reflect the documentation (#903) * fix(anta): Fix tags behavior * Use total_test_count for better performance * Update anta/runner.py --------- Co-authored-by: Guillaume Mulocher --- anta/runner.py | 14 ++++++++++---- docs/cli/tag-management.md | 4 +--- tests/data/test_inventory_with_tags.yml | 2 +- tests/units/test_runner.py | 1 + 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/anta/runner.py b/anta/runner.py index 0147c3ccd..7b0eadf75 100644 --- a/anta/runner.py +++ b/anta/runner.py @@ -146,20 +146,26 @@ def prepare_tests( # Using a set to avoid inserting duplicate tests device_to_tests: defaultdict[AntaDevice, set[AntaTestDefinition]] = defaultdict(set) + total_test_count = 0 + # Create the device to tests mapping from the tags for device in inventory.devices: if tags: - if not any(tag in device.tags for tag in tags): + # If there are CLI tags, execute tests with matching tags for this device + if not (matching_tags := tags.intersection(device.tags)): # The device does not have any selected tag, skipping continue + device_to_tests[device].update(catalog.get_tests_by_tags(matching_tags)) else: # If there is no CLI tags, execute all tests that do not have any tags device_to_tests[device].update(catalog.tag_to_tests[None]) - # Add the tests with matching tags from device tags - device_to_tests[device].update(catalog.get_tests_by_tags(device.tags)) + # Then add the tests with matching tags from device tags + device_to_tests[device].update(catalog.get_tests_by_tags(device.tags)) + + total_test_count += len(device_to_tests[device]) - if len(device_to_tests.values()) == 0: + if total_test_count == 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." ) diff --git a/docs/cli/tag-management.md b/docs/cli/tag-management.md index ad5ccf3ab..4108d75bb 100644 --- a/docs/cli/tag-management.md +++ b/docs/cli/tag-management.md @@ -4,9 +4,7 @@ ~ that can be found in the LICENSE file. --> -ANTA commands can be used with a `--tags` option. This option **filters the inventory** with the specified tag(s) when running the command. - -Tags can also be used to **restrict a specific test** to a set of devices when using `anta nrfu`. +ANTA uses tags to define test-to-device mappings (tests run on devices with matching tags) and the `--tags` CLI option acts as a filter to execute specific test/device combinations. ## Defining tags diff --git a/tests/data/test_inventory_with_tags.yml b/tests/data/test_inventory_with_tags.yml index cbbcd75e6..16a9df4c0 100644 --- a/tests/data/test_inventory_with_tags.yml +++ b/tests/data/test_inventory_with_tags.yml @@ -3,7 +3,7 @@ anta_inventory: hosts: - name: leaf1 host: leaf1.anta.arista.com - tags: ["leaf"] + tags: ["leaf", "dc1"] - name: leaf2 host: leaf2.anta.arista.com tags: ["leaf"] diff --git a/tests/units/test_runner.py b/tests/units/test_runner.py index b80259cc3..8d19a4d1a 100644 --- a/tests/units/test_runner.py +++ b/tests/units/test_runner.py @@ -138,6 +138,7 @@ def side_effect_setrlimit(resource_id: int, limits: tuple[int, int]) -> None: pytest.param({"filename": "test_inventory_with_tags.yml"}, None, {"VerifyMlagStatus", "VerifyUptime"}, 3, 5, id="filtered-tests"), pytest.param({"filename": "test_inventory_with_tags.yml"}, {"leaf"}, {"VerifyMlagStatus", "VerifyUptime"}, 2, 4, id="1-tag-filtered-tests"), pytest.param({"filename": "test_inventory_with_tags.yml"}, {"invalid"}, None, 0, 0, id="invalid-tag"), + pytest.param({"filename": "test_inventory_with_tags.yml"}, {"dc1"}, None, 0, 0, id="device-tag-no-tests"), ], indirect=["inventory"], )