diff --git a/nova/api/validation/parameter_types.py b/nova/api/validation/parameter_types.py index 5ca9a19daf..72ae212099 100644 --- a/nova/api/validation/parameter_types.py +++ b/nova/api/validation/parameter_types.py @@ -15,6 +15,7 @@ Common parameter types for validating request Body. """ + import copy import functools import re @@ -27,7 +28,6 @@ _REGEX_RANGE_CACHE = {} def memorize(func): - @functools.wraps(func) def memorizer(*args, **kwargs): global _REGEX_RANGE_CACHE @@ -37,6 +37,7 @@ def memorize(func): value = func(*args, **kwargs) _REGEX_RANGE_CACHE[key] = value return value + return memorizer @@ -103,6 +104,7 @@ def _get_all_chars(): # constraint fails and this causes issues for some unittests when # PYTHONHASHSEED is set randomly. + @memorize def _build_regex_range(ws=True, invert=False, exclude=None): """Build a range regex for a set of characters in utf8. @@ -139,8 +141,7 @@ def _build_regex_range(ws=True, invert=False, exclude=None): else: # Zs is the unicode class for space characters, of which # there are about 10 in this range. - result = (_is_printable(char) and - unicodedata.category(char) != "Zs") + result = _is_printable(char) and unicodedata.category(char) != 'Zs' if invert is True: return not result return result @@ -165,7 +166,6 @@ def _build_regex_range(ws=True, invert=False, exclude=None): valid_name_regex_base = '^(?![%s])[%s]*(?= 4.23.0 + self.assertRaisesRegex( + exception.ValidationError, + '(is too short)|(should be non-empty)', # jsonschema < / >= 4.23.0 + self.controller.create, self.req, body=body) def test_keypair_create_with_name_too_long(self): body = { @@ -138,9 +125,9 @@ class KeypairsTestV21(test.TestCase): 'name': 'a' * 256 } } - self._test_keypair_create_bad_request_case(body, - self.validation_error, - 'is too long') + self.assertRaisesRegex( + exception.ValidationError, 'is too long', + self.controller.create, self.req, body=body) def test_keypair_create_with_name_leading_trailing_spaces(self): body = { @@ -149,9 +136,9 @@ class KeypairsTestV21(test.TestCase): } } expected_msg = 'Can not start or end with whitespace.' - self._test_keypair_create_bad_request_case(body, - self.validation_error, - expected_msg) + self.assertRaisesRegex( + exception.ValidationError, expected_msg, + self.controller.create, self.req, body=body) def test_keypair_create_with_name_leading_trailing_spaces_compat_mode( self): @@ -166,10 +153,10 @@ class KeypairsTestV21(test.TestCase): 'name': 'test/keypair' } } - expected_msg = 'Only expected characters' - self._test_keypair_create_bad_request_case(body, - self.validation_error, - expected_msg) + expected_msg = 'Invalid input for field/attribute name.' + self.assertRaisesRegex( + exception.ValidationError, expected_msg, + self.controller.create, self.req, body=body) def test_keypair_create_with_special_characters(self): body = { @@ -177,10 +164,10 @@ class KeypairsTestV21(test.TestCase): 'name': keypair_name_2_92_compatible } } - expected_msg = 'Only expected characters' - self._test_keypair_create_bad_request_case(body, - self.validation_error, - expected_msg) + expected_msg = 'Invalid input for field/attribute name.' + self.assertRaisesRegex( + exception.ValidationError, expected_msg, + self.controller.create, self.req, body=body) def test_keypair_import_bad_key(self): body = { @@ -189,15 +176,16 @@ class KeypairsTestV21(test.TestCase): 'public_key': 'ssh-what negative', }, } - self._test_keypair_create_bad_request_case(body, - webob.exc.HTTPBadRequest) + self.assertRaises( + webob.exc.HTTPBadRequest, self.controller.create, self.req, + body=body) def test_keypair_create_with_invalid_keypair_body(self): body = {'alpha': {'name': 'create_test'}} expected_msg = "'keypair' is a required property" - self._test_keypair_create_bad_request_case(body, - self.validation_error, - expected_msg) + self.assertRaisesRegex( + exception.ValidationError, expected_msg, + self.controller.create, self.req, body=body) def test_keypair_import(self): body = { diff --git a/nova/tests/unit/test_api_validation.py b/nova/tests/unit/test_api_validation.py index b9b408f0cb..357e9bb5e2 100644 --- a/nova/tests/unit/test_api_validation.py +++ b/nova/tests/unit/test_api_validation.py @@ -731,33 +731,6 @@ class NameWithLeadingTrailingSpacesTestCase(APIValidationTestCase): pass -class NoneTypeTestCase(APIValidationTestCase): - - post_schema = { - 'type': 'object', - 'properties': { - 'foo': parameter_types.none - } - } - - def test_validate_none(self): - self.post(body={'foo': 'None'}, req=FakeRequest()) - self.post(body={'foo': None}, req=FakeRequest()) - self.post(body={'foo': {}}, req=FakeRequest()) - - def test_validate_none_fails(self): - detail = ("Invalid input for field/attribute foo. Value: ." - " '' is not one of ['None', None, {}]") - self.check_validation_error(self.post, body={'foo': ''}, - expected_detail=detail) - - detail = ("Invalid input for field/attribute foo. Value: " - "{'key': 'val'}. {'key': 'val'} is not one of " - "['None', None, {}]") - self.check_validation_error(self.post, body={'foo': {'key': 'val'}}, - expected_detail=detail) - - class NameOrNoneTestCase(APIValidationTestCase): post_schema = {