Skip to content

Commit c9920f6

Browse files
committed
fix: Add get_port_by_mac for DellOS9
Fixes #4 Signed-off-by: Rafał Głombiowski <[email protected]>
1 parent acd05ae commit c9920f6

File tree

2 files changed

+74
-0
lines changed
  • mfd_switchmanagement/vendors/dell/dell_os9
  • tests/unit/test_mfd_switchmanagement/test_vendors/test_dell/test_dell_os9

2 files changed

+74
-0
lines changed

mfd_switchmanagement/vendors/dell/dell_os9/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,33 @@ def default_ports(self, ports: str) -> None:
154154
]
155155
)
156156

157+
def get_port_by_mac(self, mac: str) -> str:
158+
"""
159+
Get port of switch with the specified MAC address.
160+
:param mac: device MAC address in AA:BB:CC:DD:EE:FF form
161+
:return: port name
162+
:raises SwitchException: if port not found
163+
:raises ValueError: if provided MAC address is incorrect
164+
"""
165+
if self.is_mac_address(mac):
166+
output = self._connection.send_command(f"show mac-address-table address {mac.lower()}")
167+
"""
168+
GK6031-DR12-S6000-19505#show mac-address-table address 68:05:ca:c1:c8:ea
169+
Codes: *N - VLT Peer Synced MAC
170+
*I - Internal MAC Address used for Inter Process Communication
171+
VlanId Mac Address Type Interface State
172+
1 68:05:ca:c1:c8:ea Dynamic Te 0/32 Active
173+
"""
174+
pattern = r"\s+\d+\s+(?:[0-9a-f]{2}:){5}[0-9a-f]{2}\s+\w+\s+(.+?)\s+Active"
175+
match = re.search(pattern, output, re.IGNORECASE)
176+
177+
if match:
178+
return match.group(1) # return the port name
179+
else:
180+
raise SwitchException(f"Could not find port for MAC address {mac}")
181+
else:
182+
raise ValueError(f"Incorrect MAC address: {mac}")
183+
157184
def get_vlan_by_mac(self, mac: str) -> int:
158185
"""
159186
Get VLAN of port with the specified MAC address.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (C) 2025 Intel Corporation
2+
# SPDX-License-Identifier: MIT
3+
4+
from pytest import fixture, raises
5+
from textwrap import dedent
6+
7+
from mfd_switchmanagement import DellOS9
8+
from mfd_switchmanagement.exceptions import SwitchException
9+
10+
11+
class TestDellOS9:
12+
"""Class for DellOS9 tests."""
13+
14+
@fixture
15+
def switch(self, mocker) -> DellOS9:
16+
switch = DellOS9.__new__(DellOS9)
17+
switch.__init__ = mocker.create_autospec(switch.__init__, return_value=None)
18+
return switch
19+
20+
def test_get_port_by_mac(self, switch, mocker):
21+
"""Test get_port_by_mac method."""
22+
switch._connection = mocker.Mock()
23+
out = """
24+
Codes: *N - VLT Peer Synced MAC
25+
*I - Internal MAC Address used for Inter Process Communication
26+
VlanId Mac Address Type Interface State
27+
1 68:05:ca:c1:c8:ea Dynamic Te 0/32 Active
28+
"""
29+
switch._connection.send_command = mocker.Mock(return_value=dedent(out))
30+
assert switch.get_port_by_mac("68:05:ca:c1:c8:ea") == "Te 0/32"
31+
32+
def test_get_port_by_mac_not_found(self, switch, mocker):
33+
"""Test get_port_by_mac method when MAC address is not found."""
34+
switch._connection = mocker.Mock()
35+
out = """
36+
Codes: *N - VLT Peer Synced MAC
37+
*I - Internal MAC Address used for Inter Process Communication
38+
VlanId Mac Address Type Interface State
39+
"""
40+
switch._connection.send_command = mocker.Mock(return_value=dedent(out))
41+
with raises(SwitchException, match="Could not find port for MAC address 68:05:ca:c1:c8:ea"):
42+
switch.get_port_by_mac("68:05:ca:c1:c8:ea")
43+
44+
def test_get_port_by_mac_invalid_mac(self, switch):
45+
"""Test get_port_by_mac method with invalid MAC address."""
46+
with raises(ValueError, match="Incorrect MAC address: ZZ:ZZ:ZZ:ZZ:ZZ:ZZ"):
47+
switch.get_port_by_mac("ZZ:ZZ:ZZ:ZZ:ZZ:ZZ")

0 commit comments

Comments
 (0)