Skip to content

Commit 6600a71

Browse files
author
Erwan Velu
committed
detect: Adding test code for lldp parsing
As the output could be weird, let's add some code to handle the testability of it
1 parent f1a1818 commit 6600a71

3 files changed

Lines changed: 41 additions & 22 deletions

File tree

src/detect.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import sys
3636
import xml.etree.ElementTree as ET
3737
import re
38+
import detect_utils
3839

3940
SIOCGIFNETMASK = 0x891b
4041

@@ -498,28 +499,7 @@ def find_element(xml, xml_spec, sys_subtype,
498499
find_element(elt, 'serial', 'serial', name.text, 'network',
499500
transform=string.lower)
500501

501-
lines = output_lines("lldptool -t -n -i %s" % name.text)
502-
content = ""
503-
header = ""
504-
sub_header = ""
505-
for line in lines:
506-
if line.startswith('\t'):
507-
content = line.strip().strip('\n').strip('\t').replace("/", "_")
508-
else:
509-
header = line
510-
header = line.strip().strip('\n').strip('\t').replace("/", "_").replace(" TLV", "")
511-
content = ""
512-
sub_header = ""
513-
if header and content:
514-
if ":" in content:
515-
left, right = content.split(": ")
516-
# If we never had this sub_header for this header
517-
# let's add one
518-
if (left != sub_header):
519-
sub_header = left
520-
header = header + "/" + sub_header
521-
content = right
522-
hw_lst.append(('lldp', name.text, header, content))
502+
detect_utils.get_lld_status(hw_lst, name.text)
523503

524504
for elt in xml.findall(".//node[@class='processor']"):
525505
name = elt.find('physid')

src/detect_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import detect
2+
3+
4+
def parse_lldtool(hw_lst, interface_name, lines):
5+
content = ""
6+
header = ""
7+
sub_header = ""
8+
for line in lines:
9+
if line.startswith('\t'):
10+
content = line.strip().strip('\n').strip('\t').replace("/", "_")
11+
else:
12+
header = line
13+
header = line.strip().strip('\n').strip('\t').replace("/", "_").replace(" TLV", "")
14+
content = ""
15+
sub_header = ""
16+
if header and content:
17+
if ":" in content:
18+
left, right = content.split(": ")
19+
# If we never had this sub_header for this header
20+
# let's add one
21+
if (left != sub_header):
22+
sub_header = left
23+
header = header + "/" + sub_header
24+
content = right
25+
hw_lst.append(('lldp', interface_name, header, content))
26+
27+
return hw_lst
28+
29+
30+
def get_lld_status(hw_lst, interface_name):
31+
return parse_lldtool(hw_lst, interface_name, detect.output_lines("lldptool -t -n -i %s" % interface_name))

src/test_detect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import unittest
2222

2323
import detect
24+
import detect_utils
2425

2526

2627
class Keeper:
@@ -59,6 +60,7 @@ def _save_functions(self, nbproc, nbphys):
5960
self.saved_ntoa = socket.inet_ntoa
6061
self.saved_ioctl = fcntl.ioctl
6162
self.saved_get_uuid = detect.get_uuid
63+
self.saved_lld_status = detect_utils.get_lld_status
6264

6365
def fake(x):
6466
return (0, nbproc)
@@ -72,6 +74,9 @@ def fake_ioctl(arg, arg2, arg3):
7274
def fake_get_uuid():
7375
return '83462C81-52BA-11CB-870F'
7476

77+
def fake_lld_status(arg, arg1):
78+
return []
79+
7580
detect.cmd = fake
7681
keeper = Keeper('detect.output_lines',
7782
[('vmx', ) for idx in range(nbphys)] +
@@ -84,13 +89,16 @@ def fake_get_uuid():
8489
socket.inet_ntoa = fake_ntoa
8590
fcntl.ioctl = fake_ioctl
8691
detect.get_uuid = fake_get_uuid
92+
detect_utils.get_lld_status = fake_lld_status
8793

8894
def _restore_functions(self):
8995
detect.cmd = self.save
9096
detect.output_lines = self.output_lines
9197
socket.inet_ntoa = self.saved_ntoa
9298
fcntl.ioctl = self.saved_ioctl
9399
detect.get_uuid = self.saved_get_uuid
100+
detect_utils.get_lld_status = self.saved_lld_status
101+
94102

95103
def test_detect_system_3(self):
96104
l = []

0 commit comments

Comments
 (0)