test: fix check_required_by (#84153)

* Update the documentation for check_required_by
* Fix return value for check_required_by (now returns empty list on success)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/84237/head^2
Abhijeet Kasurde 3 weeks ago committed by GitHub
parent c49e94017a
commit f92e99fd8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -185,7 +185,7 @@ def check_required_by(requirements, parameters, options_context=None):
:kwarg options_context: List of strings of parent key names if ``requirements`` are :kwarg options_context: List of strings of parent key names if ``requirements`` are
in a sub spec. in a sub spec.
:returns: Empty dictionary or raises :class:`TypeError` if the :returns: Empty dictionary or raises :class:`TypeError` if the check fails.
""" """
result = {} result = {}
@ -195,22 +195,15 @@ def check_required_by(requirements, parameters, options_context=None):
for (key, value) in requirements.items(): for (key, value) in requirements.items():
if key not in parameters or parameters[key] is None: if key not in parameters or parameters[key] is None:
continue continue
result[key] = []
# Support strings (single-item lists) # Support strings (single-item lists)
if isinstance(value, string_types): if isinstance(value, string_types):
value = [value] value = [value]
for required in value:
if required not in parameters or parameters[required] is None:
result[key].append(required)
if result:
for key, missing in result.items():
if len(missing) > 0:
msg = "missing parameter(s) required by '%s': %s" % (key, ', '.join(missing))
if options_context:
msg = "{0} found in {1}".format(msg, " -> ".join(options_context))
raise TypeError(to_native(msg))
if missing := [required for required in value if required not in parameters or parameters[required] is None]:
msg = f"missing parameter(s) required by '{key}': {', '.join(missing)}"
if options_context:
msg = f"{msg} found in {' -> '.join(options_context)}"
raise TypeError(to_native(msg))
return result return result

@ -4,10 +4,10 @@
from __future__ import annotations from __future__ import annotations
import re
import pytest import pytest
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.common.validation import check_required_by from ansible.module_utils.common.validation import check_required_by
@ -19,9 +19,7 @@ def path_arguments_terms():
def test_check_required_by(): def test_check_required_by():
arguments_terms = {} assert check_required_by({}, {}) == {}
params = {}
assert check_required_by(arguments_terms, params) == {}
def test_check_required_by_missing(): def test_check_required_by_missing():
@ -30,12 +28,9 @@ def test_check_required_by_missing():
} }
params = {"force": True} params = {"force": True}
expected = "missing parameter(s) required by 'force': force_reason" expected = "missing parameter(s) required by 'force': force_reason"
with pytest.raises(TypeError, match=re.escape(expected)):
with pytest.raises(TypeError) as e:
check_required_by(arguments_terms, params) check_required_by(arguments_terms, params)
assert to_native(e.value) == expected
def test_check_required_by_multiple(path_arguments_terms): def test_check_required_by_multiple(path_arguments_terms):
params = { params = {
@ -43,21 +38,17 @@ def test_check_required_by_multiple(path_arguments_terms):
} }
expected = "missing parameter(s) required by 'path': mode, owner" expected = "missing parameter(s) required by 'path': mode, owner"
with pytest.raises(TypeError) as e: with pytest.raises(TypeError, match=re.escape(expected)):
check_required_by(path_arguments_terms, params) check_required_by(path_arguments_terms, params)
assert to_native(e.value) == expected
def test_check_required_by_single(path_arguments_terms): def test_check_required_by_single(path_arguments_terms):
params = {"path": "/foo/bar", "mode": "0700"} params = {"path": "/foo/bar", "mode": "0700"}
expected = "missing parameter(s) required by 'path': owner" expected = "missing parameter(s) required by 'path': owner"
with pytest.raises(TypeError) as e: with pytest.raises(TypeError, match=re.escape(expected)):
check_required_by(path_arguments_terms, params) check_required_by(path_arguments_terms, params)
assert to_native(e.value) == expected
def test_check_required_by_missing_none(path_arguments_terms): def test_check_required_by_missing_none(path_arguments_terms):
params = { params = {
@ -65,7 +56,7 @@ def test_check_required_by_missing_none(path_arguments_terms):
"mode": "0700", "mode": "0700",
"owner": "root", "owner": "root",
} }
assert check_required_by(path_arguments_terms, params) assert check_required_by(path_arguments_terms, params) == {}
def test_check_required_by_options_context(path_arguments_terms): def test_check_required_by_options_context(path_arguments_terms):
@ -75,11 +66,9 @@ def test_check_required_by_options_context(path_arguments_terms):
expected = "missing parameter(s) required by 'path': owner found in foo_context" expected = "missing parameter(s) required by 'path': owner found in foo_context"
with pytest.raises(TypeError) as e: with pytest.raises(TypeError, match=re.escape(expected)):
check_required_by(path_arguments_terms, params, options_context) check_required_by(path_arguments_terms, params, options_context)
assert to_native(e.value) == expected
def test_check_required_by_missing_multiple_options_context(path_arguments_terms): def test_check_required_by_missing_multiple_options_context(path_arguments_terms):
params = { params = {
@ -91,7 +80,5 @@ def test_check_required_by_missing_multiple_options_context(path_arguments_terms
"missing parameter(s) required by 'path': mode, owner found in foo_context" "missing parameter(s) required by 'path': mode, owner found in foo_context"
) )
with pytest.raises(TypeError) as e: with pytest.raises(TypeError, match=re.escape(expected)):
check_required_by(path_arguments_terms, params, options_context) check_required_by(path_arguments_terms, params, options_context)
assert to_native(e.value) == expected

Loading…
Cancel
Save