1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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.
This commit is contained in:
Christoph 2016-09-22 05:36:14 +02:00 committed by Toshio Kuratomi
parent 8aa8e07d13
commit f9e49de2ef

View file

@ -343,6 +343,51 @@ class TestModuleUtilsBasic(ModuleTestCase):
supports_check_mode=True, 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): def test_module_utils_basic_ansible_module_load_file_common_arguments(self):
from ansible.module_utils import basic from ansible.module_utils import basic
basic._ANSIBLE_ARGS = None basic._ANSIBLE_ARGS = None
@ -582,6 +627,7 @@ class TestModuleUtilsBasic(ModuleTestCase):
def test_module_utils_basic_ansible_module_user_and_group(self): def test_module_utils_basic_ansible_module_user_and_group(self):
from ansible.module_utils import basic from ansible.module_utils import basic
basic._ANSIBLE_ARGS = None
am = basic.AnsibleModule( am = basic.AnsibleModule(
argument_spec = dict(), argument_spec = dict(),