Skip to content

Commit aeda3fe

Browse files
Added unit test for str function, fixed congnetive complexity issue
1 parent 6e8a860 commit aeda3fe

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

anta/input_models/routing/bgp.py

-9
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@
4040
}
4141
"""Dictionary mapping AFI/SAFI to EOS key representation."""
4242

43-
AFI_SAFI_REDISTRIBUTED_ROUTE_KEY = {"ipv4Unicast": "v4u", "ipv4Multicast": "v4m", "ipv6Unicast": "v6u", "ipv6Multicast": "v6m"}
44-
45-
"""Dictionary mapping of AFI/SAFI to redistributed routes key representation."""
46-
4743

4844
class BgpAddressFamily(BaseModel):
4945
"""Model for a BGP address family."""
@@ -103,11 +99,6 @@ def eos_key(self) -> str:
10399
# Pydantic handles the validation of the AFI/SAFI combination, so we can ignore error handling here.
104100
return AFI_SAFI_EOS_KEY[(self.afi, self.safi)]
105101

106-
@property
107-
def redistributed_route_key(self) -> str:
108-
"""AFI/SAFI Redistributed route key representation."""
109-
return AFI_SAFI_REDISTRIBUTED_ROUTE_KEY[self.eos_key]
110-
111102
def __str__(self) -> str:
112103
"""Return a human-readable string representation of the BgpAddressFamily for reporting.
113104

anta/tests/routing/bgp.py

+36-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# mypy: disable-error-code=attr-defined
88
from __future__ import annotations
99

10-
from typing import ClassVar, TypeVar
10+
from typing import Any, ClassVar, TypeVar
1111

1212
from pydantic import field_validator
1313

@@ -1716,24 +1716,24 @@ class VerifyBGPRedistribution(AntaTest):
17161716
anta.tests.routing:
17171717
bgp:
17181718
- VerifyBGPRedistribution:
1719-
vrfs:
1720-
- vrf: default
1721-
address_families:
1722-
- afi_safi: ipv4Unicast
1723-
redistributed_routes:
1724-
- proto: Connected
1725-
include_leaked: True
1726-
route_map: RM-CONN-2-BGP
1727-
- proto: Static
1728-
include_leaked: True
1729-
route_map: RM-CONN-2-BGP
1730-
- afi_safi: IPv6 Unicast
1731-
redistributed_routes:
1732-
- proto: Dynamic
1733-
route_map: RM-CONN-2-BGP
1734-
- proto: Static
1735-
include_leaked: True
1736-
route_map: RM-CONN-2-BGP
1719+
vrfs:
1720+
- vrf: default
1721+
address_families:
1722+
- afi_safi: ipv4Unicast
1723+
redistributed_routes:
1724+
- proto: Connected
1725+
include_leaked: True
1726+
route_map: RM-CONN-2-BGP
1727+
- proto: Static
1728+
include_leaked: True
1729+
route_map: RM-CONN-2-BGP
1730+
- afi_safi: IPv6 Unicast
1731+
redistributed_routes:
1732+
- proto: Dynamic
1733+
route_map: RM-CONN-2-BGP
1734+
- proto: Static
1735+
include_leaked: True
1736+
route_map: RM-CONN-2-BGP
17371737
```
17381738
"""
17391739

@@ -1746,6 +1746,22 @@ class Input(AntaTest.Input):
17461746
vrfs: list[BgpVrf]
17471747
"""List of address families."""
17481748

