| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# Copyright 2017, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from ansible.errors import AnsibleParserError |
| 10 |
from ansible.parsing.mod_args import ModuleArgsParser |
| 11 |
from ansible.utils.sentinel import Sentinel |
| 12 |
|
| 13 |
|
| 14 |
class TestModArgsDwim: |
| 15 |
|
| 16 |
# TODO: add tests that construct ModuleArgsParser with a task reference |
| 17 |
# TODO: verify the AnsibleError raised on failure knows the task |
| 18 |
# and the task knows the line numbers |
| 19 |
|
| 20 |
INVALID_MULTIPLE_ACTIONS = ( |
| 21 |
({'action': 'shell echo hi', 'local_action': 'shell echo hi'}, "action and local_action are mutually exclusive"), |
| 22 |
({'action': 'shell echo hi', 'shell': 'echo hi'}, "conflicting action statements: shell, shell"), |
| 23 |
({'local_action': 'shell echo hi', 'shell': 'echo hi'}, "conflicting action statements: shell, shell"), |
| 24 |
) |
| 25 |
|
| 26 |
def _debug(self, mod, args, to): |
| 27 |
print("RETURNED module = {0}".format(mod)) |
| 28 |
print(" args = {0}".format(args)) |
| 29 |
print(" to = {0}".format(to)) |
| 30 |
|
| 31 |
def test_basic_shell(self): |
| 32 |
m = ModuleArgsParser(dict(shell='echo hi')) |
| 33 |
mod, args, to = m.parse() |
| 34 |
self._debug(mod, args, to) |
| 35 |
|
| 36 |
assert mod == 'shell' |
| 37 |
assert args == dict( |
| 38 |
_raw_params='echo hi', |
| 39 |
) |
| 40 |
assert to is Sentinel |
| 41 |
|
| 42 |
def test_basic_command(self): |
| 43 |
m = ModuleArgsParser(dict(command='echo hi')) |
| 44 |
mod, args, to = m.parse() |
| 45 |
self._debug(mod, args, to) |
| 46 |
|
| 47 |
assert mod == 'command' |
| 48 |
assert args == dict( |
| 49 |
_raw_params='echo hi', |
| 50 |
) |
| 51 |
assert to is Sentinel |
| 52 |
|
| 53 |
def test_shell_with_modifiers(self): |
| 54 |
m = ModuleArgsParser(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep')) |
| 55 |
mod, args, to = m.parse() |
| 56 |
self._debug(mod, args, to) |
| 57 |
|
| 58 |
assert mod == 'shell' |
| 59 |
assert args == dict( |
| 60 |
creates='/tmp/baz', |
| 61 |
removes='/tmp/bleep', |
| 62 |
_raw_params='/bin/foo', |
| 63 |
) |
| 64 |
assert to is Sentinel |
| 65 |
|
| 66 |
def test_normal_usage(self): |
| 67 |
m = ModuleArgsParser(dict(copy='src=a dest=b')) |
| 68 |
mod, args, to = m.parse() |
| 69 |
self._debug(mod, args, to) |
| 70 |
|
| 71 |
assert mod, 'copy' |
| 72 |
assert args, dict(src='a', dest='b') |
| 73 |
assert to is Sentinel |
| 74 |
|
| 75 |
def test_complex_args(self): |
| 76 |
m = ModuleArgsParser(dict(copy=dict(src='a', dest='b'))) |
| 77 |
mod, args, to = m.parse() |
| 78 |
self._debug(mod, args, to) |
| 79 |
|
| 80 |
assert mod, 'copy' |
| 81 |
assert args, dict(src='a', dest='b') |
| 82 |
assert to is Sentinel |
| 83 |
|
| 84 |
def test_action_with_complex(self): |
| 85 |
m = ModuleArgsParser(dict(action=dict(module='copy', src='a', dest='b'))) |
| 86 |
mod, args, to = m.parse() |
| 87 |
self._debug(mod, args, to) |
| 88 |
|
| 89 |
assert mod == 'copy' |
| 90 |
assert args == dict(src='a', dest='b') |
| 91 |
assert to is Sentinel |
| 92 |
|
| 93 |
def test_action_with_complex_and_complex_args(self): |
| 94 |
m = ModuleArgsParser(dict(action=dict(module='copy', args=dict(src='a', dest='b')))) |
| 95 |
mod, args, to = m.parse() |
| 96 |
self._debug(mod, args, to) |
| 97 |
|
| 98 |
assert mod == 'copy' |
| 99 |
assert args == dict(src='a', dest='b') |
| 100 |
assert to is Sentinel |
| 101 |
|
| 102 |
def test_local_action_string(self): |
| 103 |
m = ModuleArgsParser(dict(local_action='copy src=a dest=b')) |
| 104 |
mod, args, delegate_to = m.parse() |
| 105 |
self._debug(mod, args, delegate_to) |
| 106 |
|
| 107 |
assert mod == 'copy' |
| 108 |
assert args == dict(src='a', dest='b') |
| 109 |
assert delegate_to == 'localhost' |
| 110 |
|
| 111 |
@pytest.mark.parametrize("args_dict, msg", INVALID_MULTIPLE_ACTIONS) |
| 112 |
def test_multiple_actions(self, args_dict, msg): |
| 113 |
m = ModuleArgsParser(args_dict) |
| 114 |
with pytest.raises(AnsibleParserError) as err: |
| 115 |
m.parse() |
| 116 |
|
| 117 |
assert err.value.args[0] == msg |
| 118 |
|
| 119 |
@pytest.mark.usefixtures('collection_loader') |
| 120 |
def test_multiple_actions_ping_shell(self): |
| 121 |
args_dict = {'ping': 'data=hi', 'shell': 'echo hi'} |
| 122 |
m = ModuleArgsParser(args_dict) |
| 123 |
with pytest.raises(AnsibleParserError) as err: |
| 124 |
m.parse() |
| 125 |
|
| 126 |
assert err.value.args[0] == f'conflicting action statements: {", ".join(args_dict)}' |
| 127 |
|
| 128 |
@pytest.mark.usefixtures('collection_loader') |
| 129 |
def test_bogus_action(self): |
| 130 |
args_dict = {'bogusaction': {}} |
| 131 |
m = ModuleArgsParser(args_dict) |
| 132 |
with pytest.raises(AnsibleParserError) as err: |
| 133 |
m.parse() |
| 134 |
|
| 135 |
assert err.value.args[0].startswith(f"couldn't resolve module/action '{next(iter(args_dict))}'") |