Skip to content

Commit

Permalink
Issue_864: Updated docstring and the testcase logic
Browse files Browse the repository at this point in the history
  • Loading branch information
geetanjalimanegslab committed Jan 8, 2025
1 parent 8174dc9 commit ece4d84
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
2 changes: 1 addition & 1 deletion anta/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,4 @@ def validate_regex(value: str) -> str:
"Route Cache Route",
"CBF Leaked Route",
]
DynamicVLANSource = Literal["dmf", "dot1x", "dynvtep", "evpn", "mlag", "mlagsync", "mvpn", "swfwd", "vccbfd"]
DynamicVlanSource = Literal["dmf", "dot1x", "dynvtep", "evpn", "mlag", "mlagsync", "mvpn", "swfwd", "vccbfd"]
50 changes: 24 additions & 26 deletions anta/tests/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

from typing import TYPE_CHECKING, ClassVar, Literal

from pydantic import ConfigDict

from anta.custom_types import DynamicVLANSource, Vlan
from anta.custom_types import DynamicVlanSource, Vlan
from anta.models import AntaCommand, AntaTest
from anta.tools import get_failed_logs, get_value

Expand Down Expand Up @@ -73,33 +71,33 @@ def test(self) -> None:


class VerifyDynamicVlanSource(AntaTest):
"""Verifies dynamic VLAN(s) source.
"""Verifies dynamic VLAN(s) sources.
This test performs the following checks for specified dynamic VLAN(s):
1. Ensures that dynamic VLAN(s) are properly configured in the system.
2. Confirms that dynamic VLAN(s) are enabled for any/all the designated sources and disabled for all others.
2. Confirms that dynamic VLAN(s) are enabled on all the designated sources.
3. When strict mode is enabled (`strict: true`):
- Dynamic VLAN(s) are enabled for all designated sources.
- The dynamic VLAN(s) are enabled on all the designated sources and disabled for non designated sources.
Expected Results
----------------
* Success: The test will pass if all of the following conditions are met:
- The dynamic VLAN(s) are properly configured in the system.
- The dynamic VLAN(s) are enabled for any/all of the designated sources and disabled for all others.
- In strict mode, dynamic VLAN(s) are enabled for all designated sources.
- The dynamic VLAN(s) are enabled for all of the designated sources.
- In strict mode, The dynamic VLAN(s) are enabled on all the designated sources and disabled for non designated sources.
* Failure: The test will fail if any of the following conditions is met:
- The dynamic VLAN(s) are disabled on all designated sources, or active on non designated sources.
- In strict mode, dynamic VLAN(s) are disabled on any of the designated sources.
* Skipped: The test will Skip if the following conditions is met:
- The dynamic VLAN(s) are disabled on any of designated sources.
- In strict mode, dynamic VLAN(s) are disabled on any of the designated sources. or enabled for non designated sources.
* Skipped: The test will skip if the following conditions is met:
- Dynamic VLAN(s) are not configured on the device.
Examples
--------
```yaml
anta.tests.vlan:
- VerifyDynamicVlanSource:
source:
sources:
- evpn
- mlagsync
strict: False
Expand All @@ -112,11 +110,10 @@ class VerifyDynamicVlanSource(AntaTest):
class Input(AntaTest.Input):
"""Input model for the VerifyDynamicVlanSource test."""

model_config = ConfigDict(extra="forbid")
source: list[DynamicVLANSource]
sources: list[DynamicVlanSource]
"""The dynamic VLAN(s) source list."""
strict: bool = False
"""If True, requires exact match of the provided dynamic VLAN(s) sources, Defaults to `False`"""
"""If True, dynamic VLAN(s) should be enabled only on designated sources, Defaults to `False`."""

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

actual_source = [source for source, data in dynamic_vlans.items() if data.get("vlanIds")]
actual_sources = [source for source, data in dynamic_vlans.items() if data.get("vlanIds")]
# If the dynamic vlans are not configured, skipping the test.
if not actual_source:
if not actual_sources:
self.result.is_skipped("Dynamic VLANs are not configured")
return

expected_source = self.inputs.source
str_expected_source = ", ".join(expected_source)
str_actual_source = ", ".join(actual_source)
expected_sources = self.inputs.sources
str_expected_sources = ", ".join(expected_sources)
str_actual_sources = ", ".join(actual_sources)

# If strict flag is True and dynamic VLAN(s) are disabled on any of the designated sources, test fails.
if self.inputs.strict and sorted(actual_source) != (expected_source):
self.result.is_failure(f"Dynamic VLAN(s) source mismatch - Expected: {str_expected_source} Actual: {str_actual_source}")
# If stric 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(s) sources mismatch - Expected: {str_expected_sources} Actual: {str_actual_sources}")
return

# The dynamic VLAN(s) are disabled on all designated sources, or active on non designated sources, test fails.
if not set(actual_source).issubset(expected_source):
self.result.is_failure(f"Dynamic VLAN(s) source mismatch - {str_actual_source} are not in the expected sources: {str_expected_source}.")
# 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: {', '.join(absent_sources)} not in the Actual: {str_actual_sources}.")
4 changes: 2 additions & 2 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,8 @@ anta.tests.system:
minimum: 86400
anta.tests.vlan:
- VerifyDynamicVlanSource:
# Verifies dynamic VLAN(s) source.
source:
# Verifies dynamic VLAN(s) sources.
sources:
- evpn
- mlagsync
strict: False
Expand Down
37 changes: 15 additions & 22 deletions tests/units/anta_tests/test_vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,50 +36,43 @@
{
"name": "success",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": []}}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": False},
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": [1401]}, "vccbfd": {"vlanIds": [1501]}}}],
"inputs": {"sources": ["evpn", "mlagsync"], "strict": False},
"expected": {"result": "success"},
},
{
"name": "failure-no-dynamic-vlans",
"name": "skip-no-dynamic-vlans",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": False},
"inputs": {"sources": ["evpn", "mlagsync"], "strict": False},
"expected": {"result": "skipped", "messages": ["Dynamic VLANs are not configured"]},
},
{
"name": "failure-dynamic-vlan-source-invalid",
"name": "failure-dynamic-vlan-sources-mismatch",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"vccbfd": {"vlanIds": [1500]}, "mlagsync": {"vlanIds": [1501]}}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": False},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) source mismatch - vccbfd, mlagsync are not in the expected sources: evpn, mlagsync."]},
},
{
"name": "failure-any-source-match-additional-source-found",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": [1501]}, "vccbfd": {"vlanIds": [1500]}}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": False},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) source mismatch - evpn, mlagsync, vccbfd are not in the expected sources: evpn, mlagsync."]},
"inputs": {"sources": ["evpn", "mlagsync"], "strict": False},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) sources mismatch - Expected: evpn not in the Actual: vccbfd, mlagsync."]},
},
{
"name": "success-strict-mode",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": [1502]}}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": True},
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": [1502], "vccbfd": {"vlanIds": []}}}}],
"inputs": {"sources": ["evpn", "mlagsync"], "strict": True},
"expected": {"result": "success"},
},
{
"name": "failure-all-source-exact-match-additional-source-found",
"name": "failure-all-sources-exact-match-additional-source-found",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": [1500]}, "vccbfd": {"vlanIds": [1500]}}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": True},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) source mismatch - Expected: evpn, mlagsync Actual: evpn, mlagsync, vccbfd"]},
"inputs": {"sources": ["evpn", "mlagsync"], "strict": True},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) sources mismatch - Expected: evpn, mlagsync Actual: evpn, mlagsync, vccbfd"]},
},
{
"name": "failure-all-source-exact-match-expected-source-not-found",
"name": "failure-all-sources-exact-match-expected-source-not-found",
"test": VerifyDynamicVlanSource,
"eos_data": [{"dynamicVlans": {"evpn": {"vlanIds": [1199]}, "mlagsync": {"vlanIds": []}}}],
"inputs": {"source": ["evpn", "mlagsync"], "strict": True},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) source mismatch - Expected: evpn, mlagsync Actual: evpn"]},
"inputs": {"sources": ["evpn", "mlagsync"], "strict": True},
"expected": {"result": "failure", "messages": ["Dynamic VLAN(s) sources mismatch - Expected: evpn, mlagsync Actual: evpn"]},
},
]

0 comments on commit ece4d84

Please sign in to comment.