Raise an exception if qemu-img fails
Raise an exception if the qemu image doesn't exist or qemu-img fails. Co-Authored-By: Chuck Short <chuck.short@canonical.com> Closes-Bug: #1168318 Change-Id: I6b4123590e7d2934de0bc6add900d708d5986039
This commit is contained in:
committed by
Davanum Srinivas (dims)
parent
c52f0a8b24
commit
2ed3e28442
@@ -10691,7 +10691,7 @@ class LibvirtUtilsTestCase(test.TestCase):
|
||||
def test_create_cow_image(self):
|
||||
self.mox.StubOutWithMock(os.path, 'exists')
|
||||
self.mox.StubOutWithMock(utils, 'execute')
|
||||
rval = ('', '')
|
||||
rval = ('stdout', None)
|
||||
os.path.exists('/some/path').AndReturn(True)
|
||||
utils.execute('env', 'LC_ALL=C', 'LANG=C',
|
||||
'qemu-img', 'info', '/some/path').AndReturn(rval)
|
||||
|
||||
@@ -26,11 +26,13 @@ from oslo.config import cfg
|
||||
from nova import context
|
||||
from nova import exception
|
||||
from nova import keymgr
|
||||
from nova.openstack.common import imageutils
|
||||
from nova.openstack.common import units
|
||||
from nova.openstack.common import uuidutils
|
||||
from nova import test
|
||||
from nova.tests import fake_processutils
|
||||
from nova.tests.virt.libvirt import fake_libvirt_utils
|
||||
from nova.virt import images
|
||||
from nova.virt.libvirt import imagebackend
|
||||
from nova.virt.libvirt import rbd_utils
|
||||
|
||||
@@ -221,7 +223,9 @@ class RawTestCase(_ImageTestCase, test.NoDBTestCase):
|
||||
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_create_image_extend(self):
|
||||
@mock.patch.object(images, 'qemu_img_info',
|
||||
return_value=imageutils.QemuImgInfo())
|
||||
def test_create_image_extend(self, fake_qemu_img_info):
|
||||
fn = self.prepare_mocks()
|
||||
fn(max_size=self.SIZE, target=self.TEMPLATE_PATH, image_id=None)
|
||||
imagebackend.libvirt_utils.copy_image(self.TEMPLATE_PATH, self.PATH)
|
||||
@@ -252,7 +256,10 @@ class RawTestCase(_ImageTestCase, test.NoDBTestCase):
|
||||
|
||||
self.mox.VerifyAll()
|
||||
|
||||
def test_resolve_driver_format(self):
|
||||
@mock.patch.object(images, 'qemu_img_info',
|
||||
side_effect=exception.InvalidDiskInfo(
|
||||
reason='invalid path'))
|
||||
def test_resolve_driver_format(self, fake_qemu_img_info):
|
||||
image = self.image_class(self.INSTANCE, self.NAME)
|
||||
driver_format = image.resolve_driver_format()
|
||||
self.assertEqual(driver_format, 'raw')
|
||||
|
||||
@@ -12,13 +12,34 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import os
|
||||
|
||||
import mock
|
||||
|
||||
from nova import exception
|
||||
from nova.openstack.common import processutils
|
||||
from nova import test
|
||||
from nova import utils
|
||||
from nova.virt import images
|
||||
|
||||
|
||||
class QemuTestCase(test.NoDBTestCase):
|
||||
def test_qemu_info_with_bad_path(self):
|
||||
image_info = images.qemu_img_info("/path/that/does/not/exist")
|
||||
self.assertRaises(exception.InvalidDiskInfo,
|
||||
images.qemu_img_info,
|
||||
'/path/that/does/not/exist')
|
||||
|
||||
@mock.patch.object(os.path, 'exists', return_value=True)
|
||||
def test_qemu_info_with_errors(self, path_exists):
|
||||
self.assertRaises(processutils.ProcessExecutionError,
|
||||
images.qemu_img_info,
|
||||
'/fake/path')
|
||||
|
||||
@mock.patch.object(os.path, 'exists', return_value=True)
|
||||
@mock.patch.object(utils, 'execute',
|
||||
return_value=('stdout', None))
|
||||
def test_qemu_info_with_no_errors(self, path_exists,
|
||||
utils_execute):
|
||||
image_info = images.qemu_img_info('/fake/path')
|
||||
self.assertTrue(image_info)
|
||||
self.assertTrue(str(image_info))
|
||||
self.assertTrue(str(image_info))
|
||||
@@ -49,10 +49,16 @@ def qemu_img_info(path):
|
||||
# TODO(mikal): this code should not be referring to a libvirt specific
|
||||
# flag.
|
||||
if not os.path.exists(path) and CONF.libvirt.images_type != 'rbd':
|
||||
return imageutils.QemuImgInfo()
|
||||
msg = (_("Path does not exist %(path)s") % {'path': path})
|
||||
raise exception.InvalidDiskInfo(reason=msg)
|
||||
|
||||
out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C',
|
||||
'qemu-img', 'info', path)
|
||||
if not out:
|
||||
msg = (_("Failed to run qemu-img info on %(path)s : %(error)s") %
|
||||
{'path': path, 'error': err})
|
||||
raise exception.InvalidDiskInfo(reason=msg)
|
||||
|
||||
return imageutils.QemuImgInfo(out)
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import six
|
||||
|
||||
from nova import exception
|
||||
from nova.i18n import _
|
||||
from nova.i18n import _LE
|
||||
from nova.i18n import _LE, _LI
|
||||
from nova import image
|
||||
from nova import keymgr
|
||||
from nova.openstack.common import excutils
|
||||
@@ -355,8 +355,15 @@ class Raw(Image):
|
||||
self.correct_format()
|
||||
|
||||
def _get_driver_format(self):
|
||||
data = images.qemu_img_info(self.path)
|
||||
return data.file_format or 'raw'
|
||||
try:
|
||||
data = images.qemu_img_info(self.path)
|
||||
return data.file_format
|
||||
except exception.InvalidDiskInfo as e:
|
||||
LOG.info(_LI('Failed to get image info from %(path)s, '
|
||||
'error was %(error)s'),
|
||||
{'path': self.path,
|
||||
'error': e})
|
||||
return 'raw'
|
||||
|
||||
def _supports_encryption(self):
|
||||
# NOTE(dgenin): Kernel, ramdisk and disk.config are fetched using
|
||||
|
||||
Reference in New Issue
Block a user