Should not skip volume_size check for bdm.image_id == image_ref case

The volume size should be checked in bdm.sourece_type=image,
dest_type=volume case no matter what the image
is, but we skipped the check if the bdm.image_id == image_ref,
we should not skipped the volume_size check.

Change-Id: Ib10579280b63a4dd59ac76733aa0ff48fd2e024b
closes-bug: 1818798
This commit is contained in:
Kevin_Zheng 2019-03-06 16:48:10 +08:00
parent 14c4c8040c
commit 24fe74d126
2 changed files with 37 additions and 6 deletions

View File

@ -1493,12 +1493,12 @@ class API(base.Base):
snapshot_id = bdm.snapshot_id
volume_id = bdm.volume_id
image_id = bdm.image_id
if (image_id is not None and
image_id != instance.get('image_ref')):
try:
self._get_image(context, image_id)
except Exception:
raise exception.InvalidBDMImage(id=image_id)
if image_id is not None:
if image_id != instance.get('image_ref'):
try:
self._get_image(context, image_id)
except Exception:
raise exception.InvalidBDMImage(id=image_id)
if (bdm.source_type == 'image' and
bdm.destination_type == 'volume' and
not bdm.volume_size):

View File

@ -4540,6 +4540,37 @@ class _ComputeAPIUnitTestMixIn(object):
vol_type_requested.assert_any_call(bdms[1], volume_type,
volume_types)
@mock.patch('nova.compute.api.API._get_image')
def test_validate_bdm_missing_volume_size(self, mock_get_image):
"""Tests that _validate_bdm fail if there volume_size not provided
"""
instance = self._create_instance_obj()
# first we test the case of instance.image_ref == bdm.image_id
bdms = objects.BlockDeviceMappingList(objects=[
objects.BlockDeviceMapping(
boot_index=0, image_id=instance.image_ref,
source_type='image', destination_type='volume',
volume_type=None, snapshot_id=None, volume_id=None,
volume_size=None)])
self.assertRaises(exception.InvalidBDM,
self.compute_api._validate_bdm,
self.context, instance, objects.Flavor(),
bdms)
self.assertEqual(0, mock_get_image.call_count)
# then we test the case of instance.image_ref != bdm.image_id
image_id = uuids.image_id
bdms = objects.BlockDeviceMappingList(objects=[
objects.BlockDeviceMapping(
boot_index=0, image_id=image_id,
source_type='image', destination_type='volume',
volume_type=None, snapshot_id=None, volume_id=None,
volume_size=None)])
self.assertRaises(exception.InvalidBDM,
self.compute_api._validate_bdm,
self.context, instance, objects.Flavor(),
bdms)
mock_get_image.assert_called_once_with(self.context, image_id)
@mock.patch.object(objects.service, 'get_minimum_version_all_cells',
return_value=compute_api.MIN_COMPUTE_VOLUME_TYPE)
def test_the_specified_volume_type_id_assignment_to_name(