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

View File

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