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

Merged
merged 17 commits into from
Jan 14, 2025
Merged
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
2 changes: 2 additions & 0 deletions anta/input_models/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class BgpPeer(BaseModel):
"""IPv4 address of the BGP peer."""
vrf: str = "default"
"""Optional VRF for the BGP peer. Defaults to `default`."""
peer_group: str | None = None
"""Peer group of the BGP peer. Required field in the `VerifyBGPPeerGroup` test."""
advertised_routes: list[IPv4Network] | None = None
"""List of advertised routes in CIDR format. Required field in the `VerifyBGPExchangedRoutes` test."""
received_routes: list[IPv4Network] | None = None
Expand Down
73 changes: 71 additions & 2 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ def validate_bgp_peers(cls, bgp_peers: list[T]) -> list[T]:
"""Validate that 'inbound_route_map' or 'outbound_route_map' field is provided in each BGP peer."""
for peer in bgp_peers:
if not (peer.inbound_route_map or peer.outbound_route_map):
msg = f"{peer}; At least one of 'inbound_route_map' or 'outbound_route_map' must be provided."
msg = f"{peer} 'inbound_route_map' or 'outbound_route_map' field missing in the input"
raise ValueError(msg)
return bgp_peers

Expand Down Expand Up @@ -1304,7 +1304,7 @@ def validate_bgp_peers(cls, bgp_peers: list[T]) -> list[T]:
"""Validate that 'maximum_routes' field is provided in each BGP peer."""
for peer in bgp_peers:
if peer.maximum_routes is None:
msg = f"{peer}; 'maximum_routes' field missing in the input"
msg = f"{peer} 'maximum_routes' field missing in the input"
raise ValueError(msg)
return bgp_peers

Expand Down Expand Up @@ -1335,6 +1335,75 @@ def test(self) -> None:
self.result.is_failure(f"{peer} - Maximum routes warning limit mismatch - Expected: {warning_limit}, Actual: {actual_warning_limit}")


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

This test performs the following checks for each specified peer:

1. Verifies that the peer is found in its VRF in the BGP configuration.
2. Confirms the peer group is correctly assigned to the specified BGP peer.

Expected Results
----------------
* Success: If all of the following conditions are met:
- All specified peers are found in the BGP configuration.
- The peer group is correctly assigned to the specified BGP peer.
* Failure: If any of the following occur:
- A specified peer is not found in the BGP configuration.
- The peer group is not correctly assigned to the specified BGP peer.

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

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

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

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

@field_validator("bgp_peers")
@classmethod
def validate_bgp_peers(cls, bgp_peers: list[BgpPeer]) -> list[BgpPeer]:
"""Validate that 'peer_group' field is provided in each BGP peer."""
for peer in bgp_peers:
if peer.peer_group is None:
msg = f"{peer} 'peer_group' field missing in the input"
raise ValueError(msg)
return bgp_peers

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

output = self.instance_commands[0].json_output

for peer in self.inputs.bgp_peers:
peer_ip = str(peer.peer_address)
peer_list = get_value(output, f"vrfs.{peer.vrf}.peerList", default=[])

# Check if the peer is found
if (peer_data := get_item(peer_list, "peerAddress", peer_ip)) is None:
self.result.is_failure(f"{peer} - Not found")
continue

if (actual_peer_group := peer_data.get("peerGroupName", "Not Found")) != peer.peer_group:
self.result.is_failure(f"{peer} - Incorrect peer group configured - Expected: {peer.peer_group} Actual: {actual_peer_group}")


