From e5f9f2cc68e04621882b00a0d09dbcc52cc4d401 Mon Sep 17 00:00:00 2001 From: "Geetanjali.mane" Date: Mon, 10 Feb 2025 13:50:40 +0000 Subject: [PATCH] Fixed test coverage issue, and updated test failure messages --- anta/tests/routing/bgp.py | 16 ++++--- tests/units/anta_tests/routing/test_bgp.py | 46 ++++++++++++++++++-- tests/units/input_models/routing/test_bgp.py | 15 +++++++ 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/anta/tests/routing/bgp.py b/anta/tests/routing/bgp.py index 44231e150..b136923ee 100644 --- a/anta/tests/routing/bgp.py +++ b/anta/tests/routing/bgp.py @@ -1857,21 +1857,23 @@ class Input(AntaTest.Input): vrfs: list[BgpVrf] """List of VRFs in the BGP instance.""" - def _validate_redistribute_route(self, vrf_data: str, addr_family: str, afi_safi_configs: list[dict[str, Any]], route_info: dict[str, Any]) -> str | None: + def _validate_redistribute_route(self, vrf_data: str, addr_family: str, afi_safi_configs: list[dict[str, Any]], route_info: dict[str, Any]) -> list[Any]: """Validate the redstributed route details for a given address family.""" + failure_msg = [] # If the redistributed route protocol does not match the expected value, test fails. if not (actual_route := get_item(afi_safi_configs.get("redistributedRoutes"), "proto", route_info.proto)): - return f"{vrf_data}, {addr_family}, {route_info} - Not configured" + failure_msg.append(f"{vrf_data}, {addr_family}, Proto: {route_info.proto} - Not configured") + 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" - return f"{vrf_data}, {addr_family}, {route_info} - Value for include leaked mismatch - Actual: {act_include_leaked}" + failure_msg.append(f"{vrf_data}, {addr_family}, {route_info} - Value for 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]): - return f"{vrf_data}, {addr_family}, {route_info} - Route map mismatch - Actual: {act_route_map}" - return None + failure_msg.append(f"{vrf_data}, {addr_family}, {route_info} - Route map mismatch - Actual: {act_route_map}") + return failure_msg @AntaTest.anta_test def test(self) -> None: @@ -1892,5 +1894,5 @@ def test(self) -> None: for route_info in address_family.redistributed_routes: failure_msg = self._validate_redistribute_route(str(vrf_data), str(address_family), afi_safi_configs, route_info) - if failure_msg: - self.result.is_failure(failure_msg) + for msg in failure_msg: + self.result.is_failure(msg) diff --git a/tests/units/anta_tests/routing/test_bgp.py b/tests/units/anta_tests/routing/test_bgp.py index 6be2c7036..57c736a4b 100644 --- a/tests/units/anta_tests/routing/test_bgp.py +++ b/tests/units/anta_tests/routing/test_bgp.py @@ -5874,6 +5874,43 @@ def test_check_bgp_neighbor_capability(input_dict: dict[str, bool], expected: bo }, "expected": {"result": "success"}, }, + { + "name": "failure-vrf-not-found", + "test": VerifyBGPRedistribution, + "eos_data": [ + { + "vrfs": { + "default": {"afiSafiConfig": {"v6m": {"redistributedRoutes": [{"proto": "Connected", "routeMap": "RM-CONN-2-BGP"}]}}}, + "tenant": {"afiSafiConfig": {"v4u": {"redistributedRoutes": [{"proto": "Connected"}]}}}, + } + } + ], + "inputs": { + "vrfs": [ + { + "vrf": "default", + "address_families": [ + { + "afi_safi": "ipv6 Multicast", + "redistributed_routes": [{"proto": "Connected", "include_leaked": False, "route_map": "RM-CONN-2-BGP"}], + }, + ], + }, + { + "vrf": "test", + "address_families": [ + { + "afi_safi": "ipv6 Multicast", + "redistributed_routes": [ + {"proto": "Connected", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}, + ], + }, + ], + }, + ] + }, + "expected": {"result": "failure", "messages": ["VRF: test - Not configured"]}, + }, { "name": "failure-afi-safi-config-not-found", "test": VerifyBGPRedistribution, @@ -5916,7 +5953,7 @@ def test_check_bgp_neighbor_capability(input_dict: dict[str, bool], expected: bo }, "test": { "afiSafiConfig": { - "v6m": { + "v6u": { "redistributedRoutes": [{"proto": "Static", "routeMap": "RM-CONN-2-BGP"}], } } @@ -5955,9 +5992,10 @@ def test_check_bgp_neighbor_capability(input_dict: dict[str, bool], expected: bo "expected": { "result": "failure", "messages": [ - "VRF: default, AFI-SAFI: IPv4 Multicast, Proto: OSPFv3 External, Include Leaked: present, Route Map: RM-CONN-2-BGP - Not configured", - "VRF: default, AFI-SAFI: IPv4 Multicast, Proto: OSPFv3 Nssa-External, Include Leaked: present, Route Map: RM-CONN-2-BGP - Not configured", - "VRF: test, AFI-SAFI: IPv6 Unicast - Not configured", + "VRF: default, AFI-SAFI: IPv4 Multicast, Proto: OSPFv3 External - Not configured", + "VRF: default, AFI-SAFI: IPv4 Multicast, Proto: OSPFv3 Nssa-External - Not configured", + "VRF: test, AFI-SAFI: IPv6 Unicast, Proto: RIP - Not configured", + "VRF: test, AFI-SAFI: IPv6 Unicast, Proto: Bgp - Not configured", ], }, }, diff --git a/tests/units/input_models/routing/test_bgp.py b/tests/units/input_models/routing/test_bgp.py index f027a787b..e39131e4c 100644 --- a/tests/units/input_models/routing/test_bgp.py +++ b/tests/units/input_models/routing/test_bgp.py @@ -420,3 +420,18 @@ def test_invalid(self, afi_safi: RedistributedAfiSafi, redistributed_routes: lis """Test AddressFamilyConfig invalid inputs.""" with pytest.raises(ValidationError): AddressFamilyConfig(afi_safi=afi_safi, redistributed_routes=redistributed_routes) + + @pytest.mark.parametrize( + ("afi_safi", "redistributed_routes", "expected"), + [ + pytest.param( + "v4u", [{"proto": "OSPFv3 Nssa-External", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}], "AFI-SAFI: IPv4 Unicast", id="valid-ipv4-unicast" + ), + pytest.param("v4m", [{"proto": "RIP", "route_map": "RM-CONN-2-BGP"}], "AFI-SAFI: IPv4 Multicast", id="valid-ipv4-multicast"), + pytest.param("v6u", [{"proto": "Bgp", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}], "AFI-SAFI: IPv6 Unicast", id="valid-ipv6-unicast"), + pytest.param("v6m", [{"proto": "Static", "include_leaked": True, "route_map": "RM-CONN-2-BGP"}], "AFI-SAFI: IPv6 Multicast", id="valid-ipv6-multicast"), + ], + ) + def test_valid_str(self, afi_safi: RedistributedAfiSafi, redistributed_routes: list[Any], expected: str) -> None: + """Test AddressFamilyConfig invalid inputs.""" + assert str(AddressFamilyConfig(afi_safi=afi_safi, redistributed_routes=redistributed_routes)) == expected