From 986944b5252ad23ed33c2b30450cdbbb8e19b51c Mon Sep 17 00:00:00 2001 From: vitthalmagadum Date: Fri, 21 Feb 2025 02:39:24 -0500 Subject: [PATCH] Added negative testing in VerifyReachability --- anta/input_models/connectivity.py | 10 +++--- anta/tests/connectivity.py | 8 ++++- examples/tests.yaml | 1 + tests/units/anta_tests/test_connectivity.py | 38 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/anta/input_models/connectivity.py b/anta/input_models/connectivity.py index f967e775b..4c526c04c 100644 --- a/anta/input_models/connectivity.py +++ b/anta/input_models/connectivity.py @@ -23,13 +23,15 @@ class Host(BaseModel): source: IPv4Address | IPv6Address | Interface """Source address IP or egress interface to use.""" vrf: str = "default" - """VRF context. Defaults to `default`.""" + """VRF context.""" repeat: int = 2 - """Number of ping repetition. Defaults to 2.""" + """Number of ping repetition.""" size: int = 100 - """Specify datagram size. Defaults to 100.""" + """Specify datagram size.""" df_bit: bool = False - """Enable do not fragment bit in IP header. Defaults to False.""" + """Enable do not fragment bit in IP header.""" + expected_unreachable: bool = False + """Indicates whether the expected network should be unreachable.""" def __str__(self) -> str: """Return a human-readable string representation of the Host for reporting. diff --git a/anta/tests/connectivity.py b/anta/tests/connectivity.py index 2fd58c1bb..84d6bc965 100644 --- a/anta/tests/connectivity.py +++ b/anta/tests/connectivity.py @@ -47,6 +47,7 @@ class VerifyReachability(AntaTest): vrf: default df_bit: True size: 100 + expected_unreachable: true ``` """ @@ -89,9 +90,14 @@ def test(self) -> None: self.result.is_success() for command, host in zip(self.instance_commands, self.inputs.hosts): - if f"{host.repeat} received" not in command.json_output["messages"][0]: + # Verifies the network is reachable + if not host.expected_unreachable and f"{host.repeat} received" not in command.json_output["messages"][0]: self.result.is_failure(f"{host} - Unreachable") + # Verifies the network is unreachable. + if host.expected_unreachable and "0 received" not in command.json_output["messages"][0]: + self.result.is_failure(f"{host} - Network is expected to be unreachable but found reachable.") + class VerifyLLDPNeighbors(AntaTest): """Verifies the connection status of the specified LLDP (Link Layer Discovery Protocol) neighbors. diff --git a/examples/tests.yaml b/examples/tests.yaml index f5fd3ebd0..65e5de64b 100644 --- a/examples/tests.yaml +++ b/examples/tests.yaml @@ -140,6 +140,7 @@ anta.tests.connectivity: vrf: default df_bit: True size: 100 + expected_unreachable: true anta.tests.cvx: - VerifyActiveCVXConnections: # Verifies the number of active CVX Connections. diff --git a/tests/units/anta_tests/test_connectivity.py b/tests/units/anta_tests/test_connectivity.py index 86ada5dea..96d39dde5 100644 --- a/tests/units/anta_tests/test_connectivity.py +++ b/tests/units/anta_tests/test_connectivity.py @@ -45,6 +45,23 @@ ], "expected": {"result": "success"}, }, + { + "name": "success-expected-unreachable", + "test": VerifyReachability, + "eos_data": [ + { + "messages": [ + """PING 10.0.0.1 (10.0.0.1) from 10.0.0.5 : 72(100) bytes of data. + + --- 10.0.0.1 ping statistics --- + 2 packets transmitted, 0 received, 100% packet loss, time 10ms + """, + ], + }, + ], + "inputs": {"hosts": [{"destination": "10.0.0.1", "source": "10.0.0.5", "expected_unreachable": True}]}, + "expected": {"result": "success"}, + }, { "name": "success-ipv6", "test": VerifyReachability, @@ -268,6 +285,27 @@ ], "expected": {"result": "failure", "messages": ["Host: 10.0.0.1 Source: Management0 VRF: default - Unreachable"]}, }, + { + "name": "failure-expected-unreachable", + "test": VerifyReachability, + "eos_data": [ + { + "messages": [ + """PING 10.0.0.1 (10.0.0.1) from 10.0.0.5 : 72(100) bytes of data. + 80 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.247 ms + 80 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.072 ms + + --- 10.0.0.1 ping statistics --- + 2 packets transmitted, 2 received, 0% packet loss, time 0ms + rtt min/avg/max/mdev = 0.072/0.159/0.247/0.088 ms, ipg/ewma 0.370/0.225 ms + + """, + ], + }, + ], + "inputs": {"hosts": [{"destination": "10.0.0.1", "source": "10.0.0.5", "expected_unreachable": True}]}, + "expected": {"result": "failure", "messages": ["Host: 10.0.0.1 Source: 10.0.0.5 VRF: default - Network is expected to be unreachable but found reachable."]}, + }, { "name": "success", "test": VerifyLLDPNeighbors,