Skip to content

Commit

Permalink
feat(anta.tests): Adding the test case to verify BGP timers (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaheshGSLAB committed Feb 14, 2024
1 parent c88794d commit b67f186
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 1 deletion.
66 changes: 65 additions & 1 deletion anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 pydantic_extra_types.mac_address import MacAddress

from anta.custom_types import Afi, MultiProtocolCaps, Safi, Vni
Expand Down Expand Up @@ -937,3 +937,67 @@ def test(self) -> None:
self.result.is_success()
else:
self.result.is_failure(f"Following BGP peers are not configured or advertised communities are not standard, extended, and large:\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}")
10 changes: 10 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ anta.tests.routing:
vrf: default
- peer_address: 172.30.11.21
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:
Expand Down
141 changes: 141 additions & 0 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
VerifyBGPPeerRouteRefreshCap,
VerifyBGPPeersHealth,
VerifyBGPSpecificPeers,
VerifyBGPTimers,
VerifyEVPNType2Route,
)
from tests.lib.anta import test # noqa: F401; pylint: disable=W0611
Expand Down Expand Up @@ -3241,4 +3242,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}}}"
],
},
},
]

0 comments on commit b67f186

Please sign in to comment.