Fallback to fakelibvirt in test_libvirt.py test suite

If the native 'libvirt' module is not available on the host,
currently test_libvirt.py stubs it out to ''. While this
has sort-of worked for some test cases, others have had to
be configured to skip. As the complexity of the libvirt driver
increases, ever more tests will need to be skipped.

There is, however, the fakelibvirt.py module which provides
a dummy impl of the libvirt APIs for use in test_virt_drivers.py
With a few enhancements this fake impl is good enough to use
with test_libvirt.py too.

The constructor of libvirtError is changed to accept the
'msg' as first arg to match the regular libvirt module.
A few missing constants are added. The migrateToURI
method is stubbed out

All the previously skipped tests can now be enabled as
normal.

Change-Id: I15efbef8c676cc0e263a9c9bdd7b14dfb3771105
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange
2012-07-05 19:40:47 +01:00
parent 1b467638ac
commit 818c23d32c
2 changed files with 41 additions and 49 deletions

View File

@@ -78,6 +78,9 @@ VIR_CPU_COMPARE_SUPERSET = 2
VIR_CRED_AUTHNAME = 2 VIR_CRED_AUTHNAME = 2
VIR_CRED_NOECHOPROMPT = 7 VIR_CRED_NOECHOPROMPT = 7
VIR_MIGRATE_PEER2PEER = 2
VIR_MIGRATE_UNDEFINE_SOURCE = 16
# libvirtError enums # libvirtError enums
# (Intentionally different from what's in libvirt. We do this to check, # (Intentionally different from what's in libvirt. We do this to check,
# that consumers of the library are using the symbolic names rather than # that consumers of the library are using the symbolic names rather than
@@ -85,9 +88,13 @@ VIR_CRED_NOECHOPROMPT = 7
VIR_FROM_QEMU = 100 VIR_FROM_QEMU = 100
VIR_FROM_DOMAIN = 200 VIR_FROM_DOMAIN = 200
VIR_FROM_NWFILTER = 330 VIR_FROM_NWFILTER = 330
VIR_FROM_REMOTE = 340
VIR_FROM_RPC = 345
VIR_ERR_XML_DETAIL = 350 VIR_ERR_XML_DETAIL = 350
VIR_ERR_NO_DOMAIN = 420 VIR_ERR_NO_DOMAIN = 420
VIR_ERR_NO_NWFILTER = 620 VIR_ERR_NO_NWFILTER = 620
VIR_ERR_SYSTEM_ERROR = 900
VIR_ERR_INTERNAL_ERROR = 950
def _parse_disk_info(element): def _parse_disk_info(element):
@@ -118,7 +125,9 @@ def _parse_disk_info(element):
class libvirtError(Exception): class libvirtError(Exception):
def __init__(self, error_code, error_domain, msg): def __init__(self, msg,
error_code=VIR_ERR_INTERNAL_ERROR,
error_domain=VIR_FROM_QEMU):
self.error_code = error_code self.error_code = error_code
self.error_domain = error_domain self.error_domain = error_domain
Exception(self, msg) Exception(self, msg)
@@ -162,8 +171,8 @@ class Domain(object):
try: try:
tree = etree.fromstring(xml) tree = etree.fromstring(xml)
except etree.ParseError: except etree.ParseError:
raise libvirtError(VIR_ERR_XML_DETAIL, VIR_FROM_DOMAIN, raise libvirtError("Invalid XML.",
"Invalid XML.") VIR_ERR_XML_DETAIL, VIR_FROM_DOMAIN)
definition = {} definition = {}
@@ -302,6 +311,9 @@ class Domain(object):
self._def['vcpu'], self._def['vcpu'],
123456789L] 123456789L]
def migrateToURI(self, desturi, flags, dname, bandwidth):
raise libvirtError("Migration always fails for fake libvirt!")
def attachDevice(self, xml): def attachDevice(self, xml):
disk_info = _parse_disk_info(etree.fromstring(xml)) disk_info = _parse_disk_info(etree.fromstring(xml))
disk_info['_attached'] = True disk_info['_attached'] = True
@@ -424,7 +436,7 @@ class DomainSnapshot(object):
class Connection(object): class Connection(object):
def __init__(self, uri, readonly): def __init__(self, uri, readonly):
if not uri: if not uri or uri == '':
if allow_default_uri_connection: if allow_default_uri_connection:
uri = 'qemu:///session' uri = 'qemu:///session'
else: else:
@@ -434,12 +446,13 @@ class Connection(object):
uri_whitelist = ['qemu:///system', uri_whitelist = ['qemu:///system',
'qemu:///session', 'qemu:///session',
'xen:///system', 'xen:///system',
'uml:///system'] 'uml:///system',
'test:///default']
if uri not in uri_whitelist: if uri not in uri_whitelist:
raise libvirtError(5, 0, raise libvirtError("libvir: error : no connection driver "
"libvir: error : no connection driver " "available for No connection for URI %s" % uri,
"available for No connection for URI %s" % uri) 5, 0)
self.readonly = readonly self.readonly = readonly
self._uri = uri self._uri = uri
@@ -489,16 +502,16 @@ class Connection(object):
def lookupByID(self, id): def lookupByID(self, id):
if id in self._running_vms: if id in self._running_vms:
return self._running_vms[id] return self._running_vms[id]
raise libvirtError(VIR_ERR_NO_DOMAIN, VIR_FROM_QEMU, raise libvirtError('Domain not found: no domain with matching '
'Domain not found: no domain with matching ' 'id %d' % id,
'id %d' % id) VIR_ERR_NO_DOMAIN, VIR_FROM_QEMU)
def lookupByName(self, name): def lookupByName(self, name):
if name in self._vms: if name in self._vms:
return self._vms[name] return self._vms[name]
raise libvirtError(VIR_ERR_NO_DOMAIN, VIR_FROM_QEMU, raise libvirtError('Domain not found: no domain with matching '
'Domain not found: no domain with matching ' 'name "%s"' % name,
'name "%s"' % name) VIR_ERR_NO_DOMAIN, VIR_FROM_QEMU)
def defineXML(self, xml): def defineXML(self, xml):
dom = Domain(connection=self, running=False, transient=False, xml=xml) dom = Domain(connection=self, running=False, transient=False, xml=xml)
@@ -778,8 +791,8 @@ class Connection(object):
try: try:
return self._nwfilters[name] return self._nwfilters[name]
except KeyError: except KeyError:
raise libvirtError(VIR_ERR_NO_NWFILTER, VIR_FROM_NWFILTER, raise libvirtError("no nwfilter with matching name %s" % name,
"no nwfilter with matching name %s" % name) VIR_ERR_NO_NWFILTER, VIR_FROM_NWFILTER)
def nwfilterDefineXML(self, xml): def nwfilterDefineXML(self, xml):
nwfilter = NWFilter(self, xml) nwfilter = NWFilter(self, xml)

