diff --git a/anta/input_models/connectivity.py b/anta/input_models/connectivity.py index f967e775b..464d22a51 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.""" + reachable: bool = True + """Indicates whether the destination should be reachable.""" 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..6568d8424 100644 --- a/anta/tests/connectivity.py +++ b/anta/tests/connectivity.py @@ -37,6 +37,7 @@ class VerifyReachability(AntaTest): vrf: MGMT df_bit: True size: 100 + reachable: true - source: Management0 destination: 8.8.8.8 vrf: MGMT @@ -47,6 +48,7 @@ class VerifyReachability(AntaTest): vrf: default df_bit: True size: 100 + reachable: false ``` """ @@ -89,9 +91,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 host.reachable 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 not host.reachable and f"{host.repeat} received" in command.json_output["messages"][0]: + self.result.is_failure(f"{host} - Destination 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..665182231 100644 --- a/examples/tests.yaml +++ b/examples/tests.yaml @@ -130,6 +130,7 @@ anta.tests.connectivity: vrf: MGMT df_bit: True size: 100 + reachable: true - source: Management0 destination: 8.8.8.8 vrf: MGMT @@ -140,6 +141,7 @@ anta.tests.connectivity: vrf: default df_bit: True size: 100 + reachable: false 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..1f5044b04 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", "reachable": False}]}, + "expected": {"result": "success"}, + }, { "name": "success-ipv6", "test": VerifyReachability, @@ -268,6 +285,30 @@ ], "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", "reachable": False}]}, + "expected": { + "result": "failure", + "messages": ["Host: 10.0.0.1 Source: 10.0.0.5 VRF: default - Destination is expected to be unreachable but found reachable."], + }, + }, { "name": "success", "test": VerifyLLDPNeighbors,