From d21e44354d0c4a84c90ebce70444a87ff04a060c Mon Sep 17 00:00:00 2001 From: vitthalmagadum <122079046+vitthalmagadum@users.noreply.github.com> Date: Wed, 8 Jan 2025 18:52:41 +0530 Subject: [PATCH] refactor(anta): Refactor the VerifyBFDPeersIntervals test to add detection time (#858) --- anta/input_models/bfd.py | 4 + anta/tests/bfd.py | 9 +++ examples/tests.yaml | 1 + tests/units/anta_tests/test_bfd.py | 119 ++++++++++++++++++++++++++++- 4 files changed, 131 insertions(+), 2 deletions(-) diff --git a/anta/input_models/bfd.py b/anta/input_models/bfd.py index 2d057d670..06838d0e7 100644 --- a/anta/input_models/bfd.py +++ b/anta/input_models/bfd.py @@ -31,6 +31,10 @@ class BFDPeer(BaseModel): """Multiplier of BFD peer. Required field in the `VerifyBFDPeersIntervals` test.""" protocols: list[BfdProtocol] | None = None """List of protocols to be verified. Required field in the `VerifyBFDPeersRegProtocols` test.""" + detection_time: int | None = None + """Detection time of BFD peer in milliseconds. Defines how long to wait without receiving BFD packets before declaring the peer session as down. + + Optional field in the `VerifyBFDPeersIntervals` test.""" def __str__(self) -> str: """Return a human-readable string representation of the BFDPeer for reporting.""" diff --git a/anta/tests/bfd.py b/anta/tests/bfd.py index a27a786cf..861a6a2e4 100644 --- a/anta/tests/bfd.py +++ b/anta/tests/bfd.py @@ -99,15 +99,18 @@ class VerifyBFDPeersIntervals(AntaTest): 1. Confirms that the specified VRF is configured. 2. Verifies that the peer exists in the BFD configuration. 3. Confirms that BFD peer is correctly configured with the `Transmit interval, Receive interval and Multiplier`. + 4. Verifies that BFD peer is correctly configured with the `Detection time`, if provided. Expected Results ---------------- * Success: If all of the following conditions are met: - All specified peers are found in the BFD configuration within the specified VRF. - All BFD peers are correctly configured with the `Transmit interval, Receive interval and Multiplier`. + - If provided, the `Detection time` is correctly configured. * Failure: If any of the following occur: - A specified peer is not found in the BFD configuration within the specified VRF. - Any BFD peer not correctly configured with the `Transmit interval, Receive interval and Multiplier`. + - Any BFD peer is not correctly configured with `Detection time`, if provided. Examples -------- @@ -125,6 +128,7 @@ class VerifyBFDPeersIntervals(AntaTest): tx_interval: 1200 rx_interval: 1200 multiplier: 3 + detection_time: 3600 ``` """ @@ -151,6 +155,7 @@ def test(self) -> None: tx_interval = bfd_peer.tx_interval rx_interval = bfd_peer.rx_interval multiplier = bfd_peer.multiplier + detect_time = bfd_peer.detection_time # Check if BFD peer configured bfd_output = get_value( @@ -166,6 +171,7 @@ def test(self) -> None: bfd_details = bfd_output.get("peerStatsDetail", {}) op_tx_interval = bfd_details.get("operTxInterval") // 1000 op_rx_interval = bfd_details.get("operRxInterval") // 1000 + op_detection_time = bfd_details.get("detectTime") // 1000 detect_multiplier = bfd_details.get("detectMult") if op_tx_interval != tx_interval: @@ -177,6 +183,9 @@ def test(self) -> None: if detect_multiplier != multiplier: self.result.is_failure(f"{bfd_peer} - Incorrect Multiplier - Expected: {multiplier} Actual: {detect_multiplier}") + if detect_time and op_detection_time != detect_time: + self.result.is_failure(f"{bfd_peer} - Incorrect Detection Time - Expected: {detect_time} Actual: {op_detection_time}") + class VerifyBFDPeersHealth(AntaTest): """Verifies the health of IPv4 BFD peers across all VRFs. diff --git a/examples/tests.yaml b/examples/tests.yaml index a4bc1fabf..77b534a74 100644 --- a/examples/tests.yaml +++ b/examples/tests.yaml @@ -87,6 +87,7 @@ anta.tests.bfd: tx_interval: 1200 rx_interval: 1200 multiplier: 3 + detection_time: 3600 - VerifyBFDPeersRegProtocols: # Verifies the registered routing protocol of IPv4 BFD peer sessions. bfd_peers: diff --git a/tests/units/anta_tests/test_bfd.py b/tests/units/anta_tests/test_bfd.py index af1329f94..8b234222f 100644 --- a/tests/units/anta_tests/test_bfd.py +++ b/tests/units/anta_tests/test_bfd.py @@ -27,6 +27,7 @@ "operTxInterval": 1200000, "operRxInterval": 1200000, "detectMult": 3, + "detectTime": 3600000, } } } @@ -42,6 +43,7 @@ "operTxInterval": 1200000, "operRxInterval": 1200000, "detectMult": 3, + "detectTime": 3600000, } } } @@ -59,6 +61,55 @@ }, "expected": {"result": "success"}, }, + { + "name": "success-detection-time", + "test": VerifyBFDPeersIntervals, + "eos_data": [ + { + "vrfs": { + "default": { + "ipv4Neighbors": { + "192.0.255.7": { + "peerStats": { + "": { + "peerStatsDetail": { + "operTxInterval": 1200000, + "operRxInterval": 1200000, + "detectMult": 3, + "detectTime": 3600000, + } + } + } + } + } + }, + "MGMT": { + "ipv4Neighbors": { + "192.0.255.70": { + "peerStats": { + "": { + "peerStatsDetail": { + "operTxInterval": 1200000, + "operRxInterval": 1200000, + "detectMult": 3, + "detectTime": 3600000, + } + } + } + } + } + }, + } + } + ], + "inputs": { + "bfd_peers": [ + {"peer_address": "192.0.255.7", "vrf": "default", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3, "detection_time": 3600}, + {"peer_address": "192.0.255.70", "vrf": "MGMT", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3, "detection_time": 3600}, + ] + }, + "expected": {"result": "success"}, + }, { "name": "failure-no-peer", "test": VerifyBFDPeersIntervals, @@ -74,6 +125,7 @@ "operTxInterval": 1200000, "operRxInterval": 1200000, "detectMult": 3, + "detectTime": 3600000, } } } @@ -89,6 +141,7 @@ "operTxInterval": 1200000, "operRxInterval": 1200000, "detectMult": 3, + "detectTime": 3600000, } } } @@ -100,8 +153,8 @@ ], "inputs": { "bfd_peers": [ - {"peer_address": "192.0.255.7", "vrf": "CS", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3}, - {"peer_address": "192.0.255.70", "vrf": "MGMT", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3}, + {"peer_address": "192.0.255.7", "vrf": "CS", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3, "detection_time": 3600}, + {"peer_address": "192.0.255.70", "vrf": "MGMT", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3, "detection_time": 3600}, ] }, "expected": { @@ -127,6 +180,7 @@ "operTxInterval": 1300000, "operRxInterval": 1200000, "detectMult": 4, + "detectTime": 4000000, } } } @@ -142,6 +196,7 @@ "operTxInterval": 120000, "operRxInterval": 120000, "detectMult": 5, + "detectTime": 4000000, } } } @@ -168,6 +223,66 @@ ], }, }, + { + "name": "failure-incorrect-timers-with-detection-time", + "test": VerifyBFDPeersIntervals, + "eos_data": [ + { + "vrfs": { + "default": { + "ipv4Neighbors": { + "192.0.255.7": { + "peerStats": { + "": { + "peerStatsDetail": { + "operTxInterval": 1300000, + "operRxInterval": 1200000, + "detectMult": 4, + "detectTime": 4000000, + } + } + } + } + } + }, + "MGMT": { + "ipv4Neighbors": { + "192.0.255.70": { + "peerStats": { + "": { + "peerStatsDetail": { + "operTxInterval": 120000, + "operRxInterval": 120000, + "detectMult": 5, + "detectTime": 4000000, + } + } + } + } + } + }, + } + } + ], + "inputs": { + "bfd_peers": [ + {"peer_address": "192.0.255.7", "vrf": "default", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3, "detection_time": 3600}, + {"peer_address": "192.0.255.70", "vrf": "MGMT", "tx_interval": 1200, "rx_interval": 1200, "multiplier": 3, "detection_time": 3600}, + ] + }, + "expected": { + "result": "failure", + "messages": [ + "Peer: 192.0.255.7 VRF: default - Incorrect Transmit interval - Expected: 1200 Actual: 1300", + "Peer: 192.0.255.7 VRF: default - Incorrect Multiplier - Expected: 3 Actual: 4", + "Peer: 192.0.255.7 VRF: default - Incorrect Detection Time - Expected: 3600 Actual: 4000", + "Peer: 192.0.255.70 VRF: MGMT - Incorrect Transmit interval - Expected: 1200 Actual: 120", + "Peer: 192.0.255.70 VRF: MGMT - Incorrect Receive interval - Expected: 1200 Actual: 120", + "Peer: 192.0.255.70 VRF: MGMT - Incorrect Multiplier - Expected: 3 Actual: 5", + "Peer: 192.0.255.70 VRF: MGMT - Incorrect Detection Time - Expected: 3600 Actual: 4000", + ], + }, + }, { "name": "success", "test": VerifyBFDSpecificPeers,