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
2 changed files with 33 additions and 33 deletions

View File

@@ -17,14 +17,11 @@
# limitations under the License. # limitations under the License.
# #
import jsonschema
from oslo_log import log from oslo_log import log
import six
import voluptuous
from watcher._i18n import _
from watcher.applier.actions import base from watcher.applier.actions import base
from watcher.common import nova_helper from watcher.common import nova_helper
from watcher.common import utils
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@@ -49,21 +46,33 @@ class Resize(base.BaseAction):
# input parameters constants # input parameters constants
FLAVOR = 'flavor' 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 @property
def schema(self): def schema(self):
return voluptuous.Schema({ return {
voluptuous.Required(self.RESOURCE_ID): self.check_resource_id, 'type': 'object',
voluptuous.Required(self.FLAVOR): 'properties': {
voluptuous.All(voluptuous.Any(*six.string_types), 'resource_id': {
voluptuous.Length(min=1)), '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 @property
def instance_uuid(self): def instance_uuid(self):

View File

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