]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Create volume fail when image id is ""
authorling-yun <zengyunling@huawei.com>
Fri, 4 Apr 2014 13:58:14 +0000 (21:58 +0800)
committerling-yun <zengyunling@huawei.com>
Wed, 16 Apr 2014 08:13:21 +0000 (16:13 +0800)
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

cinder/api/v1/volumes.py
cinder/api/v2/volumes.py
cinder/tests/api/v1/test_volumes.py
cinder/tests/api/v2/test_volumes.py
cinder/volume/flows/api/create_volume.py

index 1b1f1b0c249e2e849232405785a4883084ebf491..56458ddaef6c27c6f5ceb97bf22a70b98ba2075b 100644 (file)
@@ -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
 
index 4a938bac01b8b19334d4de9e2c8ba2c3a5858f58..a0910ba0e22e8e76d7a568a859428d4b62a3cf05 100644 (file)
@@ -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
 
index 1dcf653ecf932b21bb416b2c3c1be7ad54892bdf..4553f4ca616cfe4887f8cf9ce8981b9ff6628434 100644 (file)
@@ -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)
index fc6c183fedf86f9e5e1c7b84950cc1128d3e0894..662537a00cd692ae6a9a7d91bb10bb1b1011d16e 100644 (file)
@@ -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)
index da985cbc226a2459d29c0252a3a367606d9294bb..8ef6bb5067d571fba39f8647bd7cb1f8db394f6d 100644 (file)
@@ -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