Skip to content

Commit

Permalink
feat: Optimize VerifyRoutingTableEntry by quering all routes for a vrf.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlobato committed May 16, 2024
1 parent 28ba8e1 commit 93be1ef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 43 deletions.
13 changes: 5 additions & 8 deletions anta/tests/routing/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class VerifyRoutingTableEntry(AntaTest):
name = "VerifyRoutingTableEntry"
description = "Verifies that the provided routes are present in the routing table of a specified VRF."
categories: ClassVar[list[str]] = ["routing"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show ip route vrf {vrf} {route}", revision=4)]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaTemplate(template="show ip route vrf {vrf}", revision=4)]

class Input(AntaTest.Input):
"""Input model for the VerifyRoutingTableEntry test."""
Expand All @@ -143,17 +143,14 @@ class Input(AntaTest.Input):

def render(self, template: AntaTemplate) -> list[AntaCommand]:
"""Render the template for each route in the input list."""
return [template.render(vrf=self.inputs.vrf, route=route) for route in self.inputs.routes]
return [template.render(vrf=self.inputs.vrf)]

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyRoutingTableEntry."""
missing_routes = []

for command in self.instance_commands:
vrf, route = command.params.vrf, command.params.route
if len(routes := command.json_output["vrfs"][vrf]["routes"]) == 0 or route != ip_interface(next(iter(routes))).ip:
missing_routes.append(str(route))
command_output_vrf = self.instance_commands[0].json_output["vrfs"][self.inputs.vrf]
command_output_route_ips = [ip_interface(route).ip for route in command_output_vrf["routes"]]
missing_routes = [str(route) for route in self.inputs.routes if route not in command_output_route_ips]

if not missing_routes:
self.result.is_success()
Expand Down
35 changes: 0 additions & 35 deletions tests/units/anta_tests/routing/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,6 @@
"metric": 0,
"vias": [{"nexthopAddr": "10.1.255.4", "interface": "Ethernet1"}],
},
},
},
},
},
{
"vrfs": {
"default": {
"routingDisabled": False,
"allRoutesProgrammedHardware": True,
"allRoutesProgrammedKernel": True,
"defaultRouteState": "notSet",
"routes": {
"10.1.0.2/32": {
"hardwareProgrammed": True,
"routeType": "eBGP",
Expand All @@ -134,17 +122,6 @@
"name": "failure-missing-route",
"test": VerifyRoutingTableEntry,
"eos_data": [
{
"vrfs": {
"default": {
"routingDisabled": False,
"allRoutesProgrammedHardware": True,
"allRoutesProgrammedKernel": True,
"defaultRouteState": "notSet",
"routes": {},
},
},
},
{
"vrfs": {
"default": {
Expand Down Expand Up @@ -195,18 +172,6 @@
"metric": 0,
"vias": [{"nexthopAddr": "10.1.255.4", "interface": "Ethernet1"}],
},
},
},
},
},
{
"vrfs": {
"default": {
"routingDisabled": False,
"allRoutesProgrammedHardware": True,
"allRoutesProgrammedKernel": True,
"defaultRouteState": "notSet",
"routes": {
"10.1.0.55/32": {
"hardwareProgrammed": True,
"routeType": "eBGP",
Expand Down

0 comments on commit 93be1ef

Please sign in to comment.