From abdaea152c515a1da8bdff17c6c753852566a443 Mon Sep 17 00:00:00 2001 From: vitthalmagadum <122079046+vitthalmagadum@users.noreply.github.com> Date: Thu, 5 Sep 2024 02:20:42 +0530 Subject: [PATCH] refactor(anta): Update VerifySnmpContact , VerifySnmpLocation tests to have a more human readable format for the test result failures messages (#806) --- anta/tests/snmp.py | 13 +++++++++++-- tests/units/anta_tests/test_snmp.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/anta/tests/snmp.py b/anta/tests/snmp.py index ac98bfd2f..c7329b6d7 100644 --- a/anta/tests/snmp.py +++ b/anta/tests/snmp.py @@ -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 @@ -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: @@ -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: diff --git a/tests/units/anta_tests/test_snmp.py b/tests/units/anta_tests/test_snmp.py index b4d31521e..64c44382e 100644 --- a/tests/units/anta_tests/test_snmp.py +++ b/tests/units/anta_tests/test_snmp.py @@ -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, @@ -124,4 +138,18 @@ "messages": ["Expected `Bob@example.com` as the contact, but found `Jon@example.com` instead."], }, }, + { + "name": "failure-details-not-configured", + "test": VerifySnmpContact, + "eos_data": [ + { + "contact": {"contact": ""}, + } + ], + "inputs": {"contact": "Bob@example.com"}, + "expected": { + "result": "failure", + "messages": ["SNMP contact is not configured."], + }, + }, ]