2014-11-04 22:16:11 +01:00
|
|
|
# (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
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import patch, MagicMock
|
2014-11-04 22:16:11 +01:00
|
|
|
|
2019-08-02 17:18:14 +02:00
|
|
|
from ansible.errors import AnsibleParserError
|
2014-11-04 22:16:11 +01:00
|
|
|
from ansible.playbook.play import Play
|
|
|
|
|
2015-05-08 18:34:19 +02:00
|
|
|
from units.mock.loader import DictDataLoader
|
2016-06-01 00:02:39 +02:00
|
|
|
from units.mock.path import mock_unfrackpath_noop
|
|
|
|
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
class TestPlay(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_empty_play(self):
|
|
|
|
p = Play.load(dict())
|
2015-07-02 16:11:56 +02:00
|
|
|
self.assertEqual(str(p), '')
|
2014-11-04 22:16:11 +01:00
|
|
|
|
|
|
|
def test_basic_play(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
|
|
|
connection='local',
|
|
|
|
remote_user="root",
|
2016-09-21 22:58:35 +02:00
|
|
|
become=True,
|
|
|
|
become_user="testing",
|
2014-11-04 22:16:11 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
def test_play_with_user_conflict(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
user="testing",
|
|
|
|
gather_facts=False,
|
|
|
|
))
|
|
|
|
self.assertEqual(p.remote_user, "testing")
|
|
|
|
|
|
|
|
def test_play_with_user_conflict(self):
|
|
|
|
play_data = dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
user="testing",
|
|
|
|
remote_user="testing",
|
|
|
|
)
|
|
|
|
self.assertRaises(AnsibleParserError, Play.load, play_data)
|
|
|
|
|
|
|
|
def test_play_with_tasks(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
2017-01-28 00:20:31 +01:00
|
|
|
tasks=[dict(action='shell echo "hello world"')],
|
2014-11-04 22:16:11 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
def test_play_with_handlers(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
|
|
|
handlers=[dict(action='shell echo "hello world"')],
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_play_with_pre_tasks(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
|
|
|
pre_tasks=[dict(action='shell echo "hello world"')],
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_play_with_post_tasks(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
|
|
|
post_tasks=[dict(action='shell echo "hello world"')],
|
|
|
|
))
|
|
|
|
|
2016-06-01 00:02:39 +02:00
|
|
|
@patch('ansible.playbook.role.definition.unfrackpath', mock_unfrackpath_noop)
|
2014-11-04 22:16:11 +01:00
|
|
|
def test_play_with_roles(self):
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
'/etc/ansible/roles/foo/tasks.yml': """
|
|
|
|
- name: role task
|
|
|
|
shell: echo "hello world"
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2015-09-30 14:05:55 +02:00
|
|
|
mock_var_manager = MagicMock()
|
|
|
|
mock_var_manager.get_vars.return_value = dict()
|
|
|
|
|
2014-11-04 22:16:11 +01:00
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
|
|
|
roles=['foo'],
|
2015-09-30 14:05:55 +02:00
|
|
|
), loader=fake_loader, variable_manager=mock_var_manager)
|
2014-11-04 22:16:11 +01:00
|
|
|
|
2015-07-22 17:30:05 +02:00
|
|
|
blocks = p.compile()
|
2014-11-04 22:16:11 +01:00
|
|
|
|
2014-11-06 20:14:38 +01:00
|
|
|
def test_play_compile(self):
|
|
|
|
p = Play.load(dict(
|
|
|
|
name="test play",
|
|
|
|
hosts=['foo'],
|
|
|
|
gather_facts=False,
|
|
|
|
tasks=[dict(action='shell echo "hello world"')],
|
|
|
|
))
|
|
|
|
|
2015-07-22 17:30:05 +02:00
|
|
|
blocks = p.compile()
|
|
|
|
|
|
|
|
# with a single block, there will still be three
|
|
|
|
# implicit meta flush_handler blocks inserted
|
|
|
|
self.assertEqual(len(blocks), 4)
|