def _validate(self): """Validate the data gainst the schema convert doc string in argspec if necessary :return valid: if the data passed :rtype valid: bool :return errors: errors reported during validation :rtype errors: str :return params: The original data updated with defaults :rtype params: dict """ if self._schema_format == "doc": self._convert_doc_to_schema() if self._schema_conditionals is not None: self._schema = dict_merge(self._schema, self._schema_conditionals) if self._other_args is not None: self._schema = dict_merge(self._schema, self._other_args) invalid_keys = [ k for k in self._schema.keys() if k not in VALID_ANSIBLEMODULE_ARGS ] if invalid_keys: valid = False errors = "Invalid schema. Invalid keys found: {ikeys}".format( ikeys=",".join(invalid_keys) ) updated_data = {} else: mm = MonkeyModule( data=self._data, schema=self._schema, name=self._name ) valid, errors, updated_data = mm.validate() return valid, errors, updated_data
def test_not_dict_other(self): base = {"a": "b"} other = [0] expected = "must be of type <dict>" with self.assertRaises(Exception) as exc: dict_merge(base, other) self.assertIn(expected, str(exc.exception))
def validate(self): """The public validate method check for future argspec validation that is coming in 2.11, change the check according above """ if HAS_ANSIBLE_ARG_SPEC_VALIDATOR: if self._schema_format == "doc": self._convert_doc_to_schema() if self._schema_conditionals is not None: self._schema = dict_merge(self._schema, self._schema_conditionals) invalid_keys = [ k for k in self._schema.keys() if k not in VALID_ANSIBLEMODULE_ARGS ] if invalid_keys: valid = False errors = [ "Invalid schema. Invalid keys found: {ikeys}".format( ikeys=",".join(invalid_keys)) ] updated_data = {} return valid, errors, updated_data else: validator = ArgumentSpecValidator(**self._schema) result = validator.validate(self._data) valid = not bool(result.error_messages) return ( valid, result.error_messages, result.validated_parameters, ) else: return self._validate()
def test_dict_of_dict_merged(self): base = {"a": {"b": 0}} other = {"a": {"c": 1}} expected = {"a": {"b": 0, "c": 1}} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_list_other_string(self): base = {"a": [2, 1, 0]} other = {"a": "xyz"} expected = {"a": "xyz"} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_list_other_dict(self): base = {"a": [2, 1, 0]} other = {"a": {"b": "c"}} expected = {"a": {"b": "c"}} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_list_other_none(self): base = {"a": [2, 1, 0]} other = {"a": None} expected = {"a": None} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_list_missing_from_other(self): base = {"a": [2, 1, 0]} other = {"b": [2, 1, 0]} expected = {"a": [2, 1, 0], "b": [2, 1, 0]} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_list_value_combine(self): base = {"a": [2, 1, 0]} other = {"a": [0, 3]} expected = {"a": [2, 1, 0, 3]} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_simple_other_is_none(self): base = {"a": "b"} other = {"a": None} expected = {"a": None} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_simple_other_is_string(self): base = {"a": "b"} other = {"a": "c"} expected = {"a": "c"} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_simple(self): base = {"a": "b"} other = {"c": "d"} expected = {"a": "b", "c": "d"} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_not_list_or_dict_same(self): base = {"a": 0} other = {"a": 0} expected = {"a": 0} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_dict_of_dict_replaced_other_missing(self): base = {"a": {"b": 0}} other = {"c": 1} expected = {"a": {"b": 0}, "c": 1} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_dict_of_dict_replaced_other_string(self): base = {"a": {"b": 0}} other = {"a": "xyz"} expected = {"a": "xyz"} result = dict_merge(base, other) self.assertEqual(result, expected)
def test_dict_of_dict_replaced_other_none(self): base = {"a": {"b": 0}} other = {"a": None} expected = {"a": None} result = dict_merge(base, other) self.assertEqual(result, expected)