devices: disk: Add tests for missing _storage_backend

This commit is contained in:
Cole Robinson 2019-05-23 18:13:24 -04:00
parent f22a0ec2e4
commit 08b26e9b1c
2 changed files with 34 additions and 1 deletions

View File

@ -1479,3 +1479,25 @@ class XMLParseTest(unittest.TestCase):
parsexml=mkdisk("sdw").get_xml())
guest.devices.replace_child(guest.devices.disk[4], newdisk)
utils.diff_compare(guest.get_xml(), parsefile)
def testDiskRevalidate(self):
"""
Test that calling validate() on parsed disk XML doesn't attempt
to verify the path exists. Assume it's a working config
"""
xml = ("<disk type='file' device='disk'>"
"<source file='/A/B/C/D/NOPE'/>"
"</disk>")
disk = virtinst.DeviceDisk(self.conn, parsexml=xml)
disk.validate()
disk.is_size_conflict()
disk.build_storage(None)
self.assertEqual(getattr(disk, "_storage_backend"), None)
disk.set_backend_for_existing_path()
self.assertEqual(bool(getattr(disk, "_storage_backend")), True)
try:
disk.validate()
raise AssertionError("expected disk validate failure")
except ValueError:
pass

View File

@ -386,6 +386,14 @@ class DeviceDisk(Device):
self._set_xmlpath(self.path)
path = property(_get_path, _set_path)
def set_backend_for_existing_path(self):
# This is an entry point for parsexml Disk instances to request
# a _storage_backend to be initialized from the XML path. That
# will cause validate() to actually validate the path exists.
# We need this so addhw XML editing will still validate the disk path
if not self._storage_backend:
self._set_default_storage_backend()
def set_vol_object(self, vol_object, parent_pool):
logging.debug("disk.set_vol_object: volxml=\n%s",
vol_object.XMLDesc(0))
@ -797,7 +805,8 @@ class DeviceDisk(Device):
If storage doesn't exist (a non-existent file 'path', or 'vol_install'
was specified), we create it.
"""
if not self._storage_backend.will_create_storage():
if (not self._storage_backend or
not self._storage_backend.will_create_storage()):
return
meter = util.ensure_meter(meter)
@ -819,6 +828,8 @@ class DeviceDisk(Device):
Non fatal conflicts (sparse disk exceeds available space) will
return (False, "description of collision")
"""
if not self._storage_backend:
return (False, None)
return self._storage_backend.is_size_conflict()
def is_conflict_disk(self, conn=None):