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
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
Reference in New Issue
Block a user