1749+
def _validate_redistribute_route_details(self, vrf_data: str, address_family: dict[str, Any], afi_safi_configs: list[dict[str, Any]]) -> None:
1750+
"""Validate the redstributed route details for a given address family."""
1751+
for route_info in address_family.redistributed_routes:
1752+
# If the redistributed route protocol does not match the expected value, test fails.
1753+
if not (actual_route := get_item(afi_safi_configs.get("redistributedRoutes"), "proto", route_info.proto)):
1754+
self.result.is_failure(f"{vrf_data}, {address_family}, {route_info} - Not configured")
1755+
continue
1756+
1757+
# If includes leaked field applicable, and it does not matches the expected value, test fails.
1758+
if all([route_info.include_leaked is not None, (act_include_leaked := actual_route.get("includeLeaked", "Not Found")) != route_info.include_leaked]):
1759+
self.result.is_failure(f"{vrf_data}, {address_family}, {route_info} - Value for included leaked mismatch - Actual: {act_include_leaked}")
1760+
1761+
# If route map is required and it is not matching the expected value, test fails.
1762+
if all([route_info.route_map, (act_route_map := actual_route.get("routeMap", "Not Found")) != route_info.route_map]):
1763+
self.result.is_failure(f"{vrf_data}, {address_family}, {route_info} - Route map mismatch - Actual: {act_route_map}")
1764+
17491765
@AntaTest.anta_test
17501766
def test(self) -> None:
17511767
"""Main test function for VerifyBGPRedistribution."""
@@ -1758,16 +1774,4 @@ def test(self) -> None:
17581774
if not (afi_safi_configs := get_value(command_output, f"vrfs.{vrf_data.vrf}.afiSafiConfig.{address_family.afi_safi}")):
17591775
self.result.is_failure(f"{vrf_data}, {address_family} - Not configured")
17601776
continue
1761-
for route_info in address_family.redistributed_routes:
1762-
# If the redistributed route protocol does not match the expected value, test fails.
1763-
if not (actual_route := get_item(afi_safi_configs.get("redistributedRoutes"), "proto", route_info.proto)):
1764-
self.result.is_failure(f"{vrf_data}, {address_family}, {route_info} - Not configured")
1765-
continue
1766-
1767-
# If includes leaked field applicable, and it does not matches the expected value, test fails.
1768-
if all([route_info.include_leaked is not None, (act_include_leaked := actual_route.get("includeLeaked")) != route_info.include_leaked]):
1769-
self.result.is_failure(f"{vrf_data}, {address_family}, {route_info} - Value for included leaked mismatch - Actual: {act_include_leaked}")
1770-
1771-
# If route map is required and it is not matching the expected value, test fails.
1772-
if all([route_info.route_map, (act_route_map := actual_route.get("routeMap", "Not Found")) != route_info.route_map]):
1773-
self.result.is_failure(f"{vrf_data}, {address_family}, {route_info} - Route map mismatch - Actual: {act_route_map}")
1777+
self._validate_redistribute_route_details(str(vrf_data), address_family, afi_safi_configs)

tests/units/input_models/routing/test_bgp.py

+12
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,18 @@ def test_invalid(self, proto: RedistributedProtocol, include_leaked: bool) -> No
317317
with pytest.raises(ValidationError):
318318
RedistributedRoute(proto=proto, include_leaked=include_leaked)
319319

320+
@pytest.mark.parametrize(
321+
("proto", "include_leaked", "route_map", "expected"),
322+
[
323+
pytest.param("Connected", True, "RM-CONN-2-BGP", "Proto: Connected, Included Leaked: True, Route Map: RM-CONN-2-BGP", id="check-all-params"),
324+
pytest.param("Static", False, None, "Proto: Static, Included Leaked: False", id="check-proto-include_leaked"),
325+
pytest.param("Bgp", None, "RM-CONN-2-BGP", "Proto: Bgp, Route Map: RM-CONN-2-BGP", id="check-proto-route_map"),
326+
],
327+
)
328+
def test_valid_str(self, proto: RedistributedProtocol, include_leaked: bool | None, route_map: str | None, expected: str) -> None:
329+
"""Test RedistributedRoute __str__."""
330+
assert str(RedistributedRoute(proto=proto, include_leaked=include_leaked, route_map=route_map)) == expected
331+
320332

321333
class TestVerifyBGPAddressFamilyConfig:
322334
"""Test anta.tests.routing.bgp.AddressFamilyConfig.Input."""

0 commit comments

Comments
 (0)