Skip to content

Commit

Permalink
Fix args validation with setup module (#2813)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Dec 13, 2022
1 parent 712a23a commit 6fd2268
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions examples/playbooks/rule-args-module-fail-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@
# module should produce: 'Unsupported parameters for ansible.builtin.systemd module: foo. Supported parameters include: no_block, state, daemon_reload (daemon-reload), name (service, unit), force, masked, daemon_reexec (daemon-reexec), scope, enabled.'
ansible.builtin.systemd:
foo: true

- name: An invalid call of setup module
# setup module in ansible is the only module that has a .. relative
# import that used to cause problems
ansible.builtin.setup:
foo: # this is a nested object which will have the __ injections
# that we later need to clean
bar: true
10 changes: 7 additions & 3 deletions src/ansiblelint/rules/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ansiblelint.file_utils import Lintable
from ansiblelint.rules import AnsibleLintRule, RulesCollection
from ansiblelint.text import has_jinja
from ansiblelint.yaml_utils import clean_json

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -80,7 +81,8 @@ def matchtask(
mock_ansible_module, "AnsibleModule", CustomAnsibleModule
):
spec = importlib.util.spec_from_file_location(
"plugin", loaded_module.plugin_resolved_path
name=loaded_module.resolved_fqcn,
location=loaded_module.plugin_resolved_path,
)
if spec:
assert spec.loader is not None
Expand All @@ -106,7 +108,7 @@ def matchtask(
with patch.object(
sys,
"argv",
["", json.dumps({"ANSIBLE_MODULE_ARGS": module_args})],
["", json.dumps({"ANSIBLE_MODULE_ARGS": clean_json(module_args)})],
):
fio = io.StringIO()
failed_msg = ""
Expand Down Expand Up @@ -234,13 +236,15 @@ def test_args_module_fail() -> None:
collection.register(ArgsRule())
success = "examples/playbooks/rule-args-module-fail-1.yml"
results = Runner(success, rules=collection).run()
assert len(results) == 3
assert len(results) == 4
assert results[0].tag == "args[module]"
assert "missing required arguments" in results[0].message
assert results[1].tag == "args[module]"
assert "missing parameter(s) required by " in results[1].message
assert results[2].tag == "args[module]"
assert "Unsupported parameters for" in results[2].message
assert results[3].tag == "args[module]"
assert "Unsupported parameters for" in results[2].message

def test_args_module_pass() -> None:
"""Test rule valid module options."""
Expand Down

0 comments on commit 6fd2268

Please sign in to comment.