mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-27 21:01:27 -07:00
Remove shell module specialcases
Shell is implemented via the command module. There was a special case in mod_args to do that. Make shell into an action plugin to handle that instead. Also move the special case for the command nanny into a command module action plugin. This is more appropriate as we then do not have to send a parameter that is only for the command module to every single module.
This commit is contained in:
parent
39800e1390
commit
235bdfb996
8 changed files with 125 additions and 103 deletions
|
@ -1,129 +1,128 @@
|
|||
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
# Copyright 2017, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
import pytest
|
||||
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.parsing.mod_args import ModuleArgsParser
|
||||
|
||||
|
||||
class TestModArgsDwim(unittest.TestCase):
|
||||
class TestModArgsDwim:
|
||||
|
||||
# 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
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
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"),
|
||||
)
|
||||
|
||||
def _debug(self, mod, args, to):
|
||||
print("RETURNED module = {0}".format(mod))
|
||||
print(" args = {0}".format(args))
|
||||
print(" to = {0}".format(to))
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_basic_shell(self):
|
||||
m = ModuleArgsParser(dict(shell='echo hi'))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'command')
|
||||
self.assertEqual(args, dict(
|
||||
|
||||
assert mod == 'shell'
|
||||
assert args == dict(
|
||||
_raw_params='echo hi',
|
||||
_uses_shell=True,
|
||||
))
|
||||
self.assertIsNone(to)
|
||||
)
|
||||
assert to is None
|
||||
|
||||
def test_basic_command(self):
|
||||
m = ModuleArgsParser(dict(command='echo hi'))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'command')
|
||||
self.assertEqual(args, dict(
|
||||
|
||||
assert mod == 'command'
|
||||
assert args == dict(
|
||||
_raw_params='echo hi',
|
||||
))
|
||||
self.assertIsNone(to)
|
||||
)
|
||||
assert to is None
|
||||
|
||||
def test_shell_with_modifiers(self):
|
||||
m = ModuleArgsParser(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep'))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'command')
|
||||
self.assertEqual(args, dict(
|
||||
|
||||
assert mod == 'shell'
|
||||
assert args == dict(
|
||||
creates='/tmp/baz',
|
||||
removes='/tmp/bleep',
|
||||
_raw_params='/bin/foo',
|
||||
_uses_shell=True,
|
||||
))
|
||||
self.assertIsNone(to)
|
||||
)
|
||||
assert to is None
|
||||
|
||||
def test_normal_usage(self):
|
||||
m = ModuleArgsParser(dict(copy='src=a dest=b'))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'copy')
|
||||
self.assertEqual(args, dict(src='a', dest='b'))
|
||||
self.assertIsNone(to)
|
||||
|
||||
assert mod, 'copy'
|
||||
assert args, dict(src='a', dest='b')
|
||||
assert to is None
|
||||
|
||||
def test_complex_args(self):
|
||||
m = ModuleArgsParser(dict(copy=dict(src='a', dest='b')))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'copy')
|
||||
self.assertEqual(args, dict(src='a', dest='b'))
|
||||
self.assertIsNone(to)
|
||||
|
||||
assert mod, 'copy'
|
||||
assert args, dict(src='a', dest='b')
|
||||
assert to is None
|
||||
|
||||
def test_action_with_complex(self):
|
||||
m = ModuleArgsParser(dict(action=dict(module='copy', src='a', dest='b')))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'copy')
|
||||
self.assertEqual(args, dict(src='a', dest='b'))
|
||||
self.assertIsNone(to)
|
||||
|
||||
assert mod == 'copy'
|
||||
assert args == dict(src='a', dest='b')
|
||||
assert to is None
|
||||
|
||||
def test_action_with_complex_and_complex_args(self):
|
||||
m = ModuleArgsParser(dict(action=dict(module='copy', args=dict(src='a', dest='b'))))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'copy')
|
||||
self.assertEqual(args, dict(src='a', dest='b'))
|
||||
self.assertIsNone(to)
|
||||
|
||||
assert mod == 'copy'
|
||||
assert args == dict(src='a', dest='b')
|
||||
assert to is None
|
||||
|
||||
def test_local_action_string(self):
|
||||
m = ModuleArgsParser(dict(local_action='copy src=a dest=b'))
|
||||
mod, args, delegate_to = m.parse()
|
||||
self._debug(mod, args, delegate_to)
|
||||
self.assertEqual(mod, 'copy')
|
||||
self.assertEqual(args, dict(src='a', dest='b'))
|
||||
self.assertIs(delegate_to, 'localhost')
|
||||
|
||||
assert mod == 'copy'
|
||||
assert args == dict(src='a', dest='b')
|
||||
assert delegate_to == 'localhost'
|
||||
|
||||
@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):
|
||||
m = ModuleArgsParser(dict(action='shell echo hi', local_action='shell echo hi'))
|
||||
self.assertRaises(AnsibleParserError, m.parse)
|
||||
args_dict = {'ping': 'data=hi', 'shell': 'echo hi'}
|
||||
m = ModuleArgsParser(args_dict)
|
||||
with pytest.raises(AnsibleParserError) as err:
|
||||
m.parse()
|
||||
|
||||
m = ModuleArgsParser(dict(action='shell echo hi', shell='echo hi'))
|
||||
self.assertRaises(AnsibleParserError, m.parse)
|
||||
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'))
|
||||
|
||||
m = ModuleArgsParser(dict(local_action='shell echo hi', shell='echo hi'))
|
||||
self.assertRaises(AnsibleParserError, m.parse)
|
||||
|
||||
m = ModuleArgsParser(dict(ping='data=hi', shell='echo hi'))
|
||||
self.assertRaises(AnsibleParserError, m.parse)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue