Commit 578b3e34 authored by Xavier Thompson's avatar Xavier Thompson

slapconfiguration:jsonchema: Allow skipping validation

Add `validate-parameters` options to slapconfiguration:jsonschema.
Accepted values are `all|main|shared|none`.
Default value is `all`.

When shared parameters validation is skipped, shared parameters are
left in `slave-instance-list`.
parent d288911a
...@@ -368,13 +368,12 @@ class JsonSchema(Recipe): ...@@ -368,13 +368,12 @@ class JsonSchema(Recipe):
Default value: none. Default value: none.
Example: Example:
shared shared
validate-parameters
Enum to control validating instance parameters
for both/neither/either-of main and shared instances.
Accepted values: all|main|shared|none.
Example: Example:
true shared
set-shared-default
Flag to add defaults specified by the JSON schema for shared instances.
False by default; any value makes this flag behave as true.
Example:
true
""" """
def _schema(self, options): def _schema(self, options):
path = options['jsonschema'] path = options['jsonschema']
...@@ -440,16 +439,22 @@ class JsonSchema(Recipe): ...@@ -440,16 +439,22 @@ class JsonSchema(Recipe):
def _expandParameterDict(self, options, parameter_dict): def _expandParameterDict(self, options, parameter_dict):
set_main, set_shared = self._parseOption(options, 'set-default', 'none') set_main, set_shared = self._parseOption(options, 'set-default', 'none')
validate_tuple = self._parseOption(options, 'validate-parameters', 'all')
validate_main, validate_shared = validate_tuple
self.Validator = DefaultValidator if set_main else BasicValidator self.Validator = DefaultValidator if set_main else BasicValidator
self.SharedValidator = DefaultValidator if set_shared else BasicValidator self.SharedValidator = DefaultValidator if set_shared else BasicValidator
software_schema = self._schema(options) software_schema = self._schema(options)
serialisation = software_schema.getSerialisation(strict=True) serialisation = software_schema.getSerialisation(strict=True)
if serialisation == SoftwareReleaseSerialisation.JsonInXml: if serialisation == SoftwareReleaseSerialisation.JsonInXml:
parameter_dict = unwrap(parameter_dict) parameter_dict = unwrap(parameter_dict)
if validate_shared:
self._parseSharedParameterDict(software_schema, options) self._parseSharedParameterDict(software_schema, options)
if validate_main:
parameter_dict = self._parseParameterDict(software_schema, parameter_dict) parameter_dict = self._parseParameterDict(software_schema, parameter_dict)
options['configuration'] = parameter_dict options['configuration'] = parameter_dict
if validate_main or isinstance(parameter_dict, dict):
return parameter_dict return parameter_dict
return {}
class JsonDump(Recipe): class JsonDump(Recipe):
......
...@@ -268,6 +268,23 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -268,6 +268,23 @@ class SlapConfigurationTest(unittest.TestCase):
self.receiveParameters, self.receiveParameters,
) )
def test_jsonschema_json_in_xml_wrong_type_json_input_skip_main(self):
self.writeJsonSchema()
parameters = {"number": "1"}
with self.patchSlap(parameters, True):
received = self.receiveParameters({'validate-parameters': 'shared'})
self.assertEqual(received, parameters)
def test_jsonschema_json_in_xml_wrong_type_json_input_skip_shared(self):
self.writeJsonSchema()
parameters = {"number": "1"}
with self.patchSlap(parameters, True):
self.assertRaises(
slapconfiguration.UserError,
self.receiveParameters,
{'validate-parameters': 'main'},
)
def test_jsonschema_json_in_xml_incomplete_xml_input(self): def test_jsonschema_json_in_xml_incomplete_xml_input(self):
self.writeJsonSchema() self.writeJsonSchema()
parameters = {} parameters = {}
...@@ -401,3 +418,28 @@ class SlapConfigurationTest(unittest.TestCase): ...@@ -401,3 +418,28 @@ class SlapConfigurationTest(unittest.TestCase):
valid, invalid = self.receiveSharedParameters() valid, invalid = self.receiveSharedParameters()
self.assertEqual(list(valid.values()), [{"kind": 1, "thing": "hello"}]) self.assertEqual(list(valid.values()), [{"kind": 1, "thing": "hello"}])
self.assertEqual(list(invalid.values()), [{"kind": 2, "thing": "hello"}]) self.assertEqual(list(invalid.values()), [{"kind": 2, "thing": "hello"}])
def test_jsonschema_shared_valid_and_invalid_skip_shared(self):
self.writeJsonSchema()
parameters = {"number": 1}
shared = [{"kind": 1}, {"kind": 2, "thing": "hello"}]
with self.patchSlap(parameters, True, shared):
options = self.runJsonSchemaRecipe({'validate-parameters': 'main'})
shared = options['slave-instance-list']
self.assertEqual(
shared,
[
{"slave_reference": "SHARED0", "kind": 1},
{"slave_reference": "SHARED1", "kind": 2, "thing": "hello"},
],
)
def test_jsonschema_shared_valid_and_invalid_skip_main(self):
self.writeJsonSchema()
parameters = {"number": 1}
shared = [{"kind": 1}, {"kind": 2, "thing": "hello"}]
with self.patchSlap(parameters, True, shared):
options = {'validate-parameters': 'shared', 'set-default': 'all'}
valid, invalid = self.receiveSharedParameters(options)
self.assertEqual(list(valid.values()), [{"kind": 1, "thing": "hello"}])
self.assertEqual(list(invalid.values()), [{"kind": 2, "thing": "hello"}])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment