From adc158a499fde1ab51732ff211ae90cdfea32198 Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 22 Sep 2016 05:36:14 +0200 Subject: [PATCH] Add a test for int/float parameter type checking (#16741) A parameter of type int should accept int and string, but not float. A parameter of type float should accept float, int, and string. Also reset the arguments in another test so that it runs cleanly. This agrees with what all the other tests are doing. --- test/units/module_utils/test_basic.py | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/test/units/module_utils/test_basic.py b/test/units/module_utils/test_basic.py index d2025235bcc..ba46f71e6fe 100644 --- a/test/units/module_utils/test_basic.py +++ b/test/units/module_utils/test_basic.py @@ -353,6 +353,51 @@ class TestModuleUtilsBasic(unittest.TestCase): supports_check_mode=True, ) + def test_module_utils_basic_ansible_module_type_check(self): + from ansible.module_utils import basic + + arg_spec = dict( + foo = dict(type='float'), + foo2 = dict(type='float'), + foo3 = dict(type='float'), + bar = dict(type='int'), + bar2 = dict(type='int'), + ) + + # should test ok + args = json.dumps(dict(ANSIBLE_MODULE_ARGS={ + "foo": 123.0, # float + "foo2": 123, # int + "foo3": "123", # string + "bar": 123, # int + "bar2": "123", # string + })) + + with swap_stdin_and_argv(stdin_data=args): + basic._ANSIBLE_ARGS = None + am = basic.AnsibleModule( + argument_spec = arg_spec, + no_log=True, + check_invalid_arguments=False, + add_file_common_args=True, + supports_check_mode=True, + ) + + # fail, because bar does not accept floating point numbers + args = json.dumps(dict(ANSIBLE_MODULE_ARGS={"bar": 123.0})) + + with swap_stdin_and_argv(stdin_data=args): + basic._ANSIBLE_ARGS = None + self.assertRaises( + SystemExit, + basic.AnsibleModule, + argument_spec = arg_spec, + no_log=True, + check_invalid_arguments=False, + add_file_common_args=True, + supports_check_mode=True, + ) + def test_module_utils_basic_ansible_module_load_file_common_arguments(self): from ansible.module_utils import basic basic._ANSIBLE_ARGS = None @@ -603,6 +648,7 @@ class TestModuleUtilsBasic(unittest.TestCase): def test_module_utils_basic_ansible_module_user_and_group(self): from ansible.module_utils import basic + basic._ANSIBLE_ARGS = None am = basic.AnsibleModule( argument_spec = dict(),