diff --git a/anta/tests/routing/bgp.py b/anta/tests/routing/bgp.py index a37328608..82483d63a 100644 --- a/anta/tests/routing/bgp.py +++ b/anta/tests/routing/bgp.py @@ -1628,3 +1628,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}") diff --git a/examples/tests.yaml b/examples/tests.yaml index ade4e7640..a80460c29 100644 --- a/examples/tests.yaml +++ b/examples/tests.yaml @@ -623,6 +623,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: diff --git a/tests/units/anta_tests/routing/test_bgp.py b/tests/units/anta_tests/routing/test_bgp.py index e256b04dd..63a622262 100644 --- a/tests/units/anta_tests/routing/test_bgp.py +++ b/tests/units/anta_tests/routing/test_bgp.py @@ -14,6 +14,7 @@ VerifyBGPPeerASNCap, VerifyBGPPeerCount, VerifyBGPPeerDropStats, + VerifyBGPPeerGroup, VerifyBGPPeerMD5Auth, VerifyBGPPeerMPCaps, VerifyBGPPeerRouteLimit, @@ -4836,4 +4837,293 @@ ], }, }, + { + "name": "success", + "test": VerifyBGPPeerGroup, + "eos_data": [ + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.0.8", + "peerGroupName": "IPv4-UNDERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "MGMT": { + "peerList": [ + { + "peerAddress": "10.100.0.10", + "peerGroupName": "IPv4-UNDERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.1.1", + "peerGroupName": "EVPN-OVERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "MGMT": { + "peerList": [ + { + "peerAddress": "10.100.1.2", + "peerGroupName": "EVPN-OVERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.4.5", + "peerGroupName": "MLAG-IPv4-UNDERLAY-PEER", + } + ] + }, + }, + }, + ], + "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", + } + ] + }, + }, + }, + { + "vrfs": { + "MGMT": { + "peerList": [ + { + "peerAddress": "10.100.0.10", + "peerGroupName": "UNDERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.1.1", + "peerGroupName": "OVERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "MGMT": { + "peerList": [ + { + "peerAddress": "10.100.1.2", + "peerGroupName": "OVERLAY-PEERS", + } + ] + }, + }, + }, + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.4.5", + "peerGroupName": "UNDERLAY-PEER", + } + ] + }, + }, + }, + ], + "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": [ + "The following BGP peer(s) are not configured or have incorrect peer-group configured:\n" + "{'10.100.0.8': {'default': 'Expected `IPv4-UNDERLAY-PEERS` as the configured peer-group, but found `UNDERLAY-PEERS` instead.'}, " + "'10.100.0.10': {'MGMT': 'Expected `IPv4-UNDERLAY-PEERS` as the configured peer-group, but found `UNDERLAY-PEERS` instead.'}, " + "'10.100.1.1': {'default': 'Expected `EVPN-OVERLAY-PEERS` as the configured peer-group, but found `OVERLAY-PEERS` instead.'}, " + "'10.100.1.2': {'MGMT': 'Expected `EVPN-OVERLAY-PEERS` as the configured peer-group, but found `OVERLAY-PEERS` instead.'}, " + "'10.100.4.5': {'default': 'Expected `MLAG-IPv4-UNDERLAY-PEER` as the configured peer-group, but found `UNDERLAY-PEER` instead.'}}" + ], + }, + }, + { + "name": "failure-peers-not-found", + "test": VerifyBGPPeerGroup, + "eos_data": [ + { + "vrfs": { + "default": {"peerList": []}, + }, + }, + { + "vrfs": { + "MGMT": {"peerList": []}, + }, + }, + { + "vrfs": { + "default": {"peerList": []}, + }, + }, + { + "vrfs": { + "MGMT": {"peerList": []}, + }, + }, + { + "vrfs": { + "default": {"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": [ + "The following BGP peer(s) are not configured or have incorrect peer-group configured:\n" + "{'10.100.0.8': {'default': 'Not configured'}, '10.100.0.10': {'MGMT': 'Not configured'}, '10.100.1.1': " + "{'default': 'Not configured'}, '10.100.1.2': {'MGMT': 'Not configured'}, '10.100.4.5': {'default': 'Not configured'}}" + ], + }, + }, + { + "name": "failure-peer-group-not-found", + "test": VerifyBGPPeerGroup, + "eos_data": [ + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.0.8", + } + ] + }, + }, + }, + { + "vrfs": { + "MGMT": { + "peerList": [ + { + "peerAddress": "10.100.0.10", + } + ] + }, + }, + }, + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.1.1", + } + ] + }, + }, + }, + { + "vrfs": { + "MGMT": { + "peerList": [ + { + "peerAddress": "10.100.1.2", + } + ] + }, + }, + }, + { + "vrfs": { + "default": { + "peerList": [ + { + "peerAddress": "10.100.4.5", + } + ] + }, + }, + }, + ], + "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": [ + "The following BGP peer(s) are not configured or have incorrect peer-group configured:\n" + "{'10.100.0.8': {'default': 'Peer-group not configured.'}, '10.100.0.10': {'MGMT': 'Peer-group not configured.'}, '10.100.1.1': " + "{'default': 'Peer-group not configured.'}, '10.100.1.2': {'MGMT': 'Peer-group not configured.'}, " + "'10.100.4.5': {'default': 'Peer-group not configured.'}}" + ], + }, + }, ]