Replace voluptuous with JSONSchema to validate sleep action

Now in watcher,both JSONSchema and voluptuous are used to validate
JSON payloads. We want to remove voluptuous and Use
JSONSchema as our only JSON validation tool to keep consistence
and also to make it easier to expose the validation schema
through our API in future work.

In this patch, we replace voluptuous with JSONSchema to validate
the sleep action in watcher applier.

Partially Implements: blueprint jsonschema-validation

Change-Id: I3032490236536a11e7045a56ad0bd40ef979407e
This commit is contained in:
Yumeng_Bao
2017-06-08 19:41:32 +08:00
parent 32dacdca8b
commit d76b5d2f7e
2 changed files with 27 additions and 10 deletions

View File

@@ -17,11 +17,10 @@
# limitations under the License.
#
import jsonschema
import time
from oslo_log import log
import voluptuous
from watcher.applier.actions import base
LOG = log.getLogger(__name__)
@@ -43,10 +42,24 @@ class Sleep(base.BaseAction):
@property
def schema(self):
return voluptuous.Schema({
voluptuous.Required(self.DURATION, default=1):
voluptuous.All(float, voluptuous.Range(min=0))
})
return {
'type': 'object',
'properties': {
'duration': {
'type': 'number',
'minimum': 0
},
},
'required': ['duration'],
'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 duration(self):

View File

@@ -13,8 +13,9 @@
# limitations under the License.
#
import jsonschema
import mock
import voluptuous
from watcher.applier.actions import sleep
from watcher.tests import base
@@ -31,12 +32,15 @@ class TestSleep(base.TestCase):
def test_parameters_duration_empty(self):
self.s.input_parameters = {self.s.DURATION: None}
self.assertRaises(voluptuous.Invalid, self.s.validate_parameters)
self.assertRaises(jsonschema.ValidationError,
self.s.validate_parameters)
def test_parameters_wrong_parameter(self):
self.s.input_parameters = {self.s.DURATION: "ef"}
self.assertRaises(voluptuous.Invalid, self.s.validate_parameters)
self.assertRaises(jsonschema.ValidationError,
self.s.validate_parameters)
def test_parameters_add_field(self):
self.s.input_parameters = {self.s.DURATION: 1.0, "not_required": "nop"}
self.assertRaises(voluptuous.Invalid, self.s.validate_parameters)
self.assertRaises(jsonschema.ValidationError,
self.s.validate_parameters)