test_mod_args.py 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# (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/>.

18 19 20 21
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

22
from ansible.parsing.mod_args import ModuleArgsParser
23
from ansible.errors import AnsibleParserError
24

25
from ansible.compat.tests import unittest
26

Toshio Kuratomi committed
27
class TestModArgsDwim(unittest.TestCase):
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

    # 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

    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):
45 46
        m = ModuleArgsParser(dict(shell='echo hi'))
        mod, args, to = m.parse()
47 48 49 50 51 52 53 54 55
        self._debug(mod, args, to)
        self.assertEqual(mod, 'command')
        self.assertEqual(args, dict(
                                _raw_params = 'echo hi',
                                _uses_shell = True,
                        ))
        self.assertIsNone(to)

    def test_basic_command(self):
56 57
        m = ModuleArgsParser(dict(command='echo hi'))
        mod, args, to = m.parse()
58 59 60 61 62 63 64 65
        self._debug(mod, args, to)
        self.assertEqual(mod, 'command')
        self.assertEqual(args, dict(
                                _raw_params = 'echo hi',
                        ))
        self.assertIsNone(to)

    def test_shell_with_modifiers(self):
66 67
        m = ModuleArgsParser(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep'))
        mod, args, to = m.parse()
68 69 70 71 72 73 74 75 76 77 78
        self._debug(mod, args, to)
        self.assertEqual(mod, 'command')
        self.assertEqual(args, dict(
                                creates     = '/tmp/baz',
                                removes     = '/tmp/bleep',
                                _raw_params = '/bin/foo',
                                _uses_shell = True,
                        ))
        self.assertIsNone(to)

    def test_normal_usage(self):
79 80
        m = ModuleArgsParser(dict(copy='src=a dest=b'))
        mod, args, to = m.parse()
81 82 83 84 85 86
        self._debug(mod, args, to)
        self.assertEqual(mod, 'copy')
        self.assertEqual(args, dict(src='a', dest='b'))
        self.assertIsNone(to)

    def test_complex_args(self):
87 88
        m = ModuleArgsParser(dict(copy=dict(src='a', dest='b')))
        mod, args, to = m.parse()
89 90 91 92 93 94
        self._debug(mod, args, to)
        self.assertEqual(mod, 'copy')
        self.assertEqual(args, dict(src='a', dest='b'))
        self.assertIsNone(to)

    def test_action_with_complex(self):
95 96
        m = ModuleArgsParser(dict(action=dict(module='copy', src='a', dest='b')))
        mod, args, to = m.parse()
97 98 99 100 101 102
        self._debug(mod, args, to)
        self.assertEqual(mod, 'copy')
        self.assertEqual(args, dict(src='a', dest='b'))
        self.assertIsNone(to)

    def test_action_with_complex_and_complex_args(self):
103 104
        m = ModuleArgsParser(dict(action=dict(module='copy', args=dict(src='a', dest='b'))))
        mod, args, to = m.parse()
105 106 107 108 109 110
        self._debug(mod, args, to)
        self.assertEqual(mod, 'copy')
        self.assertEqual(args, dict(src='a', dest='b'))
        self.assertIsNone(to)

    def test_local_action_string(self):
111 112
        m = ModuleArgsParser(dict(local_action='copy src=a dest=b'))
        mod, args, to = m.parse()
113 114 115 116
        self._debug(mod, args, to)
        self.assertEqual(mod, 'copy')
        self.assertEqual(args, dict(src='a', dest='b'))
        self.assertIs(to, 'localhost')
117 118

    def test_multiple_actions(self):
119 120 121 122 123 124 125 126 127 128 129 130
        m = ModuleArgsParser(dict(action='shell echo hi', local_action='shell echo hi'))
        self.assertRaises(AnsibleParserError, m.parse)

        m = ModuleArgsParser(dict(action='shell echo hi', shell='echo hi'))
        self.assertRaises(AnsibleParserError, m.parse)

        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)