Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anta): Added the test case to verify BGP NLRI prefixes #792

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,3 +1333,85 @@ def test(self) -> None:
# Verify warning limit if provided. By default, EOS does not have a warning limit and `totalRoutesWarnLimit` is not present in the output.
if warning_limit is not None and (actual_warning_limit := peer_data.get("totalRoutesWarnLimit", 0)) != warning_limit:
self.result.is_failure(f"{peer} - Maximum routes warning limit mismatch - Expected: {warning_limit}, Actual: {actual_warning_limit}")


class VerifyBGPPeerPrefixes(AntaTest):
"""Verifies the consistency of IPv4 prefixes in a BGP session.

This test performs the following checks for each specified address family:

1. Confirms that the specified VRF is configured.
2. Confirms all received prefixes were accepted.

Expected Results
----------------
* Success: If the `PfxRcd` equals `PfxAcc`, indicating that all received prefixes were accepted.
* Failure: If any of the following occur:
- The specified VRF is not configured.
- The `PfxRcd` is not equal to `PfxAcc`, indicating that some prefix(s) were rejected/filtered out or peers are not configured.

Examples
--------
```yaml
anta.tests.routing:
bgp:
- VerifyBGPPeerPrefixes:
address_families:
- afi: ipv4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have to check for specific peer for specific AFI/SAFI. Please recheck.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks!

safi: unicast
peers:
- 10.100.0.8
- 10.100.0.10
- afi: ipv4
safi: multicast
- afi: evpn
- afi: vpn-ipv4
- afi: ipv4
safi: labeled-unicast
```
"""

categories: ClassVar[list[str]] = ["bgp"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bgp summary vrf all", revision=1)]

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

address_families: list[BgpAddressFamily]
"""List of BGP address."""

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyBGPPeerPrefixes."""
self.result.is_success()

output = self.instance_commands[0].json_output
for address_family in self.inputs.address_families:
# Check if the VRF is configured
vrf_output = get_value(output, f"vrfs.{address_family.vrf}")
if vrf_output is None:
self.result.is_failure(f"{address_family} - VRF not configured")
continue

peers_data = vrf_output.get("peers", {})
eos_key = address_family.eos_key

# Determine peers to check, all peers if not explicitly provided.
peers_to_check = (
{peer: details[eos_key] for peer, details in peers_data.items() if eos_key in details}
if not address_family.peers
else {peer: get_value(peers_data, f"{peer}..{eos_key}", separator="..") for peer in address_family.peers}
)

if not peers_to_check:
self.result.is_failure(f"{address_family} - None of the peers are negotiated")
continue

# Verify peer details
for peer, peer_detail in peers_to_check.items():
if not peer_detail:
self.result.is_failure(f"{address_family} Peer: {peer} - Not negotiated")
continue

if (received := peer_detail.get("nlrisReceived")) != (accepted := peer_detail.get("nlrisAccepted")):
self.result.is_failure(f"{address_family} Peer: {peer} - prefixes are not consistent - Accepted: {accepted} Received: {received}")
14 changes: 14 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ anta.tests.routing.bgp:
strict: False
capabilities:
- ipv4Unicast
- VerifyBGPPeerPrefixes:
# Verifies the consistency of IPv4 prefixes in a BGP session.
address_families:
- afi: ipv4
safi: unicast
peers:
- 10.100.0.8
- 10.100.0.10
- afi: ipv4
safi: multicast
- afi: evpn
- afi: vpn-ipv4
- afi: ipv4
safi: labeled-unicast
- VerifyBGPPeerRouteLimit:
# Verifies maximum routes and warning limit for BGP IPv4 peer(s).
bgp_peers:
Expand Down
Loading
Loading