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 for verify BGP peer group of the BGP IPv4 peer(s) #815

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
74 changes: 74 additions & 0 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1620,3 +1620,77 @@ def test(self) -> None:
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}")


class VerifyBGPPeerGroup(AntaTest):
"""Verifies BGP peer group of the BGP IPv4 peer(s).

Expected Results
----------------
* Success: The test will pass if the peer group is correctly assigned to the BGP peer(s).
* Failure: The test will fail if the BGP peer group not correctly assigned or peer is not configured.

Examples
--------
```yaml
anta.tests.routing:
bgp:
- VerifyBGPPeerGroup:
bgp_peers:
- peer_address: 172.30.11.1
vrf: default
peer_group: IPv4-UNDERLAY-PEERS
```
"""

name = "VerifyBGPPeerGroup"
description = "Verifies BGP peer group of the 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 VerifyBGPPeerGroup 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`."""
peer_group: str
"""The name of the peer group, BGP neighbor is associated with."""

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 VerifyBGPPeerGroup."""
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
peer_group = input_entry.peer_group

# 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

if (actual_peer_group := peer_detail.get("peerGroupName")) != peer_group:
failure_log = f"Expected `{peer_group}` as the configured peer-group, but found `{actual_peer_group}` instead."
if not actual_peer_group:
failure_log = "Peer-group not configured."
failures[peer] = {vrf: failure_log}

# 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 have incorrect peer-group configured:\n{failures}")
19 changes: 18 additions & 1 deletion examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,23 @@ anta.tests.routing:
vrf: default
maximum_routes: 12000
warning_limit: 10000
- VerifyBGPPeerGroup:
bgp_peers:
- peer_address: 10.100.0.8
vrf: default
peer_group: IPv4-UNDERLAY-PEERS
- peer_address: 10.100.0.10
vrf: MGMT
peer_group: IPv4-UNDERLAY-PEERS
- peer_address: 10.100.1.1
vrf: default
peer_group: EVPN-OVERLAY-PEERS
- peer_address: 10.100.1.2
vrf: MGMT
peer_group: EVPN-OVERLAY-PEERS
- peer_address: 10.100.4.5
vrf: default
peer_group: MLAG-IPv4-UNDERLAY-PEER
ospf:
- VerifyOSPFNeighborState:
- VerifyOSPFNeighborCount:
Expand Down Expand Up @@ -678,4 +695,4 @@ anta.tests.routing:
- endpoint: 1.0.0.14/32
vias:
- type: ip
nexthop: 1.1.1.1
nexthop: 1.1.1.1
Copy link
Contributor

Choose a reason for hiding this comment

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

please fix the double blank line space

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated thanks!!

Loading
Loading