Skip to content

Commit

Permalink
feat(anta): Added test case to verify maximum total number of routes …
Browse files Browse the repository at this point in the history
…configured and optionally verifies the warning limit (#798)
  • Loading branch information
vitthalmagadum authored Sep 17, 2024
1 parent 900ebb7 commit 345bdb9
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 0 deletions.
86 changes: 86 additions & 0 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,3 +1534,89 @@ def test(self) -> None:
self.result.is_failure(
f"The following BGP peers are not configured or has an incorrect or missing route map in either the inbound or outbound direction:\n{failures}"
)


class VerifyBGPPeerRouteLimit(AntaTest):
"""Verifies the maximum routes and optionally verifies the maximum routes warning limit for the provided BGP IPv4 peer(s).
Expected Results
----------------
* Success: The test will pass if the BGP peer's maximum routes and, if provided, the maximum routes warning limit are equal to the given limits.
* Failure: The test will fail if the BGP peer's maximum routes do not match the given limit, or if the maximum routes warning limit is provided
and does not match the given limit, or if the peer is not configured.
Examples
--------
```yaml
anta.tests.routing:
bgp:
- VerifyBGPPeerRouteLimit:
bgp_peers:
- peer_address: 172.30.11.1
vrf: default
maximum_routes: 12000
warning_limit: 10000
```
"""

name = "VerifyBGPPeerRouteLimit"
description = "Verifies maximum routes and maximum routes warning limit for the provided BGP IPv4 peer(s)."
categories: ClassVar[list[str]] = ["bgp"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show bgp neighbors {peer} vrf {vrf}", revision=3)]

class Input(AntaTest.Input):
"""Input model for the VerifyBGPPeerRouteLimit test."""

bgp_peers: list[BgpPeer]
"""List of BGP peers"""

class BgpPeer(BaseModel):
"""Model for 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`."""
maximum_routes: int = Field(ge=0, le=4294967294)
"""The maximum allowable number of BGP routes, `0` means unlimited."""
warning_limit: int = Field(default=0, ge=0, le=4294967294)
"""Optional maximum routes warning limit. If not provided, it defaults to `0` meaning no warning limit."""

def render(self, template: AntaTemplate) -> list[AntaCommand]:
"""Render the template for each BGP peer in the input list."""
return [template.render(peer=str(bgp_peer.peer_address), vrf=bgp_peer.vrf) for bgp_peer in self.inputs.bgp_peers]

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyBGPPeerRouteLimit."""
failures: dict[Any, Any] = {}

for command, input_entry in zip(self.instance_commands, self.inputs.bgp_peers):
peer = str(input_entry.peer_address)
vrf = input_entry.vrf
maximum_routes = input_entry.maximum_routes
warning_limit = input_entry.warning_limit
failure: dict[Any, Any] = {}

# Verify BGP peer.
if not (peer_list := get_value(command.json_output, f"vrfs.{vrf}.peerList")) or (peer_detail := get_item(peer_list, "peerAddress", peer)) is None:
failures[peer] = {vrf: "Not configured"}
continue

# Verify maximum routes configured.
if (actual_routes := peer_detail.get("maxTotalRoutes", "Not Found")) != maximum_routes:
failure["Maximum total routes"] = actual_routes

# Verify warning limit if given.
if warning_limit and (actual_warning_limit := peer_detail.get("totalRoutesWarnLimit", "Not Found")) != warning_limit:
failure["Warning limit"] = actual_warning_limit

# Updated failures if any.
if failure:
failures[peer] = {vrf: failure}

# Check if any failures
if not failures:
self.result.is_success()
else:
self.result.is_failure(f"The following BGP peer(s) are not configured or maximum routes and maximum routes warning limit is not correct:\n{failures}")
6 changes: 6 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ anta.tests.routing:
vrf: default
inbound_route_map: RM-MLAG-PEER-IN
outbound_route_map: RM-MLAG-PEER-IN
- VerifyBGPPeerRouteLimit:
bgp_peers:
- peer_address: 10.100.0.8
vrf: default
maximum_routes: 12000
warning_limit: 10000
ospf:
- VerifyOSPFNeighborState:
- VerifyOSPFNeighborCount:
Expand Down
158 changes: 158 additions & 0 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
VerifyBGPPeerDropStats,
VerifyBGPPeerMD5Auth,
VerifyBGPPeerMPCaps,
VerifyBGPPeerRouteLimit,
VerifyBGPPeerRouteRefreshCap,
VerifyBGPPeersHealth,
VerifyBGPPeerUpdateErrors,
Expand Down Expand Up @@ -4705,4 +4706,161 @@
],
},
},
{
"name": "success",
"test": VerifyBGPPeerRouteLimit,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "10.100.0.8",
"maxTotalRoutes": 12000,
"totalRoutesWarnLimit": 10000,
}
]
},
},
},
{
"vrfs": {
"MGMT": {
"peerList": [
{
"peerAddress": "10.100.0.9",
"maxTotalRoutes": 10000,
"totalRoutesWarnLimit": 9000,
}
]
},
},
},
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default", "maximum_routes": 12000, "warning_limit": 10000},
{"peer_address": "10.100.0.9", "vrf": "MGMT", "maximum_routes": 10000},
]
},
"expected": {"result": "success"},
},
{
"name": "failure-peer-not-found",
"test": VerifyBGPPeerRouteLimit,
"eos_data": [
{
"vrfs": {
"default": {},
},
},
{
"vrfs": {
"MGMT": {},
},
},
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default", "maximum_routes": 12000, "warning_limit": 10000},
{"peer_address": "10.100.0.9", "vrf": "MGMT", "maximum_routes": 10000, "warning_limit": 9000},
]
},
"expected": {
"result": "failure",
"messages": [
"The following BGP peer(s) are not configured or maximum routes and maximum routes warning limit is not correct:\n"
"{'10.100.0.8': {'default': 'Not configured'}, '10.100.0.9': {'MGMT': 'Not configured'}}"
],
},
},
{
"name": "failure-incorrect-max-routes",
"test": VerifyBGPPeerRouteLimit,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "10.100.0.8",
"maxTotalRoutes": 13000,
"totalRoutesWarnLimit": 11000,
}
]
},
},
},
{
"vrfs": {
"MGMT": {
"peerList": [
{
"peerAddress": "10.100.0.9",
"maxTotalRoutes": 11000,
"totalRoutesWarnLimit": 10000,
}
]
},
},
},
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default", "maximum_routes": 12000, "warning_limit": 10000},
{"peer_address": "10.100.0.9", "vrf": "MGMT", "maximum_routes": 10000, "warning_limit": 9000},
]
},
"expected": {
"result": "failure",
"messages": [
"The following BGP peer(s) are not configured or maximum routes and maximum routes warning limit is not correct:\n"
"{'10.100.0.8': {'default': {'Maximum total routes': 13000, 'Warning limit': 11000}}, "
"'10.100.0.9': {'MGMT': {'Maximum total routes': 11000, 'Warning limit': 10000}}}"
],
},
},
{
"name": "failure-routes-not-found",
"test": VerifyBGPPeerRouteLimit,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "10.100.0.8",
"maxTotalRoutes": 12000,
}
]
},
},
},
{
"vrfs": {
"MGMT": {
"peerList": [
{
"peerAddress": "10.100.0.9",
}
]
},
},
},
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default", "maximum_routes": 12000, "warning_limit": 10000},
{"peer_address": "10.100.0.9", "vrf": "MGMT", "maximum_routes": 10000, "warning_limit": 9000},
]
},
"expected": {
"result": "failure",
"messages": [
"The following BGP peer(s) are not configured or maximum routes and maximum routes warning limit is not correct:\n"
"{'10.100.0.8': {'default': {'Warning limit': 'Not Found'}}, "
"'10.100.0.9': {'MGMT': {'Maximum total routes': 'Not Found', 'Warning limit': 'Not Found'}}}"
],
},
},
]

0 comments on commit 345bdb9

Please sign in to comment.