Merge "Replace voluptuous with JSONSchema to validate resize action"

This commit is contained in:
Jenkins 2017-06-21 09:45:39 +00:00 committed by Gerrit Code Review
commit 3fb4cadd76
2 changed files with 33 additions and 33 deletions

View File

@ -17,14 +17,11 @@
# limitations under the License.
#
import jsonschema
from oslo_log import log
import six
import voluptuous
from watcher._i18n import _
from watcher.applier.actions import base
from watcher.common import nova_helper
from watcher.common import utils
LOG = log.getLogger(__name__)
@ -49,21 +46,33 @@ class Resize(base.BaseAction):
# input parameters constants
FLAVOR = 'flavor'
def check_resource_id(self, value):
if (value is not None and
len(value) > 0 and not
utils.is_uuid_like(value)):
raise voluptuous.Invalid(_("The parameter "
"resource_id is invalid."))
@property
def schema(self):
return voluptuous.Schema({
voluptuous.Required(self.RESOURCE_ID): self.check_resource_id,
voluptuous.Required(self.FLAVOR):
voluptuous.All(voluptuous.Any(*six.string_types),
voluptuous.Length(min=1)),
})
return {
'type': 'object',
'properties': {
'resource_id': {
'type': 'string',
'minlength': 1,
'pattern': ('^([a-fA-F0-9]){8}-([a-fA-F0-9]){4}-'
'([a-fA-F0-9]){4}-([a-fA-F0-9]){4}-'
'([a-fA-F0-9]){12}$')
},
'flavor': {
'type': 'string',
'minlength': 1,
},
},
'required': ['resource_id', 'flavor'],
'additionalProperties': False,
}
def validate_parameters(self):
try:
jsonschema.validate(self.input_parameters, self.schema)
return True
except jsonschema.ValidationError as e:
raise e
@property
def instance_uuid(self):

View File

@ -14,9 +14,8 @@
# limitations under the License.
from __future__ import unicode_literals
import jsonschema
import mock
import voluptuous
from watcher.applier.actions import base as baction
from watcher.applier.actions import resize
@ -69,31 +68,23 @@ class TestResize(base.TestCase):
self.INSTANCE_UUID,
self.action.FLAVOR: None}
self.action.input_parameters = parameters
exc = self.assertRaises(
voluptuous.MultipleInvalid, self.action.validate_parameters)
self.assertEqual([(['flavor'], voluptuous.TypeInvalid)],
[(e.path, type(e)) for e in exc.errors])
self.assertRaises(jsonschema.ValidationError,
self.action.validate_parameters)
def test_parameters_exception_flavor(self):
parameters = {baction.BaseAction.RESOURCE_ID:
self.INSTANCE_UUID,
self.action.FLAVOR: None}
self.action.input_parameters = parameters
exc = self.assertRaises(
voluptuous.MultipleInvalid, self.action.validate_parameters)
self.assertEqual(
[(['flavor'], voluptuous.TypeInvalid)],
[(e.path, type(e)) for e in exc.errors])
self.assertRaises(jsonschema.ValidationError,
self.action.validate_parameters)
def test_parameters_exception_resource_id(self):
parameters = {baction.BaseAction.RESOURCE_ID: "EFEF",
self.action.FLAVOR: 'x1'}
self.action.input_parameters = parameters
exc = self.assertRaises(
voluptuous.MultipleInvalid, self.action.validate_parameters)
self.assertEqual(
[(['resource_id'], voluptuous.Invalid)],
[(e.path, type(e)) for e in exc.errors])
self.assertRaises(jsonschema.ValidationError,
self.action.validate_parameters)
def test_execute_resize(self):
self.r_helper.find_instance.return_value = self.INSTANCE_UUID