| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.input_models.routing.generic.py.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from typing import TYPE_CHECKING |
| 9 |
|
| 10 |
import pytest |
| 11 |
from pydantic import ValidationError |
| 12 |
|
| 13 |
from anta.input_models.routing.generic import RoutingTableSizeRouteSource, RoutingTableSizeVRF |
| 14 |
from anta.tests.routing.generic import VerifyIPv4RouteNextHops, VerifyIPv4RouteType |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from anta.input_models.routing.generic import IPv4RouteEntry |
| 18 |
|
| 19 |
|
| 20 |
class TestRoutingTableSizeRouteSource: |
| 21 |
"""Tests for anta.input_models.routing.generic.RoutingTableSizeRouteSource.""" |
| 22 |
|
| 23 |
def test_default_source_is_total(self) -> None: |
| 24 |
"""`source` defaults to `total`.""" |
| 25 |
rs = RoutingTableSizeRouteSource(minimum=1, maximum=10) |
| 26 |
assert rs.source == "total_routes" |
| 27 |
|
| 28 |
def test_valid_bounds(self) -> None: |
| 29 |
"""Both bounds provided is valid.""" |
| 30 |
rs = RoutingTableSizeRouteSource(source="bgp", minimum=10, maximum=100) |
| 31 |
assert rs.minimum == 10 |
| 32 |
assert rs.maximum == 100 |
| 33 |
|
| 34 |
def test_invalid_min_greater_than_max(self) -> None: |
| 35 |
"""`minimum > maximum` is rejected.""" |
| 36 |
with pytest.raises(ValidationError): |
| 37 |
RoutingTableSizeRouteSource(source="bgp", minimum=100, maximum=10) |
| 38 |
|
| 39 |
def test_invalid_unknown_source(self) -> None: |
| 40 |
"""Unknown `source` literals are rejected.""" |
| 41 |
with pytest.raises(ValidationError): |
| 42 |
RoutingTableSizeRouteSource(source="unknown", minimum=1, maximum=10) |
| 43 |
|
| 44 |
|
| 45 |
class TestRoutingTableSizeVRF: |
| 46 |
"""Tests for anta.input_models.routing.generic.RoutingTableSizeVRF.""" |
| 47 |
|
| 48 |
def test_valid_defaults(self) -> None: |
| 49 |
"""A VRF entry with defaults has vrf='default'.""" |
| 50 |
v = RoutingTableSizeVRF(route_sources=[RoutingTableSizeRouteSource(source="total_routes", minimum=1, maximum=10)]) |
| 51 |
assert str(v) == "VRF: default" |
| 52 |
assert len(v.route_sources) == 1 |
| 53 |
assert v.route_sources[0].source == "total_routes" |
| 54 |
|
| 55 |
def test_valid_with_route_sources(self) -> None: |
| 56 |
"""A VRF entry with explicit route sources is valid.""" |
| 57 |
RoutingTableSizeVRF(vrf="PROD", route_sources=[RoutingTableSizeRouteSource(source="bgp", minimum=1, maximum=10)]) |
| 58 |
|
| 59 |
def test_invalid_duplicate_route_sources(self) -> None: |
| 60 |
"""Duplicate route sources within the same VRF are rejected.""" |
| 61 |
with pytest.raises(ValidationError): |
| 62 |
RoutingTableSizeVRF( |
| 63 |
vrf="PROD", |
| 64 |
route_sources=[ |
| 65 |
RoutingTableSizeRouteSource(source="bgp", minimum=1, maximum=10), |
| 66 |
RoutingTableSizeRouteSource(source="bgp", minimum=1, maximum=10), |
| 67 |
], |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
class TestVerifyRouteEntryInput: |
| 72 |
"""Test anta.tests.routing.generic.VerifyIPv4RouteNextHops.Input.""" |
| 73 |
|
| 74 |
@pytest.mark.parametrize( |
| 75 |
("route_entries"), |
| 76 |
[ |
| 77 |
pytest.param([{"prefix": "10.10.0.1/32", "vrf": "default", "strict": True, "nexthops": ["10.100.0.8", "10.100.0.10"]}], id="valid"), |
| 78 |
], |
| 79 |
) |
| 80 |
def test_valid(self, route_entries: list[IPv4RouteEntry]) -> None: |
| 81 |
"""Test VerifyIPv4RouteNextHops.Input valid inputs.""" |
| 82 |
VerifyIPv4RouteNextHops.Input(route_entries=route_entries) |
| 83 |
|
| 84 |
@pytest.mark.parametrize( |
| 85 |
("route_entries"), |
| 86 |
[ |
| 87 |
pytest.param([{"prefix": "10.10.0.1/32", "vrf": "default"}], id="invalid"), |
| 88 |
], |
| 89 |
) |
| 90 |
def test_invalid(self, route_entries: list[IPv4RouteEntry]) -> None: |
| 91 |
"""Test VerifyIPv4RouteNextHops.Input invalid inputs.""" |
| 92 |
with pytest.raises(ValidationError): |
| 93 |
VerifyIPv4RouteNextHops.Input(route_entries=route_entries) |
| 94 |
|
| 95 |
|
| 96 |
class TestVerifyIPv4RouteTypeInput: |
| 97 |
"""Test anta.tests.routing.bgp.VerifyIPv4RouteType.Input.""" |
| 98 |
|
| 99 |
@pytest.mark.parametrize( |
| 100 |
("routes_entries"), |
| 101 |
[ |
| 102 |
pytest.param([{"prefix": "192.168.0.0/24", "vrf": "default", "route_type": "eBGP"}], id="valid"), |
| 103 |
], |
| 104 |
) |
| 105 |
def test_valid(self, routes_entries: list[IPv4RouteEntry]) -> None: |
| 106 |
"""Test VerifyIPv4RouteType.Input valid inputs.""" |
| 107 |
VerifyIPv4RouteType.Input(routes_entries=routes_entries) |
| 108 |
|
| 109 |
@pytest.mark.parametrize( |
| 110 |
("routes_entries"), |
| 111 |
[ |
| 112 |
pytest.param([{"prefix": "192.168.0.0/24", "vrf": "default"}], id="invalid"), |
| 113 |
], |
| 114 |
) |
| 115 |
def test_invalid(self, routes_entries: list[IPv4RouteEntry]) -> None: |
| 116 |
"""Test VerifyIPv4RouteType.Input invalid inputs.""" |
| 117 |
with pytest.raises(ValidationError): |
| 118 |
VerifyIPv4RouteType.Input(routes_entries=routes_entries) |