From: ling-yun Date: Fri, 4 Apr 2014 13:58:14 +0000 (+0800) Subject: Create volume fail when image id is "" X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=eacab523be3d8f2f97809c785dba98e5e2e75c7b;p=openstack-build%2Fcinder-build.git Create volume fail when image id is "" When call create volume api with image id "", it will create an empty volume. But when create volume with snapshot id "" or srv volume "", it will return an 404 error. So it should create volume fail when call create volume api with image id "". Change-Id: If2c7b061753bbbd172a09d35b343e73e90a1b7b4 Closes-Bug: #1302609 --- diff --git a/cinder/api/v1/volumes.py b/cinder/api/v1/volumes.py index 1b1f1b0c2..56458ddae 100644 --- a/cinder/api/v1/volumes.py +++ b/cinder/api/v1/volumes.py @@ -419,7 +419,7 @@ class VolumeController(wsgi.Controller): if self.ext_mgr.is_loaded('os-image-create'): # NOTE(jdg): misleading name "imageRef" as it's an image-id image_href = volume.get('imageRef') - if image_href: + if image_href is not None: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index 4a938bac0..a0910ba0e 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -332,7 +332,7 @@ class VolumeController(wsgi.Controller): if self.ext_mgr.is_loaded('os-image-create'): image_href = volume.get('imageRef') - if image_href: + if image_href is not None: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid diff --git a/cinder/tests/api/v1/test_volumes.py b/cinder/tests/api/v1/test_volumes.py index 1dcf653ec..4553f4ca6 100644 --- a/cinder/tests/api/v1/test_volumes.py +++ b/cinder/tests/api/v1/test_volumes.py @@ -231,6 +231,21 @@ class VolumeApiTest(test.TestCase): req, body) + def test_volume_create_with_image_id_with_empty_string(self): + self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.ext_mgr.extensions = {'os-image-create': 'fake'} + vol = {"size": 1, + "display_name": "Volume Test Name", + "display_description": "Volume Test Desc", + "availability_zone": "cinder", + "imageRef": ''} + body = {"volume": vol} + req = fakes.HTTPRequest.blank('/v1/volumes') + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, + req, + body) + def test_volume_update(self): self.stubs.Set(db, 'volume_get', stubs.stub_volume_get_db) self.stubs.Set(volume_api.API, "update", stubs.stub_volume_update) diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index fc6c183fe..662537a00 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -257,6 +257,21 @@ class VolumeApiTest(test.TestCase): req, body) + def test_volume_create_with_image_id_with_empty_string(self): + self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.ext_mgr.extensions = {'os-image-create': 'fake'} + vol = {"size": 1, + "display_name": "Volume Test Name", + "display_description": "Volume Test Desc", + "availability_zone": "cinder", + "imageRef": ''} + body = {"volume": vol} + req = fakes.HTTPRequest.blank('/v2/volumes') + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, + req, + body) + def test_volume_update(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) self.stubs.Set(volume_api.API, "update", stubs.stub_volume_update) diff --git a/cinder/volume/flows/api/create_volume.py b/cinder/volume/flows/api/create_volume.py index da985cbc2..8ef6bb506 100644 --- a/cinder/volume/flows/api/create_volume.py +++ b/cinder/volume/flows/api/create_volume.py @@ -172,7 +172,7 @@ class ExtractVolumeRequestTask(flow_utils.CinderTask): """Checks image existence and validates that the image metadata.""" # Check image existence - if not image_id: + if image_id is None: return # NOTE(harlowja): this should raise an error if the image does not