Skip to content

Commit

Permalink
refactor(anta): Update VerifySnmpContact , VerifySnmpLocation tests t…
Browse files Browse the repository at this point in the history
…o have a more human readable format for the test result failures messages (#806)
  • Loading branch information
vitthalmagadum authored Sep 4, 2024
1 parent 522ae9d commit abdaea1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
13 changes: 11 additions & 2 deletions anta/tests/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

if TYPE_CHECKING:
from anta.models import AntaTemplate
Expand Down Expand Up @@ -183,8 +184,12 @@ class Input(AntaTest.Input):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifySnmpLocation."""
location = self.instance_commands[0].json_output["location"]["location"]
# Verifies the SNMP location is configured.
if not (location := get_value(self.instance_commands[0].json_output, "location.location")):
self.result.is_failure("SNMP location is not configured.")
return

# Verifies the expected SNMP location.
if location != self.inputs.location:
self.result.is_failure(f"Expected `{self.inputs.location}` as the location, but found `{location}` instead.")
else:
Expand Down Expand Up @@ -222,8 +227,12 @@ class Input(AntaTest.Input):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifySnmpContact."""
contact = self.instance_commands[0].json_output["contact"]["contact"]
# Verifies the SNMP contact is configured.
if not (contact := get_value(self.instance_commands[0].json_output, "contact.contact")):
self.result.is_failure("SNMP contact is not configured.")
return

# Verifies the expected SNMP contact.
if contact != self.inputs.contact:
self.result.is_failure(f"Expected `{self.inputs.contact}` as the contact, but found `{contact}` instead.")
else:
Expand Down
28 changes: 28 additions & 0 deletions tests/units/anta_tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@
"messages": ["Expected `New York` as the location, but found `Europe` instead."],
},
},
{
"name": "failure-details-not-configured",
"test": VerifySnmpLocation,
"eos_data": [
{
"location": {"location": ""},
}
],
"inputs": {"location": "New York"},
"expected": {
"result": "failure",
"messages": ["SNMP location is not configured."],
},
},
{
"name": "success",
"test": VerifySnmpContact,
Expand All @@ -124,4 +138,18 @@
"messages": ["Expected `[email protected]` as the contact, but found `[email protected]` instead."],
},
},
{
"name": "failure-details-not-configured",
"test": VerifySnmpContact,
"eos_data": [
{
"contact": {"contact": ""},
}
],
"inputs": {"contact": "[email protected]"},
"expected": {
"result": "failure",
"messages": ["SNMP contact is not configured."],
},
},
]

0 comments on commit abdaea1

Please sign in to comment.