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):
Default value: none.
Example:
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:
true
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
shared
"""
def _schema(self, options):
path = options['jsonschema']
......@@ -440,16 +439,22 @@ class JsonSchema(Recipe):
def _expandParameterDict(self, options, parameter_dict):
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.SharedValidator = DefaultValidator if set_shared else BasicValidator
software_schema = self._schema(options)
serialisation = software_schema.getSerialisation(strict=True)
if serialisation == SoftwareReleaseSerialisation.JsonInXml:
parameter_dict = unwrap(parameter_dict)
self._parseSharedParameterDict(software_schema, options)
parameter_dict = self._parseParameterDict(software_schema, parameter_dict)
if validate_shared:
self._parseSharedParameterDict(software_schema, options)
if validate_main:
parameter_dict = self._parseParameterDict(software_schema, parameter_dict)
options['configuration'] = parameter_dict
return parameter_dict
if validate_main or isinstance(parameter_dict, dict):
return parameter_dict
return {}
class JsonDump(Recipe):
......
......@@ -268,6 +268,23 @@ class SlapConfigurationTest(unittest.TestCase):
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):
self.writeJsonSchema()
parameters = {}
......@@ -401,3 +418,28 @@ class SlapConfigurationTest(unittest.TestCase):
valid, invalid = self.receiveSharedParameters()
self.assertEqual(list(valid.values()), [{"kind": 1, "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