Skip to content

Commit

Permalink
issue_786 Handling review comments: updated specific peer check
Browse files Browse the repository at this point in the history
  • Loading branch information
VitthalMagadum committed Sep 4, 2024
1 parent 2858d06 commit de886f7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 15 deletions.
74 changes: 59 additions & 15 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ def _add_bgp_routes_failure(
return failure_routes


def _get_inconsistent_peers(peer_details: dict[Any, Any], bgp_peers: list[IPv4Address] | None) -> dict[Any, Any]:
"""Identify BGP peers with inconsistency of prefix(s) received and accepted in a BGP session.
bgp_peers: list of IPv4 address of a BGP peer to be verified. If not provided, test will verifies all the BGP peers.
Parameters
----------
peer_details: The BGP peer data dictionary.
bgp_peers: The list of IPv4 address of a BGP peer(s) to be verified.
Returns
-------
dict[Any, Any]: A dictionary containing the BGP peer(s) with inconsistent prefix(s).
"""
failure: dict[Any, Any] = {}

# Update the peer details as per input peer addresses for verification.
if bgp_peers:
details: dict[Any, Any] = {}
for peer in bgp_peers:
if not (peer_detail := peer_details.get(str(peer))):
failure[str(peer)] = "Not Configured"
else:
details[str(peer)] = peer_detail
peer_details = details

# Iterate over each peer and verify the prefix(s) consistency.
for peer, peer_detail in peer_details.items():
prefix_rcv = peer_detail.get("prefixReceived", "Not Found")
prefix_acc = peer_detail.get("prefixAccepted", "Not Found")
if (prefix_acc and prefix_rcv) == "Not Found":
failure[peer] = {"prefix received": prefix_rcv, "prefix accepted": prefix_acc}
continue
if prefix_rcv != prefix_acc:
failure[peer] = {"prefix received": prefix_rcv, "prefix accepted": prefix_acc}

return failure


class VerifyBGPPeerCount(AntaTest):
"""Verifies the count of BGP peers for a given address family.
Expand Down Expand Up @@ -1411,8 +1451,8 @@ class VerifyBGPPeerPrefixes(AntaTest):
Expected Results
----------------
* Success: The test will pass if the `prefixReceived` equals `prefixAccepted`, indicating that all received prefix(s) were accepted.
* Failure: The test will fail if the `prefixReceived` is not equal to `prefixAccepted`, indicating that some prefix(s) were rejected/filtered out or
* Success: The test will pass if the `PfxRcd` equals `PfxAcc`, indicating that all received prefix(s) were accepted.
* Failure: The test will fail if the `PfxRcd` is not equal to `PfxAcc`, indicating that some prefix(s) were rejected/filtered out or
peer(s) are not configured.
Examples
Expand All @@ -1424,6 +1464,9 @@ class VerifyBGPPeerPrefixes(AntaTest):
address_families:
- afi: ipv4
safi: unicast
peers:
- 10.100.0.8
- 10.100.0.10
- afi: ipv4
safi: multicast
- afi: evpn
Expand Down Expand Up @@ -1463,6 +1506,8 @@ class BgpAfi(BaseModel):
If the input `afi` is not `ipv4` or `ipv6`, e.g. `evpn`, `vrf` must be `default`.
"""
peers: list[IPv4Address] | None = None
"""Optional list of IPv4 address of a BGP peer to be verified. If not provided, test will verifies all the BGP peers."""

@model_validator(mode="after")
def validate_inputs(self: BaseModel) -> BaseModel:
Expand Down Expand Up @@ -1503,11 +1548,12 @@ def test(self) -> None:
"""Main test function for VerifyBGPPeerPrefixes."""
failures: dict[Any, Any] = {}

for command in self.instance_commands:
for command, input_entry in zip(self.instance_commands, self.inputs.address_families):
command_output = command.json_output
afi = command.params.afi
safi = command.params.safi if hasattr(command.params, "safi") else None
afi_vrf = command.params.vrf if hasattr(command.params, "vrf") else "default"
afi = input_entry.afi
safi = input_entry.safi
afi_vrf = input_entry.vrf
peers = input_entry.peers

# Update failures dictionary for `afi`, later on removing the same if no failure found.
if not failures.get(afi):
Expand All @@ -1520,19 +1566,17 @@ def test(self) -> None:
failures[afi] = "Peers not configured"
continue

# Iterate over each peer and verify the prefix(s) consistency.
failure: dict[Any, Any] = {}
for peer, peer_detail in peer_details.items():
if (prefix_rcv := peer_detail.get("prefixReceived", "Not Found")) != (prefix_acc := peer_detail.get("prefixAccepted", "Not Found")):
failure[peer] = {"prefix received": prefix_rcv, "prefix accepted": prefix_acc}
# Verify the received and accepted prefix(s).
failure_logs = _get_inconsistent_peers(peer_details, peers)

# Update failures if any.
if failure:
if failure_logs:
if safi:
failures[afi].update({safi: failure})
failures[afi].update({safi: failure_logs})
else:
failures[afi].update(failure)
else:
failures[afi].update(failure_logs)
# Remove AFI from failures if empty.
if not failures.get(afi):
failures.pop(afi, None)

# Check if any failures
Expand Down
3 changes: 3 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ anta.tests.routing:
address_families:
- afi: ipv4
safi: unicast
peers:
- 10.100.0.8
- 10.100.0.10
- afi: ipv4
safi: multicast
- afi: evpn
Expand Down
69 changes: 69 additions & 0 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4566,4 +4566,73 @@
],
},
},
{
"name": "failure-not-consitent-specific-peers",
"test": VerifyBGPPeerPrefixes,
"eos_data": [
{
"vrfs": {
"default": {
"peers": {
"10.100.0.8": {
"prefixAccepted": 17,
"prefixReceived": 17,
},
"10.100.0.10": {
"prefixAccepted": 15,
"prefixReceived": 20,
},
}
}
}
},
{
"vrfs": {
"MGMT": {
"peers": {
"10.100.0.8": {
"prefixAccepted": 17,
"prefixReceived": 17,
},
"10.100.0.10": {
"prefixAccepted": 17,
"prefixReceived": 17,
},
}
}
}
},
{
"vrfs": {
"default": {
"peers": {
"10.100.0.8": {
"prefixAccepted": 14,
"prefixReceived": 15,
},
"10.100.0.10": {
"prefixAccepted": 11,
"prefixReceived": 15,
},
}
}
}
},
],
"inputs": {
"address_families": [
{"afi": "ipv4", "safi": "unicast", "peers": ["10.100.0.15"]},
{"afi": "ipv4", "safi": "multicast", "vrf": "MGMT", "peers": ["10.100.0.8", "10.100.0.10"]},
{"afi": "evpn", "peers": ["10.100.0.8", "10.100.0.10"]},
]
},
"expected": {
"result": "failure",
"messages": [
"The following BGP address family(s), peers are not configured or prefix(s) received and accepted are not consistent:\n"
"{'ipv4': {'unicast': {'10.100.0.15': 'Not Configured'}}, 'evpn': {'10.100.0.8': {'prefix received': 15, 'prefix accepted': 14}, "
"'10.100.0.10': {'prefix received': 15, 'prefix accepted': 11}}}"
],
},
},
]

0 comments on commit de886f7

Please sign in to comment.