From 40f4c6aa703734b59106c4d362566c2f7e38529d Mon Sep 17 00:00:00 2001 From: Mahesh Kumar Date: Wed, 3 Jan 2024 09:06:32 +0000 Subject: [PATCH] issue-509: Added testcase to verify bgp timers --- anta/tests/routing/bgp.py | 66 +++++++++- examples/tests.yaml | 10 ++ tests/units/anta_tests/routing/test_bgp.py | 141 +++++++++++++++++++++ 3 files changed, 216 insertions(+), 1 deletion(-) diff --git a/anta/tests/routing/bgp.py b/anta/tests/routing/bgp.py index 771ed8f0e..ae4c1f1fe 100644 --- a/anta/tests/routing/bgp.py +++ b/anta/tests/routing/bgp.py @@ -11,7 +11,7 @@ from ipaddress import IPv4Address, IPv4Network, IPv6Address from typing import Any, List, Optional, Union, cast -from pydantic import BaseModel, PositiveInt, model_validator, utils +from pydantic import BaseModel, Field, PositiveInt, model_validator, utils from anta.custom_types import Afi, MultiProtocolCaps, Safi from anta.models import AntaCommand, AntaTemplate, AntaTest @@ -789,3 +789,67 @@ def test(self) -> None: self.result.is_success() else: self.result.is_failure(f"Following BGP peer route refresh capabilities are not found or not ok:\n{failures}") + + +class VerifyBGPTimers(AntaTest): + """ + Verifies if the BGP peers are configured with the correct hold and keep-alive timers in the specified VRF. + Expected results: + * success: The test will pass if the hold and keep-alive timers are correct for BGP peers in the specified VRF. + * failure: The test will fail if BGP peers are not found or hold and keep-alive timers are not correct in the specified VRF. + """ + + name = "VerifyBGPTimers" + description = "Verifies if the BGP peers are configured with the correct hold and keep alive timers in the specified VRF." + categories = ["routing", "bgp"] + commands = [AntaCommand(command="show bgp neighbors vrf all")] + + class Input(AntaTest.Input): + """ + Input parameters for the test. + """ + + bgp_peers: List[BgpPeers] + """List of BGP peers""" + + class BgpPeers(BaseModel): + """ + This class defines the details of a BGP peer. + """ + + peer_address: IPv4Address + """IPv4 address of a BGP peer""" + vrf: str = "default" + """Optional VRF for BGP peer. If not provided, it defaults to `default`.""" + hold_time: int = Field(ge=3, le=7200) + """BGP hold time in seconds""" + keep_alive_time: int = Field(ge=0, le=3600) + """BGP keep-alive time in seconds""" + + @AntaTest.anta_test + def test(self) -> None: + failures: dict[str, Any] = {} + + # Iterate over each bgp peer + for bgp_peer in self.inputs.bgp_peers: + peer_address = str(bgp_peer.peer_address) + vrf = bgp_peer.vrf + hold_time = bgp_peer.hold_time + keep_alive_time = bgp_peer.keep_alive_time + + # Verify BGP peer + if ( + not (bgp_output := get_value(self.instance_commands[0].json_output, f"vrfs.{vrf}.peerList")) + or (bgp_output := get_item(bgp_output, "peerAddress", peer_address)) is None + ): + failures[peer_address] = {vrf: "Not configured"} + continue + + # Verify BGP peer's hold and keep alive timers + if bgp_output.get("holdTime") != hold_time or bgp_output.get("keepaliveTime") != keep_alive_time: + failures[peer_address] = {vrf: {"hold_time": bgp_output.get("holdTime"), "keep_alive_time": bgp_output.get("keepaliveTime")}} + + if not failures: + self.result.is_success() + else: + self.result.is_failure(f"Following BGP peers are not configured or hold and keep-alive timers are not correct:\n{failures}") diff --git a/examples/tests.yaml b/examples/tests.yaml index e395ba872..be146447a 100644 --- a/examples/tests.yaml +++ b/examples/tests.yaml @@ -349,6 +349,16 @@ anta.tests.routing: bgp_peers: - peer_address: 172.30.11.1 vrf: default + - VerifyBGPTimers: + bgp_peers: + - peer_address: 172.30.11.1 + vrf: default + hold_time: 180 + keep_alive_time: 60 + - peer_address: 172.30.11.5 + vrf: default + hold_time: 180 + keep_alive_time: 60 ospf: - VerifyOSPFNeighborState: - VerifyOSPFNeighborCount: diff --git a/tests/units/anta_tests/routing/test_bgp.py b/tests/units/anta_tests/routing/test_bgp.py index b3152690b..603676349 100644 --- a/tests/units/anta_tests/routing/test_bgp.py +++ b/tests/units/anta_tests/routing/test_bgp.py @@ -19,6 +19,7 @@ VerifyBGPPeerRouteRefreshCap, VerifyBGPPeersHealth, VerifyBGPSpecificPeers, + VerifyBGPTimers, ) from tests.lib.anta import test # noqa: F401; pylint: disable=W0611 @@ -2406,4 +2407,144 @@ ], }, }, + { + "name": "success", + "test": VerifyBGPTimers, + "eos_data": [ + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "172.30.11.1", + "holdTime": 180, + "keepaliveTime": 60, + } + ] + }, + "MGMT": { + "peerList": [ + { + "peerAddress": "172.30.11.11", + "holdTime": 180, + "keepaliveTime": 60, + } + ] + }, + } + } + ], + "inputs": { + "bgp_peers": [ + { + "peer_address": "172.30.11.1", + "vrf": "default", + "hold_time": 180, + "keep_alive_time": 60, + }, + { + "peer_address": "172.30.11.11", + "vrf": "MGMT", + "hold_time": 180, + "keep_alive_time": 60, + }, + ] + }, + "expected": {"result": "success"}, + }, + { + "name": "failure-no-peer", + "test": VerifyBGPTimers, + "eos_data": [ + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "172.30.11.1", + "holdTime": 180, + "keepaliveTime": 60, + } + ] + }, + "MGMT": {"peerList": []}, + } + } + ], + "inputs": { + "bgp_peers": [ + { + "peer_address": "172.30.11.1", + "vrf": "MGMT", + "hold_time": 180, + "keep_alive_time": 60, + }, + { + "peer_address": "172.30.11.11", + "vrf": "MGMT", + "hold_time": 180, + "keep_alive_time": 60, + }, + ] + }, + "expected": { + "result": "failure", + "messages": [ + "Following BGP peers are not configured or hold and keep-alive timers are not correct:\n" + "{'172.30.11.1': {'MGMT': 'Not configured'}, '172.30.11.11': {'MGMT': 'Not configured'}}" + ], + }, + }, + { + "name": "failure-not-correct-timers", + "test": VerifyBGPTimers, + "eos_data": [ + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "172.30.11.1", + "holdTime": 160, + "keepaliveTime": 60, + } + ] + }, + "MGMT": { + "peerList": [ + { + "peerAddress": "172.30.11.11", + "holdTime": 120, + "keepaliveTime": 40, + } + ] + }, + } + } + ], + "inputs": { + "bgp_peers": [ + { + "peer_address": "172.30.11.1", + "vrf": "default", + "hold_time": 180, + "keep_alive_time": 60, + }, + { + "peer_address": "172.30.11.11", + "vrf": "MGMT", + "hold_time": 180, + "keep_alive_time": 60, + }, + ] + }, + "expected": { + "result": "failure", + "messages": [ + "Following BGP peers are not configured or hold and keep-alive timers are not correct:\n" + "{'172.30.11.1': {'default': {'hold_time': 160, 'keep_alive_time': 60}}, " + "'172.30.11.11': {'MGMT': {'hold_time': 120, 'keep_alive_time': 40}}}" + ], + }, + }, ]