Skip to content

Commit cff6c17

Browse files
committed
Fix up switch name inspector plugin
1 parent 00419b6 commit cff6c17

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

stackhpc_inspector_plugins/plugins/ib_physnet.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def __call__(self, task, inventory, plugin_data):
5454

5555
for interface in inventory['interfaces']:
5656
if interface['name'] not in plugin_data['all_interfaces']:
57+
LOG.debug("No processed data for interface %s on node %s, "
58+
"skipping physical network processing.",
59+
interface['name'], task.node.uuid)
5760
continue
5861

5962
mac_address = interface['mac_address']
@@ -70,8 +73,7 @@ def __call__(self, task, inventory, plugin_data):
7073
if phys_network is None:
7174
LOG.debug("Skipping physical network processing for interface "
7275
"%s on node %s - no physical network mapping.",
73-
mac_address,
74-
task.node.uuid)
76+
mac_address, task.node.uuid)
7577
continue
7678

7779
if getattr(port, 'physical_network', '') != phys_network:
@@ -135,18 +137,32 @@ def get_physical_network(self, interface, plugin_data):
135137
:param introspection_data: Introspection data.
136138
:returns: The physical network to set, or None.
137139
"""
138-
# Check if LLDP data was already processed by lldp_basic plugin
139-
# which stores data in 'all_interfaces'
140-
iface_name = interface['name']
141-
proc_data = plugin_data['all_interfaces'][iface_name]
142-
if 'parsed_lldp' not in proc_data:
140+
# Check if LLDP data was already processed
141+
if 'parsed_lldp' not in plugin_data:
142+
LOG.error("No LLDP data, parse_lldp hook is required. ")
143143
return
144144

145-
lldp_proc = proc_data['parsed_lldp']
145+
# check we have data for this interface
146+
iface_name = interface['name']
147+
lldp_proc = plugin_data['parsed_lldp'].get(iface_name)
148+
if not lldp_proc:
149+
LOG.debug("No LLDP data for interface %s", iface_name)
150+
return
146151

147152
# Switch system name mapping.
148153
switch_sys_name = lldp_proc.get(lldp_parsers.LLDP_SYS_NAME_NM)
149-
if switch_sys_name:
150-
mapping = self._get_switch_sys_name_mapping()
151-
if switch_sys_name in mapping:
152-
return mapping[switch_sys_name]
154+
if not switch_sys_name:
155+
LOG.debug("No switch system name in LLDP data for interface %s",
156+
iface_name)
157+
return
158+
159+
mapping = self._get_switch_sys_name_mapping()
160+
if switch_sys_name not in mapping:
161+
LOG.debug("No config set for switch system name %s for "
162+
"interface %s", switch_sys_name, iface_name)
163+
return
164+
165+
LOG.debug("Interface %s connected to switch with system name "
166+
"%s, physnet %s", iface_name, switch_sys_name,
167+
mapping[switch_sys_name])
168+
return mapping[switch_sys_name]

stackhpc_inspector_plugins/tests/unit/test_plugins_ib_physnet.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
'ipv6_address': 'fe80:5054::',
4040
'client_id': ('ff:00:00:00:00:00:02:00:00:02:c9:00:7c:fe:'
4141
'90:03:00:3a:4b:0a'),
42-
'parsed_lldp': {
43-
'switch_system_name': 'switch-1',
44-
}
4542
}
4643

4744
_INTERFACE_3 = {
@@ -56,7 +53,8 @@
5653
}
5754

5855
_PLUGIN_DATA = {
59-
'all_interfaces': {'em0': _INTERFACE_1, 'em1': _INTERFACE_2}
56+
'all_interfaces': {'em0': _INTERFACE_1, 'em1': _INTERFACE_2},
57+
'parsed_lldp': {'em1': {'switch_system_name': 'switch-1'}},
6058
}
6159

6260

@@ -106,7 +104,7 @@ def setUp(self):
106104
self.plugin_data = _PLUGIN_DATA
107105

108106
@mock.patch.object(objects.Port, 'list_by_node_id', autospec=True)
109-
def test_physical_network(self, mock_list_by_nodeid):
107+
def test_sys_name_success(self, mock_list_by_nodeid):
110108
sys_name_mapping = 'switch-1:ibphysnet,switch-2:physnet2'
111109
CONF.set_override('switch_sys_name_mapping', sys_name_mapping,
112110
group='port_physnet')
@@ -130,6 +128,32 @@ def test_physical_network(self, mock_list_by_nodeid):
130128
self.assertEqual(port2.physical_network, 'ibphysnet')
131129
self.assertIsNone(port1.physical_network)
132130

131+
@mock.patch.object(objects.Port, 'list_by_node_id', autospec=True)
132+
def test_sys_name_success_no_data(self, mock_list_by_nodeid):
133+
sys_name_mapping = 'switch-1:ibphysnet,switch-2:physnet2'
134+
CONF.set_override('switch_sys_name_mapping', sys_name_mapping,
135+
group='port_physnet')
136+
del _PLUGIN_DATA['parsed_lldp']
137+
with task_manager.acquire(self.context, self.node.id) as task:
138+
port1 = obj_utils.create_test_port(self.context,
139+
address='11:11:11:11:11:11',
140+
node_id=self.node.id)
141+
port2 = obj_utils.create_test_port(
142+
self.context, id=988,
143+
uuid='2be26c0b-03f2-4d2e-ae87-c02d7f33c781',
144+
address='22:22:22:22:22:22', node_id=self.node.id)
145+
ports = [port1, port2]
146+
147+
mock_list_by_nodeid.return_value = ports
148+
149+
ib_physnet.SystemNamePhysnetHook().__call__(
150+
task, self.inventory, self.plugin_data)
151+
152+
port1.refresh()
153+
port2.refresh()
154+
self.assertIsNone(port2.physical_network)
155+
self.assertIsNone(port1.physical_network)
156+
133157
def parse(self, mapping_list):
134158
return ib_physnet.parse_mappings(mapping_list)
135159

0 commit comments

Comments
 (0)