libvirt: Mock libvirt'y things in setUp

While addressing a nit from change
I8e5a122cc547222249973cf595d90c2d8d5658d4, I realized pretty much every
test using the nova.tests.functional.libvirt.base.ServersTestBase' base
class was now mocking the same things. This results in a lot of noise
and distracts from what the tests are trying to do. Centralize the
common mocks in the base class and clean up said noise.

Change-Id: I5895865751e8e1fb08b3515bc9f8119cfcb9f35e
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2019-08-15 16:25:04 +01:00
parent 872a823d9a
commit 85ea4f703d
4 changed files with 30 additions and 120 deletions

View File

@ -15,6 +15,7 @@
import copy import copy
import fixtures import fixtures
import io
import mock import mock
from nova.objects import fields as obj_fields from nova.objects import fields as obj_fields
@ -45,7 +46,20 @@ class ServersTestBase(base.ServersTestBase):
self.useFixture(fakelibvirt.FakeLibvirtFixture()) self.useFixture(fakelibvirt.FakeLibvirtFixture())
self.useFixture(func_fixtures.PlacementFixture()) self.useFixture(func_fixtures.PlacementFixture())
self.stub_out('nova.privsep.utils.supports_direct_io', lambda _: True) self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.LibvirtDriver._create_image'))
self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.LibvirtDriver._get_local_gb_info',
return_value={'total': 128, 'used': 44, 'free': 84}))
self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True))
self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')]))
self.useFixture(fixtures.MockPatch(
'nova.privsep.utils.supports_direct_io',
return_value=True))
# Mock the 'get_connection' function, as we're going to need to provide # Mock the 'get_connection' function, as we're going to need to provide
# custom capabilities for each test # custom capabilities for each test

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import io
import six import six
import mock import mock
@ -57,14 +56,6 @@ class NUMAServersTestBase(base.ServersTestBase):
return self.start_service('scheduler') return self.start_service('scheduler')
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info',
new=mock.Mock(return_value={'total': 128,
'used': 44,
'free': 84}))
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
new=mock.Mock(return_value=True))
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
new=mock.Mock(side_effect=[io.BytesIO(b''), io.BytesIO(b'')]))
class NUMAServersTest(NUMAServersTestBase): class NUMAServersTest(NUMAServersTestBase):
def _run_build_test(self, flavor_id, end_status='ACTIVE'): def _run_build_test(self, flavor_id, end_status='ACTIVE'):
@ -188,16 +179,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
return self._wait_for_state_change(found_server, 'BUILD') return self._wait_for_state_change(found_server, 'BUILD')
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_create_server_with_single_physnet(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_create_server_with_single_physnet(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
extra_spec = {'hw:numa_nodes': '1'} extra_spec = {'hw:numa_nodes': '1'}
flavor_id = self._create_flavor(extra_spec=extra_spec) flavor_id = self._create_flavor(extra_spec=extra_spec)
networks = [ networks = [
@ -210,16 +192,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
self.assertTrue(self.mock_filter.called) self.assertTrue(self.mock_filter.called)
self.assertEqual('ACTIVE', status) self.assertEqual('ACTIVE', status)
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_create_server_with_multiple_physnets(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_create_server_with_multiple_physnets(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
"""Test multiple networks split across host NUMA nodes. """Test multiple networks split across host NUMA nodes.
This should pass because the networks requested are split across This should pass because the networks requested are split across
@ -239,16 +212,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
self.assertTrue(self.mock_filter.called) self.assertTrue(self.mock_filter.called)
self.assertEqual('ACTIVE', status) self.assertEqual('ACTIVE', status)
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_create_server_with_multiple_physnets_fail(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_create_server_with_multiple_physnets_fail(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
"""Test multiple networks split across host NUMA nodes. """Test multiple networks split across host NUMA nodes.
This should fail because we've requested a single-node instance but the This should fail because we've requested a single-node instance but the
@ -267,16 +231,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
self.assertTrue(self.mock_filter.called) self.assertTrue(self.mock_filter.called)
self.assertEqual('ERROR', status) self.assertEqual('ERROR', status)
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_create_server_with_physnet_and_tunneled_net(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_create_server_with_physnet_and_tunneled_net(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
"""Test combination of physnet and tunneled network. """Test combination of physnet and tunneled network.
This should pass because we've requested a single-node instance and the This should pass because we've requested a single-node instance and the
@ -295,16 +250,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
self.assertTrue(self.mock_filter.called) self.assertTrue(self.mock_filter.called)
self.assertEqual('ACTIVE', status) self.assertEqual('ACTIVE', status)
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_rebuild_server_with_network_affinity(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_rebuild_server_with_network_affinity(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
extra_spec = {'hw:numa_nodes': '1'} extra_spec = {'hw:numa_nodes': '1'}
flavor_id = self._create_flavor(extra_spec=extra_spec) flavor_id = self._create_flavor(extra_spec=extra_spec)
networks = [ networks = [
@ -353,16 +299,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
self.assertEqual(500, ex.response.status_code) self.assertEqual(500, ex.response.status_code)
self.assertIn('NoValidHost', six.text_type(ex)) self.assertIn('NoValidHost', six.text_type(ex))
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_cold_migrate_with_physnet(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_cold_migrate_with_physnet(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,
cpu_cores=2, cpu_threads=2, cpu_cores=2, cpu_threads=2,
kB_mem=15740000) kB_mem=15740000)
@ -426,16 +363,7 @@ class NUMAServersWithNetworksTest(NUMAServersTestBase):
self.assertIsNotNone(network_metadata) self.assertIsNotNone(network_metadata)
self.assertEqual(set(['foo']), network_metadata.physnets) self.assertEqual(set(['foo']), network_metadata.physnets)
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_cold_migrate_with_physnet_fails(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_cold_migrate_with_physnet_fails(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
host_infos = [ host_infos = [
# host 1 has room on both nodes # host 1 has room on both nodes
fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import io
import fixtures import fixtures
import mock import mock
from oslo_config import cfg from oslo_config import cfg
@ -50,17 +48,6 @@ class _PCIServersTestBase(base.ServersTestBase):
'nova.scheduler.filters.pci_passthrough_filter' 'nova.scheduler.filters.pci_passthrough_filter'
'.PciPassthroughFilter.host_passes', '.PciPassthroughFilter.host_passes',
side_effect=host_pass_mock)).mock side_effect=host_pass_mock)).mock
self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.LibvirtDriver._get_local_gb_info',
return_value={'total': 128,
'used': 44,
'free': 84}))
self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True))
self.useFixture(fixtures.MockPatch(
'nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')]))
def _setup_scheduler_service(self): def _setup_scheduler_service(self):
# Enable the 'NUMATopologyFilter', 'PciPassthroughFilter' # Enable the 'NUMATopologyFilter', 'PciPassthroughFilter'
@ -139,8 +126,7 @@ class SRIOVServersTest(_PCIServersTestBase):
}, },
)] )]
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_VF(self):
def test_create_server_with_VF(self, img_mock):
host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,
cpu_cores=2, cpu_threads=2, cpu_cores=2, cpu_threads=2,
@ -155,8 +141,7 @@ class SRIOVServersTest(_PCIServersTestBase):
self._run_build_test(flavor_id) self._run_build_test(flavor_id)
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_PF(self):
def test_create_server_with_PF(self, img_mock):
host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,
cpu_cores=2, cpu_threads=2, cpu_cores=2, cpu_threads=2,
@ -171,8 +156,7 @@ class SRIOVServersTest(_PCIServersTestBase):
self._run_build_test(flavor_id) self._run_build_test(flavor_id)
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_PF_no_VF(self):
def test_create_server_with_PF_no_VF(self, img_mock):
host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,
cpu_cores=2, cpu_threads=2, cpu_cores=2, cpu_threads=2,
@ -192,8 +176,7 @@ class SRIOVServersTest(_PCIServersTestBase):
self._run_build_test(flavor_id_pfs) self._run_build_test(flavor_id_pfs)
self._run_build_test(flavor_id_vfs, end_status='ERROR') self._run_build_test(flavor_id_vfs, end_status='ERROR')
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_VF_no_PF(self):
def test_create_server_with_VF_no_PF(self, img_mock):
host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,
cpu_cores=2, cpu_threads=2, cpu_cores=2, cpu_threads=2,
@ -307,8 +290,7 @@ class PCIServersTest(_PCIServersTestBase):
} }
)] )]
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_pci_dev_and_numa(self):
def test_create_server_with_pci_dev_and_numa(self, img_mock):
"""Verifies that an instance can be booted with cpu pinning and with an """Verifies that an instance can be booted with cpu pinning and with an
assigned pci device. assigned pci device.
""" """
@ -329,8 +311,7 @@ class PCIServersTest(_PCIServersTestBase):
self._run_build_test(flavor_id) self._run_build_test(flavor_id)
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_pci_dev_and_numa_fails(self):
def test_create_server_with_pci_dev_and_numa_fails(self, img_mock):
"""This test ensures that it is not possible to allocated CPU and """This test ensures that it is not possible to allocated CPU and
memory resources from one NUMA node and a PCI device from another. memory resources from one NUMA node and a PCI device from another.
""" """
@ -376,8 +357,7 @@ class PCIServersWithNUMAPoliciesTest(_PCIServersTestBase):
} }
)] )]
@mock.patch('nova.virt.libvirt.LibvirtDriver._create_image') def test_create_server_with_pci_dev_and_numa(self):
def test_create_server_with_pci_dev_and_numa(self, img_mock):
"""Validate behavior of 'preferred' PCI NUMA policy. """Validate behavior of 'preferred' PCI NUMA policy.
This test ensures that it *is* possible to allocate CPU and memory This test ensures that it *is* possible to allocate CPU and memory

View File

@ -13,9 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import io
import mock
from nova.tests.functional.api import client from nova.tests.functional.api import client
from nova.tests.functional.libvirt import base from nova.tests.functional.libvirt import base
from nova.tests.unit.virt.libvirt import fakelibvirt from nova.tests.unit.virt.libvirt import fakelibvirt
@ -46,16 +43,7 @@ class RealTimeServersTest(base.ServersTestBase):
client.OpenStackApiException, client.OpenStackApiException,
self.api.post_server, {'server': server}) self.api.post_server, {'server': server})
@mock.patch('nova.virt.libvirt.LibvirtDriver._get_local_gb_info', def test_success(self):
return_value={'total': 128,
'used': 44,
'free': 84})
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.is_valid_hostname',
return_value=True)
@mock.patch('nova.virt.libvirt.driver.libvirt_utils.file_open',
side_effect=[io.BytesIO(b''), io.BytesIO(b'')])
def test_success(
self, mock_file_open, mock_valid_hostname, mock_get_fs_info):
host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1, host_info = fakelibvirt.NUMAHostInfo(cpu_nodes=2, cpu_sockets=1,
cpu_cores=2, cpu_threads=2, cpu_cores=2, cpu_threads=2,
kB_mem=15740000) kB_mem=15740000)