From 1501a4e4f75efb63385a1dadc728c2ef245bf7de Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Thu, 14 Feb 2013 10:37:21 +0000 Subject: remove intermediate libvirt downloaded images * nova/virt/images.py (fetch_to_raw): When converting a downloaded image to raw, ensure we delete the original download image. I.E. the .part file. The regression was introduced in 7c265343. * nova/tests/test_libvirt.py (test_fetch_raw_image): A new test for this function which wasn't tested previously. Fixes bug: 1125068 Change-Id: Ie6f2cff1a554b8500fd66f0832566ae1b317be40 --- nova/tests/test_libvirt.py | 75 ++++++++++++++++++++++++++++++++++++++++++++-- nova/virt/images.py | 2 +- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index d2cd5a757..837b4b1f5 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -1278,7 +1278,7 @@ class LibvirtConnTestCase(test.TestCase): def convert_image(source, dest, out_format): libvirt_driver.libvirt_utils.files[dest] = '' - images.convert_image = convert_image + self.stubs.Set(images, 'convert_image', convert_image) self.mox.ReplayAll() @@ -1330,7 +1330,7 @@ class LibvirtConnTestCase(test.TestCase): def convert_image(source, dest, out_format): libvirt_driver.libvirt_utils.files[dest] = '' - images.convert_image = convert_image + self.stubs.Set(images, 'convert_image', convert_image) self.mox.ReplayAll() @@ -4158,6 +4158,77 @@ disk size: 4.4M''', '')) libvirt_utils.fetch_image(context, target, image_id, user_id, project_id) + def test_fetch_raw_image(self): + + def fake_execute(*cmd, **kwargs): + self.executes.append(cmd) + return None, None + + def fake_rename(old, new): + self.executes.append(('mv', old, new)) + + def fake_unlink(path): + self.executes.append(('rm', path)) + + def fake_rm_on_errror(path): + self.executes.append(('rm', '-f', path)) + + def fake_qemu_img_info(path): + class FakeImgInfo(object): + pass + + file_format = path.split('.')[-1] + if file_format == 'part': + file_format = path.split('.')[-2] + elif file_format == 'converted': + file_format = 'raw' + if 'backing' in path: + backing_file = 'backing' + else: + backing_file = None + + FakeImgInfo.file_format = file_format + FakeImgInfo.backing_file = backing_file + + return FakeImgInfo() + + self.stubs.Set(utils, 'execute', fake_execute) + self.stubs.Set(os, 'rename', fake_rename) + self.stubs.Set(os, 'unlink', fake_unlink) + self.stubs.Set(images, 'fetch', lambda *_: None) + self.stubs.Set(images, 'qemu_img_info', fake_qemu_img_info) + self.stubs.Set(utils, 'delete_if_exists', fake_rm_on_errror) + + context = 'opaque context' + image_id = '4' + user_id = 'fake' + project_id = 'fake' + + target = 't.qcow2' + self.executes = [] + expected_commands = [('qemu-img', 'convert', '-O', 'raw', + 't.qcow2.part', 't.qcow2.converted'), + ('rm', 't.qcow2.part'), + ('mv', 't.qcow2.converted', 't.qcow2')] + images.fetch_to_raw(context, image_id, target, user_id, project_id) + self.assertEqual(self.executes, expected_commands) + + target = 't.raw' + self.executes = [] + expected_commands = [('mv', 't.raw.part', 't.raw')] + images.fetch_to_raw(context, image_id, target, user_id, project_id) + self.assertEqual(self.executes, expected_commands) + + target = 'backing.qcow2' + self.executes = [] + expected_commands = [('rm', '-f', 'backing.qcow2.part')] + self.assertRaises(exception.ImageUnacceptable, + images.fetch_to_raw, + context, image_id, target, user_id, project_id) + self.assertEqual(self.executes, expected_commands) + + del self.executes + def test_get_disk_backing_file(self): with_actual_path = False diff --git a/nova/virt/images.py b/nova/virt/images.py index a5c960486..b5ca8fa47 100755 --- a/nova/virt/images.py +++ b/nova/virt/images.py @@ -225,6 +225,7 @@ def fetch_to_raw(context, image_href, path, user_id, project_id): LOG.debug("%s was %s, converting to raw" % (image_href, fmt)) with utils.remove_path_on_error(staged): convert_image(path_tmp, staged, 'raw') + os.unlink(path_tmp) data = qemu_img_info(staged) if data.file_format != "raw": @@ -233,6 +234,5 @@ def fetch_to_raw(context, image_href, path, user_id, project_id): data.file_format) os.rename(staged, path) - else: os.rename(path_tmp, path) -- cgit