Skip to content

Commit

Permalink
Cache ip from route conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
dlobato committed Jul 19, 2024
1 parent f39375c commit 4ea6296
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions anta/tests/routing/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# mypy: disable-error-code=attr-defined
from __future__ import annotations

from ipaddress import IPv4Address, ip_interface
from functools import cache
from ipaddress import IPv4Address, IPv4Interface
from typing import ClassVar, Literal

from pydantic import model_validator
Expand Down Expand Up @@ -142,14 +143,20 @@ class Input(AntaTest.Input):
"""List of routes to verify."""

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

@staticmethod
@cache
def ip_interface_ip(route: str) -> IPv4Address:
"""Return the IP address of the provided ip route with mask."""
return IPv4Interface(route).ip

@AntaTest.anta_test
def test(self) -> None:
"""Main test function for VerifyRoutingTableEntry."""
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"]]
command_output_route_ips = [self.ip_interface_ip(route) 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:
Expand Down

0 comments on commit 4ea6296

Please sign in to comment.