Merge "tests: Poison os.uname"

This commit is contained in:
Zuul 2021-03-04 12:27:38 +00:00 committed by Gerrit Code Review
commit db666e2118
7 changed files with 62 additions and 23 deletions

View File

@ -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):

View File

@ -3155,9 +3155,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):

View File

@ -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',

View File

@ -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

View File

@ -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')

View File

@ -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")

View File

@ -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: