Skip to content

Commit

Permalink
Issue_821: Refactored testcase with latest changes of input modules a…
Browse files Browse the repository at this point in the history
…nd updated TC, docstrings
  • Loading branch information
geetanjalimanegslab committed Jan 9, 2025
1 parent 9397f70 commit 9764580
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 40 deletions.
31 changes: 31 additions & 0 deletions anta/input_models/snmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Module containing input models for SNMP tests."""

from __future__ import annotations

from ipaddress import IPv4Address

from pydantic import BaseModel, ConfigDict

from anta.custom_types import Hostname


class SNMPHost(BaseModel):
"""Model for a SNMP Host."""

model_config = ConfigDict(extra="forbid")
hostname: IPv4Address | Hostname
"""IPv4 address of the SNMP notification host."""
vrf: str = "default"
"""Optional VRF for SNMP Hosts. If not provided, it defaults to `default`."""

def __str__(self) -> str:
"""Return a human-readable string representation of the Host for reporting.
Examples
--------
- Host: 192.168.1.100 VRF: default
"""
return f"Host: {self.hostname} VRF: {self.vrf}"
65 changes: 31 additions & 34 deletions anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
# mypy: disable-error-code=attr-defined
from __future__ import annotations

from ipaddress import IPv4Address
from typing import TYPE_CHECKING, ClassVar, get_args

from pydantic import BaseModel

from anta.custom_types import Hostname, PositiveInteger, SnmpErrorCounter, SnmpPdu
from anta.custom_types import PositiveInteger, SnmpErrorCounter, SnmpPdu
from anta.input_models.snmp import SNMPHost
from anta.models import AntaCommand, AntaTest
from anta.tools import get_value

Expand Down Expand Up @@ -345,12 +343,24 @@ def test(self) -> None:


class VerifySnmpLogging(AntaTest):
"""Verifies whether the SNMP logging is enabled and SNMP manager(host) details in a specified VRF.
"""Verifies SNMP logging and SNMP manager(host) details.
This test performs the following checks for each specified host:
1. Verifies that the SNMP logging is enabled on the device.
2. Verifies SNMP host matches the expected value.
3. Ensures that VRF provided matches the expected value.
Expected Results
----------------
* Success: The test will pass if the SNMP logging is enabled and manager(host) details is in the specified VRF.
* Failure: The test will fail if the SNMP logging is disabled or the SNMP manager details are not correct.
* Success: The test will pass if all of the following conditions are met:
- The SNMP logging is enabled on the device.
- The SNMP host matches the expected value.
- The VRF provided matches the expected value.
* Failure: The test will fail if any of the following conditions is met:
- The SNMP logging is disabled on the device.
- The SNMP host do not matches the expected value.
- The VRF provided do not matches the expected value.
Examples
--------
Expand All @@ -365,8 +375,6 @@ class VerifySnmpLogging(AntaTest):
```
"""

