Skip to content

Commit

Permalink
Added TC for SNMP source interface
Browse files Browse the repository at this point in the history
  • Loading branch information
VitthalMagadum committed Oct 10, 2024
1 parent 93a4b44 commit b765061
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
72 changes: 71 additions & 1 deletion anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

from typing import TYPE_CHECKING, ClassVar

from anta.custom_types import PositiveInteger
from pydantic import BaseModel

from anta.custom_types import Interface, PositiveInteger
from anta.models import AntaCommand, AntaTest
from anta.tools import get_value

Expand Down Expand Up @@ -237,3 +239,71 @@ def test(self) -> None:
self.result.is_failure(f"Expected `{self.inputs.contact}` as the contact, but found `{contact}` instead.")
else:
self.result.is_success()


class VerifySnmpSourceIntf(AntaTest):
"""Verifies SNMP source-interface for a specified VRF.
Expected Results
----------------
* Success: The test will pass if the provided SNMP source-interface is configured in the specified VRF.
* Failure: The test will fail if the provided SNMP source-interface is NOT configured in the specified VRF.
Examples
--------
```yaml
anta.tests.snmp:
- VerifySnmpSourceIntf:
interfaces:
- interface: Ethernet1
vrf: default
- interface: Management0
vrf: MGMT
```
"""

name = "VerifySnmpSourceIntf"
description = "Verifies SNMP source-interface for a specified VRF."
categories: ClassVar[list[str]] = ["snmp"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show snmp", revision=1)]

class Input(AntaTest.Input):
"""Input model for the VerifySnmpSourceIntf test."""

interfaces: list[SourceInterface]
"""List of source interfaces"""

class SourceInterface(BaseModel):
"""Model for a SNMP source-interface."""

interface: Interface
"""Source-interface to use as source IP of log messages."""
vrf: str = "default"
"""The name of the VRF in which to check for the SNMP agent. Defaults to `default`."""

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

command_output = self.instance_commands[0].json_output.get("srcIntf", {})
if not (interface_output := command_output.get("sourceInterfaces")):
self.result.is_failure("SNMP source interface(s) are not configured.")
return

for interface_details in self.inputs.interfaces:
interface = interface_details.interface
vrf = interface_details.vrf
actual_interface = interface_output.get(vrf)

# Verify source-interface details.
if not actual_interface:
failures += f"Source interface '{interface}' is not configured.\n"
elif actual_interface != interface:
failures += f"Source interface '{interface}' is not correctly configured in vrf '{vrf}'.\n"

# Check if there are any failures.
if not failures:
self.result.is_success()
else:
self.result.is_failure(failures)
6 changes: 6 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ anta.tests.snmp:
location: New York
- VerifySnmpContact:
contact: [email protected]
- VerifySnmpSourceIntf:
interfaces:
- interface: Ethernet1
vrf: default
- interface: Management0
vrf: MGMT

anta.tests.software:
- VerifyEOSVersion:
Expand Down
42 changes: 41 additions & 1 deletion tests/units/anta_tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from typing import Any

from anta.tests.snmp import VerifySnmpContact, VerifySnmpIPv4Acl, VerifySnmpIPv6Acl, VerifySnmpLocation, VerifySnmpStatus
from anta.tests.snmp import VerifySnmpContact, VerifySnmpIPv4Acl, VerifySnmpIPv6Acl, VerifySnmpLocation, VerifySnmpSourceIntf, VerifySnmpStatus
from tests.units.anta_tests import test

DATA: list[dict[str, Any]] = [
Expand Down Expand Up @@ -152,4 +152,44 @@
"messages": ["SNMP contact is not configured."],
},
},
{
"name": "success",
"test": VerifySnmpSourceIntf,
"eos_data": [
{
"srcIntf": {"sourceInterfaces": {"default": "Ethernet1", "MGMT": "Management0"}},
}
],
"inputs": {"interfaces": [{"interface": "Ethernet1", "vrf": "default"}, {"interface": "Management0", "vrf": "MGMT"}]},
"expected": {"result": "success"},
},
{
"name": "failure-not-configured",
"test": VerifySnmpSourceIntf,
"eos_data": [
{
"srcIntf": {},
}
],
"inputs": {"interfaces": [{"interface": "Ethernet1", "vrf": "default"}, {"interface": "Management0", "vrf": "MGMT"}]},
"expected": {"result": "failure", "messages": ["SNMP source interface(s) are not configured."]},
},
{
"name": "failure-incorrect-interfaces",
"test": VerifySnmpSourceIntf,
"eos_data": [
{
"srcIntf": {
"sourceInterfaces": {
"default": "Management0",
}
},
}
],
"inputs": {"interfaces": [{"interface": "Ethernet1", "vrf": "default"}, {"interface": "Management0", "vrf": "MGMT"}]},
"expected": {
"result": "failure",
"messages": ["Source interface 'Ethernet1' is not correctly configured in vrf 'default'.\nSource interface 'Management0' is not configured."],
},
},
]

0 comments on commit b765061

Please sign in to comment.