Skip to content

Commit

Permalink
Updated docstrings and set default value for include linked field
Browse files Browse the repository at this point in the history
  • Loading branch information
geetanjalimanegslab committed Feb 13, 2025
1 parent c4fd840 commit ddf5721
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions anta/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ def snmp_v3_prefix(auth_type: Literal["auth", "priv", "noauth"]) -> str:
Literal[
"AttachedHost",
"Bgp",
"BGP",
"Connected",
"Dynamic",
"IS-IS",
Expand Down
6 changes: 2 additions & 4 deletions anta/input_models/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ class RedistributedRouteConfig(BaseModel):
"""Model representing a BGP redistributed route configuration."""

proto: RedistributedProtocol
"""he redistributed protocol."""
include_leaked: bool | None = None
"""The redistributed protocol."""
include_leaked: bool = False
"""Flag to include leaked routes of the redistributed protocol while redistributing."""
route_map: str | None = None
"""Optional route map applied to the redistribution."""
Expand All @@ -301,8 +301,6 @@ def __str__(self) -> str:
- Proto: Connected, Include Leaked: absent, Route Map: RM-CONN-2-BGP
"""
base_string = f"Proto: {self.proto}"
if not self.include_leaked and self.include_leaked is not None:
base_string += ", Include Leaked: absent"
if self.include_leaked:
base_string += ", Include Leaked: present"
if self.route_map:
Expand Down
13 changes: 6 additions & 7 deletions anta/tests/routing/bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# Using a TypeVar for the BgpPeer model since mypy thinks it's a ClassVar and not a valid type when used in field validators
T = TypeVar("T", bound=BgpPeer)

# pylint: disable=C0302
# TODO: Refactor to reduce the number of lines in this module later


Expand Down Expand Up @@ -1810,7 +1809,8 @@ class VerifyBGPRedistribution(AntaTest):
1. Ensures that the expected address-family is configured on the device.
2. Confirms that the redistributed route protocol, include leaked and route map match the expected values for a route.
Note: For "User" proto field, checking that it's "EOS SDK" versus User.
!!! Note
For "User" proto field, checking that it's "EOS SDK" versus User.
Expected Results
----------------
Expand All @@ -1819,7 +1819,7 @@ class VerifyBGPRedistribution(AntaTest):
- The redistributed route protocol, include leaked and route map align with the expected values for the route.
* Failure: If any of the following occur:
- The expected address-family is not configured on device.
- The redistributed route protocolor, include leaked or route map does not match the expected value for a route.
- The redistributed route protocol, include leaked or route map does not match the expected value for a route.
Examples
--------
Expand Down Expand Up @@ -1866,9 +1866,8 @@ def _validate_redistribute_route(self, vrf_data: str, addr_family: str, afi_safi
return failure_msg

# If includes leaked field applicable, and it does not matches the expected value, test fails.
if all([route_info.include_leaked is not None, (act_include_leaked := actual_route.get("includeLeaked", False)) != route_info.include_leaked]):
act_include_leaked = "present" if act_include_leaked else "absent"
failure_msg.append(f"{vrf_data}, {addr_family}, {route_info} - Value for include leaked mismatch - Actual: {act_include_leaked}")
if all([route_info.include_leaked, (act_include_leaked := actual_route.get("includeLeaked", "absent")) != route_info.include_leaked]):
failure_msg.append(f"{vrf_data}, {addr_family}, {route_info} - Include leaked mismatch - Actual: {act_include_leaked}")

# If route map is required and it is not matching the expected value, test fails.
if all([route_info.route_map, (act_route_map := actual_route.get("routeMap", "Not Found")) != route_info.route_map]):
Expand All @@ -1889,7 +1888,7 @@ def test(self) -> None:
for address_family in vrf_data.address_families:
# If the AFI-SAFI configuration details are not found, test fails.
if not (afi_safi_configs := get_value(instance_details, f"afiSafiConfig.{address_family.afi_safi}")):
self.result.is_failure(f"{vrf_data}, {address_family} - Not configured")
self.result.is_failure(f"{vrf_data}, {address_family} - Not redistributed")
continue

for route_info in address_family.redistributed_routes:
Expand Down
7 changes: 2 additions & 5 deletions tests/units/anta_tests/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5938,7 +5938,7 @@ def test_check_bgp_neighbor_capability(input_dict: dict[str, bool], expected: bo
},
]
},
"expected": {"result": "failure", "messages": ["VRF: default, AFI-SAFI: IPv6 Multicast - Not configured"]},
"expected": {"result": "failure", "messages": ["VRF: default, AFI-SAFI: IPv6 Multicast - Not redistributed"]},
},
{
"name": "failure-expected-proto-not-found",
Expand Down Expand Up @@ -6113,10 +6113,7 @@ def test_check_bgp_neighbor_capability(input_dict: dict[str, bool], expected: bo
"expected": {
"result": "failure",
"messages": [
"VRF: default, AFI-SAFI: IPv4 Multicast, Proto: IS-IS, Include Leaked: present, Route Map: RM-CONN-2-BGP - Value for include leaked mismatch "
"- Actual: absent",
"VRF: test, AFI-SAFI: IPv6 Unicast, Proto: Bgp, Include Leaked: absent, Route Map: RM-CONN-2-BGP - Value for include leaked mismatch "
"- Actual: present",
"VRF: default, AFI-SAFI: IPv4 Multicast, Proto: IS-IS, Include Leaked: present, Route Map: RM-CONN-2-BGP - Include leaked mismatch - Actual: False"
],
},
},
Expand Down
18 changes: 9 additions & 9 deletions tests/units/input_models/routing/test_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def test_invalid(self, route_entries: list[BgpRoute]) -> None:


class TestVerifyBGPRedistributedRoute:
"""Test anta.tests.routing.bgp.RedistributedRouteConfig.Input."""
"""Test anta.input_models.routing.bgp.RedistributedRouteConfig."""

@pytest.mark.parametrize(
("proto", "include_leaked"),
Expand All @@ -361,7 +361,7 @@ class TestVerifyBGPRedistributedRoute:
pytest.param("User", False, id="proto-User"),
],
)
def test_valid(self, proto: RedistributedProtocol, include_leaked: bool) -> None:
def test_validate_inputs(self, proto: RedistributedProtocol, include_leaked: bool) -> None:
"""Test RedistributedRouteConfig valid inputs."""
RedistributedRouteConfig(proto=proto, include_leaked=include_leaked)

Expand All @@ -381,25 +381,25 @@ def test_invalid(self, proto: RedistributedProtocol, include_leaked: bool) -> No
("proto", "include_leaked", "route_map", "expected"),
[
pytest.param("Connected", True, "RM-CONN-2-BGP", "Proto: Connected, Include Leaked: present, Route Map: RM-CONN-2-BGP", id="check-all-params"),
pytest.param("Static", False, None, "Proto: Static, Include Leaked: absent", id="check-proto-include_leaked"),
pytest.param("User", None, "RM-CONN-2-BGP", "Proto: EOS SDK, Route Map: RM-CONN-2-BGP", id="check-proto-route_map"),
pytest.param("Dynamic", None, None, "Proto: Dynamic", id="check-proto-only"),
pytest.param("Static", False, None, "Proto: Static", id="check-proto-include_leaked"),
pytest.param("User", False, "RM-CONN-2-BGP", "Proto: EOS SDK, Route Map: RM-CONN-2-BGP", id="check-proto-route_map"),
pytest.param("Dynamic", False, None, "Proto: Dynamic", id="check-proto-only"),
],
)
def test_valid_str(self, proto: RedistributedProtocol, include_leaked: bool | None, route_map: str | None, expected: str) -> None:
def test_valid_str(self, proto: RedistributedProtocol, include_leaked: bool, route_map: str | None, expected: str) -> None:
"""Test RedistributedRouteConfig __str__."""
assert str(RedistributedRouteConfig(proto=proto, include_leaked=include_leaked, route_map=route_map)) == expected


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

@pytest.mark.parametrize(
("afi_safi", "redistributed_routes"),
[
pytest.param("ipv4Unicast", [{"proto": "OSPFv3 External", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}], id="afisafi-ipv4-unicast"),
pytest.param("ipv6 Multicast", [{"proto": "OSPF Internal", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}], id="afisafi-ipv6-multicast"),
pytest.param("ipv4-Multicast", [{"proto": "IS-IS", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}], id="afisafi-ipv4-multicast"),
pytest.param("ipv4-Multicast", [{"proto": "IS-IS", "include_leaked": False, "route_map": "RM-CONN-2-BGP"}], id="afisafi-ipv4-multicast"),
pytest.param("ipv6_Unicast", [{"proto": "AttachedHost", "route_map": "RM-CONN-2-BGP"}], id="afisafi-ipv6-unicast"),
],
)
Expand Down Expand Up @@ -433,5 +433,5 @@ def test_invalid(self, afi_safi: RedistributedAfiSafi, redistributed_routes: lis
],
)
def test_valid_str(self, afi_safi: RedistributedAfiSafi, redistributed_routes: list[Any], expected: str) -> None:
"""Test AddressFamilyConfig invalid inputs."""
"""Test AddressFamilyConfig __str__."""
assert str(AddressFamilyConfig(afi_safi=afi_safi, redistributed_routes=redistributed_routes)) == expected

0 comments on commit ddf5721

Please sign in to comment.