name = "VerifySnmpLogging"
description = "Verifies whether the SNMP logging is enabled and SNMP manager(host) in a specified VRF"
categories: ClassVar[list[str]] = ["snmp"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show snmp", revision=1)]

Expand All @@ -376,41 +384,30 @@ class Input(AntaTest.Input):
hosts: list[SNMPHost]
"""List of SNMP hosts."""

class SNMPHost(BaseModel):
"""Model for a SNMP Host."""

hostname: IPv4Address | Hostname
"""IPv4 address or hostname of the SNMP notification host."""
vrf: str = "default"
"""Optional VRF for SNMP Hosts. If not provided, it defaults to `default`."""

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifySnmpLogging."""
failures: str = ""
self.result.is_success()

logging_output = self.instance_commands[0].json_output.get("logging", {})
if not logging_output.get("loggingEnabled"):
self.result.is_failure("SNMP logging is disabled.")
command_output = self.instance_commands[0].json_output.get("logging", {})
# If SNMP logging is disabled, test fails.
if not command_output.get("loggingEnabled"):
self.result.is_failure("SNMP logging is disabled")
return

host_details = logging_output.get("hosts")
host_details = command_output.get("hosts")

for host in self.inputs.hosts:
hostname = str(host.hostname)
vrf = host.vrf
actual_snmp_host = host_details.get(hostname, {})
actual_vrf = "default" if (vrf_name := actual_snmp_host.get("vrf")) == "" else vrf_name

# Verify SNMP host details.
snmp_host = host_details.get(hostname, {})
actual_vrf = "default" if (vrf_name := snmp_host.get("vrf")) == "" else vrf_name
if not snmp_host:
failures += f"SNMP host '{hostname }' is not configured.\n"
# If SNMP host is not configured on the system, test fails.
if not actual_snmp_host:
self.result.is_failure(f"{host} - Not configured")
continue
if actual_vrf != vrf:
failures += f"For SNMP host '{hostname }', expected '{vrf}' as vrf but found '{actual_vrf}' instead.\n"

# Check if there are any failures.
if not failures:
self.result.is_success()
else:
self.result.is_failure(failures)
# If VRF is not matches the expected value, test fails.
if actual_vrf != vrf:
self.result.is_failure(f"{host} - Incorrect VRF - Actual: {actual_vrf}")
15 changes: 15 additions & 0 deletions docs/api/tests.snmp.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ anta_title: ANTA catalog for SNMP tests
~ that can be found in the LICENSE file.
-->

# Test

::: anta.tests.snmp
options:
show_root_heading: false
Expand All @@ -18,3 +20,16 @@ anta_title: ANTA catalog for SNMP tests
filters:
- "!test"
- "!render"

# Input models

::: anta.input_models.snmp

options:
show_root_heading: false
show_root_toc_entry: false
show_bases: false
merge_init_into_class: false
anta_hide_test_module_description: true
show_labels: true
filters: ["!^__str__"]
7 changes: 7 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,13 @@ anta.tests.snmp:
- VerifySnmpLocation:
# Verifies the SNMP location of a device.
location: New York
- VerifySnmpLogging:
# Verifies SNMP logging and SNMP manager(host) details.
hosts:
- hostname: 192.168.1.100
vrf: default
- hostname: 192.168.1.103
vrf: MGMT
- VerifySnmpPDUCounters:
# Verifies the SNMP PDU counters.
pdus:
Expand Down
26 changes: 20 additions & 6 deletions tests/units/anta_tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,17 @@
"hosts": {
"192.168.1.100": {"port": 162, "vrf": ""},
"192.168.1.101": {"port": 162, "vrf": "MGMT"},
"snmp-server-01": {"port": 162, "vrf": "MGMT"},
"snmp-server-01": {"port": 162, "vrf": "default"},
},
}
}
],
"inputs": {
"hosts": [{"hostname": "192.168.1.100", "vrf": "default"}, {"hostname": "192.168.1.101", "vrf": "MGMT"}, {"hostname": "snmp-server-01", "vrf": "MGMT"}]
"hosts": [
{"hostname": "192.168.1.100", "vrf": "default"},
{"hostname": "192.168.1.101", "vrf": "MGMT"},
{"hostname": "snmp-server-01", "vrf": "default"},
]
},
"expected": {"result": "success"},
},
Expand All @@ -345,16 +349,26 @@
"test": VerifySnmpLogging,
"eos_data": [{"logging": {"loggingEnabled": False}}],
"inputs": {"hosts": [{"hostname": "192.168.1.100", "vrf": "default"}, {"hostname": "192.168.1.101", "vrf": "MGMT"}]},
"expected": {"result": "failure", "messages": ["SNMP logging is disabled."]},
"expected": {"result": "failure", "messages": ["SNMP logging is disabled"]},
},
{
"name": "failure-incorrect-hosts",
"name": "failure-mismatch-vrf",
"test": VerifySnmpLogging,
"eos_data": [{"logging": {"loggingEnabled": True, "hosts": {"192.168.1.100": {"port": 162, "vrf": "MGMT"}}}}],
"eos_data": [{"logging": {"loggingEnabled": True, "hosts": {"192.168.1.100": {"port": 162, "vrf": "MGMT"}, "192.168.1.101": {"port": 162, "vrf": "Test"}}}}],
"inputs": {"hosts": [{"hostname": "192.168.1.100", "vrf": "default"}, {"hostname": "192.168.1.101", "vrf": "MGMT"}]},
"expected": {
"result": "failure",
"messages": ["For SNMP host '192.168.1.100', expected 'default' as vrf but found 'MGMT' instead.\nSNMP host '192.168.1.101' is not configured."],
"messages": ["Host: 192.168.1.100 VRF: default - Incorrect VRF - Actual: MGMT", "Host: 192.168.1.101 VRF: MGMT - Incorrect VRF - Actual: Test"],
},
},
{
"name": "failure-host-not-configured",
"test": VerifySnmpLogging,
"eos_data": [{"logging": {"loggingEnabled": True, "hosts": {"192.168.1.100": {"port": 162, "vrf": "MGMT"}, "192.168.1.103": {"port": 162, "vrf": "Test"}}}}],
"inputs": {"hosts": [{"hostname": "192.168.1.101", "vrf": "default"}, {"hostname": "192.168.1.102", "vrf": "MGMT"}]},
"expected": {
"result": "failure",
"messages": ["Host: 192.168.1.101 VRF: default - Not configured", "Host: 192.168.1.102 VRF: MGMT - Not configured"],
},
},
]

0 comments on commit 9764580

Please sign in to comment.