from cinder import exception
from cinder.i18n import _
from cinder import rpc
+from cinder import utils
from cinder.volume import volume_types
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,
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
# License for the specific language governing permissions and limitations
# under the License.
+import mock
import six
import webob
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)
{'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)