class VerifyBGPPeerSessionRibd(AntaTest):
"""Verifies the session state of BGP IPv4 peer(s).

Expand Down
6 changes: 6 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ anta.tests.routing.bgp:
drop_stats:
- inDropAsloop
- prefixEvpnDroppedUnsupportedRouteType
- VerifyBGPPeerGroup:
# Verifies BGP peer group of BGP IPv4 peer(s).
bgp_peers:
- peer_address: 172.30.11.1
vrf: default
peer_group: IPv4-UNDERLAY-PEERS
- VerifyBGPPeerMD5Auth:
# Verifies the MD5 authentication and state of IPv4 BGP peer(s) in a specified VRF.
bgp_peers:
Expand Down
189 changes: 189 additions & 0 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
VerifyBGPPeerASNCap,
VerifyBGPPeerCount,
VerifyBGPPeerDropStats,
VerifyBGPPeerGroup,
VerifyBGPPeerMD5Auth,
VerifyBGPPeerMPCaps,
VerifyBGPPeerRouteLimit,
Expand Down Expand Up @@ -4050,6 +4051,194 @@ def test_check_bgp_neighbor_capability(input_dict: dict[str, bool], expected: bo
],
},
},
{
"name": "success",
"test": VerifyBGPPeerGroup,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "10.100.0.8",
"peerGroupName": "IPv4-UNDERLAY-PEERS",
},
{
"peerAddress": "10.100.4.5",
"peerGroupName": "MLAG-IPv4-UNDERLAY-PEER",
},
{
"peerAddress": "10.100.1.1",
"peerGroupName": "EVPN-OVERLAY-PEERS",
},
]
},
"MGMT": {
"peerList": [
{
"peerAddress": "10.100.0.10",
"peerGroupName": "IPv4-UNDERLAY-PEERS",
},
{
"peerAddress": "10.100.1.2",
"peerGroupName": "EVPN-OVERLAY-PEERS",
},
]
},
},
},
],
"inputs": {
"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"},
]
},
"expected": {"result": "success"},
},
{
"name": "failure-incorrect-peer-group",
"test": VerifyBGPPeerGroup,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "10.100.0.8",
"peerGroupName": "UNDERLAY-PEERS",
},
{
"peerAddress": "10.100.1.1",
"peerGroupName": "OVERLAY-PEERS",
},
{
"peerAddress": "10.100.4.5",
"peerGroupName": "UNDERLAY-PEER",
},
]
},
"MGMT": {
"peerList": [
{
"peerAddress": "10.100.0.10",
"peerGroupName": "UNDERLAY-PEERS",
},
{
"peerAddress": "10.100.1.2",
"peerGroupName": "OVERLAY-PEERS",
},
]
},
},
},
],
"inputs": {
"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"},
]
},
"expected": {
"result": "failure",
"messages": [
"Peer: 10.100.0.8 VRF: default - Incorrect peer group configured - Expected: IPv4-UNDERLAY-PEERS Actual: UNDERLAY-PEERS",
"Peer: 10.100.0.10 VRF: MGMT - Incorrect peer group configured - Expected: IPv4-UNDERLAY-PEERS Actual: UNDERLAY-PEERS",
"Peer: 10.100.1.1 VRF: default - Incorrect peer group configured - Expected: EVPN-OVERLAY-PEERS Actual: OVERLAY-PEERS",
"Peer: 10.100.1.2 VRF: MGMT - Incorrect peer group configured - Expected: EVPN-OVERLAY-PEERS Actual: OVERLAY-PEERS",
"Peer: 10.100.4.5 VRF: default - Incorrect peer group configured - Expected: MLAG-IPv4-UNDERLAY-PEER Actual: UNDERLAY-PEER",
],
},
},
{
"name": "failure-peers-not-found",
"test": VerifyBGPPeerGroup,
"eos_data": [
{
"vrfs": {
"default": {"peerList": []},
"MGMT": {"peerList": []},
},
},
],
"inputs": {
"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"},
]
},
"expected": {
"result": "failure",
"messages": [
"Peer: 10.100.0.8 VRF: default - Not found",
"Peer: 10.100.0.10 VRF: MGMT - Not found",
"Peer: 10.100.1.1 VRF: default - Not found",
"Peer: 10.100.1.2 VRF: MGMT - Not found",
"Peer: 10.100.4.5 VRF: default - Not found",
],
},
},
{
"name": "failure-peer-group-not-found",
"test": VerifyBGPPeerGroup,
"eos_data": [
{
"vrfs": {
"default": {
"peerList": [
{
"peerAddress": "10.100.0.8",
},
{
"peerAddress": "10.100.1.1",
},
{
"peerAddress": "10.100.4.5",
},
]
},
"MGMT": {
"peerList": [
{
"peerAddress": "10.100.0.10",
},
{
"peerAddress": "10.100.1.2",
},
]
},
},
},
],
"inputs": {
"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"},
]
},
"expected": {
"result": "failure",
"messages": [
"Peer: 10.100.0.8 VRF: default - Incorrect peer group configured - Expected: IPv4-UNDERLAY-PEERS Actual: Not Found",
"Peer: 10.100.0.10 VRF: MGMT - Incorrect peer group configured - Expected: IPv4-UNDERLAY-PEERS Actual: Not Found",
"Peer: 10.100.1.1 VRF: default - Incorrect peer group configured - Expected: EVPN-OVERLAY-PEERS Actual: Not Found",
"Peer: 10.100.1.2 VRF: MGMT - Incorrect peer group configured - Expected: EVPN-OVERLAY-PEERS Actual: Not Found",
"Peer: 10.100.4.5 VRF: default - Incorrect peer group configured - Expected: MLAG-IPv4-UNDERLAY-PEER Actual: Not Found",
],
},
},
{
"name": "success-no-check-tcp-queues",
"test": VerifyBGPPeerSessionRibd,
Expand Down
26 changes: 26 additions & 0 deletions tests/units/input_models/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from anta.tests.routing.bgp import (
VerifyBGPExchangedRoutes,
VerifyBGPPeerCount,
VerifyBGPPeerGroup,
VerifyBGPPeerMPCaps,
VerifyBGPPeerRouteLimit,
VerifyBgpRouteMaps,
Expand Down Expand Up @@ -236,3 +237,28 @@ def test_invalid(self, bgp_peers: list[BgpPeer]) -> None:
"""Test VerifyBGPPeerRouteLimit.Input invalid inputs."""
with pytest.raises(ValidationError):
VerifyBGPPeerRouteLimit.Input(bgp_peers=bgp_peers)


class TestVerifyBGPPeerGroupInput:
"""Test anta.tests.routing.bgp.VerifyBGPPeerGroup.Input."""

@pytest.mark.parametrize(
("bgp_peers"),
[
pytest.param([{"peer_address": "172.30.255.5", "vrf": "default", "peer_group": "IPv4-UNDERLAY-PEERS"}], id="valid"),
],
)
def test_valid(self, bgp_peers: list[BgpPeer]) -> None:
"""Test VerifyBGPPeerGroup.Input valid inputs."""
VerifyBGPPeerGroup.Input(bgp_peers=bgp_peers)

@pytest.mark.parametrize(
("bgp_peers"),
[
pytest.param([{"peer_address": "172.30.255.5", "vrf": "default"}], id="invalid"),
],
)
def test_invalid(self, bgp_peers: list[BgpPeer]) -> None:
"""Test VerifyBGPPeerGroup.Input invalid inputs."""
with pytest.raises(ValidationError):
VerifyBGPPeerGroup.Input(bgp_peers=bgp_peers)
Loading