Skip to content

Commit b7ae499

Browse files
committed
Remove support for older psutil versions
Global requirements was recently updated to force psutil=>3.0.1. This patch removes support for older versions of psutil as well as changing to opportunistically attempt to work if a version >5 is released but doesn't change the interface we use. Change-Id: I1f7fab33fd275fb8b5cd7704dc13375402756d06 Related-bug: #1659137
1 parent e4919e0 commit b7ae499

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

ironic_python_agent/hardware.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,16 +594,12 @@ def get_cpus(self):
594594

595595
def get_memory(self):
596596
# psutil returns a long, so we force it to an int
597-
if psutil.version_info[0] == 1:
598-
total = int(psutil.TOTAL_PHYMEM)
599-
elif psutil.version_info[0] == 2:
600-
total = int(psutil.phymem_usage().total)
601-
elif psutil.version_info[0] == 5:
597+
try:
602598
total = int(psutil.virtual_memory().total)
603-
else:
599+
except Exception:
604600
total = None
605-
LOG.warning("Cannot fetch total memory size: unsupported psutil "
606-
"version %s", psutil.version_info[0])
601+
LOG.exception(("Cannot fetch total memory size using psutil "
602+
"version %s"), psutil.version_info[0])
607603

608604
try:
609605
out, _e = utils.execute("dmidecode --type 17 | grep Size",

ironic_python_agent/tests/unit/test_hardware.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@
239239
flags : fpu vme de pse
240240
"""
241241

242+
DMIDECODE_MEMORY_OUTPUT = ("""
243+
Foo
244+
Size: 2048 MB
245+
Size: 2 GB
246+
Installed Size: Not Installed
247+
Enabled Size: Not Installed
248+
Size: No Module Installed
249+
""", "")
250+
242251

243252
class FakeHardwareManager(hardware.GenericHardwareManager):
244253
def __init__(self, hardware_support):
@@ -728,24 +737,26 @@ def test_get_cpus_illegal_flags(self, mocked_execute):
728737
self.assertEqual('x86_64', cpus.architecture)
729738
self.assertEqual([], cpus.flags)
730739

731-
@mock.patch('psutil.version_info', (5, 0))
732740
@mock.patch('psutil.virtual_memory', autospec=True)
733-
@mock.patch.object(utils, 'execute')
734-
def test_get_memory(self, mocked_execute, mocked_virtmem):
735-
mocked_virtmem.return_value.total = 3952 * 1024 * 1024
736-
mocked_execute.return_value = (
737-
("Foo\nSize: 2048 MB\nSize: 2 GB\n"
738-
"Installed Size: Not Installed\n"
739-
"Enabled Size: Not Installed\n"
740-
"Size: No Module Installed\n"),
741-
""
742-
)
743-
741+
@mock.patch.object(utils, 'execute', autospec=True)
742+
def test_get_memory_psutil(self, mocked_execute, mocked_psutil):
743+
mocked_psutil.return_value.total = 3952 * 1024 * 1024
744+
mocked_execute.return_value = DMIDECODE_MEMORY_OUTPUT
744745
mem = self.hardware.get_memory()
745746

746747
self.assertEqual(3952 * 1024 * 1024, mem.total)
747748
self.assertEqual(4096, mem.physical_mb)
748749

750+
@mock.patch('psutil.virtual_memory', autospec=True)
751+
@mock.patch.object(utils, 'execute', autospec=True)
752+
def test_get_memory_psutil_exception(self, mocked_execute, mocked_psutil):
753+
mocked_execute.return_value = DMIDECODE_MEMORY_OUTPUT
754+
mocked_psutil.side_effect = AttributeError()
755+
mem = self.hardware.get_memory()
756+
757+
self.assertIsNone(mem.total)
758+
self.assertEqual(4096, mem.physical_mb)
759+
749760
def test_list_hardware_info(self):
750761
self.hardware.list_network_interfaces = mock.Mock()
751762
self.hardware.list_network_interfaces.return_value = [

0 commit comments

Comments
 (0)