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)