View File

@@ -56,12 +56,9 @@ from nova.volume import driver as volume_driver
try: try:
import libvirt import libvirt
libvirt_driver.libvirt = libvirt
except ImportError: except ImportError:
# TODO(sdague): there should be a cleaner way to handle this import nova.tests.fakelibvirt as libvirt
# in the case where libvirt python isn't installed libvirt_driver.libvirt = libvirt
libvirt_driver.libvirt = ""
libvirt = None
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -406,10 +403,6 @@ class FakeVolumeDriver(object):
return "" return ""
def missing_libvirt():
return libvirt is None
class LibvirtConnTestCase(test.TestCase): class LibvirtConnTestCase(test.TestCase):
def setUp(self): def setUp(self):
@@ -623,7 +616,6 @@ class LibvirtConnTestCase(test.TestCase):
None, None) None, None)
self.assertEquals(conf.cpu, None) self.assertEquals(conf.cpu, None)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_guest_cpu_config_host_passthrough_new(self): def test_get_guest_cpu_config_host_passthrough_new(self):
def get_lib_version_stub(self): def get_lib_version_stub(self):
return (0 * 1000 * 1000) + (9 * 1000) + 11 return (0 * 1000 * 1000) + (9 * 1000) + 11
@@ -643,7 +635,6 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(conf.cpu.mode, "host-passthrough") self.assertEquals(conf.cpu.mode, "host-passthrough")
self.assertEquals(conf.cpu.model, None) self.assertEquals(conf.cpu.model, None)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_guest_cpu_config_host_model_new(self): def test_get_guest_cpu_config_host_model_new(self):
def get_lib_version_stub(self): def get_lib_version_stub(self):
return (0 * 1000 * 1000) + (9 * 1000) + 11 return (0 * 1000 * 1000) + (9 * 1000) + 11
@@ -663,7 +654,6 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(conf.cpu.mode, "host-model") self.assertEquals(conf.cpu.mode, "host-model")
self.assertEquals(conf.cpu.model, None) self.assertEquals(conf.cpu.model, None)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_guest_cpu_config_custom_new(self): def test_get_guest_cpu_config_custom_new(self):
def get_lib_version_stub(self): def get_lib_version_stub(self):
return (0 * 1000 * 1000) + (9 * 1000) + 11 return (0 * 1000 * 1000) + (9 * 1000) + 11
@@ -684,7 +674,6 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(conf.cpu.mode, "custom") self.assertEquals(conf.cpu.mode, "custom")
self.assertEquals(conf.cpu.model, "Penryn") self.assertEquals(conf.cpu.model, "Penryn")
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_guest_cpu_config_host_passthrough_old(self): def test_get_guest_cpu_config_host_passthrough_old(self):
def get_lib_version_stub(self): def get_lib_version_stub(self):
return (0 * 1000 * 1000) + (9 * 1000) + 7 return (0 * 1000 * 1000) + (9 * 1000) + 7
@@ -701,7 +690,6 @@ class LibvirtConnTestCase(test.TestCase):
_fake_network_info(self.stubs, 1), _fake_network_info(self.stubs, 1),
None, None) None, None)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_guest_cpu_config_host_model_old(self): def test_get_guest_cpu_config_host_model_old(self):
def get_lib_version_stub(self): def get_lib_version_stub(self):
return (0 * 1000 * 1000) + (9 * 1000) + 7 return (0 * 1000 * 1000) + (9 * 1000) + 7
@@ -736,7 +724,6 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(conf.cpu.model, "Opteron_G4") self.assertEquals(conf.cpu.model, "Opteron_G4")
self.assertEquals(conf.cpu.vendor, "AMD") self.assertEquals(conf.cpu.vendor, "AMD")
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_guest_cpu_config_custom_old(self): def test_get_guest_cpu_config_custom_old(self):
def get_lib_version_stub(self): def get_lib_version_stub(self):
return (0 * 1000 * 1000) + (9 * 1000) + 7 return (0 * 1000 * 1000) + (9 * 1000) + 7
@@ -954,8 +941,9 @@ class LibvirtConnTestCase(test.TestCase):
devices = conn.get_disks(conn.list_instances()[0]) devices = conn.get_disks(conn.list_instances()[0])
self.assertEqual(devices, ['vda', 'vdb']) self.assertEqual(devices, ['vda', 'vdb'])
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_snapshot_in_ami_format(self): def test_snapshot_in_ami_format(self):
self.flags(libvirt_snapshots_directory='./')
# Start test # Start test
image_service = nova.tests.image.fake.FakeImageService() image_service = nova.tests.image.fake.FakeImageService()
@@ -990,8 +978,9 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(snapshot['disk_format'], 'ami') self.assertEquals(snapshot['disk_format'], 'ami')
self.assertEquals(snapshot['name'], snapshot_name) self.assertEquals(snapshot['name'], snapshot_name)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_snapshot_in_raw_format(self): def test_snapshot_in_raw_format(self):
self.flags(libvirt_snapshots_directory='./')
# Start test # Start test
image_service = nova.tests.image.fake.FakeImageService() image_service = nova.tests.image.fake.FakeImageService()
@@ -1022,9 +1011,9 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(snapshot['disk_format'], 'raw') self.assertEquals(snapshot['disk_format'], 'raw')
self.assertEquals(snapshot['name'], snapshot_name) self.assertEquals(snapshot['name'], snapshot_name)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_snapshot_in_qcow2_format(self): def test_snapshot_in_qcow2_format(self):
self.flags(snapshot_image_format='qcow2') self.flags(snapshot_image_format='qcow2',
libvirt_snapshots_directory='./')
# Start test # Start test
image_service = nova.tests.image.fake.FakeImageService() image_service = nova.tests.image.fake.FakeImageService()
@@ -1056,8 +1045,9 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(snapshot['disk_format'], 'qcow2') self.assertEquals(snapshot['disk_format'], 'qcow2')
self.assertEquals(snapshot['name'], snapshot_name) self.assertEquals(snapshot['name'], snapshot_name)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_snapshot_no_image_architecture(self): def test_snapshot_no_image_architecture(self):
self.flags(libvirt_snapshots_directory='./')
# Start test # Start test
image_service = nova.tests.image.fake.FakeImageService() image_service = nova.tests.image.fake.FakeImageService()
@@ -1092,8 +1082,9 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(snapshot['status'], 'active') self.assertEquals(snapshot['status'], 'active')
self.assertEquals(snapshot['name'], snapshot_name) self.assertEquals(snapshot['name'], snapshot_name)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_snapshot_no_original_image(self): def test_snapshot_no_original_image(self):
self.flags(libvirt_snapshots_directory='./')
# Start test # Start test
image_service = nova.tests.image.fake.FakeImageService() image_service = nova.tests.image.fake.FakeImageService()
@@ -1394,7 +1385,6 @@ class LibvirtConnTestCase(test.TestCase):
self.assertEquals(conn.uri, testuri) self.assertEquals(conn.uri, testuri)
db.instance_destroy(user_context, instance_ref['uuid']) db.instance_destroy(user_context, instance_ref['uuid'])
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_ensure_filtering_rules_for_instance_timeout(self): def test_ensure_filtering_rules_for_instance_timeout(self):
"""ensure_filtering_fules_for_instance() finishes with timeout.""" """ensure_filtering_fules_for_instance() finishes with timeout."""
# Preparing mocks # Preparing mocks
@@ -1445,7 +1435,6 @@ class LibvirtConnTestCase(test.TestCase):
db.instance_destroy(self.context, instance_ref['uuid']) db.instance_destroy(self.context, instance_ref['uuid'])
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_live_migration_raises_exception(self): def test_live_migration_raises_exception(self):
"""Confirms recover method is called when exceptions are raised.""" """Confirms recover method is called when exceptions are raised."""
# Preparing data # Preparing data
@@ -1520,7 +1509,6 @@ class LibvirtConnTestCase(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
self.assertEqual(conn.pre_live_migration(vol), None) self.assertEqual(conn.pre_live_migration(vol), None)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_pre_block_migration_works_correctly(self): def test_pre_block_migration_works_correctly(self):
"""Confirms pre_block_migration works correctly.""" """Confirms pre_block_migration works correctly."""
# Replace instances_path since this testcase creates tmpfile # Replace instances_path since this testcase creates tmpfile
@@ -1545,7 +1533,6 @@ class LibvirtConnTestCase(test.TestCase):
db.instance_destroy(self.context, instance_ref['uuid']) db.instance_destroy(self.context, instance_ref['uuid'])
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_get_instance_disk_info_works_correctly(self): def test_get_instance_disk_info_works_correctly(self):
"""Confirms pre_block_migration works correctly.""" """Confirms pre_block_migration works correctly."""
# Test data # Test data
@@ -1606,7 +1593,6 @@ class LibvirtConnTestCase(test.TestCase):
db.instance_destroy(self.context, instance_ref['uuid']) db.instance_destroy(self.context, instance_ref['uuid'])
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_spawn_with_network_info(self): def test_spawn_with_network_info(self):
# Preparing mocks # Preparing mocks
def fake_none(self, instance): def fake_none(self, instance):
@@ -1739,7 +1725,6 @@ class LibvirtConnTestCase(test.TestCase):
ip = conn.get_host_ip_addr() ip = conn.get_host_ip_addr()
self.assertEquals(ip, FLAGS.my_ip) self.assertEquals(ip, FLAGS.my_ip)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_broken_connection(self): def test_broken_connection(self):
for (error, domain) in ( for (error, domain) in (
(libvirt.VIR_ERR_SYSTEM_ERROR, libvirt.VIR_FROM_REMOTE), (libvirt.VIR_ERR_SYSTEM_ERROR, libvirt.VIR_FROM_REMOTE),
@@ -1800,7 +1785,6 @@ class LibvirtConnTestCase(test.TestCase):
_assert_volume_in_mapping('sdg', False) _assert_volume_in_mapping('sdg', False)
_assert_volume_in_mapping('sdh1', False) _assert_volume_in_mapping('sdh1', False)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_immediate_delete(self): def test_immediate_delete(self):
def fake_lookup_by_name(instance_name): def fake_lookup_by_name(instance_name):
raise exception.InstanceNotFound() raise exception.InstanceNotFound()
@@ -1811,7 +1795,6 @@ class LibvirtConnTestCase(test.TestCase):
instance = db.instance_create(self.context, self.test_instance) instance = db.instance_create(self.context, self.test_instance)
conn.destroy(instance, {}) conn.destroy(instance, {})
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_destroy_saved(self): def test_destroy_saved(self):
"""Ensure destroy calls managedSaveRemove for saved instance""" """Ensure destroy calls managedSaveRemove for saved instance"""
mock = self.mox.CreateMock(libvirt.virDomain) mock = self.mox.CreateMock(libvirt.virDomain)
@@ -1835,7 +1818,6 @@ class LibvirtConnTestCase(test.TestCase):
"uuid": "875a8070-d0b9-4949-8b31-104d125c9a64"} "uuid": "875a8070-d0b9-4949-8b31-104d125c9a64"}
conn.destroy(instance, []) conn.destroy(instance, [])
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_private_destroy(self): def test_private_destroy(self):
"""Ensure Instance not found skips undefine""" """Ensure Instance not found skips undefine"""
mock = self.mox.CreateMock(libvirt.virDomain) mock = self.mox.CreateMock(libvirt.virDomain)
@@ -1856,7 +1838,6 @@ class LibvirtConnTestCase(test.TestCase):
result = conn._destroy(instance) result = conn._destroy(instance)
self.assertTrue(result) self.assertTrue(result)
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_private_destroy_not_found(self): def test_private_destroy_not_found(self):
"""Ensure Instance not found skips undefine""" """Ensure Instance not found skips undefine"""
mock = self.mox.CreateMock(libvirt.virDomain) mock = self.mox.CreateMock(libvirt.virDomain)
@@ -2279,7 +2260,6 @@ class IptablesFirewallTestCase(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
self.fw.do_refresh_security_group_rules("fake") self.fw.do_refresh_security_group_rules("fake")
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_unfilter_instance_undefines_nwfilter(self): def test_unfilter_instance_undefines_nwfilter(self):
admin_ctxt = context.get_admin_context() admin_ctxt = context.get_admin_context()
@@ -2997,7 +2977,6 @@ class LibvirtNonblockingTestCase(test.TestCase):
def tearDown(self): def tearDown(self):
super(LibvirtNonblockingTestCase, self).tearDown() super(LibvirtNonblockingTestCase, self).tearDown()
@test.skip_if(missing_libvirt(), "Test requires libvirt")
def test_connection_to_primitive(self): def test_connection_to_primitive(self):
"""Test bug 962840""" """Test bug 962840"""
import nova.virt.libvirt.driver as libvirt_driver import nova.virt.libvirt.driver as libvirt_driver