summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladyslav Drok <vdrok@mirantis.com>2020-11-06 19:03:56 +0100
committerVladyslav Drok <vdrok@mirantis.com>2020-11-16 13:47:05 +0000
commit8c38177e0ed5f4e05a23f6d1514be2efab8ffb4d (patch)
tree4a3e91a523844e677e7c87350c5bc45b00c230ef
parent0ed5ebe4048fbae9141f5cfefe27c2f5c1c2a4ca (diff)
downloadironic-python-agent-8c38177e0ed5f4e05a23f6d1514be2efab8ffb4d.tar.gz
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 (cherry picked from commit 448ded43fe1cfea9247dbd5612715124e5abbb55)
-rw-r--r--ironic_python_agent/hardware.py6
-rw-r--r--ironic_python_agent/tests/unit/test_hardware.py10
2 files changed, 14 insertions, 2 deletions
diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py
index f8107b55..a81b4638 100644
--- a/ironic_python_agent/hardware.py
+++ b/ironic_python_agent/hardware.py
@@ -1111,8 +1111,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:
diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py
index b9348912..4656311a 100644
--- a/ironic_python_agent/tests/unit/test_hardware.py
+++ b/ironic_python_agent/tests/unit/test_hardware.py
@@ -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()