From d66fa523bf400e53190da5e93a95771877885ac7 Mon Sep 17 00:00:00 2001 From: twm2016 Date: Tue, 16 Feb 2016 16:41:46 -0600 Subject: [PATCH] Reduced restriction of parsing for dmidecode output Changed implementation to strip tokens up until the first 'Size: ' string. This will allow for less parsing errors in the first six lines of the following output: "dmidecode --type 17 | grep Size" returns: Maximum Memory Module Size: 4096 MB Maximum Total Memory Size: 8192 MB Size: 2048 MB Size: 2048 MB Added a condition in the exception handling to address the issue of the bug on other outputs like: Installed Size: Not Installed Enabled Size: Not Installed Size: No Module Installed Size: 1024 MB Common strings like "No Module Installed" and "Not Installed" are normal. These two strings are hard coded in the before mentioned comparison and when found are logged as warnings instead of errors. Change-Id: If3475afcebfc7af7e9256b99924919557c4d909c Closes-Bug: #1521202 --- ironic_python_agent/hardware.py | 16 ++++++++++++---- ironic_python_agent/tests/unit/test_hardware.py | 5 ++++- .../notes/add-unit-test-cc4a1a05859ad17d.yaml | 5 +++++ 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/add-unit-test-cc4a1a05859ad17d.yaml diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py index e15926a5c..97d64162b 100644 --- a/ironic_python_agent/hardware.py +++ b/ironic_python_agent/hardware.py @@ -421,7 +421,7 @@ class GenericHardwareManager(HardwareManager): total = int(psutil.phymem_usage().total) try: - out, _e = utils.execute("dmidecode --type memory | grep Size", + out, _e = utils.execute("dmidecode --type 17 | grep Size", shell=True) except (processutils.ProcessExecutionError, OSError) as e: LOG.warning("Cannot get real physical memory size: %s", e) @@ -433,12 +433,20 @@ class GenericHardwareManager(HardwareManager): if not line: continue + if 'Size:' not in line: + continue + + value = None try: - value = line.split(None, 1)[1].strip() + value = line.split('Size: ', 1)[1] physical += int(UNIT_CONVERTER(value).to_base_units()) except Exception as exc: - LOG.error('Cannot parse size expression %s: %s', - line, exc) + if (value == "No Module Installed" or + value == "Not Installed"): + LOG.debug('One memory slot is empty') + else: + LOG.error('Cannot parse size expression %s: %s', + line, exc) if not physical: LOG.warning('failed to get real physical RAM, dmidecode ' diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py index f3523da67..c02519e3c 100644 --- a/ironic_python_agent/tests/unit/test_hardware.py +++ b/ironic_python_agent/tests/unit/test_hardware.py @@ -421,7 +421,10 @@ class TestGenericHardwareManager(test_base.BaseTestCase): def test_get_memory(self, mocked_execute, mocked_usage): mocked_usage.return_value = mock.Mock(total=3952 * 1024 * 1024) mocked_execute.return_value = ( - "Foo\nSize: 2048 MB\nSize: 2 GB\n", + ("Foo\nSize: 2048 MB\nSize: 2 GB\n" + "Installed Size: Not Installed\n" + "Enabled Size: Not Installed\n" + "Size: No Module Installed\n"), "" ) diff --git a/releasenotes/notes/add-unit-test-cc4a1a05859ad17d.yaml b/releasenotes/notes/add-unit-test-cc4a1a05859ad17d.yaml new file mode 100644 index 000000000..f4c09eb52 --- /dev/null +++ b/releasenotes/notes/add-unit-test-cc4a1a05859ad17d.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - dmidecode output produces less parsing errors and logs + common and normal output like "No Module Installed" or + "Not Installed" in debug instead of error.