Skip to content

Commit 6ac70f2

Browse files
committed
Fix #719: convert macaddr in all sorts of forms to std format
Signed-off-by: Dinesh Dutt <[email protected]>
1 parent 3d2cb9d commit 6ac70f2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

suzieq/shared/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,10 @@ def get_timestamp_from_junos_time(in_data, timestamp: int):
452452

453453

454454
def convert_macaddr_format_to_colon(macaddr: str) -> str:
455-
"""Convert NXOS/EOS . macaddr form to standard : format, lowecase
455+
"""Convert various macaddr forms to standard ':' format, lowecase
456+
457+
One unexpected side-effect, it'll convert the given string to lowercase
458+
even if it doesn't match a macaddr.
456459
457460
:param macaddr: str, the macaddr string to convert
458461
:returns: the converted macaddr string or all 0s string if arg not str
@@ -471,15 +474,16 @@ def convert_macaddr_format_to_colon(macaddr: str) -> str:
471474
if re.match(r'[0-9a-f]{4}:[0-9a-f]{4}:[0-9a-f]{4}', macaddr):
472475
return (':'.join([f'{x[:2]}:{x[2:]}'
473476
for x in macaddr.split(':')]))
474-
if re.match(r'[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}', macaddr):
475-
return (':'.join([f'{x[:2]}:{x[2:]}'
476-
for x in macaddr.split('-')]))
477477
if ':' not in macaddr and re.match(r'[0-9a-f]{12}', macaddr):
478478
newmac = ''
479479
for i in range(0, 12, 2):
480480
newmac += f'{macaddr[i:i+2]}:'
481481
newmac = newmac[:-1] # remove the trailing ':'
482482
return newmac
483+
if re.match(r'[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}', macaddr):
484+
return (':'.join([f'{x[:2]}:{x[2:]}'
485+
for x in macaddr.split('-')]))
486+
return macaddr
483487

484488
return '00:00:00:00:00:00'
485489

tests/unit/test_mac_convert.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,15 @@ def test_mac_convert():
2121
assert convert_macaddr_format_to_colon('50-9A-4C-36-A1-DF') == result, \
2222
'failed to convert 50-9A-4C-36-A1-DF'
2323

24-
assert convert_macaddr_format_to_colon('50-9Z-4C-36-A1-DF') == nullmac, \
24+
assert convert_macaddr_format_to_colon(
25+
'50-9Z-4C-36-A1-DF') == '50-9z-4c-36-a1-df', \
2526
'Incorrect conversion of 50-9Z-4C-36-A1-DF'
27+
28+
assert convert_macaddr_format_to_colon(result) == result, \
29+
f'Failed to return {result} as is'
30+
31+
assert convert_macaddr_format_to_colon(1) == nullmac, \
32+
f'Incorrect handling when int is passed'
33+
34+
assert convert_macaddr_format_to_colon([1]) == nullmac, \
35+
f'Incorrect handling when list is passed'

0 commit comments

Comments
 (0)