Merge "Replace voluptuous with JSONSchema to validate sleep action"
This commit is contained in:
commit
5b6768140f
@ -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):
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user