From 4ea6296a2db02206e65fcad574b9d24c05741112 Mon Sep 17 00:00:00 2001 From: David Lobato Date: Fri, 19 Jul 2024 10:03:22 +0100 Subject: [PATCH] Cache ip from route conversion --- anta/tests/routing/generic.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/anta/tests/routing/generic.py b/anta/tests/routing/generic.py index a6d074300..9f4756adc 100644 --- a/anta/tests/routing/generic.py +++ b/anta/tests/routing/generic.py @@ -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 @@ -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: