summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Faulkner <jay@jvf.cc>2017-01-25 08:52:46 -0800
committerJay Faulkner <jay@jvf.cc>2017-01-26 14:28:51 -0800
commitb7ae4995a751084c200a33367df57c09c61bc439 (patch)
tree4246016a8e146c0aa11ce366849788b7190c1aa5
parente4919e04aa250b51e351c4e9dbfb3761f5f93a70 (diff)
downloadironic-python-agent-b7ae4995a751084c200a33367df57c09c61bc439.tar.gz
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
-rw-r--r--ironic_python_agent/hardware.py12
-rw-r--r--ironic_python_agent/tests/unit/test_hardware.py35
2 files changed, 27 insertions, 20 deletions
diff --git a/ironic_python_agent/hardware.py b/ironic_python_agent/hardware.py
index 7fd4ee5a..68d738f0 100644
--- a/ironic_python_agent/hardware.py
+++ b/ironic_python_agent/hardware.py
@@ -594,16 +594,12 @@ class GenericHardwareManager(HardwareManager):
def get_memory(self):
# psutil returns a long, so we force it to an int
- if psutil.version_info[0] == 1:
- total = int(psutil.TOTAL_PHYMEM)
- elif psutil.version_info[0] == 2:
- total = int(psutil.phymem_usage().total)
- elif psutil.version_info[0] == 5:
+ try:
total = int(psutil.virtual_memory().total)
- else:
+ except Exception:
total = None
- LOG.warning("Cannot fetch total memory size: unsupported psutil "
- "version %s", psutil.version_info[0])
+ LOG.exception(("Cannot fetch total memory size using psutil "
+ "version %s"), psutil.version_info[0])
try:
out, _e = utils.execute("dmidecode --type 17 | grep Size",
diff --git a/ironic_python_agent/tests/unit/test_hardware.py b/ironic_python_agent/tests/unit/test_hardware.py
index db87b5f3..e7b8ed4c 100644
--- a/ironic_python_agent/tests/unit/test_hardware.py
+++ b/ironic_python_agent/tests/unit/test_hardware.py
@@ -239,6 +239,15 @@ CPUINFO_FLAGS_OUTPUT = """
flags : fpu vme de pse
"""
+DMIDECODE_MEMORY_OUTPUT = ("""
+Foo
+Size: 2048 MB
+Size: 2 GB
+Installed Size: Not Installed
+Enabled Size: Not Installed
+Size: No Module Installed
+""", "")
+
class FakeHardwareManager(hardware.GenericHardwareManager):
def __init__(self, hardware_support):
@@ -728,24 +737,26 @@ class TestGenericHardwareManager(test_base.BaseTestCase):
self.assertEqual('x86_64', cpus.architecture)
self.assertEqual([], cpus.flags)
- @mock.patch('psutil.version_info', (5, 0))
@mock.patch('psutil.virtual_memory', autospec=True)
- @mock.patch.object(utils, 'execute')
- def test_get_memory(self, mocked_execute, mocked_virtmem):
- mocked_virtmem.return_value.total = 3952 * 1024 * 1024
- mocked_execute.return_value = (
- ("Foo\nSize: 2048 MB\nSize: 2 GB\n"
- "Installed Size: Not Installed\n"
- "Enabled Size: Not Installed\n"
- "Size: No Module Installed\n"),
- ""
- )
-
+ @mock.patch.object(utils, 'execute', autospec=True)
+ def test_get_memory_psutil(self, mocked_execute, mocked_psutil):
+ mocked_psutil.return_value.total = 3952 * 1024 * 1024
+ mocked_execute.return_value = DMIDECODE_MEMORY_OUTPUT
mem = self.hardware.get_memory()
self.assertEqual(3952 * 1024 * 1024, mem.total)
self.assertEqual(4096, mem.physical_mb)
+ @mock.patch('psutil.virtual_memory', autospec=True)
+ @mock.patch.object(utils, 'execute', autospec=True)
+ def test_get_memory_psutil_exception(self, mocked_execute, mocked_psutil):
+ mocked_execute.return_value = DMIDECODE_MEMORY_OUTPUT
+ mocked_psutil.side_effect = AttributeError()
+ mem = self.hardware.get_memory()
+
+ self.assertIsNone(mem.total)
+ self.assertEqual(4096, mem.physical_mb)
+
def test_list_hardware_info(self):
self.hardware.list_network_interfaces = mock.Mock()
self.hardware.list_network_interfaces.return_value = [