From f7e3e179911e0a7fbad0d108c202c475560dbcea Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Fri, 12 Feb 2021 15:53:46 +0000 Subject: [PATCH] tests: Poison os.uname We shouldn't be looking up the architecture of the test node during tests to ensure tests work across nodes with varying architectures. Change-Id: I458b1db091e33c0b835e44b5a86de6c0a08f99a3 Signed-off-by: Stephen Finucane Co-authored-by: Lee Yarwood --- nova/objects/fields.py | 2 +- nova/tests/fixtures.py | 11 +++-- nova/tests/unit/objects/test_fields.py | 7 ++- nova/tests/unit/virt/disk/vfs/test_guestfs.py | 15 ++++-- nova/tests/unit/virt/libvirt/test_vif.py | 46 ++++++++++++++----- nova/virt/disk/vfs/guestfs.py | 2 +- nova/virt/libvirt/vif.py | 2 +- 7 files changed, 62 insertions(+), 23 deletions(-) diff --git a/nova/objects/fields.py b/nova/objects/fields.py index d9d15cc4c7dc..5b384df941f6 100644 --- a/nova/objects/fields.py +++ b/nova/objects/fields.py @@ -177,7 +177,7 @@ class Architecture(BaseNovaEnum): :returns: the canonicalized host architecture """ - return cls.canonicalize(os.uname()[4]) + return cls.canonicalize(os.uname().machine) @classmethod def is_valid(cls, name): diff --git a/nova/tests/fixtures.py b/nova/tests/fixtures.py index 58fe876b4a88..fe729adb0b1d 100644 --- a/nova/tests/fixtures.py +++ b/nova/tests/fixtures.py @@ -3145,9 +3145,14 @@ class CyborgFixture(fixtures.Fixture): class GenericPoisonFixture(fixtures.Fixture): POISON_THESE = ( - ('netifaces.interfaces', - 'a test environment should not be inspecting real interfaces on the ' - 'test node'), + ( + 'netifaces.interfaces', + 'tests should not be inspecting real interfaces on the test node', + ), + ( + 'os.uname', + 'tests should not be inspecting host information on the test node', + ), ) def setUp(self): diff --git a/nova/tests/unit/objects/test_fields.py b/nova/tests/unit/objects/test_fields.py index 47af33f65304..39f9de8cfe7e 100644 --- a/nova/tests/unit/objects/test_fields.py +++ b/nova/tests/unit/objects/test_fields.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import collections import datetime import os @@ -27,6 +28,10 @@ from nova import test from nova.tests.unit import fake_instance from nova import utils +os_uname = collections.namedtuple( + 'uname_result', ['sysname', 'nodename', 'release', 'version', 'machine'], +) + class FakeFieldType(fields.FieldType): def coerce(self, obj, attr, value): @@ -187,7 +192,7 @@ class TestEnum(TestField): class TestArchitecture(TestField): @mock.patch.object(os, 'uname') def test_host(self, mock_uname): - mock_uname.return_value = ( + mock_uname.return_value = os_uname( 'Linux', 'localhost.localdomain', '3.14.8-200.fc20.x86_64', diff --git a/nova/tests/unit/virt/disk/vfs/test_guestfs.py b/nova/tests/unit/virt/disk/vfs/test_guestfs.py index 87f58ef357ec..b1c619c95588 100644 --- a/nova/tests/unit/virt/disk/vfs/test_guestfs.py +++ b/nova/tests/unit/virt/disk/vfs/test_guestfs.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import collections + import fixtures import mock @@ -21,6 +23,10 @@ from nova.tests.unit.virt.disk.vfs import fakeguestfs from nova.virt.disk.vfs import guestfs as vfsimpl from nova.virt.image import model as imgmodel +os_uname = collections.namedtuple( + 'uname_result', ['sysname', 'nodename', 'release', 'version', 'machine'], +) + class VirtDiskVFSGuestFSTest(test.NoDBTestCase): def setUp(self): @@ -326,10 +332,11 @@ class VirtDiskVFSGuestFSTest(test.NoDBTestCase): self.assertFalse(setup_os.called) @mock.patch('os.access') - @mock.patch('os.uname', return_value=('Linux', '', 'kernel_name')) - def test_appliance_setup_inspect_capabilties_fail_with_ubuntu(self, - mock_uname, - mock_access): + @mock.patch('os.uname', return_value=os_uname( + 'Linux', '', 'kernel_name', '', '')) + def test_appliance_setup_inspect_capabilties_fail_with_ubuntu( + self, mock_uname, mock_access, + ): # In ubuntu os will default host kernel as 600 permission m = mock.MagicMock() m.launch.side_effect = Exception diff --git a/nova/tests/unit/virt/libvirt/test_vif.py b/nova/tests/unit/virt/libvirt/test_vif.py index f842f51c9369..f50c75953e72 100644 --- a/nova/tests/unit/virt/libvirt/test_vif.py +++ b/nova/tests/unit/virt/libvirt/test_vif.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +import collections + import fixtures from lxml import etree import mock @@ -36,6 +38,11 @@ from nova.virt.libvirt import vif CONF = cfg.CONF +os_uname = collections.namedtuple( + 'uname_result', ['sysname', 'nodename', 'release', 'version', 'machine'], +) + + class LibvirtVifTestCase(test.NoDBTestCase): gateway_bridge_4 = network_model.IP(address='101.168.1.1', type='gateway') @@ -497,11 +504,20 @@ class LibvirtVifTestCase(test.NoDBTestCase): def setUp(self): super(LibvirtVifTestCase, self).setUp() + self.useFixture(fakelibvirt.FakeLibvirtFixture(stub_os_vif=False)) + # os_vif.initialize is typically done in nova-compute startup os_vif.initialize() self.setup_os_vif_objects() + # multiqueue configuration is host OS specific + _a = mock.patch('os.uname') + self.mock_uname = _a.start() + self.mock_uname.return_value = os_uname( + 'Linux', '', '5.10.13-200-generic', '', 'x86_64') + self.addCleanup(_a.stop) + def _get_node(self, xml): doc = etree.fromstring(xml) ret = doc.findall('./devices/interface') @@ -636,30 +652,36 @@ class LibvirtVifTestCase(test.NoDBTestCase): def test_virtio_multiqueue(self): self._test_virtio_multiqueue(4, '4') - @mock.patch('os.uname', return_value=('Linux', '', '2.6.32-21-generic')) - def test_virtio_multiqueue_in_kernel_2(self, mock_uname): + def test_virtio_multiqueue_in_kernel_2(self): + self.mock_uname.return_value = os_uname( + 'Linux', '', '2.6.32-21-generic', '', '') self._test_virtio_multiqueue(10, '1') - @mock.patch('os.uname', return_value=('Linux', '', '3.19.0-47-generic')) - def test_virtio_multiqueue_in_kernel_3(self, mock_uname): + def test_virtio_multiqueue_in_kernel_3(self): + self.mock_uname.return_value = os_uname( + 'Linux', '', '3.19.0-47-generic', '', '') self._test_virtio_multiqueue(10, '8') - @mock.patch('os.uname', return_value=('Linux', '', '4.2.0-35-generic')) - def test_virtio_multiqueue_in_kernel_4(self, mock_uname): + def test_virtio_multiqueue_in_kernel_4(self): + self.mock_uname.return_value = os_uname( + 'Linux', '', '4.2.0-35-generic', '', '') self._test_virtio_multiqueue(10, '10') - @mock.patch('os.uname', return_value=('Linux', '', '2.6.32-21-generic')) - def test_virtio_multiqueue_in_kernel_2_max_queues(self, mock_uname): + def test_virtio_multiqueue_in_kernel_2_max_queues(self): + self.mock_uname.return_value = os_uname( + 'Linux', '', '2.6.32-21-generic', '', '') self.flags(max_queues=2, group='libvirt') self._test_virtio_multiqueue(10, '2') - @mock.patch('os.uname', return_value=('Linux', '', '3.19.0-47-generic')) - def test_virtio_multiqueue_in_kernel_3_max_queues(self, mock_uname): + def test_virtio_multiqueue_in_kernel_3_max_queues(self): + self.mock_uname.return_value = os_uname( + 'Linux', '', '3.19.0-47-generic', '', '') self.flags(max_queues=2, group='libvirt') self._test_virtio_multiqueue(10, '2') - @mock.patch('os.uname', return_value=('Linux', '', '4.2.0-35-generic')) - def test_virtio_multiqueue_in_kernel_4_max_queues(self, mock_uname): + def test_virtio_multiqueue_in_kernel_4_max_queues(self): + self.mock_uname.return_value = os_uname( + 'Linux', '', '4.2.0-35-generic', '', '') self.flags(max_queues=2, group='libvirt') self._test_virtio_multiqueue(10, '2') diff --git a/nova/virt/disk/vfs/guestfs.py b/nova/virt/disk/vfs/guestfs.py index da8a39210117..90586ef8f176 100644 --- a/nova/virt/disk/vfs/guestfs.py +++ b/nova/virt/disk/vfs/guestfs.py @@ -85,7 +85,7 @@ class VFSGuestFS(vfs.VFS): g.add_drive("/dev/null") # sic g.launch() except Exception as e: - kernel_file = "/boot/vmlinuz-%s" % os.uname()[2] + kernel_file = "/boot/vmlinuz-%s" % os.uname().release if not os.access(kernel_file, os.R_OK): raise exception.LibguestfsCannotReadKernel( _("Please change permissions on %s to 0x644") diff --git a/nova/virt/libvirt/vif.py b/nova/virt/libvirt/vif.py index 34f0893bc683..2d1751c10481 100644 --- a/nova/virt/libvirt/vif.py +++ b/nova/virt/libvirt/vif.py @@ -288,7 +288,7 @@ class LibvirtGenericVIFDriver(object): # In kernels 3.x, the number of queues on a tap interface # is limited to 8. From 4.0, the number is 256. # See: https://bugs.launchpad.net/nova/+bug/1570631 - kernel_version = int(os.uname()[2].split(".")[0]) + kernel_version = int(os.uname().release.split(".")[0]) if kernel_version <= 2: return 1 elif kernel_version == 3: