mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 23:21:22 -07:00
Adding new playbook objects for v2
* Playbook * TaskInclude
This commit is contained in:
parent
cbad867f24
commit
229d49fe36
42 changed files with 2041 additions and 81 deletions
|
@ -51,7 +51,7 @@ class TestErrors(unittest.TestCase):
|
|||
mock_method.return_value = ('this is line 1\n', '')
|
||||
e = AnsibleError(self.message, self.obj)
|
||||
|
||||
self.assertEqual(e.message, "This is the error message\nThe error appears to have been in 'foo.yml': line 1, column 1,\nbut may actually be before there depending on the exact syntax problem.\n\nthis is line 1\n^\n")
|
||||
self.assertEqual(e.message, "This is the error message\n\nThe error appears to have been in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nthis is line 1\n^\n")
|
||||
|
||||
def test_get_error_lines_from_file(self):
|
||||
m = mock_open()
|
||||
|
@ -63,12 +63,12 @@ class TestErrors(unittest.TestCase):
|
|||
self.obj._line_number = 1
|
||||
self.obj._column_number = 1
|
||||
e = AnsibleError(self.message, self.obj)
|
||||
self.assertEqual(e.message, "This is the error message\nThe error appears to have been in 'foo.yml': line 1, column 1,\nbut may actually be before there depending on the exact syntax problem.\n\nthis is line 1\n^\n")
|
||||
self.assertEqual(e.message, "This is the error message\n\nThe error appears to have been in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nthis is line 1\n^\n")
|
||||
|
||||
# this line will not be found, as it is out of the index range
|
||||
self.obj._data_source = 'foo.yml'
|
||||
self.obj._line_number = 2
|
||||
self.obj._column_number = 1
|
||||
e = AnsibleError(self.message, self.obj)
|
||||
self.assertEqual(e.message, "This is the error message\nThe error appears to have been in 'foo.yml': line 2, column 1,\nbut may actually be before there depending on the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)")
|
||||
self.assertEqual(e.message, "This is the error message\n\nThe error appears to have been in 'foo.yml': line 2, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)")
|
||||
|
||||
|
|
|
@ -31,7 +31,6 @@ class TestModArgsDwim(unittest.TestCase):
|
|||
# and the task knows the line numbers
|
||||
|
||||
def setUp(self):
|
||||
self.m = ModuleArgsParser()
|
||||
pass
|
||||
|
||||
def _debug(self, mod, args, to):
|
||||
|
@ -43,7 +42,8 @@ class TestModArgsDwim(unittest.TestCase):
|
|||
pass
|
||||
|
||||
def test_basic_shell(self):
|
||||
mod, args, to = self.m.parse(dict(shell='echo hi'))
|
||||
m = ModuleArgsParser(dict(shell='echo hi'))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'command')
|
||||
self.assertEqual(args, dict(
|
||||
|
@ -53,7 +53,8 @@ class TestModArgsDwim(unittest.TestCase):
|
|||
self.assertIsNone(to)
|
||||
|
||||
def test_basic_command(self):
|
||||
mod, args, to = self.m.parse(dict(command='echo hi'))
|
||||
m = ModuleArgsParser(dict(command='echo hi'))
|
||||
mod, args, to = m.parse()
|
||||
self._debug(mod, args, to)
|
||||
self.assertEqual(mod, 'command')
|
||||
self.assertEqual(args, dict(
|
||||
|
@ -62,7 +63,8 @@ class TestModArgsDwim(unittest.TestCase):
|
|||
self.assertIsNone(to)
|
||||
|
||||
def test_shell_with_modifiers(self):
|
||||
mod, args, to = self.m.parse(dict(shell='/bin/foo creates=/tmp/baz removes=/tmp/bleep'))
|
||||
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(
|
||||
|
@ -74,42 +76,55 @@ class TestModArgsDwim(unittest.TestCase):
|
|||
self.assertIsNone(to)
|
||||
|
||||
def test_normal_usage(self):
|
||||
mod, args, to = self.m.parse(dict(copy='src=a dest=b'))
|
||||
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)
|
||||
|
||||
def test_complex_args(self):
|
||||
mod, args, to = self.m.parse(dict(copy=dict(src='a', dest='b')))
|
||||
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)
|
||||
|
||||
def test_action_with_complex(self):
|
||||
mod, args, to = self.m.parse(dict(action=dict(module='copy', src='a', dest='b')))
|
||||
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)
|
||||
|
||||
def test_action_with_complex_and_complex_args(self):
|
||||
mod, args, to = self.m.parse(dict(action=dict(module='copy', args=dict(src='a', dest='b'))))
|
||||
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)
|
||||
|
||||
def test_local_action_string(self):
|
||||
mod, args, to = self.m.parse(dict(local_action='copy src=a dest=b'))
|
||||
m = ModuleArgsParser(dict(local_action='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.assertIs(to, 'localhost')
|
||||
|
||||
def test_multiple_actions(self):
|
||||
self.assertRaises(AnsibleParserError, self.m.parse, dict(action='shell echo hi', local_action='shell echo hi'))
|
||||
self.assertRaises(AnsibleParserError, self.m.parse, dict(action='shell echo hi', shell='echo hi'))
|
||||
self.assertRaises(AnsibleParserError, self.m.parse, dict(local_action='shell echo hi', shell='echo hi'))
|
||||
self.assertRaises(AnsibleParserError, self.m.parse, dict(ping='data=hi', shell='echo hi'))
|
||||
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)
|
||||
|
||||
|
|
65
v2/test/playbook/test_playbook.py
Normal file
65
v2/test/playbook/test_playbook.py
Normal file
|
@ -0,0 +1,65 @@
|
|||
# (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/>.
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.compat.tests.mock import patch, MagicMock
|
||||
|
||||
from ansible.errors import AnsibleError, AnsibleParserError
|
||||
from ansible.playbook import Playbook
|
||||
|
||||
from test.mock.loader import DictDataLoader
|
||||
|
||||
class TestPlaybook(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_empty_playbook(self):
|
||||
p = Playbook()
|
||||
|
||||
def test_basic_playbook(self):
|
||||
fake_loader = DictDataLoader({
|
||||
"test_file.yml":"""
|
||||
- hosts: all
|
||||
""",
|
||||
})
|
||||
p = Playbook.load("test_file.yml", loader=fake_loader)
|
||||
|
||||
def test_bad_playbook_files(self):
|
||||
fake_loader = DictDataLoader({
|
||||
# represents a playbook which is not a list of plays
|
||||
"bad_list.yml": """
|
||||
foo: bar
|
||||
|
||||
""",
|
||||
# represents a playbook where a play entry is mis-formatted
|
||||
"bad_entry.yml": """
|
||||
-
|
||||
- "This should be a mapping..."
|
||||
|
||||
""",
|
||||
})
|
||||
self.assertRaises(AnsibleParserError, Playbook.load, "bad_list.yml", fake_loader)
|
||||
self.assertRaises(AnsibleParserError, Playbook.load, "bad_entry.yml", fake_loader)
|
||||
|
63
v2/test/playbook/test_task_include.py
Normal file
63
v2/test/playbook/test_task_include.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
# (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/>.
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.parsing.yaml.objects import AnsibleMapping
|
||||
from ansible.playbook.task_include import TaskInclude
|
||||
|
||||
from test.mock.loader import DictDataLoader
|
||||
|
||||
class TestTaskInclude(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self._fake_loader = DictDataLoader({
|
||||
"foo.yml": """
|
||||
- shell: echo "hello world"
|
||||
"""
|
||||
})
|
||||
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_empty_task_include(self):
|
||||
ti = TaskInclude()
|
||||
|
||||
def test_basic_task_include(self):
|
||||
ti = TaskInclude.load(AnsibleMapping(include='foo.yml'), loader=self._fake_loader)
|
||||
|
||||
def test_task_include_with_loop(self):
|
||||
ti = TaskInclude.load(AnsibleMapping(include='foo.yml', with_items=['a', 'b', 'c']), loader=self._fake_loader)
|
||||
|
||||
def test_task_include_with_conditional(self):
|
||||
ti = TaskInclude.load(AnsibleMapping(include='foo.yml', when="1 == 1"), loader=self._fake_loader)
|
||||
|
||||
def test_task_include_with_tags(self):
|
||||
ti = TaskInclude.load(AnsibleMapping(include='foo.yml', tags="foo"), loader=self._fake_loader)
|
||||
ti = TaskInclude.load(AnsibleMapping(include='foo.yml', tags=["foo", "bar"]), loader=self._fake_loader)
|
||||
|
||||
def test_task_include_errors(self):
|
||||
self.assertRaises(AnsibleParserError, TaskInclude.load, AnsibleMapping(include=''), loader=self._fake_loader)
|
||||
self.assertRaises(AnsibleParserError, TaskInclude.load, AnsibleMapping(include='foo.yml', vars="1"), loader=self._fake_loader)
|
||||
self.assertRaises(AnsibleParserError, TaskInclude.load, AnsibleMapping(include='foo.yml a=1', vars=dict(b=2)), loader=self._fake_loader)
|
||||
|
|
@ -36,10 +36,6 @@ class TestErrors(unittest.TestCase):
|
|||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def test_push_basedir(self):
|
||||
push_basedir('/root/foo/bar')
|
||||
self.assertEqual(_basedirs, ['/root/foo/bar'])
|
||||
|
||||
@patch.object(PluginLoader, '_get_paths')
|
||||
def test_print_paths(self, mock_method):
|
||||
mock_method.return_value = ['/path/one', '/path/two', '/path/three']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue