diff --git a/nova/image/glance.py b/nova/image/glance.py index df6d537a9a..cb5633fe68 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -645,10 +645,17 @@ class GlanceImageServiceV2(object): # where we have to hardcode this parameters. if force_activate: data = '' + # NOTE(danms): If we are using this terrible hack to upload + # zero-length data to activate the image, we cannot claim it + # is some format other than 'raw'. If the caller asked for + # something specific, that's a bug. Otherwise, we must force + # disk_format=raw. if 'disk_format' not in sent_service_image_meta: - sent_service_image_meta['disk_format'] = ( - self._get_image_create_disk_format_default(context) - ) + sent_service_image_meta['disk_format'] = 'raw' + elif sent_service_image_meta['disk_format'] != 'raw': + raise exception.ImageBadRequest( + 'Unable to force activate with disk_format=%s' % ( + sent_service_image_meta['disk_format'])) if 'container_format' not in sent_service_image_meta: sent_service_image_meta['container_format'] = 'bare' diff --git a/nova/tests/unit/image/test_glance.py b/nova/tests/unit/image/test_glance.py index 1623dc85f9..a7051a733f 100644 --- a/nova/tests/unit/image/test_glance.py +++ b/nova/tests/unit/image/test_glance.py @@ -1721,18 +1721,27 @@ class TestCreate(test.NoDBTestCase): client.call.return_value = {'id': '123'} ctx = mock.sentinel.ctx service = glance.GlanceImageServiceV2(client) - with mock.patch.object(service, - '_get_image_create_disk_format_default', - return_value='vdi'): - image_meta = service.create(ctx, image_mock) + image_meta = service.create(ctx, image_mock) trans_to_mock.assert_called_once_with(image_mock) # Verify that the disk_format and container_format kwargs are passed. create_call_kwargs = client.call.call_args_list[0][1]['kwargs'] - self.assertEqual('vdi', create_call_kwargs['disk_format']) + self.assertEqual('raw', create_call_kwargs['disk_format']) self.assertEqual('bare', create_call_kwargs['container_format']) trans_from_mock.assert_called_once_with({'id': '123'}) self.assertEqual(mock.sentinel.trans_from, image_meta) + def test_create_v2_rejects_incompatible_disk_format(self): + client = mock.MagicMock() + client.call.return_value = {'id': '123'} + ctx = mock.sentinel.ctx + service = glance.GlanceImageServiceV2(client) + self.assertRaisesRegex(exception.ImageBadRequest, + 'Unable to force activate', + service._create_v2, + ctx, + {'disk_format': 'qcow2'}, + force_activate=True) + @mock.patch('nova.image.glance._translate_from_glance') @mock.patch('nova.image.glance._translate_to_glance') def test_create_success_v2_with_location(