Skip to content

Commit

Permalink
fix(anta)!: Fix tags behavior to reflect the documentation (#903)
Browse files Browse the repository at this point in the history
* fix(anta): Fix tags behavior

* Use total_test_count for better performance

* Update anta/runner.py

---------

Co-authored-by: Guillaume Mulocher <gmulocher@arista.com>
  • Loading branch information
carl-baillargeon and gmuloc authored Oct 30, 2024
1 parent 5350747 commit 898fdd3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
14 changes: 10 additions & 4 deletions anta/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
Expand Down
4 changes: 1 addition & 3 deletions docs/cli/tag-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/data/test_inventory_with_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions tests/units/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down

0 comments on commit 898fdd3

Please sign in to comment.