2014-10-16 00:35:16 +02:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
2017-12-21 19:45:33 +01:00
|
|
|
# Copyright 2017, Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
2014-10-16 01:25:48 +02:00
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-12-21 19:45:33 +01:00
|
|
|
import pytest
|
|
|
|
|
2014-10-21 23:04:49 +02:00
|
|
|
from ansible.errors import AnsibleParserError
|
2017-05-30 19:05:19 +02:00
|
|
|
from ansible.parsing.mod_args import ModuleArgsParser
|
2019-01-23 18:40:07 +01:00
|
|
|
from ansible.utils.sentinel import Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
|
2017-12-21 19:45:33 +01:00
|
|
|
class TestModArgsDwim:
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
# TODO: add tests that construct ModuleArgsParser with a task reference
|
|
|
|
# TODO: verify the AnsibleError raised on failure knows the task
|
|
|
|
# and the task knows the line numbers
|
|
|
|
|
2017-12-21 19:45:33 +01:00
|
|
|
INVALID_MULTIPLE_ACTIONS = (
|
|
|
|
({'action': 'shell echo hi', 'local_action': 'shell echo hi'}, "action and local_action are mutually exclusive"),
|
|
|
|
({'action': 'shell echo hi', 'shell': 'echo hi'}, "conflicting action statements: shell, shell"),
|
|
|
|
({'local_action': 'shell echo hi', 'shell': 'echo hi'}, "conflicting action statements: shell, shell"),
|
|
|
|
)
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def _debug(self, mod, args, to):
|
|
|
|
print("RETURNED module = {0}".format(mod))
|
|
|
|
print(" args = {0}".format(args))
|
|
|
|
print(" to = {0}".format(to))
|
|
|
|
|
|
|
|
def test_basic_shell(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(shell='echo hi'))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod == 'shell'
|
|
|
|
assert args == dict(
|
2017-05-30 19:05:19 +02:00
|
|
|
_raw_params='echo hi',
|
2017-12-21 19:45:33 +01:00
|
|
|
)
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_basic_command(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(command='echo hi'))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod == 'command'
|
|
|
|
assert args == dict(
|
2017-05-30 19:05:19 +02:00
|
|
|
_raw_params='echo hi',
|
2017-12-21 19:45:33 +01:00
|
|
|
)
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_shell_with_modifiers(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep'))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod == 'shell'
|
|
|
|
assert args == dict(
|
2017-05-30 19:05:19 +02:00
|
|
|
creates='/tmp/baz',
|
|
|
|
removes='/tmp/bleep',
|
|
|
|
_raw_params='/bin/foo',
|
2017-12-21 19:45:33 +01:00
|
|
|
)
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_normal_usage(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(copy='src=a dest=b'))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod, 'copy'
|
|
|
|
assert args, dict(src='a', dest='b')
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_complex_args(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(copy=dict(src='a', dest='b')))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod, 'copy'
|
|
|
|
assert args, dict(src='a', dest='b')
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_action_with_complex(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(action=dict(module='copy', src='a', dest='b')))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod == 'copy'
|
|
|
|
assert args == dict(src='a', dest='b')
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_action_with_complex_and_complex_args(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(action=dict(module='copy', args=dict(src='a', dest='b'))))
|
|
|
|
mod, args, to = m.parse()
|
2014-10-16 00:35:16 +02:00
|
|
|
self._debug(mod, args, to)
|
2017-12-21 19:45:33 +01:00
|
|
|
|
|
|
|
assert mod == 'copy'
|
|
|
|
assert args == dict(src='a', dest='b')
|
2019-01-23 18:40:07 +01:00
|
|
|
assert to is Sentinel
|
2014-10-16 00:35:16 +02:00
|
|
|
|
|
|
|
def test_local_action_string(self):
|
2014-11-05 15:00:00 +01:00
|
|
|
m = ModuleArgsParser(dict(local_action='copy src=a dest=b'))
|
2015-10-09 18:48:14 +02:00
|
|
|
mod, args, delegate_to = m.parse()
|
|
|
|
self._debug(mod, args, delegate_to)
|
2014-10-21 23:04:49 +02:00
|
|
|
|
2017-12-21 19:45:33 +01:00
|
|
|
assert mod == 'copy'
|
|
|
|
assert args == dict(src='a', dest='b')
|
|
|
|
assert delegate_to == 'localhost'
|
2014-11-05 15:00:00 +01:00
|
|
|
|
2017-12-21 19:45:33 +01:00
|
|
|
@pytest.mark.parametrize("args_dict, msg", INVALID_MULTIPLE_ACTIONS)
|
|
|
|
def test_multiple_actions(self, args_dict, msg):
|
|
|
|
m = ModuleArgsParser(args_dict)
|
|
|
|
with pytest.raises(AnsibleParserError) as err:
|
|
|
|
m.parse()
|
|
|
|
|
|
|
|
assert err.value.args[0] == msg
|
|
|
|
|
|
|
|
def test_multiple_actions(self):
|
|
|
|
args_dict = {'ping': 'data=hi', 'shell': 'echo hi'}
|
|
|
|
m = ModuleArgsParser(args_dict)
|
|
|
|
with pytest.raises(AnsibleParserError) as err:
|
|
|
|
m.parse()
|
2014-11-05 15:00:00 +01:00
|
|
|
|
2017-12-21 19:45:33 +01:00
|
|
|
assert err.value.args[0].startswith("conflicting action statements: ")
|
|
|
|
conflicts = set(err.value.args[0][len("conflicting action statements: "):].split(', '))
|
|
|
|
assert conflicts == set(('ping', 'shell'))
|