refactor all uses of the qemu-img info
command
* nova/virt/images.py: Define a single function that calls `qemu-img info` in a robust manner, by avoiding locale issues etc. * nova/virt/libvirt/utils.py: Define secondary functions to parse items returned from images.qemu_img_info(). * nova/virt/libvirt/driver.py: Use the libvirt.utils functions. * nova/virt/disk/api.py: Likewise. * nova/tests/test_libvirt.py: Remove no longer needed stub for utils.execute('qemu-img info'). Adjust to a more restrictive `qemu-img info` format, and more robust calling signature. * nova/tests/test_imagebackend.py: Adjust to moved method. Change-Id: I739dd246410c215f401ebd3a92b8207f46e8fb9a
This commit is contained in:
@@ -236,21 +236,21 @@ class LvmTestCase(_ImageTestCase):
|
||||
|
||||
def prepare_mocks(self):
|
||||
fn = self.mox.CreateMockAnything()
|
||||
self.mox.StubOutWithMock(self.disk, 'get_image_virtual_size')
|
||||
self.mox.StubOutWithMock(self.disk, 'resize2fs')
|
||||
self.mox.StubOutWithMock(self.libvirt_utils, 'create_lvm_image')
|
||||
self.mox.StubOutWithMock(self.libvirt_utils, 'get_disk_size')
|
||||
self.mox.StubOutWithMock(self.utils, 'execute')
|
||||
return fn
|
||||
|
||||
def _create_image(self, sparse):
|
||||
fn = self.prepare_mocks()
|
||||
fn(target=self.TEMPLATE_PATH)
|
||||
self.disk.get_image_virtual_size(self.TEMPLATE_PATH
|
||||
).AndReturn(self.TEMPLATE_SIZE)
|
||||
self.libvirt_utils.create_lvm_image(self.VG,
|
||||
self.LV,
|
||||
self.TEMPLATE_SIZE,
|
||||
sparse=sparse)
|
||||
self.libvirt_utils.get_disk_size(self.TEMPLATE_PATH
|
||||
).AndReturn(self.TEMPLATE_SIZE)
|
||||
cmd = ('dd', 'if=%s' % self.TEMPLATE_PATH,
|
||||
'of=%s' % self.PATH, 'bs=4M')
|
||||
self.utils.execute(*cmd, run_as_root=True)
|
||||
@@ -277,10 +277,10 @@ class LvmTestCase(_ImageTestCase):
|
||||
def _create_image_resize(self, sparse):
|
||||
fn = self.prepare_mocks()
|
||||
fn(target=self.TEMPLATE_PATH)
|
||||
self.disk.get_image_virtual_size(self.TEMPLATE_PATH
|
||||
).AndReturn(self.TEMPLATE_SIZE)
|
||||
self.libvirt_utils.create_lvm_image(self.VG, self.LV,
|
||||
self.SIZE, sparse=sparse)
|
||||
self.libvirt_utils.get_disk_size(self.TEMPLATE_PATH
|
||||
).AndReturn(self.TEMPLATE_SIZE)
|
||||
cmd = ('dd', 'if=%s' % self.TEMPLATE_PATH,
|
||||
'of=%s' % self.PATH, 'bs=4M')
|
||||
self.utils.execute(*cmd, run_as_root=True)
|
||||
@@ -316,13 +316,13 @@ class LvmTestCase(_ImageTestCase):
|
||||
def test_create_image_negative(self):
|
||||
fn = self.prepare_mocks()
|
||||
fn(target=self.TEMPLATE_PATH)
|
||||
self.disk.get_image_virtual_size(self.TEMPLATE_PATH
|
||||
).AndReturn(self.TEMPLATE_SIZE)
|
||||
self.libvirt_utils.create_lvm_image(self.VG,
|
||||
self.LV,
|
||||
self.SIZE,
|
||||
sparse=False
|
||||
).AndRaise(RuntimeError())
|
||||
self.libvirt_utils.get_disk_size(self.TEMPLATE_PATH
|
||||
).AndReturn(self.TEMPLATE_SIZE)
|
||||
self.mox.StubOutWithMock(self.libvirt_utils, 'remove_logical_volumes')
|
||||
self.libvirt_utils.remove_logical_volumes(self.PATH)
|
||||
self.mox.ReplayAll()
|
||||
|
@@ -1825,11 +1825,6 @@ class LibvirtConnTestCase(test.TestCase):
|
||||
|
||||
self.mox.StubOutWithMock(os.path, "getsize")
|
||||
os.path.getsize('/test/disk').AndReturn((10737418240))
|
||||
|
||||
self.mox.StubOutWithMock(utils, "execute")
|
||||
utils.execute('qemu-img', 'info',
|
||||
'/test/disk.local').AndReturn((ret, ''))
|
||||
|
||||
os.path.getsize('/test/disk.local').AndReturn((21474836480))
|
||||
|
||||
self.mox.ReplayAll()
|
||||
@@ -3189,8 +3184,7 @@ class LibvirtUtilsTestCase(test.TestCase):
|
||||
|
||||
def test_get_disk_size(self):
|
||||
self.mox.StubOutWithMock(utils, 'execute')
|
||||
utils.execute('qemu-img',
|
||||
'info',
|
||||
utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info',
|
||||
'/some/path').AndReturn(('''image: 00000001
|
||||
file format: raw
|
||||
virtual size: 4.4M (4592640 bytes)
|
||||
@@ -3346,15 +3340,13 @@ disk size: 4.4M''', ''))
|
||||
|
||||
def fake_execute(*args, **kwargs):
|
||||
if with_actual_path:
|
||||
return ("some\n"
|
||||
"output\n"
|
||||
return ("some: output\n"
|
||||
"backing file: /foo/bar/baz (actual path: /a/b/c)\n"
|
||||
"...\n"), ''
|
||||
"...: ...\n"), ''
|
||||
else:
|
||||
return ("some\n"
|
||||
"output\n"
|
||||
return ("some: output\n"
|
||||
"backing file: /foo/bar/baz\n"
|
||||
"...\n"), ''
|
||||
"...: ...\n"), ''
|
||||
|
||||
self.stubs.Set(utils, 'execute', fake_execute)
|
||||
|
||||
|
Reference in New Issue
Block a user