2014-11-06 19:14:38 +00: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
|
|
|
|
|
|
|
|
from ansible.compat.tests import unittest
|
|
|
|
from ansible.compat.tests.mock import patch, MagicMock
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError, AnsibleParserError
|
2014-11-14 22:14:08 +00:00
|
|
|
from ansible.executor.play_iterator import PlayIterator
|
2014-11-06 19:14:38 +00:00
|
|
|
from ansible.playbook import Playbook
|
2015-07-21 16:12:22 +00:00
|
|
|
from ansible.playbook.play_context import PlayContext
|
2014-11-06 19:14:38 +00:00
|
|
|
|
2015-05-08 16:34:19 +00:00
|
|
|
from units.mock.loader import DictDataLoader
|
2014-11-06 19:14:38 +00:00
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
class TestPlayIterator(unittest.TestCase):
|
2014-11-06 19:14:38 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
2014-11-14 22:14:08 +00:00
|
|
|
def test_play_iterator(self):
|
2014-11-06 19:14:38 +00:00
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"test_play.yml": """
|
|
|
|
- hosts: all
|
2014-11-14 22:14:08 +00:00
|
|
|
gather_facts: false
|
2014-11-06 19:14:38 +00:00
|
|
|
roles:
|
|
|
|
- test_role
|
|
|
|
pre_tasks:
|
|
|
|
- debug: msg="this is a pre_task"
|
|
|
|
tasks:
|
|
|
|
- debug: msg="this is a regular task"
|
|
|
|
post_tasks:
|
|
|
|
- debug: msg="this is a post_task"
|
|
|
|
""",
|
|
|
|
'/etc/ansible/roles/test_role/tasks/main.yml': """
|
|
|
|
- debug: msg="this is a role task"
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2015-09-30 12:05:55 +00:00
|
|
|
mock_var_manager = MagicMock()
|
|
|
|
mock_var_manager._fact_cache = dict()
|
|
|
|
mock_var_manager.get_vars.return_value = dict()
|
|
|
|
|
|
|
|
p = Playbook.load('test_play.yml', loader=fake_loader, variable_manager=mock_var_manager)
|
2014-11-06 19:14:38 +00:00
|
|
|
|
|
|
|
hosts = []
|
|
|
|
for i in range(0, 10):
|
|
|
|
host = MagicMock()
|
|
|
|
host.get_name.return_value = 'host%02d' % i
|
|
|
|
hosts.append(host)
|
|
|
|
|
|
|
|
inventory = MagicMock()
|
|
|
|
inventory.get_hosts.return_value = hosts
|
2014-11-14 22:14:08 +00:00
|
|
|
inventory.filter_hosts.return_value = hosts
|
2014-11-06 19:14:38 +00:00
|
|
|
|
2015-07-21 16:12:22 +00:00
|
|
|
play_context = PlayContext(play=p._entries[0])
|
2015-05-12 17:24:57 +00:00
|
|
|
|
|
|
|
itr = PlayIterator(
|
|
|
|
inventory=inventory,
|
|
|
|
play=p._entries[0],
|
2015-07-21 16:12:22 +00:00
|
|
|
play_context=play_context,
|
2015-09-03 18:25:43 +00:00
|
|
|
variable_manager=mock_var_manager,
|
2015-05-12 17:24:57 +00:00
|
|
|
all_vars=dict(),
|
|
|
|
)
|
|
|
|
|
2015-07-22 15:30:05 +00:00
|
|
|
# pre task
|
2015-05-12 17:24:57 +00:00
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
2014-11-06 19:14:38 +00:00
|
|
|
self.assertIsNotNone(task)
|
2015-07-22 15:30:05 +00:00
|
|
|
self.assertEqual(task.action, 'debug')
|
|
|
|
# implicit meta: flush_handlers
|
2015-05-12 17:24:57 +00:00
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
2014-11-06 19:14:38 +00:00
|
|
|
self.assertIsNotNone(task)
|
2015-07-22 15:30:05 +00:00
|
|
|
self.assertEqual(task.action, 'meta')
|
|
|
|
# role task
|
2015-05-12 17:24:57 +00:00
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
2014-11-06 19:14:38 +00:00
|
|
|
self.assertIsNotNone(task)
|
2015-07-22 15:30:05 +00:00
|
|
|
self.assertEqual(task.action, 'debug')
|
|
|
|
self.assertIsNotNone(task._role)
|
|
|
|
# regular play task
|
2015-05-12 17:24:57 +00:00
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
2014-11-06 19:14:38 +00:00
|
|
|
self.assertIsNotNone(task)
|
2015-07-22 15:30:05 +00:00
|
|
|
self.assertEqual(task.action, 'debug')
|
|
|
|
self.assertIsNone(task._role)
|
|
|
|
# implicit meta: flush_handlers
|
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
|
|
|
self.assertIsNotNone(task)
|
|
|
|
self.assertEqual(task.action, 'meta')
|
|
|
|
# post task
|
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
|
|
|
self.assertIsNotNone(task)
|
|
|
|
self.assertEqual(task.action, 'debug')
|
|
|
|
# implicit meta: flush_handlers
|
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
|
|
|
self.assertIsNotNone(task)
|
|
|
|
self.assertEqual(task.action, 'meta')
|
|
|
|
# end of iteration
|
2015-05-12 17:24:57 +00:00
|
|
|
(host_state, task) = itr.get_next_task_for_host(hosts[0])
|
2014-11-06 19:14:38 +00:00
|
|
|
self.assertIsNone(task)
|
2015-05-12 17:24:57 +00:00
|
|
|
|