From d5f1720e028e702fb77f595959341787c339abe0 Mon Sep 17 00:00:00 2001 From: xiexs Date: Thu, 23 Jun 2016 10:31:12 -0400 Subject: [PATCH] Add validation for type extra_specs There is no checking for type extra_specs by the volume type creation API /v2/types, so that a 500 error will be encountered when invalid extra_specs specified. This patch tries to add a validating logic for it by using utils.validate_extra_specs(). Change-Id: Idda9a3e69b43f71d866de70df72f9d12e5f2f1c9 Closes-Bug: #1593588 --- cinder/api/contrib/types_manage.py | 1 + .../tests/unit/api/contrib/test_types_manage.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/cinder/api/contrib/types_manage.py b/cinder/api/contrib/types_manage.py index b22d239ad74..9998da443f3 100644 --- a/cinder/api/contrib/types_manage.py +++ b/cinder/api/contrib/types_manage.py @@ -58,6 +58,7 @@ class VolumeTypesManageController(wsgi.Controller): name = vol_type.get('name', None) description = vol_type.get('description') specs = vol_type.get('extra_specs', {}) + utils.validate_extra_specs(specs) is_public = vol_type.get('os-volume-type-access:is_public', True) if name is None or len(name.strip()) == 0: diff --git a/cinder/tests/unit/api/contrib/test_types_manage.py b/cinder/tests/unit/api/contrib/test_types_manage.py index 48cffe45253..81beff6ae85 100644 --- a/cinder/tests/unit/api/contrib/test_types_manage.py +++ b/cinder/tests/unit/api/contrib/test_types_manage.py @@ -17,6 +17,8 @@ import mock import six import webob +import ddt + from cinder.api.contrib import types_manage from cinder import context from cinder import exception @@ -134,6 +136,7 @@ def return_volume_types_get_default_not_found(): return {} +@ddt.ddt class VolumeTypesManageApiTest(test.TestCase): def setUp(self): super(VolumeTypesManageApiTest, self).setUp() @@ -339,6 +342,18 @@ class VolumeTypesManageApiTest(test.TestCase): self.controller._create, req, body) + @ddt.data({'a' * 256: 'a'}, + {'a': 'a' * 256}, + {'': 'a'}) + def test_create_type_with_invalid_extra_specs(self, value): + body = {"volume_type": {"name": "vol_type_1", + "os-volume-type-access:is_public": False, + "description": "test description"}} + body['volume_type']['extra_specs'] = value + req = fakes.HTTPRequest.blank('/v2/%s/types' % fake.PROJECT_ID) + self.assertRaises(exception.InvalidInput, + self.controller._create, req, body) + @mock.patch('cinder.volume.volume_types.update') @mock.patch('cinder.volume.volume_types.get_volume_type') def test_update(self, mock_get, mock_update):