-
Notifications
You must be signed in to change notification settings - Fork 30
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 the test case to verify SNMP groups #886
base: main
Are you sure you want to change the base?
Changes from all commits
fea9674
212147d
cb1d3d7
de48bd8
c383c57
68a5acb
7886dbd
819c92b
c12f970
69308b9
05f1c99
8d3d1ab
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 | ||||
---|---|---|---|---|---|---|
|
@@ -12,7 +12,7 @@ | |||||
from pydantic import field_validator | ||||||
|
||||||
from anta.custom_types import PositiveInteger, SnmpErrorCounter, SnmpPdu | ||||||
from anta.input_models.snmp import SnmpHost, SnmpSourceInterface, SnmpUser | ||||||
from anta.input_models.snmp import SnmpGroup, SnmpHost, SnmpSourceInterface, SnmpUser | ||||||
from anta.models import AntaCommand, AntaTest | ||||||
from anta.tools import get_value | ||||||
|
||||||
|
@@ -543,3 +543,81 @@ def test(self) -> None: | |||||
self.result.is_failure(f"{interface_details} - Not configured") | ||||||
elif actual_interface != interface_details.interface: | ||||||
self.result.is_failure(f"{interface_details} - Incorrect source interface - Actual: {actual_interface}") | ||||||
|
||||||
|
||||||
class VerifySnmpGroup(AntaTest): | ||||||
"""Verifies the SNMP group configurations for specified version(s). | ||||||
This test performs the following checks: | ||||||
1. Verifies that the SNMP group is configured for the specified version. | ||||||
2. For SNMP version 3, verify that the security model matches the expected value. | ||||||
3. Ensures that SNMP group configurations, including read, write, and notify views, align with version-specific requirements. | ||||||
Expected Results | ||||||
---------------- | ||||||
* Success: The test will pass if the provided SNMP group and all specified parameters are correctly configured. | ||||||
* Failure: The test will fail if the provided SNMP group is not configured or specified parameters are not correctly configured. | ||||||
Examples | ||||||
-------- | ||||||
```yaml | ||||||
anta.tests.snmp: | ||||||
- VerifySnmpGroup: | ||||||
snmp_groups: | ||||||
- group_name: Group1 | ||||||
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. show other versions in example - especially with the security model for v3 |
||||||
version: v1 | ||||||
read_view: group_read_1 | ||||||
write_view: group_write_1 | ||||||
notify_view: group_notify_1 | ||||||
``` | ||||||
""" | ||||||
|
||||||
categories: ClassVar[list[str]] = ["snmp"] | ||||||
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show snmp group", revision=1)] | ||||||
|
||||||
class Input(AntaTest.Input): | ||||||
"""Input model for the VerifySnmpGroup test.""" | ||||||
|
||||||
snmp_groups: list[SnmpGroup] | ||||||
"""List of SNMP groups.""" | ||||||
|
||||||
@field_validator("snmp_groups") | ||||||
@classmethod | ||||||
def validate_snmp_groups(cls, snmp_groups: list[SnmpGroup]) -> list[SnmpGroup]: | ||||||
"""Validate the inputs provided to the SnmpGroup class.""" | ||||||
for snmp_group in snmp_groups: | ||||||
if snmp_group.version == "v3" and snmp_group.authentication is None: | ||||||
msg = f"{snmp_group}; `authentication` field is required for `version: v3`" | ||||||
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. Please don't use semi columns
Suggested change
|
||||||
raise ValueError(msg) | ||||||
return snmp_groups | ||||||
|
||||||
@AntaTest.anta_test | ||||||
def test(self) -> None: | ||||||
"""Main test function for VerifySnmpGroup.""" | ||||||
self.result.is_success() | ||||||
for group in self.inputs.snmp_groups: | ||||||
# Verify SNMP group details. | ||||||
if not (group_details := get_value(self.instance_commands[0].json_output, f"groups.{group.group_name}.versions.{group.version}")): | ||||||
self.result.is_failure(f"{group} - Not configured") | ||||||
continue | ||||||
|
||||||
# Verify SNMP views, the read, write and notify settings aligning with version-specific requirements. | ||||||
for view_type in ["read", "write", "notify"]: | ||||||
expected_view = getattr(group, f"{view_type}_view") | ||||||
|
||||||
# Verify actual view is configured. | ||||||
if expected_view and group_details.get(f"{view_type}View") == "": | ||||||
self.result.is_failure(f"{group} View: {view_type} - Not configured") | ||||||
elif expected_view and not all( | ||||||
[(act_view := group_details.get(f"{view_type}View")) == expected_view, (view_configured := group_details.get(f"{view_type}ViewConfig"))] | ||||||
): | ||||||
self.result.is_failure( | ||||||
f"{group} {view_type.title()} View: {expected_view} - " | ||||||
f"View configuration mismatch - {view_type.title()} View: {act_view}, " | ||||||
f"Configured: {view_configured}" | ||||||
) | ||||||
Comment on lines
+612
to
+619
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. I would rather make two error messages, 1 to indicate the view name is not correct (matcging the expecting one) and 2. to indicate that when the view name is matching, it is not properly configured (Configured: 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. Agreed |
||||||
|
||||||
# For version v3, verify that the security model aligns with the expected value. | ||||||
if group.version == "v3" and (actual_auth := group_details.get("secModel")) != group.authentication: | ||||||
self.result.is_failure(f"{group} - Incorrect security model - Expected: {group.authentication} Actual: {actual_auth}") |
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.