Skip to content

Commit

Permalink
issue-505: Added testcase for BGP ECMP path install
Browse files Browse the repository at this point in the history
  • Loading branch information
MaheshGSLAB committed Dec 29, 2023
1 parent f68244e commit 07710ac
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
59 changes: 59 additions & 0 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,62 @@ def test(self) -> None:

if failures:
self.result.is_failure(f"Failures: {list(failures.values())}")


class VerifyBGPEcmpPath(AntaTest):
"""
Verifies if a BGP route is contributed in ecmp in the specified VRF.
Expected results:
* success: The test will pass if a BGP route is contributed in ecmp and at least one path is an ecmp head in the specified VRF.
* failure: The test will fail if a BGP route is not found, no contributed in ecmp, or ecmp head is not found for any path in the specified VRF.
"""

name = "VerifyBGPEcmpPath"
description = "Verifies if BGP route is contributed in ecmp in the specified VRF."
categories = ["routing", "bgp"]
commands = [AntaCommand(command="show ip bgp")]

class Input(AntaTest.Input):
"""
Input parameters for the test.
"""

bgp_routes: List[BgpPeers]
"""List of BGP routes"""

class BgpPeers(BaseModel):
"""
This class defines the details of a BGP route.
"""

route: str
"""IPv4/IPv6 BGP route"""
vrf: str = "default"
"""VRF context"""

@AntaTest.anta_test
def test(self) -> None:
failures: dict[str, Any] = {}

# Iterate over each bgp route
for bgp_route in self.inputs.bgp_routes:
route = str(bgp_route.route)
vrf = bgp_route.vrf

# Verify if BGP route exist
if not (bgp_output := get_value(self.instance_commands[0].json_output, f"vrfs..{vrf}..bgpRouteEntries..{route}", separator="..")):
failures[str(route)] = {vrf: "Not configured"}
continue

# Verify BGP route's ecmp head and contribution
bgp_paths = bgp_output.get("bgpRoutePaths", [])
failures[route] = {vrf: "ECMP path is not installed"}
for path in bgp_paths:
if path.get("routeType", {}).get("ecmpHead") and path.get("routeType", {}).get("ecmpContributor"):
failures.pop(route)
break

if not failures:
self.result.is_success()
else:
self.result.is_failure(f"Following BGP routes are not configured, not contributed in ecmp, or ecmp head is not found:\n{failures}")
6 changes: 6 additions & 0 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ anta.tests.routing:
- 10.1.255.0
- 10.1.255.2
- 10.1.255.4
- VerifyBGPEcmpPath:
bgp_routes:
- route: 192.0.255.1/32
vrf: default
- route: 192.0.254.3/32
vrf: default
ospf:
- VerifyOSPFNeighborState:
- VerifyOSPFNeighborCount:
Expand Down
85 changes: 84 additions & 1 deletion tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# pylint: disable=C0413
# because of the patch above
from anta.tests.routing.bgp import VerifyBGPPeerCount, VerifyBGPPeersHealth, VerifyBGPSpecificPeers # noqa: E402
from anta.tests.routing.bgp import VerifyBGPEcmpPath, VerifyBGPPeerCount, VerifyBGPPeersHealth, VerifyBGPSpecificPeers # noqa: E402
from tests.lib.anta import test # noqa: F401; pylint: disable=W0611

DATA: list[dict[str, Any]] = [
Expand Down Expand Up @@ -1239,4 +1239,87 @@
],
},
},
{
"name": "success",
"test": VerifyBGPEcmpPath,
"eos_data": [
{
"vrfs": {
"default": {
"bgpRouteEntries": {
"192.0.254.3/32": {
"bgpRoutePaths": [
{"routeType": {"ecmpHead": True, "ecmp": True, "ecmpContributor": True}},
{"routeType": {"ecmpHead": False, "ecmp": True, "ecmpContributor": True}},
]
}
}
}
}
}
],
"inputs": {
"bgp_routes": [
{
"route": "192.0.254.3/32",
"vrf": "default",
}
]
},
"expected": {"result": "success"},
},
{
"name": "failure-no-route",
"test": VerifyBGPEcmpPath,
"eos_data": [{"vrfs": {"default": {"bgpRouteEntries": {}}}}],
"inputs": {
"bgp_routes": [
{
"route": "192.0.254.3/32",
"vrf": "default",
}
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP routes are not configured, not contributed in ecmp, or ecmp head is not found:\n{'192.0.254.3/32': {'default': 'Not configured'}}"
],
},
},
{
"name": "failure-no-ecmp",
"test": VerifyBGPEcmpPath,
"eos_data": [
{
"vrfs": {
"default": {
"bgpRouteEntries": {
"192.0.254.3/32": {
"bgpRoutePaths": [
{"routeType": {"ecmpHead": False, "ecmp": True, "ecmpContributor": True}},
{"routeType": {"ecmpHead": False, "ecmp": True, "ecmpContributor": True}},
]
}
}
}
}
}
],
"inputs": {
"bgp_routes": [
{
"route": "192.0.254.3/32",
"vrf": "default",
}
]
},
"expected": {
"result": "failure",
"messages": [
"Following BGP routes are not configured, not contributed in ecmp, or ecmp head is not found:\n"
"{'192.0.254.3/32': {'default': 'ECMP path is not installed'}}"
],
},
},
]

0 comments on commit 07710ac

Please sign in to comment.