summaryrefslogtreecommitdiff
path: root/test/units
diff options
context:
space:
mode:
authorChristoph <github@christoph-d.de>2016-09-22 05:36:14 +0200
committerToshio Kuratomi <a.badger@gmail.com>2016-09-21 20:36:14 -0700
commitf9e49de2ef9f2b0da2246d9ca080cd19598749b8 (patch)
treec31f7964b83ad16558809ec28157bc7b4de4731a /test/units
parent8aa8e07d133f07818c02d26835a380ecfa0f0222 (diff)
downloadansible-f9e49de2ef9f2b0da2246d9ca080cd19598749b8.tar.gz
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.
Diffstat (limited to 'test/units')
-rw-r--r--test/units/module_utils/test_basic.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/units/module_utils/test_basic.py b/test/units/module_utils/test_basic.py
index 25400ed1e1..c4a2261512 100644
--- a/test/units/module_utils/test_basic.py
+++ b/test/units/module_utils/test_basic.py
@@ -343,6 +343,51 @@ class TestModuleUtilsBasic(ModuleTestCase):
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
@@ -582,6 +627,7 @@ class TestModuleUtilsBasic(ModuleTestCase):
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(),