Skip to content

Commit

Permalink
issue_822 Handling review comments: updated failure msgs and docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
VitthalMagadum committed Sep 30, 2024
1 parent 7fa100b commit 0c93649
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
24 changes: 16 additions & 8 deletions anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,14 @@ def test(self) -> None:
class VerifySNMPNotificationHost(AntaTest):
"""Verifies the SNMP notification host (SNMP manager) configurations.
- Verifies that the valid notification type and VRF name.
- Ensures that UDP port provided matches the expected value.
- Ensures that the community_string is properly set for SNMP v1/v2 and for SNMP v3, the user field is included, aligning with version-specific requirements.
Expected Results
----------------
* Success: The test will pass if the SNMP PDU counter(s) are non-zero/greater than zero.
* Failure: The test will fail if the SNMP PDU counter(s) are zero/None/Not Found.
* Success: The test will pass if the provided SNMP notification host and all specified parameters are correctly configured.
* Failure: The test will fail if the provided SNMP notification host is not configured or specified parameters are not correctly configured.
Examples
--------
Expand Down Expand Up @@ -281,7 +285,7 @@ class SNMPHost(BaseModel):
"""Model for a SNMP Manager."""

hostname: IPv4Address
"""IP address of the SNMP notification host."""
"""IPv4 address of the SNMP notification host."""
vrf: str = "default"
"""Optional VRF for SNMP Hosts. If not provided, it defaults to `default`."""
notification_type: Literal["trap", "inform"]
Expand Down Expand Up @@ -314,8 +318,14 @@ def validate_inputs(self: BaseModel) -> BaseModel:
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifySNMPNotificationHost."""
self.result.is_success()
failures: str = ""

# Verify SNMP host details.
if not (snmp_hosts := get_value(self.instance_commands[0].json_output, "hosts")):
self.result.is_failure("SNMP is not configured.")
return

for host in self.inputs.notification_hosts:
hostname = str(host.hostname)
vrf = host.vrf
Expand All @@ -326,8 +336,8 @@ def test(self) -> None:
user = host.user

# Verify SNMP host details.
if not (host_details := get_item(self.instance_commands[0].json_output["hosts"], "hostname", hostname)):
failures += f"Details not found for SNMP host '{hostname}'.\n"
if not (host_details := get_item(snmp_hosts, "hostname", hostname)):
failures += f"SNMP host '{hostname}' is not configured.\n"
continue

# Update expected host details.
Expand All @@ -354,7 +364,5 @@ def test(self) -> None:
failures += f"For SNMP host {hostname}:{failure_logs}\n"

# Check if there are any failures.
if not failures:
self.result.is_success()
else:
if failures:
self.result.is_failure(failures)
29 changes: 27 additions & 2 deletions tests/units/anta_tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"expected": {"result": "success"},
},
{
"name": "failure-details-not-found",
"name": "failure-not-configured",
"test": VerifySNMPNotificationHost,
"eos_data": [{"hosts": []}],
"inputs": {
Expand All @@ -195,7 +195,32 @@
{"hostname": "192.168.1.101", "vrf": "default", "notification_type": "trap", "version": "v2c", "udp_port": 162, "community_string": "public"},
]
},
"expected": {"result": "failure", "messages": ["Details not found for SNMP host '192.168.1.100'.\nDetails not found for SNMP host '192.168.1.101'.\n"]},
"expected": {"result": "failure", "messages": ["SNMP is not configured."]},
},
{
"name": "failure-details-not-found",
"test": VerifySNMPNotificationHost,
"eos_data": [
{
"hosts": [
{
"hostname": "192.168.1.100",
"port": 162,
"vrf": "",
"notificationType": "trap",
"protocolVersion": "v3",
"v3Params": {"user": "public", "securityLevel": "authNoPriv"},
},
]
}
],
"inputs": {
"notification_hosts": [
{"hostname": "192.168.1.100", "vrf": "default", "notification_type": "trap", "version": "v3", "udp_port": 162, "user": "public"},
{"hostname": "192.168.1.101", "vrf": "default", "notification_type": "trap", "version": "v2c", "udp_port": 162, "community_string": "public"},
]
},
"expected": {"result": "failure", "messages": ["SNMP host '192.168.1.101' is not configured.\n"]},
},
{
"name": "failure-incorrect-config",
Expand Down

0 comments on commit 0c93649

Please sign in to comment.