]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Validate name and description for volume type
authorPranaliDeore <pranali11.deore@nttdata.com>
Wed, 29 Apr 2015 09:35:52 +0000 (02:35 -0700)
committerPranaliDeore <pranali11.deore@nttdata.com>
Wed, 20 May 2015 13:24:36 +0000 (06:24 -0700)
If you pass name parameter with long string of more than
255 characters to type-create and type-update api, then
it returns 500 error code.

Added a check in _create() and _update() methods of 'types_manage'
extension to validate string limit and returned 400 if limit exceeds.

Closes-Bug: 1450388
Change-Id: I68c607df90a3dfeae7a841a1dddee5416fa42d13

cinder/api/contrib/types_manage.py
cinder/tests/unit/api/contrib/test_types_manage.py

index 434d4fa4300052b3f2c493cf2ff9073676f66324..33a4978bd5ec16d6c03daaf1312eb8ca97ec0ab9 100644 (file)
@@ -25,6 +25,7 @@ from cinder.api.views import types as views_types
 from cinder import exception
 from cinder.i18n import _
 from cinder import rpc
+from cinder import utils
 from cinder.volume import volume_types
 
 
@@ -66,6 +67,13 @@ class VolumeTypesManageController(wsgi.Controller):
             msg = _("Volume type name can not be empty.")
             raise webob.exc.HTTPBadRequest(explanation=msg)
 
+        utils.check_string_length(name, 'Type name',
+                                  min_length=1, max_length=255)
+
+        if description is not None:
+            utils.check_string_length(description, 'Type description',
+                                      min_length=0, max_length=255)
+
         try:
             volume_types.create(context,
                                 name,
@@ -112,6 +120,14 @@ class VolumeTypesManageController(wsgi.Controller):
             msg = _("Specify either volume type name and/or description.")
             raise webob.exc.HTTPBadRequest(explanation=msg)
 
+        if name:
+            utils.check_string_length(name, 'Type name',
+                                      min_length=1, max_length=255)
+
+        if description is not None:
+            utils.check_string_length(description, 'Type description',
+                                      min_length=0, max_length=255)
+
         try:
             volume_types.update(context, id, name, description)
             # Get the updated
index f20419d142fe43c68dab8d796a57dc983927498d..ba7b2a86116910a3763041c515a5417f5db73dc3 100644 (file)
@@ -13,6 +13,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import mock
 import six
 import webob
 
@@ -198,6 +199,44 @@ class VolumeTypesManageApiTest(test.TestCase):
         self._check_test_results(res_dict, {
             'expected_name': 'vol_type_1', 'expected_desc': 'vol_type_desc_1'})
 
+    @mock.patch('cinder.volume.volume_types.create')
+    @mock.patch('cinder.volume.volume_types.get_volume_type_by_name')
+    def test_create_with_description_of_zero_length(
+            self, mock_get_volume_type_by_name, mock_create_type):
+        mock_get_volume_type_by_name.return_value = \
+            {'extra_specs': {"key1": "value1"},
+             'id': 1,
+             'name': u'vol_type_1',
+             'description': u''}
+
+        type_description = ""
+        body = {"volume_type": {"name": "vol_type_1",
+                                "description": type_description,
+                                "extra_specs": {"key1": "value1"}}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types')
+
+        res_dict = self.controller._create(req, body)
+
+        self._check_test_results(res_dict, {
+            'expected_name': 'vol_type_1', 'expected_desc': ''})
+
+    def test_create_type_with_name_too_long(self):
+        type_name = 'a' * 256
+        body = {"volume_type": {"name": type_name,
+                                "extra_specs": {"key1": "value1"}}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types')
+        self.assertRaises(exception.InvalidInput,
+                          self.controller._create, req, body)
+
+    def test_create_type_with_description_too_long(self):
+        type_description = 'a' * 256
+        body = {"volume_type": {"name": "vol_type_1",
+                                "description": type_description,
+                                "extra_specs": {"key1": "value1"}}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types')
+        self.assertRaises(exception.InvalidInput,
+                          self.controller._create, req, body)
+
     def test_create_duplicate_type_fail(self):
         self.stubs.Set(volume_types, 'create',
                        return_volume_types_create_duplicate_type)
@@ -245,6 +284,40 @@ class VolumeTypesManageApiTest(test.TestCase):
                                  {'expected_desc': 'vol_type_desc_1_1',
                                   'expected_name': 'vol_type_1_1'})
 
+    @mock.patch('cinder.volume.volume_types.update')
+    @mock.patch('cinder.volume.volume_types.get_volume_type')
+    def test_update_type_with_description_having_length_zero(
+            self, mock_get_volume_type, mock_type_update):
+
+        mock_get_volume_type.return_value = \
+            {'id': 1, 'name': u'vol_type_1', 'description': u''}
+
+        type_description = ""
+        body = {"volume_type": {"description": type_description}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types/1')
+        req.method = 'PUT'
+        resp = self.controller._update(req, '1', body)
+        self._check_test_results(resp,
+                                 {'expected_desc': '',
+                                  'expected_name': 'vol_type_1'})
+
+    def test_update_type_with_name_too_long(self):
+        type_name = 'a' * 256
+        body = {"volume_type": {"name": type_name,
+                                "description": ""}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types/1')
+        req.method = 'PUT'
+        self.assertRaises(exception.InvalidInput,
+                          self.controller._update, req, '1', body)
+
+    def test_update_type_with_description_too_long(self):
+        type_description = 'a' * 256
+        body = {"volume_type": {"description": type_description}}
+        req = fakes.HTTPRequest.blank('/v2/fake/types/1')
+        req.method = 'PUT'
+        self.assertRaises(exception.InvalidInput,
+                          self.controller._update, req, '1', body)
+
     def test_update_non_exist(self):
         self.stubs.Set(volume_types, 'update',
                        return_volume_types_update)