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
This commit is contained in:
parent
503284a81c
commit
60e850a32a
@ -22,12 +22,12 @@ Handling of VM disk images.
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
from ironic_lib import disk_utils
|
||||||
import jinja2
|
import jinja2
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_utils import fileutils
|
from oslo_utils import fileutils
|
||||||
from oslo_utils import imageutils
|
|
||||||
|
|
||||||
from ironic.common import exception
|
from ironic.common import exception
|
||||||
from ironic.common.glance_service import service_utils as glance_utils
|
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)
|
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):
|
def fetch(context, image_href, path, force_raw=False):
|
||||||
# TODO(vish): Improve context handling and add owner and auth data
|
# TODO(vish): Improve context handling and add owner and auth data
|
||||||
# when it is added to glance. Right now there is no
|
# 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):
|
def image_to_raw(image_href, path, path_tmp):
|
||||||
with fileutils.remove_path_on_error(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
|
fmt = data.file_format
|
||||||
if fmt is None:
|
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" %
|
LOG.debug("%(image)s was %(format)s, converting to raw" %
|
||||||
{'image': image_href, 'format': fmt})
|
{'image': image_href, 'format': fmt})
|
||||||
with fileutils.remove_path_on_error(staged):
|
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)
|
os.unlink(path_tmp)
|
||||||
|
|
||||||
data = qemu_img_info(staged)
|
data = disk_utils.qemu_img_info(staged)
|
||||||
if data.file_format != "raw":
|
if data.file_format != "raw":
|
||||||
raise exception.ImageConvertFailed(
|
raise exception.ImageConvertFailed(
|
||||||
image_id=image_href,
|
image_id=image_href,
|
||||||
@ -410,7 +388,7 @@ def converted_size(path):
|
|||||||
:returns: virtual size of the image or 0 if conversion not needed.
|
: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
|
return data.virtual_size
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,10 +19,10 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
from ironic_lib import disk_utils
|
||||||
import mock
|
import mock
|
||||||
from oslo_concurrency import processutils
|
from oslo_concurrency import processutils
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_utils import imageutils
|
|
||||||
import six
|
import six
|
||||||
import six.moves.builtins as __builtin__
|
import six.moves.builtins as __builtin__
|
||||||
|
|
||||||
@ -45,33 +45,6 @@ class IronicImagesTestCase(base.TestCase):
|
|||||||
class FakeImgInfo(object):
|
class FakeImgInfo(object):
|
||||||
pass
|
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(image_service, 'get_image_service', autospec=True)
|
||||||
@mock.patch.object(__builtin__, 'open', autospec=True)
|
@mock.patch.object(__builtin__, 'open', autospec=True)
|
||||||
def test_fetch_image_service(self, open_mock, image_service_mock):
|
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_to_raw_mock.assert_called_once_with(
|
||||||
'image_href', 'path', 'path.part')
|
'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):
|
def test_image_to_raw_no_file_format(self, qemu_img_info_mock):
|
||||||
info = self.FakeImgInfo()
|
info = self.FakeImgInfo()
|
||||||
info.file_format = None
|
info.file_format = None
|
||||||
@ -115,7 +88,7 @@ class IronicImagesTestCase(base.TestCase):
|
|||||||
qemu_img_info_mock.assert_called_once_with('path_tmp')
|
qemu_img_info_mock.assert_called_once_with('path_tmp')
|
||||||
self.assertIn("'qemu-img info' parsing failed.", str(e))
|
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):
|
def test_image_to_raw_backing_file_present(self, qemu_img_info_mock):
|
||||||
info = self.FakeImgInfo()
|
info = self.FakeImgInfo()
|
||||||
info.file_format = 'raw'
|
info.file_format = 'raw'
|
||||||
@ -129,8 +102,8 @@ class IronicImagesTestCase(base.TestCase):
|
|||||||
|
|
||||||
@mock.patch.object(os, 'rename', autospec=True)
|
@mock.patch.object(os, 'rename', autospec=True)
|
||||||
@mock.patch.object(os, 'unlink', autospec=True)
|
@mock.patch.object(os, 'unlink', autospec=True)
|
||||||
@mock.patch.object(images, 'convert_image', autospec=True)
|
@mock.patch.object(disk_utils, 'convert_image', 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(self, qemu_img_info_mock, convert_image_mock,
|
def test_image_to_raw(self, qemu_img_info_mock, convert_image_mock,
|
||||||
unlink_mock, rename_mock):
|
unlink_mock, rename_mock):
|
||||||
CONF.set_override('force_raw_images', True)
|
CONF.set_override('force_raw_images', True)
|
||||||
@ -153,8 +126,8 @@ class IronicImagesTestCase(base.TestCase):
|
|||||||
rename_mock.assert_called_once_with('path.converted', 'path')
|
rename_mock.assert_called_once_with('path.converted', 'path')
|
||||||
|
|
||||||
@mock.patch.object(os, 'unlink', autospec=True)
|
@mock.patch.object(os, 'unlink', autospec=True)
|
||||||
@mock.patch.object(images, 'convert_image', autospec=True)
|
@mock.patch.object(disk_utils, 'convert_image', 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_not_raw_after_conversion(self, qemu_img_info_mock,
|
def test_image_to_raw_not_raw_after_conversion(self, qemu_img_info_mock,
|
||||||
convert_image_mock,
|
convert_image_mock,
|
||||||
unlink_mock):
|
unlink_mock):
|
||||||
@ -173,7 +146,7 @@ class IronicImagesTestCase(base.TestCase):
|
|||||||
unlink_mock.assert_called_once_with('path_tmp')
|
unlink_mock.assert_called_once_with('path_tmp')
|
||||||
|
|
||||||
@mock.patch.object(os, 'rename', autospec=True)
|
@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,
|
def test_image_to_raw_already_raw_format(self, qemu_img_info_mock,
|
||||||
rename_mock):
|
rename_mock):
|
||||||
info = self.FakeImgInfo()
|
info = self.FakeImgInfo()
|
||||||
@ -207,7 +180,7 @@ class IronicImagesTestCase(base.TestCase):
|
|||||||
show_mock.assert_called_once_with('context', 'image_href',
|
show_mock.assert_called_once_with('context', 'image_href',
|
||||||
'image_service')
|
'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):
|
def test_converted_size(self, qemu_img_info_mock):
|
||||||
info = self.FakeImgInfo()
|
info = self.FakeImgInfo()
|
||||||
info.virtual_size = 1
|
info.virtual_size = 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user