Fix physical memory calculation with new lshw

It seems that fix Id5a30028b139c51cae6232cac73a50b917fea233 was
dealing with a different issue. According to the description
in the story, and the linked commit there, the problem is the
fact that output is changed from dictionary to a list (with just
one value supposedly?). This commit changes the isinstance call
to check if an output of lshw is a list, and if so, we just use
the first element of the list.

Story: 2007588
Task: 39527
Change-Id: I87d87fd035701303e7d530a47b682db84e72ccb9
This commit is contained in:
Vladyslav Drok 2020-11-06 19:03:56 +01:00
parent f52863a4d8
commit 448ded43fe
2 changed files with 14 additions and 2 deletions

View File

@ -1140,8 +1140,10 @@ class GenericHardwareManager(HardwareManager):
LOG.warning('Could not get real physical RAM from lshw: %s', e)
physical = None
else:
if isinstance(sys_dict, str):
sys_dict = json.loads(sys_dict)
# Depending on lshw version, output might be a list, starting with
# https://github.com/lyonel/lshw/commit/135a853c60582b14c5b67e5cd988a8062d9896f4 # noqa
if isinstance(sys_dict, list):
sys_dict = sys_dict[0]
physical = _calc_memory(sys_dict)
if not physical:

View File

@ -1697,6 +1697,16 @@ class TestGenericHardwareManager(base.IronicAgentTest):
self.assertEqual(3952 * 1024 * 1024, mem.total)
self.assertEqual(3952, mem.physical_mb)
@mock.patch('psutil.virtual_memory', autospec=True)
@mock.patch.object(utils, 'execute', autospec=True)
def test_get_memory_lshw_list(self, mocked_execute, mocked_psutil):
mocked_psutil.return_value.total = 3952 * 1024 * 1024
mocked_execute.return_value = (f"[{LSHW_JSON_OUTPUT_V2[0]}]", "")
mem = self.hardware.get_memory()
self.assertEqual(3952 * 1024 * 1024, mem.total)
self.assertEqual(65536, mem.physical_mb)
@mock.patch('ironic_python_agent.netutils.get_hostname', autospec=True)
def test_list_hardware_info(self, mocked_get_hostname):
self.hardware.list_network_interfaces = mock.Mock()