diff --git a/nova/compute/api.py b/nova/compute/api.py index 9a0f47f0eec7..8e097b0f0bcf 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1586,12 +1586,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): diff --git a/nova/tests/unit/compute/test_compute_api.py b/nova/tests/unit/compute/test_compute_api.py index f18bc92627a9..719617de85d0 100644 --- a/nova/tests/unit/compute/test_compute_api.py +++ b/nova/tests/unit/compute/test_compute_api.py @@ -4604,6 +4604,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(