Merge "Replace voluptuous with JSONSchema to validate change_nova_service_state"

This commit is contained in:
Jenkins
2017-06-14 13:12:34 +00:00
committed by Gerrit Code Review
2 changed files with 33 additions and 31 deletions

View File

@@ -17,8 +17,7 @@
# limitations under the License. # limitations under the License.
# #
import six import jsonschema
import voluptuous
from watcher._i18n import _ from watcher._i18n import _
from watcher.applier.actions import base from watcher.applier.actions import base
@@ -51,15 +50,31 @@ class ChangeNovaServiceState(base.BaseAction):
@property @property
def schema(self): def schema(self):
return voluptuous.Schema({ return {
voluptuous.Required(self.RESOURCE_ID): 'type': 'object',
voluptuous.All( 'properties': {
voluptuous.Any(*six.string_types), 'resource_id': {
voluptuous.Length(min=1)), 'type': 'string',
voluptuous.Required(self.STATE): "minlength": 1
voluptuous.Any(*[state.value },
for state in list(element.ServiceState)]), 'state': {
}) 'type': 'string',
'enum': [element.ServiceState.ONLINE.value,
element.ServiceState.OFFLINE.value,
element.ServiceState.ENABLED.value,
element.ServiceState.DISABLED.value]
}
},
'required': ['resource_id', 'state'],
'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 host(self): def host(self):

View File

@@ -15,8 +15,8 @@
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 change_nova_service_state from watcher.applier.actions import change_nova_service_state
@@ -73,33 +73,20 @@ class TestChangeNovaServiceState(base.TestCase):
self.action.input_parameters = { self.action.input_parameters = {
baction.BaseAction.RESOURCE_ID: "compute-1", baction.BaseAction.RESOURCE_ID: "compute-1",
self.action.STATE: 'error'} self.action.STATE: 'error'}
exc = self.assertRaises( self.assertRaises(jsonschema.ValidationError,
voluptuous.Invalid, self.action.validate_parameters) self.action.validate_parameters)
self.assertEqual(
[(['state'], voluptuous.ScalarInvalid)],
[([str(p) for p in e.path], type(e)) for e in exc.errors])
def test_parameters_resource_id_empty(self): def test_parameters_resource_id_empty(self):
self.action.input_parameters = { self.action.input_parameters = {
self.action.STATE: element.ServiceState.ENABLED.value, self.action.STATE: element.ServiceState.ENABLED.value,
} }
exc = self.assertRaises( self.assertRaises(jsonschema.ValidationError,
voluptuous.Invalid, self.action.validate_parameters) self.action.validate_parameters)
self.assertEqual(
[(['resource_id'], voluptuous.RequiredFieldInvalid)],
[([str(p) for p in e.path], type(e)) for e in exc.errors])
def test_parameters_applies_add_extra(self): def test_parameters_applies_add_extra(self):
self.action.input_parameters = {"extra": "failed"} self.action.input_parameters = {"extra": "failed"}
exc = self.assertRaises( self.assertRaises(jsonschema.ValidationError,
voluptuous.Invalid, self.action.validate_parameters) self.action.validate_parameters)
self.assertEqual(
sorted([(['resource_id'], voluptuous.RequiredFieldInvalid),
(['state'], voluptuous.RequiredFieldInvalid),
(['extra'], voluptuous.Invalid)],
key=lambda x: str(x[0])),
sorted([([str(p) for p in e.path], type(e)) for e in exc.errors],
key=lambda x: str(x[0])))
def test_change_service_state_pre_condition(self): def test_change_service_state_pre_condition(self):
try: try: