From d76b5d2f7e61e08241aa991246ac55c0f0dbf12e Mon Sep 17 00:00:00 2001 From: Yumeng_Bao Date: Thu, 8 Jun 2017 19:41:32 +0800 Subject: [PATCH] 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 --- watcher/applier/actions/sleep.py | 25 ++++++++++++++++----- watcher/tests/applier/actions/test_sleep.py | 12 ++++++---- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/watcher/applier/actions/sleep.py b/watcher/applier/actions/sleep.py index dc2ed3d49..bf1f32464 100644 --- a/watcher/applier/actions/sleep.py +++ b/watcher/applier/actions/sleep.py @@ -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): diff --git a/watcher/tests/applier/actions/test_sleep.py b/watcher/tests/applier/actions/test_sleep.py index d5b75b9e0..0b83c8f39 100644 --- a/watcher/tests/applier/actions/test_sleep.py +++ b/watcher/tests/applier/actions/test_sleep.py @@ -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)