Skip to content

Commit

Permalink
issue_786 Added TC for BGP NLRIs
Browse files Browse the repository at this point in the history
  • Loading branch information
VitthalMagadum committed Aug 23, 2024
1 parent 61e206e commit 6453725
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
66 changes: 66 additions & 0 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,69 @@ def test(self) -> None:
self.result.is_success()
else:
self.result.is_failure(f"The following BGP peers are not configured or have non-zero update error counters:\n{failures}")


class VerifyBGPPeerNLRIs(AntaTest):
"""Verifies BGP IPv4 peer(s) consistency of NLRIs received and accepted in a BGP session.
Expected Results
----------------
* Success: The test will pass if the `nlrisReceived` equals `nlrisAccepted`, indicating that all received NLRIs were accepted..
* Failure: The test will fail if the `nlrisReceived` is not equal to `nlrisAccepted`, indicating that some NLRIs were rejected or filtered out.
Examples
--------
```yaml
anta.tests.routing:
bgp:
- VerifyBGPPeerNLRIs:
bgp_peers:
- peer_address: 172.30.11.1
vrf: default
```
"""

name = "VerifyBGPPeerNLRIs"
description = "Verifies the NLRIs received and accepted of a BGP IPv4 peer."
categories: ClassVar[list[str]] = ["bgp"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bgp summary", revision=1)]

class Input(AntaTest.Input):
"""Input model for the VerifyBGPPeerNLRIs 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`."""

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyBGPPeerNLRIs."""
failures: dict[Any, 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

if not (peer_details := get_value(self.instance_commands[0].json_output, f"vrfs..{vrf}..peers..{peer_address}", separator="..")):
failures[peer_address] = {vrf: "Not configured"}
continue

# Verifies the NLRIs received is equal to accepted.
if (nlri_rec := get_value(peer_details, "ipv4Unicast.nlrisReceived")) != (nlri_acc := get_value(peer_details, "ipv4Unicast.nlrisAccepted")):
failures[peer_address] = {
vrf: f"The NLRIs received and accepted should be consistent, but found NLRI received `{nlri_rec}` and NLRI accepted `{nlri_acc}` instead."
}

# Check if any failures
if not failures:
self.result.is_success()
else:
self.result.is_failure(f"The following BGP peers are not configured or NLRI(s) received and accepted are not consistent:\n{failures}")
6 changes: 6 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ anta.tests.routing:
update_errors:
- inUpdErrWithdraw
- inUpdErrIgnore
- VerifyBGPPeerNLRIs:
bgp_peers:
- peer_address: 10.100.0.10
vrf: default
- peer_address: 10.100.0.8
vrf: default
ospf:
- VerifyOSPFNeighborState:
- VerifyOSPFNeighborCount:
Expand Down
103 changes: 103 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,
VerifyBGPPeerNLRIs,
VerifyBGPPeerRouteRefreshCap,
VerifyBGPPeersHealth,
VerifyBGPPeerUpdateErrors,
Expand Down Expand Up @@ -4357,4 +4358,106 @@
],
},
},
{
"name": "success",
"test": VerifyBGPPeerNLRIs,
"eos_data": [
{
"vrfs": {
"default": {
"peers": {
"10.100.0.8": {
"peerState": "Established",
"peerAsn": "65100",
"ipv4Unicast": {"afiSafiState": "negotiated", "nlrisReceived": 17, "nlrisAccepted": 17},
},
}
},
"MGMT": {
"peers": {
"10.100.0.10": {
"peerState": "Established",
"peerAsn": "65100",
"ipv4Unicast": {"afiSafiState": "negotiated", "nlrisReceived": 17, "nlrisAccepted": 17},
},
}
},
}
}
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default"},
{"peer_address": "10.100.0.10", "vrf": "MGMT"},
]
},
"expected": {"result": "success"},
},
{
"name": "failure",
"test": VerifyBGPPeerNLRIs,
"eos_data": [
{
"vrfs": {
"default": {
"peers": {
"10.100.0.8": {
"peerState": "Established",
"peerAsn": "65100",
"ipv4Unicast": {"afiSafiState": "negotiated", "nlrisReceived": 20, "nlrisAccepted": 15},
},
}
},
"MGMT": {
"peers": {
"10.100.0.10": {
"peerState": "Established",
"peerAsn": "65100",
"ipv4Unicast": {"afiSafiState": "negotiated", "nlrisReceived": 18, "nlrisAccepted": 17},
},
}
},
}
}
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default"},
{"peer_address": "10.100.0.10", "vrf": "MGMT"},
]
},
"expected": {
"result": "failure",
"messages": [
"The following BGP peers are not configured or NLRI(s) received and accepted are not consistent:\n"
"{'10.100.0.8': {'default': 'The NLRIs received and accepted should be consistent, but found NLRI received `20` and NLRI accepted `15` instead.'}, "
"'10.100.0.10': {'MGMT': 'The NLRIs received and accepted should be consistent, but found NLRI received `18` and NLRI accepted `17` instead.'}}"
],
},
},
{
"name": "failure-not-found",
"test": VerifyBGPPeerNLRIs,
"eos_data": [
{
"vrfs": {
"default": {"peers": {}},
"MGMT": {"peers": {}},
}
}
],
"inputs": {
"bgp_peers": [
{"peer_address": "10.100.0.8", "vrf": "default"},
{"peer_address": "10.100.0.10", "vrf": "MGMT"},
]
},
"expected": {
"result": "failure",
"messages": [
"The following BGP peers are not configured or NLRI(s) received and accepted are not consistent:\n"
"{'10.100.0.8': {'default': 'Not configured'}, '10.100.0.10': {'MGMT': 'Not configured'}}"
],
},
},
]

0 comments on commit 6453725

Please sign in to comment.