From 60e850a32a40550a0d7c819c6aa653f68cdcff3a Mon Sep 17 00:00:00 2001 From: Ruby Loo Date: Wed, 23 Dec 2015 01:03:03 +0000 Subject: [PATCH] Use ironic-lib's qemu_img_info() & convert_image() common.images.qemu_img_info() and convert_image() were moved to ironic-lib's disk_utils. This patch removes these methods (and tests) from ironic, and uses the methods from ironic-lib. Change-Id: I5199f84356f3945f1e720a17a0a7cdfad933db41 --- ironic/common/images.py | 32 +++--------------- ironic/tests/unit/common/test_images.py | 45 +++++-------------------- 2 files changed, 14 insertions(+), 63 deletions(-) diff --git a/ironic/common/images.py b/ironic/common/images.py index 636c7712ff..ed2e49daa7 100644 --- a/ironic/common/images.py +++ b/ironic/common/images.py @@ -22,12 +22,12 @@ Handling of VM disk images. import os import shutil +from ironic_lib import disk_utils import jinja2 from oslo_concurrency import processutils from oslo_config import cfg from oslo_log import log as logging from oslo_utils import fileutils -from oslo_utils import imageutils from ironic.common import exception from ironic.common.glance_service import service_utils as glance_utils @@ -312,28 +312,6 @@ def create_isolinux_image_for_uefi(output_file, deploy_iso, kernel, ramdisk, raise exception.ImageCreationFailed(image_type='iso', error=e) -def qemu_img_info(path): - """Return an object containing the parsed output from qemu-img info.""" - # NOTE(jlvillal): This function has been moved to ironic-lib. And is - # planned to be deleted here. If need to modify this function, please also - # do the same modification in ironic-lib - if not os.path.exists(path): - return imageutils.QemuImgInfo() - - out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', - 'qemu-img', 'info', path) - return imageutils.QemuImgInfo(out) - - -def convert_image(source, dest, out_format, run_as_root=False): - """Convert image to other format.""" - # NOTE(jlvillal): This function has been moved to ironic-lib. And is - # planned to be deleted here. If need to modify this function, please also - # do the same modification in ironic-lib - cmd = ('qemu-img', 'convert', '-O', out_format, source, dest) - utils.execute(*cmd, run_as_root=run_as_root) - - def fetch(context, image_href, path, force_raw=False): # TODO(vish): Improve context handling and add owner and auth data # when it is added to glance. Right now there is no @@ -355,7 +333,7 @@ def fetch(context, image_href, path, force_raw=False): def image_to_raw(image_href, path, path_tmp): with fileutils.remove_path_on_error(path_tmp): - data = qemu_img_info(path_tmp) + data = disk_utils.qemu_img_info(path_tmp) fmt = data.file_format if fmt is None: @@ -375,10 +353,10 @@ def image_to_raw(image_href, path, path_tmp): LOG.debug("%(image)s was %(format)s, converting to raw" % {'image': image_href, 'format': fmt}) with fileutils.remove_path_on_error(staged): - convert_image(path_tmp, staged, 'raw') + disk_utils.convert_image(path_tmp, staged, 'raw') os.unlink(path_tmp) - data = qemu_img_info(staged) + data = disk_utils.qemu_img_info(staged) if data.file_format != "raw": raise exception.ImageConvertFailed( image_id=image_href, @@ -410,7 +388,7 @@ def converted_size(path): :returns: virtual size of the image or 0 if conversion not needed. """ - data = qemu_img_info(path) + data = disk_utils.qemu_img_info(path) return data.virtual_size diff --git a/ironic/tests/unit/common/test_images.py b/ironic/tests/unit/common/test_images.py index 056264f2e0..68f54e1f9d 100644 --- a/ironic/tests/unit/common/test_images.py +++ b/ironic/tests/unit/common/test_images.py @@ -19,10 +19,10 @@ import os import shutil +from ironic_lib import disk_utils import mock from oslo_concurrency import processutils from oslo_config import cfg -from oslo_utils import imageutils import six import six.moves.builtins as __builtin__ @@ -45,33 +45,6 @@ class IronicImagesTestCase(base.TestCase): class FakeImgInfo(object): pass - @mock.patch.object(imageutils, 'QemuImgInfo', autospec=True) - @mock.patch.object(os.path, 'exists', return_value=False, autospec=True) - def test_qemu_img_info_path_doesnt_exist(self, path_exists_mock, - qemu_img_info_mock): - images.qemu_img_info('noimg') - path_exists_mock.assert_called_once_with('noimg') - qemu_img_info_mock.assert_called_once_with() - - @mock.patch.object(utils, 'execute', return_value=('out', 'err'), - autospec=True) - @mock.patch.object(imageutils, 'QemuImgInfo', autospec=True) - @mock.patch.object(os.path, 'exists', return_value=True, autospec=True) - def test_qemu_img_info_path_exists(self, path_exists_mock, - qemu_img_info_mock, execute_mock): - images.qemu_img_info('img') - path_exists_mock.assert_called_once_with('img') - execute_mock.assert_called_once_with('env', 'LC_ALL=C', 'LANG=C', - 'qemu-img', 'info', 'img') - qemu_img_info_mock.assert_called_once_with('out') - - @mock.patch.object(utils, 'execute', autospec=True) - def test_convert_image(self, execute_mock): - images.convert_image('source', 'dest', 'out_format') - execute_mock.assert_called_once_with('qemu-img', 'convert', '-O', - 'out_format', 'source', 'dest', - run_as_root=False) - @mock.patch.object(image_service, 'get_image_service', autospec=True) @mock.patch.object(__builtin__, 'open', autospec=True) def test_fetch_image_service(self, open_mock, image_service_mock): @@ -104,7 +77,7 @@ class IronicImagesTestCase(base.TestCase): image_to_raw_mock.assert_called_once_with( 'image_href', 'path', 'path.part') - @mock.patch.object(images, 'qemu_img_info', autospec=True) + @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True) def test_image_to_raw_no_file_format(self, qemu_img_info_mock): info = self.FakeImgInfo() info.file_format = None @@ -115,7 +88,7 @@ class IronicImagesTestCase(base.TestCase): qemu_img_info_mock.assert_called_once_with('path_tmp') self.assertIn("'qemu-img info' parsing failed.", str(e)) - @mock.patch.object(images, 'qemu_img_info', autospec=True) + @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True) def test_image_to_raw_backing_file_present(self, qemu_img_info_mock): info = self.FakeImgInfo() info.file_format = 'raw' @@ -129,8 +102,8 @@ class IronicImagesTestCase(base.TestCase): @mock.patch.object(os, 'rename', autospec=True) @mock.patch.object(os, 'unlink', autospec=True) - @mock.patch.object(images, 'convert_image', autospec=True) - @mock.patch.object(images, 'qemu_img_info', autospec=True) + @mock.patch.object(disk_utils, 'convert_image', autospec=True) + @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True) def test_image_to_raw(self, qemu_img_info_mock, convert_image_mock, unlink_mock, rename_mock): CONF.set_override('force_raw_images', True) @@ -153,8 +126,8 @@ class IronicImagesTestCase(base.TestCase): rename_mock.assert_called_once_with('path.converted', 'path') @mock.patch.object(os, 'unlink', autospec=True) - @mock.patch.object(images, 'convert_image', autospec=True) - @mock.patch.object(images, 'qemu_img_info', autospec=True) + @mock.patch.object(disk_utils, 'convert_image', autospec=True) + @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True) def test_image_to_raw_not_raw_after_conversion(self, qemu_img_info_mock, convert_image_mock, unlink_mock): @@ -173,7 +146,7 @@ class IronicImagesTestCase(base.TestCase): unlink_mock.assert_called_once_with('path_tmp') @mock.patch.object(os, 'rename', autospec=True) - @mock.patch.object(images, 'qemu_img_info', autospec=True) + @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True) def test_image_to_raw_already_raw_format(self, qemu_img_info_mock, rename_mock): info = self.FakeImgInfo() @@ -207,7 +180,7 @@ class IronicImagesTestCase(base.TestCase): show_mock.assert_called_once_with('context', 'image_href', 'image_service') - @mock.patch.object(images, 'qemu_img_info', autospec=True) + @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True) def test_converted_size(self, qemu_img_info_mock): info = self.FakeImgInfo() info.virtual_size = 1