Skip to content

Commit

Permalink
Update code logic to reflect docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-baillargeon committed Jan 14, 2025
1 parent 24b0a70 commit fcdc00b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
33 changes: 18 additions & 15 deletions anta/tests/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Input(AntaTest.Input):
sources: list[DynamicVlanSource]
"""The dynamic VLAN source list."""
strict: bool = False
"""If True, dynamic VLAN(s) should be enabled only on designated sources, Defaults to `False`."""
"""If True, only specified sources are allowed to have VLANs allocated. Default is False."""

@AntaTest.anta_test
def test(self) -> None:
Expand All @@ -120,22 +120,25 @@ def test(self) -> None:
command_output = self.instance_commands[0].json_output
dynamic_vlans = command_output.get("dynamicVlans", {})

actual_sources = [source for source, data in dynamic_vlans.items() if data.get("vlanIds")]
expected_sources = self.inputs.sources
str_expected_sources = ", ".join(expected_sources)
# Get all configured sources and sources with VLANs allocated
configured_sources = set(dynamic_vlans.keys())
sources_with_vlans = {source for source, data in dynamic_vlans.items() if data.get("vlanIds")}
expected_sources = set(self.inputs.sources)

# If the dynamic vlans are not configured, skipping the test.
if not actual_sources:
self.result.is_failure(f"Dynamic VLANs sources {str_expected_sources} not found in the configuration")
# Check if all specified sources exist in configuration
missing_sources = expected_sources - configured_sources
if missing_sources:
self.result.is_failure(f"Dynamic VLAN source(s) not found in configuration: {', '.join(sorted(missing_sources))}")
return

str_actual_sources = ", ".join(actual_sources)
# If strict flag True, and dynamic VLAN(s) are disabled on any of the designated sources or enabled non designated sources, test fails.
if self.inputs.strict and sorted(actual_sources) != sorted(expected_sources):
self.result.is_failure(f"Dynamic VLAN allocations expected to be sources `{str_expected_sources}` only, however actual it is `{str_actual_sources}`")
# Check if configured sources have VLANs allocated
sources_without_vlans = expected_sources - sources_with_vlans
if sources_without_vlans:
self.result.is_failure(f"Dynamic VLAN source(s) exist but have no VLANs allocated: {', '.join(sorted(sources_without_vlans))}")
return

# If dynamic VLAN(s) are disabled on any of the designated sources, test fails.
absent_sources = set(expected_sources).difference(set(actual_sources))
if absent_sources:
self.result.is_failure(f"Dynamic VLAN(s) sources mismatch - Expected: `{str_expected_sources}` Actual: `{str_actual_sources}`")
# In strict mode, verify no other sources have VLANs allocated
if self.inputs.strict:
unexpected_sources = sources_with_vlans - expected_sources
if unexpected_sources:
self.result.is_failure(f"Strict mode enabled: Unexpected sources have VLANs allocated: {', '.join(sorted(unexpected_sources))}")
10 changes: 5 additions & 5 deletions tests/units/anta_tests/test_vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"expected": {"result": "success"},
},
{
"name": "failure-no-dynamic-vlans",
"name": "failure-no-dynamic-vlan-sources",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {}}],
"inputs": {"sources": ["evpn", "mlagsync"], "strict": False},
"expected": {"result": "failure", "messages": ["Dynamic VLANs sources evpn, mlagsync not found in the configuration"]},
"expected": {"result": "failure", "messages": ["Dynamic VLAN source(s) not found in configuration: evpn, mlagsync"]},
},
{
"name": "failure-dynamic-vlan-sources-mismatch",
Expand All @@ -54,7 +54,7 @@
"inputs": {"sources": ["evpn", "mlagsync"], "strict": False},
"expected": {
"result": "failure",
"messages": ["Dynamic VLAN(s) sources mismatch - Expected: `evpn, mlagsync` Actual: `vccbfd, mlagsync`"],
"messages": ["Dynamic VLAN source(s) not found in configuration: evpn"],
},
},
{
Expand All @@ -71,14 +71,14 @@
"inputs": {"sources": ["evpn", "mlagsync"], "strict": True},
"expected": {
"result": "failure",
"messages": ["Dynamic VLAN allocations expected to be sources `evpn, mlagsync` only, however actual it is `evpn, mlagsync, vccbfd`"],
"messages": ["Strict mode enabled: Unexpected sources have VLANs allocated: vccbfd"],
},
},
{
"name": "failure-all-sources-exact-match-expected-source-not-found",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": []}}}],
"inputs": {"sources": ["evpn", "mlagsync"], "strict": True},
"expected": {"result": "failure", "messages": ["Dynamic VLAN allocations expected to be sources `evpn, mlagsync` only, however actual it is `evpn`"]},
"expected": {"result": "failure", "messages": ["Dynamic VLAN source(s) exist but have no VLANs allocated: mlagsync"]},
},
]

0 comments on commit fcdc00b

Please sign in to comment.