-
Notifications
You must be signed in to change notification settings - Fork 31
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.tests): Adding the test case to verify BGP neighbor routes #484
Conversation
anta/tests/routing/bgp.py
Outdated
@@ -132,6 +132,61 @@ def _check_peer_issues(peer_data: Optional[dict[str, Any]]) -> dict[str, Any]: | |||
return {} | |||
|
|||
|
|||
def _add_bgp_routes_failure( | |||
bgp_routes: List[str], bgp_output: dict[str, Any], neighbor: str, vrf: str, route_type: str = "advertised_routes" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bgp_routes: List[str], bgp_output: dict[str, Any], neighbor: str, vrf: str, route_type: str = "advertised_routes" | |
bgp_routes: list[str], bgp_output: dict[str, Any], neighbor: str, vrf: str, route_type: str = "advertised_routes" |
anta/tests/routing/bgp.py
Outdated
It identifies any missing routes as well as any routes that are invalid or inactive. The results are returned in a dictionary. | ||
|
||
Parameters: | ||
bgp_routes (List[str]): The list of expected routes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bgp_routes (List[str]): The list of expected routes. | |
bgp_routes (list[str]): The list of expected routes. |
anta/tests/routing/bgp.py
Outdated
missing_routes = [route for route in bgp_routes if route not in bgp_output] | ||
invalid_or_inactive = [ | ||
route | ||
for route in bgp_routes | ||
if route in bgp_output | ||
and (not bgp_output[route]["bgpRoutePaths"][0]["routeType"]["valid"] or not bgp_output[route]["bgpRoutePaths"][0]["routeType"]["active"]) | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are looping twice over the bgp_routes
so it is probably more efficient to use a for loop and loop only once:
for route in bgp_routes:
if not route in bgp_output:
missing_routes.append(route)
continue
if (big condition):
invalid_or_inactive.append(route)
anta/tests/routing/bgp.py
Outdated
received_routes = command.params.get("received_routes", []) | ||
|
||
# Verify if BGP neighbor is configured with provided vrf | ||
if (bgp_routes := get_value(command.json_output, f"vrfs..{vrf}", separator="..")) is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need ..
as separator here?
anta/tests/routing/bgp.py
Outdated
# Verify if BGP neighbor is configured with provided vrf | ||
if (bgp_routes := get_value(command.json_output, f"vrfs..{vrf}", separator="..")) is None: | ||
self.result.is_failure(f"BGP neighbor {str(neighbor)} is not configured for `{vrf}` VRF.") | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you return here then you won't check all the commands right? So maybe you will miss some failures in other commands that you could report
Would it not be better with
return | |
continue |
?
(same below)
{ | ||
"neighbor": "172.30.11.1", | ||
"vrf": "default", | ||
"advertised_routes": ["192.0.254.31/32"], | ||
"received_routes": ["192.0.255.31/32"], | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test with more than one neighbor in your inputs, in particular with one success and one failure and check that the test is failing
anta/tests/routing/bgp.py
Outdated
# Check if the route is missing in the BGP output | ||
if route not in bgp_output: | ||
# If missing, add it to the failure routes dictionary | ||
failure_routes.setdefault("bgp_peers", {}).setdefault(peer, {}).setdefault(vrf, {}).setdefault(route_type, {})[route] = "Not found" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialize this at the begining of the function with the proper structure and use a flag to track if a failure happened
this chain of setdefault is dificult to understand
anta/tests/routing/bgp.py
Outdated
peer = str(command.params.get("peer", "")) | ||
vrf = command.params.get("vrf", "") | ||
advertised_routes = command.params.get("advertised_routes", []) | ||
received_routes = command.params.get("received_routes", []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, in recent ANTA refactor, it looks like command.params
is properly typed as Dict[str, Any]
so we can simply do a dict lookup without mypy complaining. No need to bother with .get()
or cast
anymore.
d4bef55
to
6cf2896
Compare
Description
Add the test case to verify the BGP neighbor routes
Run the testcase by adding catalog as below:
Fixes #482
Checklist:
pre-commit run
)tox -e testenv
)