From c589ee942350295fc8519b7fba18a6941c5686dc Mon Sep 17 00:00:00 2001 From: VitthalMagadum Date: Thu, 7 Nov 2024 01:31:19 -0500 Subject: [PATCH] Updated test for multiple neighbor --- anta/tests/connectivity.py | 23 +++++---- tests/units/anta_tests/test_connectivity.py | 54 +++++++++++++++++---- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/anta/tests/connectivity.py b/anta/tests/connectivity.py index b8950e022..7d1c89229 100644 --- a/anta/tests/connectivity.py +++ b/anta/tests/connectivity.py @@ -11,7 +11,6 @@ from anta.input_models.connectivity import Host, Neighbor from anta.models import AntaCommand, AntaTemplate, AntaTest -from anta.tools import get_item, get_value class VerifyReachability(AntaTest): @@ -121,15 +120,19 @@ def test(self) -> None: output = self.instance_commands[0].json_output["lldpNeighbors"] for neighbor in self.inputs.neighbors: - if not (lldp_neighbor_info := get_value(output, f"{neighbor.port}.lldpNeighborInfo")) or not ( - neighbor_info := get_item(lldp_neighbor_info, "systemName", neighbor.neighbor_device) - ): - self.result.is_failure(f"{neighbor} - Not configured") + if neighbor.port not in output: + self.result.is_failure(f"{neighbor} - Port not found") continue - sytem_name = neighbor_info.get("systemName") - neighbor_port = neighbor_info.get("neighborInterfaceInfo", {}).get("interfaceId_v2") + if len(lldp_neighbor_info := output[neighbor.port]["lldpNeighborInfo"]) == 0: + self.result.is_failure(f"{neighbor} - No LLDP neighbors on the port") + continue - # Check if the neighbor details matches the expected details - if sytem_name != neighbor.neighbor_device or neighbor_port != neighbor.neighbor_port: - self.result.is_failure(f"{neighbor} - Inconsistent LLDP neighbors; Neighbor device: {sytem_name}, Neighbor port: {neighbor_port}") + # Check if the system name and neighbor port matches + match_found = any( + info["systemName"] == neighbor.neighbor_device and info["neighborInterfaceInfo"]["interfaceId_v2"] == neighbor.neighbor_port + for info in lldp_neighbor_info + ) + if not match_found: + failure_msg = [f"{info['systemName']}/{info['neighborInterfaceInfo']['interfaceId_v2']}" for info in lldp_neighbor_info] + self.result.is_failure(f"{neighbor} - Wrong LLDP neighbors on the ports; {', '.join(failure_msg)}") diff --git a/tests/units/anta_tests/test_connectivity.py b/tests/units/anta_tests/test_connectivity.py index 7383071cb..c0e229ffe 100644 --- a/tests/units/anta_tests/test_connectivity.py +++ b/tests/units/anta_tests/test_connectivity.py @@ -330,7 +330,40 @@ {"port": "Ethernet2", "neighbor_device": "DC1-SPINE2", "neighbor_port": "Ethernet1"}, ], }, - "expected": {"result": "failure", "messages": ["Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - Not configured"]}, + "expected": {"result": "failure", "messages": ["Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - Port not found"]}, + }, + { + "name": "failure-no-neighbor", + "test": VerifyLLDPNeighbors, + "eos_data": [ + { + "lldpNeighbors": { + "Ethernet1": { + "lldpNeighborInfo": [ + { + "chassisIdType": "macAddress", + "chassisId": "001c.73a0.fc18", + "systemName": "DC1-SPINE1", + "neighborInterfaceInfo": { + "interfaceIdType": "interfaceName", + "interfaceId": '"Ethernet1"', + "interfaceId_v2": "Ethernet1", + "interfaceDescription": "P2P_LINK_TO_DC1-LEAF1A_Ethernet1", + }, + }, + ], + }, + "Ethernet2": {"lldpNeighborInfo": []}, + }, + }, + ], + "inputs": { + "neighbors": [ + {"port": "Ethernet1", "neighbor_device": "DC1-SPINE1", "neighbor_port": "Ethernet1"}, + {"port": "Ethernet2", "neighbor_device": "DC1-SPINE2", "neighbor_port": "Ethernet1"}, + ], + }, + "expected": {"result": "failure", "messages": ["Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - No LLDP neighbors on the port"]}, }, { "name": "failure-wrong-neighbor", @@ -379,10 +412,7 @@ }, "expected": { "result": "failure", - "messages": [ - "Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - " - "Inconsistent LLDP neighbors; Neighbor device: DC1-SPINE2, Neighbor port: Ethernet2" - ], + "messages": ["Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - Wrong LLDP neighbors on the ports; DC1-SPINE2/Ethernet2"], }, }, { @@ -420,10 +450,9 @@ "expected": { "result": "failure", "messages": [ - "Port Ethernet1 (Neighbor: DC1-SPINE1, Neighbor port: Ethernet1) - Inconsistent LLDP neighbors; " - "Neighbor device: DC1-SPINE1, Neighbor port: Ethernet2", - "Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - Not configured", - "Port Ethernet3 (Neighbor: DC1-SPINE3, Neighbor port: Ethernet1) - Not configured", + "Port Ethernet1 (Neighbor: DC1-SPINE1, Neighbor port: Ethernet1) - Wrong LLDP neighbors on the ports; DC1-SPINE1/Ethernet2", + "Port Ethernet2 (Neighbor: DC1-SPINE2, Neighbor port: Ethernet1) - No LLDP neighbors on the port", + "Port Ethernet3 (Neighbor: DC1-SPINE3, Neighbor port: Ethernet1) - Port not found", ], }, }, @@ -467,6 +496,11 @@ {"port": "Ethernet1", "neighbor_device": "DC1-SPINE3", "neighbor_port": "Ethernet1"}, ], }, - "expected": {"result": "failure", "messages": ["Port Ethernet1 (Neighbor: DC1-SPINE3, Neighbor port: Ethernet1) - Not configured"]}, + "expected": { + "result": "failure", + "messages": [ + "Port Ethernet1 (Neighbor: DC1-SPINE3, Neighbor port: Ethernet1) - Wrong LLDP neighbors on the ports; DC1-SPINE1/Ethernet1, DC1-SPINE2/Ethernet1" + ], + }, }, ]