Merge "Replace voluptuous with JSONSchema to validate sleep action"

This commit is contained in:
Jenkins 2017-06-21 14:39:14 +00:00 committed by Gerrit Code Review
commit 5b6768140f
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)