Merge "Use check_virtual_size to do the size check"

This commit is contained in:
Zuul 2018-06-26 00:12:08 +00:00 committed by Gerrit Code Review
commit 514c63bd7c
2 changed files with 39 additions and 23 deletions

@ -463,12 +463,8 @@ def fetch_verify_image(context, image_service, image_id, dest,
# NOTE(xqueralt): If the image virtual size doesn't fit in the
# requested volume there is no point on resizing it because it will
# generate an unusable image.
if size is not None and data.virtual_size > size:
params = {'image_size': data.virtual_size, 'volume_size': size}
reason = _("Size is %(image_size)dGB and doesn't fit in a "
"volume of size %(volume_size)dGB.") % params
raise exception.ImageUnacceptable(image_id=image_id,
reason=reason)
if size is not None:
check_virtual_size(data.virtual_size, size, image_id)
def fetch_to_vhd(context, image_service,
@ -532,16 +528,12 @@ def fetch_to_volume_format(context, image_service,
return
data = qemu_img_info(tmp, run_as_root=run_as_root)
virt_size = int(math.ceil(float(data.virtual_size) / units.Gi))
# NOTE(xqueralt): If the image virtual size doesn't fit in the
# requested volume there is no point on resizing it because it will
# generate an unusable image.
if size is not None and virt_size > size:
params = {'image_size': virt_size, 'volume_size': size}
reason = _("Size is %(image_size)dGB and doesn't fit in a "
"volume of size %(volume_size)dGB.") % params
raise exception.ImageUnacceptable(image_id=image_id, reason=reason)
if size is not None:
check_virtual_size(data.virtual_size, size, image_id)
fmt = data.file_format
if fmt is None:
@ -650,9 +642,11 @@ def check_available_space(dest, image_size, image_id):
free_space = psutil.disk_usage(dest).free
if free_space <= image_size:
msg = ('There is no space to convert image. '
'Requested: %(image_size)s, available: %(free_space)s'
) % {'image_size': image_size, 'free_space': free_space}
msg = ('There is no space on %(dest_dir)s to convert image. '
'Requested: %(image_size)s, available: %(free_space)s.'
) % {'dest_dir': dest,
'image_size': image_size,
'free_space': free_space}
raise exception.ImageTooBig(image_id=image_id, reason=msg)

@ -500,12 +500,13 @@ class TestVerifyImage(test.TestCase):
(mock_fileutils.remove_path_on_error.return_value.__exit__
.assert_called_once_with(None, None, None))
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.check_available_space')
@mock.patch('cinder.image.image_utils.qemu_img_info')
@mock.patch('cinder.image.image_utils.fileutils')
@mock.patch('cinder.image.image_utils.fetch')
def test_kwargs(self, mock_fetch, mock_fileutils, mock_info,
mock_check_space):
mock_check_space, mock_check_size):
ctxt = mock.sentinel.context
image_service = FakeImageService()
image_id = mock.sentinel.image_id
@ -530,6 +531,8 @@ class TestVerifyImage(test.TestCase):
.assert_called_once_with())
(mock_fileutils.remove_path_on_error.return_value.__exit__
.assert_called_once_with(None, None, None))
mock_check_size.assert_called_once_with(mock_data.virtual_size,
size, image_id)
@mock.patch('cinder.image.image_utils.qemu_img_info')
@mock.patch('cinder.image.image_utils.fileutils')
@ -563,10 +566,12 @@ class TestVerifyImage(test.TestCase):
image_utils.fetch_verify_image,
ctxt, image_service, image_id, dest)
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.qemu_img_info')
@mock.patch('cinder.image.image_utils.fileutils')
@mock.patch('cinder.image.image_utils.fetch')
def test_size_error(self, mock_fetch, mock_fileutils, mock_info):
def test_size_error(self, mock_fetch, mock_fileutils, mock_info,
mock_check_size):
ctxt = mock.sentinel.context
image_service = mock.Mock()
image_id = mock.sentinel.image_id
@ -575,7 +580,10 @@ class TestVerifyImage(test.TestCase):
mock_data = mock_info.return_value
mock_data.file_format = 'test_format'
mock_data.backing_file = None
mock_data.virtual_size = 2
mock_data.virtual_size = 2 * units.Gi
mock_check_size.side_effect = exception.ImageUnacceptable(
image_id='fake_image_id', reason='test')
self.assertRaises(exception.ImageUnacceptable,
image_utils.fetch_verify_image,
@ -903,6 +911,7 @@ class TestFetchToVolumeFormat(test.TestCase):
run_as_root=True,
src_format='raw')
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.check_available_space')
@mock.patch('cinder.image.image_utils.convert_image')
@mock.patch('cinder.image.image_utils.volume_utils.copy_volume')
@ -916,7 +925,7 @@ class TestFetchToVolumeFormat(test.TestCase):
@mock.patch('cinder.image.image_utils.CONF')
def test_kwargs(self, mock_conf, mock_temp, mock_info, mock_fetch,
mock_is_xen, mock_repl_xen, mock_copy, mock_convert,
mock_check_space):
mock_check_space, mock_check_size):
ctxt = mock.sentinel.context
image_service = FakeImageService()
image_id = mock.sentinel.image_id
@ -953,7 +962,10 @@ class TestFetchToVolumeFormat(test.TestCase):
out_subformat=out_subformat,
run_as_root=run_as_root,
src_format='raw')
mock_check_size.assert_called_once_with(data.virtual_size,
size, image_id)
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.check_available_space')
@mock.patch('cinder.image.image_utils.convert_image')
@mock.patch('cinder.image.image_utils.volume_utils.copy_volume')
@ -967,7 +979,8 @@ class TestFetchToVolumeFormat(test.TestCase):
@mock.patch('cinder.image.image_utils.CONF')
def test_convert_from_vhd(self, mock_conf, mock_temp, mock_info,
mock_fetch, mock_is_xen, mock_repl_xen,
mock_copy, mock_convert, mock_check_space):
mock_copy, mock_convert, mock_check_space,
mock_check_size):
ctxt = mock.sentinel.context
image_id = mock.sentinel.image_id
dest = mock.sentinel.dest
@ -1006,6 +1019,7 @@ class TestFetchToVolumeFormat(test.TestCase):
run_as_root=run_as_root,
src_format=expect_format)
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.check_available_space')
@mock.patch('cinder.image.image_utils.convert_image')
@mock.patch('cinder.image.image_utils.volume_utils.copy_volume')
@ -1017,7 +1031,8 @@ class TestFetchToVolumeFormat(test.TestCase):
@mock.patch('cinder.image.image_utils.CONF')
def test_convert_from_iso(self, mock_conf, mock_temp, mock_info,
mock_fetch, mock_is_xen, mock_copy,
mock_convert, mock_check_space):
mock_convert, mock_check_space,
mock_check_size):
ctxt = mock.sentinel.context
image_id = mock.sentinel.image_id
dest = mock.sentinel.dest
@ -1248,6 +1263,7 @@ class TestFetchToVolumeFormat(test.TestCase):
self.assertFalse(mock_copy.called)
self.assertFalse(mock_convert.called)
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.convert_image')
@mock.patch('cinder.image.image_utils.volume_utils.copy_volume')
@mock.patch(
@ -1259,7 +1275,8 @@ class TestFetchToVolumeFormat(test.TestCase):
@mock.patch('cinder.image.image_utils.temporary_file')
@mock.patch('cinder.image.image_utils.CONF')
def test_size_error(self, mock_conf, mock_temp, mock_info, mock_fetch,
mock_is_xen, mock_repl_xen, mock_copy, mock_convert):
mock_is_xen, mock_repl_xen, mock_copy, mock_convert,
mock_check_size):
ctxt = mock.sentinel.context
image_service = mock.Mock(temp_images=None)
image_id = mock.sentinel.image_id
@ -1277,6 +1294,9 @@ class TestFetchToVolumeFormat(test.TestCase):
data.virtual_size = int(1234.5 * units.Gi)
tmp = mock_temp.return_value.__enter__.return_value
mock_check_size.side_effect = exception.ImageUnacceptable(
image_id='fake_image_id', reason='test')
self.assertRaises(
exception.ImageUnacceptable,
image_utils.fetch_to_volume_format,
@ -1391,6 +1411,7 @@ class TestFetchToVolumeFormat(test.TestCase):
self.assertFalse(mock_copy.called)
self.assertFalse(mock_convert.called)
@mock.patch('cinder.image.image_utils.check_virtual_size')
@mock.patch('cinder.image.image_utils.check_available_space')
@mock.patch('cinder.image.image_utils.convert_image')
@mock.patch('cinder.image.image_utils.volume_utils.copy_volume')
@ -1404,7 +1425,8 @@ class TestFetchToVolumeFormat(test.TestCase):
@mock.patch('cinder.image.image_utils.CONF')
def test_xenserver_to_vhd(self, mock_conf, mock_temp, mock_info,
mock_fetch, mock_is_xen, mock_repl_xen,
mock_copy, mock_convert, mock_check_space):
mock_copy, mock_convert, mock_check_space,
mock_check_size):
ctxt = mock.sentinel.context
image_service = FakeImageService()
image_id = mock.sentinel.image_id