Merge "Remove flavor id and name validation code"

This commit is contained in:
Zuul 2019-04-03 06:39:46 +00:00 committed by Gerrit Code Review
commit 292315c00d
3 changed files with 4 additions and 74 deletions

View File

@ -24,7 +24,6 @@ from oslo_utils import strutils
from oslo_utils import uuidutils
import six
from nova.api.validation import parameter_types
import nova.conf
from nova import context
from nova.db import api as db
@ -35,11 +34,6 @@ from nova import utils
CONF = nova.conf.CONF
# NOTE(luisg): Flavor names can include non-ascii characters so that users can
# create flavor names in locales that use them, however flavor IDs are limited
# to ascii characters.
VALID_ID_REGEX = re.compile("^[\w\.\- ]*$")
# Validate extra specs key names.
VALID_EXTRASPEC_NAME_REGEX = re.compile(r"[\w\.\- :]+$", re.UNICODE)
@ -86,36 +80,11 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb=0, flavorid=None,
if isinstance(name, six.string_types):
name = name.strip()
# ensure name do not exceed 255 characters
utils.check_string_length(name, 'name', min_length=1, max_length=255)
# ensure name does not contain any special characters
valid_name = parameter_types.valid_name_regex_obj.search(name)
if not valid_name:
msg = _("Flavor names can only contain printable characters "
"and horizontal spaces.")
raise exception.InvalidInput(reason=msg)
# NOTE(vish): Internally, flavorid is stored as a string but it comes
# in through json as an integer, so we convert it here.
flavorid = six.text_type(flavorid)
# ensure leading/trailing whitespaces not present.
if flavorid.strip() != flavorid:
msg = _("id cannot contain leading and/or trailing whitespace(s)")
raise exception.InvalidInput(reason=msg)
# ensure flavor id does not exceed 255 characters
utils.check_string_length(flavorid, 'id', min_length=1,
max_length=255)
# ensure flavor id does not contain any special characters
valid_flavor_id = VALID_ID_REGEX.search(flavorid)
if not valid_flavor_id:
msg = _("Flavor id can only contain letters from A-Z (both cases), "
"periods, dashes, underscores and spaces.")
raise exception.InvalidInput(reason=msg)
# NOTE(wangbo): validate attributes of the creating flavor.
# ram and vcpus should be positive ( > 0) integers.
# disk, ephemeral and swap should be non-negative ( >= 0) integers.

View File

@ -172,6 +172,10 @@ class FlavorManageTestV21(test.NoDBTestCase):
self.request_body['flavor']['name'] = 'a' * 256
self._create_flavor_bad_request_case(self.request_body)
def test_create_with_short_name(self):
self.request_body['flavor']['name'] = ''
self._create_flavor_bad_request_case(self.request_body)
def test_create_with_name_leading_trailing_spaces(self):
self.request_body['flavor']['name'] = ' test '
self._create_flavor_bad_request_case(self.request_body)

View File

@ -172,49 +172,6 @@ class CreateInstanceTypeTest(test.TestCase):
self.assertRaises(exception.InvalidInput, flavors.create,
*create_args, **create_kwargs)
def test_create_with_valid_name(self):
# Names can contain alphanumeric and [_.- ]
flavors.create('azAZ09. -_', 64, 1, 120)
# And they are not limited to ascii characters
# E.g.: m1.huge in simplified Chinese
flavors.create(u'm1.\u5DE8\u5927', 6400, 100, 12000)
def test_name_with_special_characters(self):
# Names can contain all printable characters
flavors.create('_foo.bar-123', 64, 1, 120)
# Ensure instance types raises InvalidInput for invalid characters.
self.assertInvalidInput('foobar\x00', 64, 1, 120)
def test_name_with_non_printable_characters(self):
# Names cannot contain non printable characters
self.assertInvalidInput(u'm1.\u0C77 #', 64, 1, 120)
def test_name_length_checks(self):
MAX_LEN = 255
# Flavor name with 255 characters or less is valid.
flavors.create('a' * MAX_LEN, 64, 1, 120)
# Flavor name which is more than 255 characters will cause error.
self.assertInvalidInput('a' * (MAX_LEN + 1), 64, 1, 120)
# Flavor name which is empty should cause an error
self.assertInvalidInput('', 64, 1, 120)
def test_all_whitespace_flavor_names_rejected(self):
self.assertInvalidInput(' ', 64, 1, 120)
def test_flavorid_with_invalid_characters(self):
# Ensure Flavor ID can only contain [a-zA-Z0-9_.- ]
self.assertInvalidInput('a', 64, 1, 120, flavorid=u'\u2605')
self.assertInvalidInput('a', 64, 1, 120, flavorid='%%$%$@#$#@$@#$^%')
def test_flavorid_length_checks(self):
MAX_LEN = 255
# Flavor ID which is more than 255 characters will cause error.
self.assertInvalidInput('a', 64, 1, 120, flavorid='a' * (MAX_LEN + 1))
def test_memory_must_be_positive_db_integer(self):
self.assertInvalidInput('flavor1', 'foo', 1, 120)
self.assertInvalidInput('flavor1', -1, 1, 120)