| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for `anta.custom_types`. |
| 5 |
|
| 6 |
The intention is only to test here what is not used already in other places. |
| 7 |
|
| 8 |
TODO: Expand later. |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
import re |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from anta.custom_types import ( |
| 18 |
REGEXP_ETHERNET_INTERFACE, |
| 19 |
REGEXP_INTERFACE_ID, |
| 20 |
REGEXP_PATH_MARKERS, |
| 21 |
REGEXP_PORT_CHANNEL_INTERFACE, |
| 22 |
REGEXP_TYPE_EOS_INTERFACE, |
| 23 |
REGEXP_TYPE_HOSTNAME, |
| 24 |
REGEXP_TYPE_VXLAN_SRC_INTERFACE, |
| 25 |
aaa_group_prefix, |
| 26 |
bgp_multiprotocol_capabilities_abbreviations, |
| 27 |
convert_reload_cause, |
| 28 |
interface_autocomplete, |
| 29 |
interface_case_sensitivity, |
| 30 |
snmp_v3_prefix, |
| 31 |
validate_regex, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
def test_regexp_path_markers() -> None: |
| 40 |
"""Test REGEXP_PATH_MARKERS.""" |
| 41 |
|
| 42 |
assert re.search(REGEXP_PATH_MARKERS, "show/bgp/interfaces") is not None |
| 43 |
assert re.search(REGEXP_PATH_MARKERS, "show\\bgp") is not None |
| 44 |
assert re.search(REGEXP_PATH_MARKERS, "show bgp") is not None |
| 45 |
|
| 46 |
|
| 47 |
assert re.search(REGEXP_PATH_MARKERS, "aaaa") is None |
| 48 |
assert re.search(REGEXP_PATH_MARKERS, "11111") is None |
| 49 |
assert re.search(REGEXP_PATH_MARKERS, ".[]?<>") is None |
| 50 |
|
| 51 |
|
| 52 |
def test_regexp_type_interface_id() -> None: |
| 53 |
"""Test REGEXP_INTERFACE_ID.""" |
| 54 |
intf_id_re = re.compile(f"{REGEXP_INTERFACE_ID}") |
| 55 |
|
| 56 |
|
| 57 |
assert intf_id_re.search("123") is not None |
| 58 |
assert intf_id_re.search("123/456") is not None |
| 59 |
assert intf_id_re.search("123.456") is not None |
| 60 |
assert intf_id_re.search("123/456.789") is not None |
| 61 |
|
| 62 |
|
| 63 |
def test_regexp_type_eos_interface() -> None: |
| 64 |
"""Test REGEXP_TYPE_EOS_INTERFACE.""" |
| 65 |
|
| 66 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Ethernet0") is not None |
| 67 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Vlan100") is not None |
| 68 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Port-Channel1/0") is not None |
| 69 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Loopback0.1") is not None |
| 70 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Management0/0/0") is not None |
| 71 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Tunnel1") is not None |
| 72 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Vxlan1") is not None |
| 73 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Fabric1") is not None |
| 74 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Dps1") is not None |
| 75 |
|
| 76 |
|
| 77 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Ethernet") is None |
| 78 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Vlan") is None |
| 79 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Port-Channel") is None |
| 80 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Loopback.") is None |
| 81 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Management/") is None |
| 82 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Tunnel") is None |
| 83 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Vxlan") is None |
| 84 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Fabric") is None |
| 85 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Dps") is None |
| 86 |
|
| 87 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Ethernet1/a") is None |
| 88 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Port-Channel-100") is None |
| 89 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Loopback.10") is None |
| 90 |
assert re.match(REGEXP_TYPE_EOS_INTERFACE, "Management/10") is None |
| 91 |
|
| 92 |
|
| 93 |
def test_regexp_type_vxlan_src_interface() -> None: |
| 94 |
"""Test REGEXP_TYPE_VXLAN_SRC_INTERFACE.""" |
| 95 |
|
| 96 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback0") is not None |
| 97 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback1") is not None |
| 98 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback99") is not None |
| 99 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback100") is not None |
| 100 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback8190") is not None |
| 101 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Dps1") is not None |
| 102 |
|
| 103 |
|
| 104 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback") is None |
| 105 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback8192") is None |
| 106 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback9001") is None |
| 107 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Loopback9000") is None |
| 108 |
assert re.match(REGEXP_TYPE_VXLAN_SRC_INTERFACE, "Dps2") is None |
| 109 |
|
| 110 |
|
| 111 |
def test_regexp_port_channel_interface() -> None: |
| 112 |
"""Test REGEXP_PORT_CHANNEL_INTERFACE.""" |
| 113 |
|
| 114 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel5") is not None |
| 115 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel100") is not None |
| 116 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel999") is not None |
| 117 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel1000") is not None |
| 118 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel1000.1") is not None |
| 119 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel123456") is not None |
| 120 |
|
| 121 |
|
| 122 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel") is None |
| 123 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port_Channel") is None |
| 124 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port_Channel1000") is None |
| 125 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port_Channel5/1") is None |
| 126 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel-100") is None |
| 127 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel1000.1.1") is None |
| 128 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel1.") is None |
| 129 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel 1") is None |
| 130 |
assert re.match(REGEXP_PORT_CHANNEL_INTERFACE, "Port-Channel1234567") is None |
| 131 |
|
| 132 |
|
| 133 |
def test_regexp_ethernet_interface() -> None: |
| 134 |
"""Test REGEXP_ETHERNET_INTERFACE.""" |
| 135 |
|
| 136 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet5") is not None |
| 137 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet100") is not None |
| 138 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet999") is not None |
| 139 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1000") is not None |
| 140 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1/1") is not None |
| 141 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1/1/1") is not None |
| 142 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1000.1") is not None |
| 143 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1/1.1") is not None |
| 144 |
|
| 145 |
|
| 146 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet") is None |
| 147 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1/1/1/1") is None |
| 148 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1.1.1") is None |
| 149 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1/1.1.1") is None |
| 150 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "ethernet1") is None |
| 151 |
assert re.match(REGEXP_ETHERNET_INTERFACE, "Ethernet1/") is None |
| 152 |
|
| 153 |
|
| 154 |
def test_regexp_type_hostname() -> None: |
| 155 |
"""Test REGEXP_TYPE_HOSTNAME.""" |
| 156 |
|
| 157 |
assert re.match(REGEXP_TYPE_HOSTNAME, "hostname") is not None |
| 158 |
assert re.match(REGEXP_TYPE_HOSTNAME, "hostname.com") is not None |
| 159 |
assert re.match(REGEXP_TYPE_HOSTNAME, "host-name.com") is not None |
| 160 |
assert re.match(REGEXP_TYPE_HOSTNAME, "host.name.com") is not None |
| 161 |
assert re.match(REGEXP_TYPE_HOSTNAME, "host-name1.com") is not None |
| 162 |
|
| 163 |
|
| 164 |
assert re.match(REGEXP_TYPE_HOSTNAME, "-hostname.com") is None |
| 165 |
assert re.match(REGEXP_TYPE_HOSTNAME, ".hostname.com") is None |
| 166 |
assert re.match(REGEXP_TYPE_HOSTNAME, "hostname-.com") is None |
| 167 |
assert re.match(REGEXP_TYPE_HOSTNAME, "hostname..com") is None |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
def test_interface_autocomplete_success() -> None: |
| 176 |
"""Test interface_autocomplete with valid inputs.""" |
| 177 |
assert interface_autocomplete("et1") == "Ethernet1" |
| 178 |
assert interface_autocomplete("et1/1") == "Ethernet1/1" |
| 179 |
assert interface_autocomplete("et1.1") == "Ethernet1.1" |
| 180 |
assert interface_autocomplete("et1/1.1") == "Ethernet1/1.1" |
| 181 |
assert interface_autocomplete("eth2") == "Ethernet2" |
| 182 |
assert interface_autocomplete("po3") == "Port-Channel3" |
| 183 |
assert interface_autocomplete("lo4") == "Loopback4" |
| 184 |
assert interface_autocomplete("Po1000") == "Port-Channel1000" |
| 185 |
assert interface_autocomplete("Po 1000") == "Port-Channel1000" |
| 186 |
assert interface_autocomplete("Vl1000") == "Vlan1000" |
| 187 |
|
| 188 |
|
| 189 |
def test_interface_autocomplete_no_alias() -> None: |
| 190 |
"""Test interface_autocomplete with inputs that don't have aliases.""" |
| 191 |
assert interface_autocomplete("GigabitEthernet1") == "GigabitEthernet1" |
| 192 |
assert interface_autocomplete("Vlan10") == "Vlan10" |
| 193 |
assert interface_autocomplete("Tunnel100") == "Tunnel100" |
| 194 |
|
| 195 |
|
| 196 |
def test_interface_autocomplete_failure() -> None: |
| 197 |
"""Trigger ValueError for interface_autocomplete.""" |
| 198 |
with pytest.raises(ValueError, match="Could not parse interface ID in interface"): |
| 199 |
interface_autocomplete("ThisIsNotAnInterface") |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.parametrize( |
| 203 |
("str_input", "expected_output"), |
| 204 |
[ |
| 205 |
pytest.param("L2VPNEVPN", "l2VpnEvpn", id="l2VpnEvpn"), |
| 206 |
pytest.param("IPv4 Labeled Unicast", "ipv4MplsLabels", id="ipv4MplsLabels"), |
| 207 |
pytest.param("ipv4-mpls-vpn", "ipv4MplsVpn", id="ipv4MplsVpn"), |
| 208 |
pytest.param("ipv4_unicast", "ipv4Unicast", id="ipv4Unicast"), |
| 209 |
pytest.param("ipv4 Mvpn", "ipv4Mvpn", id="ipv4Mvpn"), |
| 210 |
pytest.param("ipv4_Flow-Spec Vpn", "ipv4FlowSpecVpn", id="ipv4FlowSpecVpn"), |
| 211 |
pytest.param("Dynamic-Path-Selection", "dps", id="dps"), |
| 212 |
pytest.param("ipv6unicast", "ipv6Unicast", id="ipv6Unicast"), |
| 213 |
pytest.param("IPv4-Multicast", "ipv4Multicast", id="ipv4Multicast"), |
| 214 |
pytest.param("IPv6_multicast", "ipv6Multicast", id="ipv6Multicast"), |
| 215 |
pytest.param("ipv6_Mpls-Labels", "ipv6MplsLabels", id="ipv6MplsLabels"), |
| 216 |
pytest.param("IPv4_SR_TE", "ipv4SrTe", id="ipv4SrTe"), |
| 217 |
pytest.param("iPv6-sR-tE", "ipv6SrTe", id="ipv6SrTe"), |
| 218 |
pytest.param("ipv6_mpls-vpn", "ipv6MplsVpn", id="ipv6MplsVpn"), |
| 219 |
pytest.param("IPv4 Flow-spec", "ipv4FlowSpec", id="ipv4FlowSpec"), |
| 220 |
pytest.param("IPv6Flow_spec", "ipv6FlowSpec", id="ipv6FlowSpec"), |
| 221 |
pytest.param("ipv6 Flow-Spec Vpn", "ipv6FlowSpecVpn", id="ipv6FlowSpecVpn"), |
| 222 |
pytest.param("L2VPN VPLS", "l2VpnVpls", id="l2VpnVpls"), |
| 223 |
pytest.param("link-state", "linkState", id="linkState"), |
| 224 |
pytest.param("RT_Membership", "rtMembership", id="rtMembership"), |
| 225 |
pytest.param("ipv4-RT_Membership", "rtMembership", id="rtMembership"), |
| 226 |
], |
| 227 |
) |
| 228 |
def test_bgp_multiprotocol_capabilities_abbreviations(str_input: str, expected_output: str) -> None: |
| 229 |
"""Test bgp_multiprotocol_capabilities_abbreviations.""" |
| 230 |
assert bgp_multiprotocol_capabilities_abbreviations(str_input) == expected_output |
| 231 |
|
| 232 |
|
| 233 |
def test_aaa_group_prefix_known_method() -> None: |
| 234 |
"""Test aaa_group_prefix with a known method.""" |
| 235 |
assert aaa_group_prefix("local") == "local" |
| 236 |
assert aaa_group_prefix("none") == "none" |
| 237 |
assert aaa_group_prefix("logging") == "logging" |
| 238 |
|
| 239 |
|
| 240 |
def test_aaa_group_prefix_unknown_method() -> None: |
| 241 |
"""Test aaa_group_prefix with an unknown method.""" |
| 242 |
assert aaa_group_prefix("demo") == "group demo" |
| 243 |
assert aaa_group_prefix("group1") == "group group1" |
| 244 |
|
| 245 |
|
| 246 |
def test_interface_case_sensitivity_lowercase() -> None: |
| 247 |
"""Test interface_case_sensitivity with lowercase inputs.""" |
| 248 |
assert interface_case_sensitivity("ethernet") == "Ethernet" |
| 249 |
assert interface_case_sensitivity("vlan") == "Vlan" |
| 250 |
assert interface_case_sensitivity("loopback") == "Loopback" |
| 251 |
|
| 252 |
|
| 253 |
def test_interface_case_sensitivity_mixed_case() -> None: |
| 254 |
"""Test interface_case_sensitivity with mixed case inputs.""" |
| 255 |
assert interface_case_sensitivity("Ethernet") == "Ethernet" |
| 256 |
assert interface_case_sensitivity("Vlan") == "Vlan" |
| 257 |
assert interface_case_sensitivity("Loopback") == "Loopback" |
| 258 |
|
| 259 |
|
| 260 |
def test_interface_case_sensitivity_uppercase() -> None: |
| 261 |
"""Test interface_case_sensitivity with uppercase inputs.""" |
| 262 |
assert interface_case_sensitivity("ETHERNET") == "ETHERNET" |
| 263 |
assert interface_case_sensitivity("VLAN") == "VLAN" |
| 264 |
assert interface_case_sensitivity("LOOPBACK") == "LOOPBACK" |
| 265 |
|
| 266 |
|
| 267 |
@pytest.mark.parametrize( |
| 268 |
"str_input", |
| 269 |
[ |
| 270 |
REGEXP_PORT_CHANNEL_INTERFACE, |
| 271 |
REGEXP_INTERFACE_ID, |
| 272 |
REGEXP_PATH_MARKERS, |
| 273 |
REGEXP_TYPE_EOS_INTERFACE, |
| 274 |
REGEXP_TYPE_HOSTNAME, |
| 275 |
REGEXP_TYPE_VXLAN_SRC_INTERFACE, |
| 276 |
], |
| 277 |
) |
| 278 |
def test_validate_regex_valid(str_input: str) -> None: |
| 279 |
"""Test validate_regex with valid regex.""" |
| 280 |
assert validate_regex(str_input) == str_input |
| 281 |
|
| 282 |
|
| 283 |
@pytest.mark.parametrize( |
| 284 |
("str_input", "error"), |
| 285 |
[ |
| 286 |
pytest.param("[", "Invalid regex: unterminated character set at position 0", id="unterminated character"), |
| 287 |
pytest.param("\\", r"Invalid regex: bad escape \(end of pattern\) at position 0", id="bad escape"), |
| 288 |
], |
| 289 |
) |
| 290 |
def test_validate_regex_invalid(str_input: str, error: str) -> None: |
| 291 |
"""Test validate_regex with invalid regex.""" |
| 292 |
with pytest.raises(ValueError, match=error): |
| 293 |
validate_regex(str_input) |
| 294 |
|
| 295 |
|
| 296 |
def test_snmp_v3_prefix_valid_input() -> None: |
| 297 |
"""Test snmp_v3_prefix with valid authentication type.""" |
| 298 |
assert snmp_v3_prefix("auth") == "v3Auth" |
| 299 |
assert snmp_v3_prefix("noauth") == "v3NoAuth" |
| 300 |
assert snmp_v3_prefix("priv") == "v3Priv" |
| 301 |
|
| 302 |
|
| 303 |
@pytest.mark.parametrize( |
| 304 |
("str_input", "expected_output"), |
| 305 |
[ |
| 306 |
pytest.param("Ztp", "System reloaded due to Zero Touch Provisioning", id="valid"), |
| 307 |
pytest.param("USER", "Reload requested by the user.", id="valid"), |
| 308 |
pytest.param("fpga", "Reload requested after FPGA upgrade", id="valid"), |
| 309 |
], |
| 310 |
) |
| 311 |
def test_convert_reload_cause(str_input: str, expected_output: str) -> None: |
| 312 |
"""Test convert_reload_cause.""" |
| 313 |
assert convert_reload_cause(str_input) == expected_output |
| 314 |
|
| 315 |
|
| 316 |
@pytest.mark.parametrize( |
| 317 |
("str_input"), |
| 318 |
[ |
| 319 |
pytest.param("ztp2", id="invalid"), |
| 320 |
], |
| 321 |
) |
| 322 |
def test_invalid_convert_reload_cause(str_input: str) -> None: |
| 323 |
"""Test invalid convert_reload_cause.""" |
| 324 |
with pytest.raises(ValueError, match=r"Invalid reload cause: 'ztp2' - expected causes are \['ZTP', 'USER', 'FPGA', 'USER_HITLESS'\]"): |
| 325 |
convert_reload_cause(str_input) |