Skip to content

Commit

Permalink
Fix(plugins): Support zeroes in interface numbers for range_expand (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausHolbechArista authored Sep 5, 2024
1 parent 107d309 commit 25da256
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
7 changes: 4 additions & 3 deletions python-avd/pyavd/j2filters/range_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from __future__ import annotations

import re
from typing import Any


def range_expand(range_to_expand):
def range_expand(range_to_expand: Any) -> list:
if not isinstance(range_to_expand, (list, str)):
raise TypeError(f"value must be of type list or str, got {type(range_to_expand)}")

Expand Down Expand Up @@ -115,7 +116,7 @@ def expand_interfaces(interface_string):

def expand_parent_interfaces(interface_string):
result = []
if last_parent_interface:
if last_parent_interface is not None:
if first_parent_interface > last_parent_interface:
raise ValueError(
f"Range {one_range} could not be expanded because the first interface {first_parent_interface} is larger than last"
Expand All @@ -131,7 +132,7 @@ def expand_parent_interfaces(interface_string):

def expand_module(interface_string):
result = []
if last_module:
if last_module is not None:
if first_module > last_module:
raise ValueError(
f"Range {one_range} could not be expanded because the first module {first_module} is larger than last module"
Expand Down
72 changes: 29 additions & 43 deletions python-avd/tests/pyavd/j2filters/test_range_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,40 @@
),
]

RANGE_TO_EXPAND_VALID_VALUES = [
"Ethernet1",
"Ethernet1-2",
"Eth 3-5,7-8",
"et2-6,po1-2",
["Ethernet1"],
["Ethernet 1-2", "Eth3-5", "7-8"],
["Ethernet2-6", "Port-channel1-2"],
["Ethernet1/1-2", "Eth1-2/3-5,5/1-2"],
["Eth1.1,9-10.1", "Eth2.2-3", "Eth3/1-2.3-4"],
"1-3",
["1", "2", "3"],
"vlan1-3",
"Et1-2/3-4/5-6",
"65100.0",
"65100.0-4",
"65100.0-2,65200.1-2",
"1-2.0-1",
]

EXPECTED_RESULT_VALID_VALUES = [
["Ethernet1"],
["Ethernet1", "Ethernet2"],
["Eth 3", "Eth 4", "Eth 5", "Eth 7", "Eth 8"],
["et2", "et3", "et4", "et5", "et6", "po1", "po2"],
["Ethernet1"],
["Ethernet 1", "Ethernet 2", "Eth3", "Eth4", "Eth5", "7", "8"],
["Ethernet2", "Ethernet3", "Ethernet4", "Ethernet5", "Ethernet6", "Port-channel1", "Port-channel2"],
["Ethernet1/1", "Ethernet1/2", "Eth1/3", "Eth1/4", "Eth1/5", "Eth2/3", "Eth2/4", "Eth2/5", "Eth5/1", "Eth5/2"],
["Eth1.1", "Eth9.1", "Eth10.1", "Eth2.2", "Eth2.3", "Eth3/1.3", "Eth3/1.4", "Eth3/2.3", "Eth3/2.4"],
["1", "2", "3"],
["1", "2", "3"],
["vlan1", "vlan2", "vlan3"],
["Et1/3/5", "Et1/3/6", "Et1/4/5", "Et1/4/6", "Et2/3/5", "Et2/3/6", "Et2/4/5", "Et2/4/6"],
["65100.0"],
["65100.0", "65100.1", "65100.2", "65100.3", "65100.4"],
["65100.0", "65100.1", "65100.2", "65200.1", "65200.2"],
["1.0", "1.1", "2.0", "2.1"],
RANGE_TO_EXPAND_VALID_TESTS = [
# (<input>, <expected_output>)
pytest.param("Ethernet1", ["Ethernet1"]),
pytest.param("Ethernet1-2", ["Ethernet1", "Ethernet2"]),
pytest.param("Eth 3-5,7-8", ["Eth 3", "Eth 4", "Eth 5", "Eth 7", "Eth 8"]),
pytest.param("et2-6,po1-2", ["et2", "et3", "et4", "et5", "et6", "po1", "po2"]),
pytest.param(["Ethernet1"], ["Ethernet1"]),
pytest.param(["Ethernet 1-2", "Eth3-5", "7-8"], ["Ethernet 1", "Ethernet 2", "Eth3", "Eth4", "Eth5", "7", "8"]),
pytest.param(["Ethernet2-6", "Port-channel1-2"], ["Ethernet2", "Ethernet3", "Ethernet4", "Ethernet5", "Ethernet6", "Port-channel1", "Port-channel2"]),
pytest.param(
["Ethernet1/1-2", "Eth1-2/3-5,5/1-2"], ["Ethernet1/1", "Ethernet1/2", "Eth1/3", "Eth1/4", "Eth1/5", "Eth2/3", "Eth2/4", "Eth2/5", "Eth5/1", "Eth5/2"]
),
pytest.param(
["Eth1.1,9-10.1", "Eth2.2-3", "Eth3/1-2.3-4"], ["Eth1.1", "Eth9.1", "Eth10.1", "Eth2.2", "Eth2.3", "Eth3/1.3", "Eth3/1.4", "Eth3/2.3", "Eth3/2.4"]
),
pytest.param("1-3", ["1", "2", "3"]),
pytest.param(["1", "2", "3"], ["1", "2", "3"]),
pytest.param("vlan1-3", ["vlan1", "vlan2", "vlan3"]),
pytest.param("Et1-2/3-4/5-6", ["Et1/3/5", "Et1/3/6", "Et1/4/5", "Et1/4/6", "Et2/3/5", "Et2/3/6", "Et2/4/5", "Et2/4/6"]),
pytest.param("65100.0", ["65100.0"]),
pytest.param("65100.0-4", ["65100.0", "65100.1", "65100.2", "65100.3", "65100.4"]),
pytest.param("65100.0-2,65200.1-2", ["65100.0", "65100.1", "65100.2", "65200.1", "65200.2"]),
pytest.param("1-2.0-1", ["1.0", "1.1", "2.0", "2.1"]),
pytest.param("Gi1/0/1-2", ["Gi1/0/1", "Gi1/0/2"]),
]


class TestRangeExpandFilter:
@pytest.mark.parametrize("input_value, expected_raise, expected_raise_message", RANGE_TO_EXPAND_INVALID_VALUES)
def test_range_expand_invalid(self, input_value, expected_raise, expected_raise_message):
def test_range_expand_invalid(self, input_value, expected_raise, expected_raise_message) -> None:
with pytest.raises(expected_raise, match=expected_raise_message):
range_expand(input_value)

@pytest.mark.parametrize("RANGE_TO_EXPAND_VALID", RANGE_TO_EXPAND_VALID_VALUES)
def test_range_expand_valid(self, RANGE_TO_EXPAND_VALID):
resp = range_expand(RANGE_TO_EXPAND_VALID)
assert resp in EXPECTED_RESULT_VALID_VALUES
@pytest.mark.parametrize(("range_to_expand", "expected_output"), RANGE_TO_EXPAND_VALID_TESTS)
def test_range_expand_valid(self, range_to_expand: list | str, expected_output: list) -> None:
resp = range_expand(range_to_expand)
assert resp == expected_output

0 comments on commit 25da256

Please sign in to comment.