From 024daa9dfec9a7b88a2a1614b965b195ca8d8b80 Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Tue, 20 Oct 2020 07:50:08 -0700 Subject: [PATCH] Argspec default fix (#12) * Return data updated with default values from aav.validate() * Update aav docs Co-authored-by: cidrblock --- .../module_utils/common/argspec_validate.py | 18 +++++++--- tests/unit/module_utils/fixtures/docstring.py | 3 ++ .../module_utils/test_argspec_validate.py | 36 ++++++++++++++----- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/plugins/module_utils/common/argspec_validate.py b/plugins/module_utils/common/argspec_validate.py index a28c6a2..2f733f9 100644 --- a/plugins/module_utils/common/argspec_validate.py +++ b/plugins/module_utils/common/argspec_validate.py @@ -14,7 +14,7 @@ def _check_argspec(self): other_args={}, name=self._task.action, ) - valid, errors = aav.validate() + valid, errors, updated_data = aav.validate() if not valid: raise AnsibleActionFail(errors) @@ -127,9 +127,11 @@ class MonkeyModule(AnsibleModule): :rtype valid: bool :return errors: errors reported during validation :rtype errors: str + :return params: The original data updated with defaults + :rtype params: dict """ super(MonkeyModule, self).__init__(**self._schema) - return self._valid, self._errors + return self._valid, self._errors, self.params class AnsibleArgSpecValidator: @@ -202,6 +204,13 @@ class AnsibleArgSpecValidator: 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() @@ -217,12 +226,13 @@ class AnsibleArgSpecValidator: 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 = mm.validate() - return valid, errors + valid, errors, updated_data = mm.validate() + return valid, errors, updated_data def validate(self): """The public validate method diff --git a/tests/unit/module_utils/fixtures/docstring.py b/tests/unit/module_utils/fixtures/docstring.py index d94bf3b..1275969 100644 --- a/tests/unit/module_utils/fixtures/docstring.py +++ b/tests/unit/module_utils/fixtures/docstring.py @@ -32,4 +32,7 @@ options: type: dict description: - A dict suboption + param_default: + type: bool + default: True """ diff --git a/tests/unit/module_utils/test_argspec_validate.py b/tests/unit/module_utils/test_argspec_validate.py index 6ed2eb2..99f0d55 100644 --- a/tests/unit/module_utils/test_argspec_validate.py +++ b/tests/unit/module_utils/test_argspec_validate.py @@ -25,10 +25,30 @@ class TestSortList(unittest.TestCase): schema_conditionals={}, name="test_action", ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertTrue(valid) self.assertEqual(errors, None) + def test_simple_defaults(self): + data = {"param_str": "string"} + aav = AnsibleArgSpecValidator( + data=data, + schema=DOCUMENTATION, + schema_format="doc", + schema_conditionals={}, + name="test_action", + ) + expected = { + "param_str": "string", + "param_default": True, + "params_bool": None, + "params_dict": None, + } + valid, errors, updated_data = aav.validate() + self.assertTrue(valid) + self.assertEqual(errors, None) + self.assertEqual(expected, updated_data) + def test_simple_fail(self): data = {} aav = AnsibleArgSpecValidator( @@ -38,7 +58,7 @@ class TestSortList(unittest.TestCase): schema_conditionals={}, name="test_action", ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertFalse(valid) self.assertIn("missing required arguments: param_str", errors) @@ -50,7 +70,7 @@ class TestSortList(unittest.TestCase): schema_format="doc", schema_conditionals={}, ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertFalse(valid) self.assertIn("missing required arguments: param_str", errors) @@ -62,7 +82,7 @@ class TestSortList(unittest.TestCase): schema_format="argspec", name="test_action", ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertTrue(valid) self.assertEqual(errors, None) @@ -77,7 +97,7 @@ class TestSortList(unittest.TestCase): }, name="test_action", ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertFalse(valid) self.assertIn( "parameters are required together: param_str, param_bool", errors @@ -92,7 +112,7 @@ class TestSortList(unittest.TestCase): name="test_action", # other_args={'bypass_checks': True}, ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertFalse(valid) self.assertIn( "Unsupported parameters for 'test_action' module: not_valid", @@ -108,7 +128,7 @@ class TestSortList(unittest.TestCase): name="test_action", other_args={"bypass_checks": True}, ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertTrue(valid) self.assertIsNone(errors) @@ -121,6 +141,6 @@ class TestSortList(unittest.TestCase): name="test_action", other_args={"bypass_checks": True}, ) - valid, errors = aav.validate() + valid, errors, _updated_data = aav.validate() self.assertFalse(valid) self.assertIn("Invalid keys found: not_valid", errors)