Skip to content

Commit

Permalink
issue_820 handling review comments: updated input schema
Browse files Browse the repository at this point in the history
  • Loading branch information
VitthalMagadum committed Oct 1, 2024
1 parent 7e7e44d commit 1004c6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
35 changes: 25 additions & 10 deletions anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# mypy: disable-error-code=attr-defined
from __future__ import annotations

from typing import TYPE_CHECKING, ClassVar, Literal
from typing import TYPE_CHECKING, ClassVar

from pydantic import BaseModel, model_validator

from anta.custom_types import PositiveInteger
from anta.models import AntaCommand, AntaTest
Expand Down Expand Up @@ -242,7 +244,7 @@ def test(self) -> None:
self.result.is_success()


class VerifySNMPPDUs(AntaTest):
class VerifySnmpPDUs(AntaTest):
"""Verifies the number of SNMP PDU(s) (Protocol Data Units) processed.
By default, all SNMP PDU counters will be checked for any non-zero values.
Expand All @@ -257,43 +259,56 @@ class VerifySNMPPDUs(AntaTest):
--------
```yaml
anta.tests.snmp:
- VerifySNMPPDUs:
- VerifySnmpPDUs:
pdus:
- outTrapPdus
- inGetNextPdus
```
"""

name = "VerifySNMPPDUs"
name = "VerifySnmpPDUs"
description = "Verifies the number of SNMP PDU(s) (Protocol Data Units) processed."
categories: ClassVar[list[str]] = ["snmp"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show snmp", revision=1)]

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

pdus: list[Literal["inGetPdus", "inGetNextPdus", "inSetPdus", "outGetResponsePdus", "outTrapPdus"]] | None = None
pdus: list[str] | None = None
"""Optional list of SNMP PDU counters to be verified. If not provided, test will verifies all the PDUs."""

@model_validator(mode="after")
def validate_inputs(self: BaseModel) -> BaseModel:
"""Validate the inputs provided to the VerifySnmpPDUs test.
The valid SNMP PDU counter must be provided.
"""
if self.pdus:
for pdu in self.pdus:
if pdu not in SNMP_PDUS:
msg = f"Invalid PDU counter {pdu}. Must be one of {SNMP_PDUS}."
raise ValueError(msg)
return self

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifySNMPPDUs."""
"""Main test function for VerifySnmpPDUs."""
snmp_pdus = self.inputs.pdus
command_output = self.instance_commands[0].json_output

# Verify SNMP PDU counters.
if not (pdu_counters := get_value(command_output, "counters")):
self.result.is_failure("SNMP counter details not found.")
self.result.is_failure("SNMP counter details are not found.")
return

# In case SNMP PDUs not provided, It will check all the update error counters.
if not snmp_pdus:
snmp_pdus = SNMP_PDUS

failures = [pdu for pdu in snmp_pdus if not pdu_counters.get(pdu)]
failures = {pdu: value for pdu in snmp_pdus if not (value := pdu_counters.get(pdu))}

# Check if any failures
if not failures:
self.result.is_success()
else:
self.result.is_failure(f"The following SNMP PDU(s) are not found or have zero PDU counter:\n{', '.join(failures)}")
self.result.is_failure(f"The following SNMP PDU(s) are not found or have zero PDU counter:\n{failures}")
2 changes: 1 addition & 1 deletion examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ anta.tests.snmp:
location: New York
- VerifySnmpContact:
contact: [email protected]
- VerifySNMPPDUs:
- VerifySnmpPDUs:
pdus:
- outTrapPdus

Expand Down
14 changes: 7 additions & 7 deletions 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, VerifySNMPPDUs, VerifySnmpStatus
from anta.tests.snmp import VerifySnmpContact, VerifySnmpIPv4Acl, VerifySnmpIPv6Acl, VerifySnmpLocation, VerifySnmpPDUs, VerifySnmpStatus
from tests.units.anta_tests import test

DATA: list[dict[str, Any]] = [
Expand Down Expand Up @@ -154,7 +154,7 @@
},
{
"name": "success",
"test": VerifySNMPPDUs,
"test": VerifySnmpPDUs,
"eos_data": [
{
"counters": {
Expand All @@ -171,7 +171,7 @@
},
{
"name": "success-specific-pdus",
"test": VerifySNMPPDUs,
"test": VerifySnmpPDUs,
"eos_data": [
{
"counters": {
Expand All @@ -188,18 +188,18 @@
},
{
"name": "failure-counters-not-found",
"test": VerifySNMPPDUs,
"test": VerifySnmpPDUs,
"eos_data": [
{
"counters": {},
}
],
"inputs": {},
"expected": {"result": "failure", "messages": ["SNMP counter details not found."]},
"expected": {"result": "failure", "messages": ["SNMP counter details are not found."]},
},
{
"name": "failure-incorrect-counters",
"test": VerifySNMPPDUs,
"test": VerifySnmpPDUs,
"eos_data": [
{
"counters": {
Expand All @@ -212,6 +212,6 @@
}
],
"inputs": {},
"expected": {"result": "failure", "messages": ["The following SNMP PDU(s) are not found or have zero PDU counter:\ninGetPdus, inSetPdus"]},
"expected": {"result": "failure", "messages": ["The following SNMP PDU(s) are not found or have zero PDU counter:\n{'inGetPdus': 0, 'inSetPdus': 0}"]},
},
]

0 comments on commit 1004c6d

Please sign in to comment.