-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(anta): Added test case to verify dynamic vlans source #978
base: main
Are you sure you want to change the base?
Changes from 8 commits
cdaeda9
ae88e8d
0db6f7a
ac52aa1
4871b79
7e24116
66ce5b0
8174dc9
ece4d84
d547c26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,9 @@ | |||||
|
||||||
from typing import TYPE_CHECKING, ClassVar, Literal | ||||||
|
||||||
from anta.custom_types import Vlan | ||||||
from pydantic import ConfigDict | ||||||
|
||||||
from anta.custom_types import DynamicVLANSource, Vlan | ||||||
from anta.models import AntaCommand, AntaTest | ||||||
from anta.tools import get_failed_logs, get_value | ||||||
|
||||||
|
@@ -68,3 +70,76 @@ def test(self) -> None: | |||||
self.result.is_failure(failed_log) | ||||||
else: | ||||||
self.result.is_success() | ||||||
|
||||||
|
||||||
class VerifyDynamicVlanSource(AntaTest): | ||||||
"""Verifies dynamic VLAN(s) source. | ||||||
|
||||||
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. | ||||||
3. When strict mode is enabled (`strict: true`): | ||||||
- Dynamic VLAN(s) are enabled for all designated sources. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you clarify the intent of the test? It could be difficult for users to know which services will appear in the output. Also, a service (or source) being present in the dynamic VLANs output just means it's registered as a potential consumer and having no VLANs allocated (empty If the goal is to check that the provided services have dynamic VLANs registered, the test logic could be simplified a lot by looping over the input sources list, check if the source is present, if it is check if its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual test criteria is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok in that case we need to update the docstring and logic to reflect this because, in my opinion, a service with an empty The test should pass if all provided source(s) have dynamic VLANs registered ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apologies if I was not super clear. Here's what I have in mind:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you @carl-baillargeon for clarifying the additional context and explanation, makes things much clearer. |
||||||
|
||||||
Expected Results | ||||||
vitthalmagadum marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
---------------- | ||||||
* 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. | ||||||
* 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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
- Dynamic VLAN(s) are not configured on the device. | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
```yaml | ||||||
anta.tests.vlan: | ||||||
- VerifyDynamicVlanSource: | ||||||
source: | ||||||
- evpn | ||||||
- mlagsync | ||||||
strict: False | ||||||
``` | ||||||
""" | ||||||
|
||||||
categories: ClassVar[list[str]] = ["vlan"] | ||||||
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show vlan dynamic", revision=1)] | ||||||
|
||||||
class Input(AntaTest.Input): | ||||||
"""Input model for the VerifyDynamicVlanSource test.""" | ||||||
|
||||||
model_config = ConfigDict(extra="forbid") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not needed here since it is already in the parent class |
||||||
source: list[DynamicVLANSource] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"""The dynamic VLAN(s) source list.""" | ||||||
strict: bool = False | ||||||
"""If True, requires exact match of the provided dynamic VLAN(s) sources, Defaults to `False`""" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
@AntaTest.anta_test | ||||||
def test(self) -> None: | ||||||
"""Main test function for VerifyDynamicVlanSource.""" | ||||||
self.result.is_success() | ||||||
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")] | ||||||
# If the dynamic vlans are not configured, skipping the test. | ||||||
if not actual_source: | ||||||
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) | ||||||
|
||||||
# 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): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could lead to false negatives if |
||||||
self.result.is_failure(f"Dynamic VLAN(s) source mismatch - Expected: {str_expected_source} Actual: {str_actual_source}") | ||||||
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): | ||||||
vitthalmagadum marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
self.result.is_failure(f"Dynamic VLAN(s) source mismatch - {str_actual_source} are not in the expected sources: {str_expected_source}.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.