Skip to content

Commit

Permalink
refactor(anta.tests): Nicer result failure messages interface(part-2)…
Browse files Browse the repository at this point in the history
… test module  (#1046)

* refactor(anta.tests): Nicer result failure messages interface(part-2) test module 

* Updated test VerifyIPProxyARP

* Updated test failure messages

* Updated test failure messages

* Fix extra spaces

---------

Co-authored-by: Carl Baillargeon <carl.baillargeon@arista.com>
  • Loading branch information
geetanjalimanegslab and carl-baillargeon authored Mar 3, 2025
1 parent 88837db commit 2672570
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 54 deletions.
81 changes: 34 additions & 47 deletions anta/tests/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,12 @@ class VerifyPortChannels(AntaTest):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyPortChannels."""
self.result.is_success()
command_output = self.instance_commands[0].json_output
po_with_inactive_ports: list[dict[str, str]] = []
for portchannel, portchannel_dict in command_output["portChannels"].items():
if len(portchannel_dict["inactivePorts"]) != 0:
po_with_inactive_ports.extend({portchannel: portchannel_dict["inactivePorts"]})
if not po_with_inactive_ports:
self.result.is_success()
else:
self.result.is_failure(f"The following port-channels have inactive port(s): {po_with_inactive_ports}")
for port_channel, port_channel_details in command_output["portChannels"].items():
# Verify that the no inactive ports in all port channels.
if inactive_ports := port_channel_details["inactivePorts"]:
self.result.is_failure(f"{port_channel} - Inactive port(s) - {', '.join(inactive_ports.keys())}")


class VerifyIllegalLACP(AntaTest):
Expand All @@ -347,16 +344,13 @@ class VerifyIllegalLACP(AntaTest):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyIllegalLACP."""
self.result.is_success()
command_output = self.instance_commands[0].json_output
po_with_illegal_lacp: list[dict[str, dict[str, int]]] = []
for portchannel, portchannel_dict in command_output["portChannels"].items():
po_with_illegal_lacp.extend(
{portchannel: interface} for interface, interface_dict in portchannel_dict["interfaces"].items() if interface_dict["illegalRxCount"] != 0
)
if not po_with_illegal_lacp:
self.result.is_success()
else:
self.result.is_failure(f"The following port-channels have received illegal LACP packets on the following ports: {po_with_illegal_lacp}")
for port_channel, port_channel_dict in command_output["portChannels"].items():
for interface, interface_details in port_channel_dict["interfaces"].items():
# Verify that the no illegal LACP packets in all port channels.
if interface_details["illegalRxCount"] != 0:
self.result.is_failure(f"{port_channel} Interface: {interface} - Illegal LACP packets found")


class VerifyLoopbackCount(AntaTest):
Expand Down Expand Up @@ -389,23 +383,20 @@ class Input(AntaTest.Input):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyLoopbackCount."""
self.result.is_success()
command_output = self.instance_commands[0].json_output
loopback_count = 0
down_loopback_interfaces = []
for interface in command_output["interfaces"]:
interface_dict = command_output["interfaces"][interface]
for interface, interface_details in command_output["interfaces"].items():
if "Loopback" in interface:
loopback_count += 1
if not (interface_dict["lineProtocolStatus"] == "up" and interface_dict["interfaceStatus"] == "connected"):
down_loopback_interfaces.append(interface)
if loopback_count == self.inputs.number and len(down_loopback_interfaces) == 0:
self.result.is_success()
else:
self.result.is_failure()
if loopback_count != self.inputs.number:
self.result.is_failure(f"Found {loopback_count} Loopbacks when expecting {self.inputs.number}")
elif len(down_loopback_interfaces) != 0: # pragma: no branch
self.result.is_failure(f"The following Loopbacks are not up: {down_loopback_interfaces}")
if (status := interface_details["lineProtocolStatus"]) != "up":
self.result.is_failure(f"Interface: {interface} - Invalid line protocol status - Expected: up Actual: {status}")

if (status := interface_details["interfaceStatus"]) != "connected":
self.result.is_failure(f"Interface: {interface} - Invalid interface status - Expected: connected Actual: {status}")

if loopback_count != self.inputs.number:
self.result.is_failure(f"Loopback interface(s) count mismatch: Expected {self.inputs.number} Actual: {loopback_count}")


class VerifySVI(AntaTest):
Expand All @@ -430,16 +421,13 @@ class VerifySVI(AntaTest):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifySVI."""
self.result.is_success()
command_output = self.instance_commands[0].json_output
down_svis = []
for interface in command_output["interfaces"]:
interface_dict = command_output["interfaces"][interface]
if "Vlan" in interface and not (interface_dict["lineProtocolStatus"] == "up" and interface_dict["interfaceStatus"] == "connected"):
down_svis.append(interface)
if len(down_svis) == 0:
self.result.is_success()
else:
self.result.is_failure(f"The following SVIs are not up: {down_svis}")
for interface, int_data in command_output["interfaces"].items():
if "Vlan" in interface and (status := int_data["lineProtocolStatus"]) != "up":
self.result.is_failure(f"SVI: {interface} - Invalid line protocol status - Expected: up Actual: {status}")
if "Vlan" in interface and int_data["interfaceStatus"] != "connected":
self.result.is_failure(f"SVI: {interface} - Invalid interface status - Expected: connected Actual: {int_data['interfaceStatus']}")


class VerifyL3MTU(AntaTest):
Expand Down Expand Up @@ -484,8 +472,7 @@ class Input(AntaTest.Input):
@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyL3MTU."""
# Parameter to save incorrect interface settings
wrong_l3mtu_intf: list[dict[str, int]] = []
self.result.is_success()
command_output = self.instance_commands[0].json_output
# Set list of interfaces with specific settings
specific_interfaces: list[str] = []
Expand All @@ -495,14 +482,14 @@ def test(self) -> None:
for interface, values in command_output["interfaces"].items():
if re.findall(r"[a-z]+", interface, re.IGNORECASE)[0] not in self.inputs.ignored_interfaces and values["forwardingModel"] == "routed":
if interface in specific_interfaces:
wrong_l3mtu_intf.extend({interface: values["mtu"]} for custom_data in self.inputs.specific_mtu if values["mtu"] != custom_data[interface])
invalid_mtu = next(
(values["mtu"] for custom_data in self.inputs.specific_mtu if values["mtu"] != (expected_mtu := custom_data[interface])), None
)
if invalid_mtu:
self.result.is_failure(f"Interface: {interface} - Incorrect MTU - Expected: {expected_mtu} Actual: {invalid_mtu}")
# Comparison with generic setting
elif values["mtu"] != self.inputs.mtu:
wrong_l3mtu_intf.append({interface: values["mtu"]})
if wrong_l3mtu_intf:
self.result.is_failure(f"Some interfaces do not have correct MTU configured:\n{wrong_l3mtu_intf}")
else:
self.result.is_success()
self.result.is_failure(f"Interface: {interface} - Incorrect MTU - Expected: {self.inputs.mtu} Actual: {values['mtu']}")


class VerifyIPProxyARP(AntaTest):
Expand Down
98 changes: 91 additions & 7 deletions tests/units/anta_tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@
},
],
"inputs": None,
"expected": {"result": "failure", "messages": ["The following port-channels have inactive port(s): ['Port-Channel42']"]},
"expected": {"result": "failure", "messages": ["Port-Channel42 - Inactive port(s) - Ethernet8"]},
},
{
"name": "success",
Expand Down Expand Up @@ -1360,7 +1360,7 @@
"inputs": None,
"expected": {
"result": "failure",
"messages": ["The following port-channels have received illegal LACP packets on the following ports: [{'Port-Channel42': 'Ethernet8'}]"],
"messages": ["Port-Channel42 Interface: Ethernet8 - Illegal LACP packets found"],
},
},
{
Expand Down Expand Up @@ -1415,7 +1415,7 @@
},
"Loopback666": {
"name": "Loopback666",
"interfaceStatus": "connected",
"interfaceStatus": "notconnect",
"interfaceAddress": {"ipAddr": {"maskLen": 32, "address": "6.6.6.6"}},
"ipv4Routable240": False,
"lineProtocolStatus": "down",
Expand All @@ -1425,7 +1425,13 @@
},
],
"inputs": {"number": 2},
"expected": {"result": "failure", "messages": ["The following Loopbacks are not up: ['Loopback666']"]},
"expected": {
"result": "failure",
"messages": [
"Interface: Loopback666 - Invalid line protocol status - Expected: up Actual: down",
"Interface: Loopback666 - Invalid interface status - Expected: connected Actual: notconnect",
],
},
},
{
"name": "failure-count-loopback",
Expand All @@ -1445,7 +1451,7 @@
},
],
"inputs": {"number": 2},
"expected": {"result": "failure", "messages": ["Found 1 Loopbacks when expecting 2"]},
"expected": {"result": "failure", "messages": ["Loopback interface(s) count mismatch: Expected 2 Actual: 1"]},
},
{
"name": "success",
Expand Down Expand Up @@ -1485,7 +1491,13 @@
},
],
"inputs": None,
"expected": {"result": "failure", "messages": ["The following SVIs are not up: ['Vlan42']"]},
"expected": {
"result": "failure",
"messages": [
"SVI: Vlan42 - Invalid line protocol status - Expected: up Actual: lowerLayerDown",
"SVI: Vlan42 - Invalid interface status - Expected: connected Actual: notconnect",
],
},
},
{
"name": "success",
Expand Down Expand Up @@ -1701,7 +1713,79 @@
},
],
"inputs": {"mtu": 1500},
"expected": {"result": "failure", "messages": ["Some interfaces do not have correct MTU configured:\n[{'Ethernet2': 1600}]"]},
"expected": {"result": "failure", "messages": ["Interface: Ethernet2 - Incorrect MTU - Expected: 1500 Actual: 1600"]},
},
{
"name": "failure-specified-interface-mtu",
"test": VerifyL3MTU,
"eos_data": [
{
"interfaces": {
"Ethernet2": {
"name": "Ethernet2",
"forwardingModel": "routed",
"lineProtocolStatus": "up",
"interfaceStatus": "connected",
"hardware": "ethernet",
"mtu": 1500,
"l3MtuConfigured": True,
"l2Mru": 0,
},
"Ethernet10": {
"name": "Ethernet10",
"forwardingModel": "routed",
"lineProtocolStatus": "up",
"interfaceStatus": "connected",
"hardware": "ethernet",
"mtu": 1502,
"l3MtuConfigured": False,
"l2Mru": 0,
},
"Management0": {
"name": "Management0",
"forwardingModel": "routed",
"lineProtocolStatus": "up",
"interfaceStatus": "connected",
"hardware": "ethernet",
"mtu": 1500,
"l3MtuConfigured": False,
"l2Mru": 0,
},
"Port-Channel2": {
"name": "Port-Channel2",
"forwardingModel": "bridged",
"lineProtocolStatus": "lowerLayerDown",
"interfaceStatus": "notconnect",
"hardware": "portChannel",
"mtu": 1500,
"l3MtuConfigured": False,
"l2Mru": 0,
},
"Loopback0": {
"name": "Loopback0",
"forwardingModel": "routed",
"lineProtocolStatus": "up",
"interfaceStatus": "connected",
"hardware": "loopback",
"mtu": 65535,
"l3MtuConfigured": False,
"l2Mru": 0,
},
"Vxlan1": {
"name": "Vxlan1",
"forwardingModel": "bridged",
"lineProtocolStatus": "down",
"interfaceStatus": "notconnect",
"hardware": "vxlan",
"mtu": 0,
"l3MtuConfigured": False,
"l2Mru": 0,
},
},
},
],
"inputs": {"mtu": 1500, "ignored_interfaces": ["Loopback", "Port-Channel", "Management", "Vxlan"], "specific_mtu": [{"Ethernet10": 1501}]},
"expected": {"result": "failure", "messages": ["Interface: Ethernet10 - Incorrect MTU - Expected: 1501 Actual: 1502"]},
},
{
"name": "success",
Expand Down

0 comments on commit 2672570

Please sign in to comment.