From ecd218b1a4408708156e6adb00e05f345dfbce36 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 5 Jun 2020 11:22:07 +0100 Subject: [PATCH] Correct reported system memory In Python 3.3+, 'sys.platform' no longer includes a major version [1]: issue 12326: On Linux, sys.platform doesn't contain the major version anymore. It is now always 'linux', instead of 'linux2' or 'linux3' depending on the Linux version used to build Python. Replace sys.platform == 'linux2' with sys.platform.startswith('linux'), or directly sys.platform == 'linux' if you don't need to support older Python versions. Since we only care about Python 3.6 or greater, we could simply check for 'linux'. However, the libvirt driver isn't supported on non-Linux hosts so this was always a dumb check. Simply remove it instead. [1] https://docs.python.org/3.3/whatsnew/3.3.html#porting-python-code Change-Id: I207a6c8c8cf562477792c69e353ba99b3ec4cb5e Signed-off-by: Stephen Finucane Closes-Bug: #1882233 (cherry picked from commit e567adf7f6f27f51cda3cd3422df462148f27b90) --- nova/tests/unit/virt/libvirt/test_host.py | 31 ++++++++--------------- nova/virt/libvirt/host.py | 4 --- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/nova/tests/unit/virt/libvirt/test_host.py b/nova/tests/unit/virt/libvirt/test_host.py index 810dd9cd0b5b..c9411d6eb756 100644 --- a/nova/tests/unit/virt/libvirt/test_host.py +++ b/nova/tests/unit/virt/libvirt/test_host.py @@ -958,11 +958,9 @@ SwapCached: 0 kB Active: 8381604 kB """) with test.nested( - mock.patch.object(six.moves.builtins, "open", m, create=True), - mock.patch.object(host.Host, - "get_connection"), - mock.patch('sys.platform', 'linux2'), - ) as (mock_file, mock_conn, mock_platform): + mock.patch.object(builtins, "open", m, create=True), + mock.patch.object(host.Host, "get_connection"), + ) as (mock_file, mock_conn): mock_conn().getInfo.return_value = [ obj_fields.Architecture.X86_64, 15814, 8, 1208, 1, 1, 4, 2] @@ -1002,8 +1000,7 @@ Active: 8381604 kB "list_guests"), mock.patch.object(libvirt_driver.LibvirtDriver, "_conn"), - mock.patch('sys.platform', 'linux2'), - ) as (mock_file, mock_list, mock_conn, mock_platform): + ) as (mock_file, mock_list, mock_conn): mock_list.return_value = [ libvirt_guest.Guest(DiagFakeDomain(0, 15814)), libvirt_guest.Guest(DiagFakeDomain(1, 750)), @@ -1016,10 +1013,9 @@ Active: 8381604 kB def test_get_memory_used_xen(self): self.flags(virt_type='xen', group='libvirt') - with test.nested( - mock.patch.object(self.host, "_sum_domain_memory_mb"), - mock.patch('sys.platform', 'linux2') - ) as (mock_sumDomainMemory, mock_platform): + with mock.patch.object( + self.host, "_sum_domain_memory_mb" + ) as mock_sumDomainMemory: mock_sumDomainMemory.return_value = 8192 self.assertEqual(8192, self.host.get_memory_mb_used()) mock_sumDomainMemory.assert_called_once_with(include_host=True) @@ -1042,11 +1038,7 @@ Active: 8381604 kB def UUIDString(self): return uuids.fake - with test.nested( - mock.patch.object(host.Host, - "list_guests"), - mock.patch('sys.platform', 'linux2'), - ) as (mock_list, mock_platform): + with mock.patch.object(host.Host, 'list_guests') as mock_list: mock_list.return_value = [ libvirt_guest.Guest(DiagFakeDomain(0, 4096)), libvirt_guest.Guest(DiagFakeDomain(1, 2048)), @@ -1060,10 +1052,9 @@ Active: 8381604 kB self.flags(file_backed_memory=1048576, group='libvirt') - with test.nested( - mock.patch.object(self.host, "_sum_domain_memory_mb"), - mock.patch('sys.platform', 'linux2') - ) as (mock_sumDomainMemory, mock_platform): + with mock.patch.object( + self.host, "_sum_domain_memory_mb" + ) as mock_sumDomainMemory: mock_sumDomainMemory.return_value = 8192 self.assertEqual(8192, self.host.get_memory_mb_used()) mock_sumDomainMemory.assert_called_once_with(include_host=False) diff --git a/nova/virt/libvirt/host.py b/nova/virt/libvirt/host.py index 3ebbdbcf9138..6f9eeca32583 100644 --- a/nova/virt/libvirt/host.py +++ b/nova/virt/libvirt/host.py @@ -32,7 +32,6 @@ import inspect import operator import os import socket -import sys import threading import traceback @@ -1103,9 +1102,6 @@ class Host(object): :returns: the total usage of memory(MB). """ - if sys.platform.upper() not in ['LINUX2', 'LINUX3']: - return 0 - if CONF.libvirt.virt_type == 'xen': # For xen, report the sum of all domains, with return self._sum_domain_memory_mb(include_host=True)