Reject zero volume size in API

According the discussion[1], set volume size to 0 makes no sense.
This fix reject zero value of volume size with integer or string type.
The previous commit[2] has reject negative volume size, with this fix,
value like 0, '0' will be rejected and value like 001, '040' will pass.

[1] https://review.openstack.org/#/c/589034/
[2] https://review.openstack.org/#/c/164460/
Signed-off-by: zhanggang <zhanggang@cmss.chinamobile.com>

Change-Id: If18a3c69a2815086cc60f9a6a24e6254cc7856df
This commit is contained in:
zhanggang 2018-08-08 16:29:19 +08:00
parent 5f2462f0ea
commit 1de7d33a0c
3 changed files with 31 additions and 5 deletions

View File

@ -50,7 +50,7 @@ configuration_positive_integer = {
"type": "string",
"maxLength": 40,
"minLength": 1,
"pattern": "^[0-9]+$"
"pattern": "^0*[1-9]+[0-9]*$"
}
configuration_non_empty_string = {
@ -72,7 +72,7 @@ volume_size = {
"oneOf": [
{
"type": "integer",
"minimum": 0
"minimum": 1
},
configuration_positive_integer]
}

View File

@ -168,7 +168,7 @@ class MalformedJson(object):
"resize['volume']['size'] %s is not valid under "
"any of the given schemas" % data,
"%s is not of type 'integer'" % data,
"%s does not match '^[0-9]+$'" % data])
"%s does not match '^0*[1-9]+[0-9]*$'" % data])
@test
def test_bad_change_user_password(self):

View File

@ -190,6 +190,12 @@ class TestInstanceController(trove_testtools.TestCase):
validator = jsonschema.Draft4Validator(schema)
self.assertTrue(validator.is_valid(body))
def test_validate_resize_volume_string_start_with_zero(self):
body = {"resize": {"volume": {"size": "0040"}}}
schema = self.controller.get_schema('action', body)
validator = jsonschema.Draft4Validator(schema)
self.assertTrue(validator.is_valid(body))
def test_validate_resize_volume_string_invalid_number(self):
body = {"resize": {"volume": {"size": '-44.0'}}}
schema = self.controller.get_schema('action', body)
@ -197,7 +203,7 @@ class TestInstanceController(trove_testtools.TestCase):
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
self.assertThat(errors[0].context[1].message,
Equals("'-44.0' does not match '^[0-9]+$'"))
Equals("'-44.0' does not match '^0*[1-9]+[0-9]*$'"))
self.assertThat(errors[0].path.pop(), Equals('size'))
def test_validate_resize_volume_invalid_characters(self):
@ -209,7 +215,27 @@ class TestInstanceController(trove_testtools.TestCase):
self.assertThat(errors[0].context[0].message,
Equals("'x' is not of type 'integer'"))
self.assertThat(errors[0].context[1].message,
Equals("'x' does not match '^[0-9]+$'"))
Equals("'x' does not match '^0*[1-9]+[0-9]*$'"))
self.assertThat(errors[0].path.pop(), Equals('size'))
def test_validate_resize_volume_zero_number(self):
body = {"resize": {"volume": {"size": 0}}}
schema = self.controller.get_schema('action', body)
validator = jsonschema.Draft4Validator(schema)
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
self.assertThat(errors[0].context[0].message,
Equals("0 is less than the minimum of 1"))
self.assertThat(errors[0].path.pop(), Equals('size'))
def test_validate_resize_volume_string_zero_number(self):
body = {"resize": {"volume": {"size": '0'}}}
schema = self.controller.get_schema('action', body)
validator = jsonschema.Draft4Validator(schema)
self.assertFalse(validator.is_valid(body))
errors = sorted(validator.iter_errors(body), key=lambda e: e.path)
self.assertThat(errors[0].context[1].message,
Equals("'0' does not match '^0*[1-9]+[0-9]*$'"))
self.assertThat(errors[0].path.pop(), Equals('size'))
def test_validate_resize_instance(self):