mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-30 21:09:09 -07:00
Fix fallback and boolean check in argspec (#27994)
Fixes #27978 * Add missing assignment for param key in fallback handling * Add check for frozenset type * Unit testcase
This commit is contained in:
parent
fee42a01d9
commit
3b0e9ded91
2 changed files with 70 additions and 6 deletions
|
@ -115,9 +115,9 @@ NoneType = type(None)
|
||||||
# this matters, make sure to check for strings before checking for sequencetype
|
# this matters, make sure to check for strings before checking for sequencetype
|
||||||
try:
|
try:
|
||||||
from collections.abc import KeysView
|
from collections.abc import KeysView
|
||||||
SEQUENCETYPE = (Sequence, KeysView)
|
SEQUENCETYPE = (Sequence, frozenset, KeysView)
|
||||||
except:
|
except:
|
||||||
SEQUENCETYPE = Sequence
|
SEQUENCETYPE = (Sequence, frozenset)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
|
@ -2044,7 +2044,7 @@ class AnsibleModule(object):
|
||||||
else:
|
else:
|
||||||
fallback_args = item
|
fallback_args = item
|
||||||
try:
|
try:
|
||||||
param = fallback_strategy(*fallback_args, **fallback_kwargs)
|
param[k] = fallback_strategy(*fallback_args, **fallback_kwargs)
|
||||||
except AnsibleFallbackNotFound:
|
except AnsibleFallbackNotFound:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
|
@ -260,6 +260,7 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_creation(self):
|
def test_module_utils_basic_ansible_module_creation(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
from ansible.module_utils.parsing.convert_bool import BOOLEANS
|
||||||
|
|
||||||
am = basic.AnsibleModule(
|
am = basic.AnsibleModule(
|
||||||
argument_spec=dict(),
|
argument_spec=dict(),
|
||||||
|
@ -269,7 +270,8 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
||||||
foo=dict(required=True),
|
foo=dict(required=True),
|
||||||
bar=dict(),
|
bar=dict(),
|
||||||
bam=dict(),
|
bam=dict(),
|
||||||
baz=dict(),
|
baz=dict(fallback=(basic.env_fallback, ['BAZ'])),
|
||||||
|
bar1=dict(type='bool', choices=BOOLEANS)
|
||||||
)
|
)
|
||||||
mut_ex = (('bar', 'bam'),)
|
mut_ex = (('bar', 'bam'),)
|
||||||
req_to = (('bam', 'baz'),)
|
req_to = (('bam', 'baz'),)
|
||||||
|
@ -342,16 +344,50 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# test fallback, should test ok as value of param required due to another param
|
||||||
|
# is set by environment variable
|
||||||
|
os.environ['BAZ'] = 'bad'
|
||||||
|
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={"foo": "hello", "bam": "bad"}))
|
||||||
|
|
||||||
|
with swap_stdin_and_argv(stdin_data=args):
|
||||||
|
basic._ANSIBLE_ARGS = None
|
||||||
|
am = basic.AnsibleModule(
|
||||||
|
argument_spec=arg_spec,
|
||||||
|
mutually_exclusive=mut_ex,
|
||||||
|
required_together=req_to,
|
||||||
|
no_log=True,
|
||||||
|
check_invalid_arguments=False,
|
||||||
|
add_file_common_args=True,
|
||||||
|
supports_check_mode=True,
|
||||||
|
)
|
||||||
|
os.environ.pop('BAZ', None)
|
||||||
|
|
||||||
|
# should test ok, check for boolean values
|
||||||
|
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={"foo": "hello", "bar1": "yes"}))
|
||||||
|
with swap_stdin_and_argv(stdin_data=args):
|
||||||
|
basic._ANSIBLE_ARGS = None
|
||||||
|
am = basic.AnsibleModule(
|
||||||
|
argument_spec=arg_spec,
|
||||||
|
mutually_exclusive=mut_ex,
|
||||||
|
required_together=req_to,
|
||||||
|
no_log=True,
|
||||||
|
check_invalid_arguments=False,
|
||||||
|
add_file_common_args=True,
|
||||||
|
supports_check_mode=True,
|
||||||
|
)
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_with_options_creation(self):
|
def test_module_utils_basic_ansible_module_with_options_creation(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
from ansible.module_utils.parsing.convert_bool import BOOLEANS
|
||||||
|
|
||||||
options_spec = dict(
|
options_spec = dict(
|
||||||
foo=dict(required=True, aliases=['dup']),
|
foo=dict(required=True, aliases=['dup']),
|
||||||
bar=dict(),
|
bar=dict(),
|
||||||
bam=dict(),
|
bam=dict(),
|
||||||
baz=dict(),
|
baz=dict(fallback=(basic.env_fallback, ['BAZ'])),
|
||||||
bam1=dict(),
|
bam1=dict(),
|
||||||
bam2=dict(default='test')
|
bam2=dict(default='test'),
|
||||||
|
bam3=dict(type='bool', choices=BOOLEANS)
|
||||||
)
|
)
|
||||||
arg_spec = dict(
|
arg_spec = dict(
|
||||||
foobar=dict(
|
foobar=dict(
|
||||||
|
@ -509,6 +545,34 @@ class TestModuleUtilsBasic(ModuleTestCase):
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# test fallback, should test ok as value of param required due to another param
|
||||||
|
# is set by environment variable
|
||||||
|
os.environ['BAZ'] = 'bad'
|
||||||
|
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={"foo": "hello", "bam": "bad"}))
|
||||||
|
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
os.environ.pop('BAZ', None)
|
||||||
|
|
||||||
|
# should test ok, check for boolean values
|
||||||
|
args = json.dumps(dict(ANSIBLE_MODULE_ARGS={"foo": "hello", "bam3": "yes"}))
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
def test_module_utils_basic_ansible_module_type_check(self):
|
def test_module_utils_basic_ansible_module_type_check(self):
|
||||||
from ansible.module_utils import basic
|
from ansible.module_utils import basic
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue