Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion mfd_network_adapter/network_adapter_owner/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@

InterfaceInfoType = Union[InterfaceInfo, WindowsInterfaceInfo, LinuxInterfaceInfo]

_nat_key_re = re.compile(r"(\d+)")


def natural_key(interface_name: str) -> list[str | int]:
"""
Use as a sorter key to properly sort interfaces.

This resolves sorting cases like ["eth0", "eth9", "eth10", "eth1"].

:param interface_name: interface name
:return: list of strings and integers
"""
parts = _nat_key_re.split(interface_name)
return [int(p) if p.isdigit() else p for p in parts]


class NetworkAdapterOwner:
"""Class for utility."""
Expand Down Expand Up @@ -525,7 +540,8 @@ def _filter_interfaces_info(
return []

if interface_indexes:
return [selected[idx] for idx in interface_indexes]
sorted_selected = sorted(selected, key=lambda i: natural_key(i.name))
return [sorted_selected[idx] for idx in interface_indexes]

# by default ALL flag will be set
if random_interface is None and all_interfaces is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,29 @@ def test_filter_interfaces_info(self, owner):
assert len(filtered) == 1
assert filtered[0].pci_address == _pci_address

# additional coverage: check that interface_indexes are counted against naturally sorted names
# prepare non-lexicographic names: eth0, eth9, eth10, eth1
extra_infos = [
InterfaceInfo(pci_address=_pci_address, pci_device=_pci_device, name="eth0"),
InterfaceInfo(pci_address=_pci_address, pci_device=_pci_device, name="eth9"),
InterfaceInfo(pci_address=_pci_address, pci_device=_pci_device, name="eth10"),
InterfaceInfo(pci_address=_pci_address, pci_device=_pci_device, name="eth1"),
]

filtered_extra = owner._filter_interfaces_info(
extra_infos,
pci_address=_pci_address,
interface_indexes=[0, 1, 2, 3],
)
assert [i.name for i in filtered_extra] == ["eth0", "eth1", "eth9", "eth10"]

filtered_middle = owner._filter_interfaces_info(
extra_infos,
pci_address=_pci_address,
interface_indexes=[1, 2],
)
assert [i.name for i in filtered_middle] == ["eth1", "eth9"]


class TestSortedInterfaces:
@pytest.fixture
Expand Down Expand Up @@ -426,3 +449,30 @@ def test_sorted_interfaces_error(self, mocker, interfaces):
NetworkInterfaceIncomparableObject, match="Incorrect object passed for comparison with PCIAddress"
):
sorted(interfaces)


class TestNaturalKey:
def test_natural_key_simple_eth(self):
from mfd_network_adapter.network_adapter_owner.base import natural_key

names = ["eth0", "eth9", "eth10", "eth1"]
assert sorted(names, key=natural_key) == ["eth0", "eth1", "eth9", "eth10"]

def test_natural_key_mixed_patterns(self):
from mfd_network_adapter.network_adapter_owner.base import natural_key

names = [
"enp0s10f1v2",
"enp0s2f1v10",
"enp0s2f1v2",
"Ethernet 10",
"Ethernet 2",
]

assert sorted(names, key=natural_key) == [
"Ethernet 2",
"Ethernet 10",
"enp0s2f1v2",
"enp0s2f1v10",
"enp0s10f1v2",
]
Loading