2015-05-19 02:26:59 +02: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
|
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
from units.mock.loader import DictDataLoader
|
2018-01-15 16:49:58 +01:00
|
|
|
from copy import deepcopy
|
2016-07-31 10:23:28 +02:00
|
|
|
import uuid
|
|
|
|
|
2015-05-19 02:26:59 +02:00
|
|
|
from ansible.compat.tests import unittest
|
|
|
|
from ansible.compat.tests.mock import patch, MagicMock
|
2015-05-21 09:03:38 +02:00
|
|
|
from ansible.errors import AnsibleError, AnsibleParserError
|
2015-12-14 09:07:20 +01:00
|
|
|
from ansible.executor.process.worker import WorkerProcess
|
2015-05-19 02:26:59 +02:00
|
|
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
2015-05-21 09:03:38 +02:00
|
|
|
from ansible.executor.task_result import TaskResult
|
2017-03-23 21:35:05 +01:00
|
|
|
from ansible.inventory.host import Host
|
|
|
|
from ansible.module_utils.six.moves import queue as Queue
|
2016-07-31 10:23:28 +02:00
|
|
|
from ansible.playbook.block import Block
|
2015-12-14 09:07:20 +01:00
|
|
|
from ansible.playbook.handler import Handler
|
2017-03-23 21:35:05 +01:00
|
|
|
from ansible.plugins.strategy import StrategyBase
|
2015-05-19 02:26:59 +02:00
|
|
|
|
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
class TestStrategyBase(unittest.TestCase):
|
2015-05-19 02:26:59 +02:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_strategy_base_init(self):
|
2016-09-16 07:14:53 +02:00
|
|
|
queue_items = []
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_empty(*args, **kwargs):
|
|
|
|
return len(queue_items) == 0
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_get(*args, **kwargs):
|
|
|
|
if len(queue_items) == 0:
|
|
|
|
raise Queue.Empty
|
|
|
|
else:
|
|
|
|
return queue_items.pop()
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_put(item, *args, **kwargs):
|
|
|
|
queue_items.append(item)
|
|
|
|
|
|
|
|
mock_queue = MagicMock()
|
|
|
|
mock_queue.empty.side_effect = _queue_empty
|
|
|
|
mock_queue.get.side_effect = _queue_get
|
|
|
|
mock_queue.put.side_effect = _queue_put
|
|
|
|
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_tqm = MagicMock(TaskQueueManager)
|
2016-09-16 07:14:53 +02:00
|
|
|
mock_tqm._final_q = mock_queue
|
2015-07-24 16:53:54 +02:00
|
|
|
mock_tqm._options = MagicMock()
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._notified_handlers = {}
|
|
|
|
mock_tqm._listening_handlers = {}
|
2015-05-19 02:26:59 +02:00
|
|
|
strategy_base = StrategyBase(tqm=mock_tqm)
|
2016-09-16 07:14:53 +02:00
|
|
|
strategy_base.cleanup()
|
2015-05-19 02:26:59 +02:00
|
|
|
|
|
|
|
def test_strategy_base_run(self):
|
2016-09-16 07:14:53 +02:00
|
|
|
queue_items = []
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_empty(*args, **kwargs):
|
|
|
|
return len(queue_items) == 0
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_get(*args, **kwargs):
|
|
|
|
if len(queue_items) == 0:
|
|
|
|
raise Queue.Empty
|
|
|
|
else:
|
|
|
|
return queue_items.pop()
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_put(item, *args, **kwargs):
|
|
|
|
queue_items.append(item)
|
|
|
|
|
|
|
|
mock_queue = MagicMock()
|
|
|
|
mock_queue.empty.side_effect = _queue_empty
|
|
|
|
mock_queue.get.side_effect = _queue_get
|
|
|
|
mock_queue.put.side_effect = _queue_put
|
|
|
|
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_tqm = MagicMock(TaskQueueManager)
|
2016-09-16 07:14:53 +02:00
|
|
|
mock_tqm._final_q = mock_queue
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_tqm._stats = MagicMock()
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._notified_handlers = {}
|
|
|
|
mock_tqm._listening_handlers = {}
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_tqm.send_callback.return_value = None
|
|
|
|
|
2016-08-05 20:44:57 +02:00
|
|
|
for attr in ('RUN_OK', 'RUN_ERROR', 'RUN_FAILED_HOSTS', 'RUN_UNREACHABLE_HOSTS'):
|
|
|
|
setattr(mock_tqm, attr, getattr(TaskQueueManager, attr))
|
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_iterator = MagicMock()
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_iterator._play = MagicMock()
|
|
|
|
mock_iterator._play.handlers = []
|
|
|
|
|
2015-07-21 18:12:22 +02:00
|
|
|
mock_play_context = MagicMock()
|
2015-05-19 02:26:59 +02:00
|
|
|
|
2015-06-05 22:34:37 +02:00
|
|
|
mock_tqm._failed_hosts = dict()
|
|
|
|
mock_tqm._unreachable_hosts = dict()
|
2015-07-24 16:53:54 +02:00
|
|
|
mock_tqm._options = MagicMock()
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._notified_handlers = {}
|
|
|
|
mock_tqm._listening_handlers = {}
|
2015-05-19 02:26:59 +02:00
|
|
|
strategy_base = StrategyBase(tqm=mock_tqm)
|
|
|
|
|
2016-06-08 17:11:34 +02:00
|
|
|
mock_host = MagicMock()
|
|
|
|
mock_host.name = 'host1'
|
|
|
|
|
|
|
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, play_context=mock_play_context), mock_tqm.RUN_OK)
|
2016-08-05 20:44:57 +02:00
|
|
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, play_context=mock_play_context, result=TaskQueueManager.RUN_ERROR), mock_tqm.RUN_ERROR)
|
2015-06-05 22:34:37 +02:00
|
|
|
mock_tqm._failed_hosts = dict(host1=True)
|
2016-06-08 17:11:34 +02:00
|
|
|
mock_iterator.get_failed_hosts.return_value = [mock_host]
|
|
|
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, play_context=mock_play_context, result=False), mock_tqm.RUN_FAILED_HOSTS)
|
2015-06-05 22:34:37 +02:00
|
|
|
mock_tqm._unreachable_hosts = dict(host1=True)
|
2016-06-08 17:11:34 +02:00
|
|
|
mock_iterator.get_failed_hosts.return_value = []
|
|
|
|
self.assertEqual(strategy_base.run(iterator=mock_iterator, play_context=mock_play_context, result=False), mock_tqm.RUN_UNREACHABLE_HOSTS)
|
2016-09-16 07:14:53 +02:00
|
|
|
strategy_base.cleanup()
|
2015-05-19 02:26:59 +02:00
|
|
|
|
|
|
|
def test_strategy_base_get_hosts(self):
|
2016-09-16 07:14:53 +02:00
|
|
|
queue_items = []
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_empty(*args, **kwargs):
|
|
|
|
return len(queue_items) == 0
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_get(*args, **kwargs):
|
|
|
|
if len(queue_items) == 0:
|
|
|
|
raise Queue.Empty
|
|
|
|
else:
|
|
|
|
return queue_items.pop()
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_put(item, *args, **kwargs):
|
|
|
|
queue_items.append(item)
|
|
|
|
|
|
|
|
mock_queue = MagicMock()
|
|
|
|
mock_queue.empty.side_effect = _queue_empty
|
|
|
|
mock_queue.get.side_effect = _queue_get
|
|
|
|
mock_queue.put.side_effect = _queue_put
|
|
|
|
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_hosts = []
|
|
|
|
for i in range(0, 5):
|
|
|
|
mock_host = MagicMock()
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_host.name = "host%02d" % (i + 1)
|
2015-12-17 03:49:33 +01:00
|
|
|
mock_host.has_hostkey = True
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_hosts.append(mock_host)
|
|
|
|
|
|
|
|
mock_inventory = MagicMock()
|
|
|
|
mock_inventory.get_hosts.return_value = mock_hosts
|
|
|
|
|
|
|
|
mock_tqm = MagicMock()
|
2016-09-16 07:14:53 +02:00
|
|
|
mock_tqm._final_q = mock_queue
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._notified_handlers = {}
|
|
|
|
mock_tqm._listening_handlers = {}
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_tqm.get_inventory.return_value = mock_inventory
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
2017-05-30 19:05:19 +02:00
|
|
|
mock_play.hosts = ["host%02d" % (i + 1) for i in range(0, 5)]
|
2015-05-19 02:26:59 +02:00
|
|
|
|
|
|
|
strategy_base = StrategyBase(tqm=mock_tqm)
|
|
|
|
|
|
|
|
mock_tqm._failed_hosts = []
|
|
|
|
mock_tqm._unreachable_hosts = []
|
|
|
|
self.assertEqual(strategy_base.get_hosts_remaining(play=mock_play), mock_hosts)
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2015-05-19 02:26:59 +02:00
|
|
|
mock_tqm._failed_hosts = ["host01"]
|
|
|
|
self.assertEqual(strategy_base.get_hosts_remaining(play=mock_play), mock_hosts[1:])
|
|
|
|
self.assertEqual(strategy_base.get_failed_hosts(play=mock_play), [mock_hosts[0]])
|
|
|
|
|
|
|
|
mock_tqm._unreachable_hosts = ["host02"]
|
|
|
|
self.assertEqual(strategy_base.get_hosts_remaining(play=mock_play), mock_hosts[2:])
|
2016-09-16 07:14:53 +02:00
|
|
|
strategy_base.cleanup()
|
2015-05-19 02:26:59 +02:00
|
|
|
|
2016-09-15 23:55:54 +02:00
|
|
|
@patch.object(WorkerProcess, 'run')
|
|
|
|
def test_strategy_base_queue_task(self, mock_worker):
|
|
|
|
def fake_run(self):
|
|
|
|
return
|
|
|
|
|
|
|
|
mock_worker.run.side_effect = fake_run
|
|
|
|
|
|
|
|
fake_loader = DictDataLoader()
|
|
|
|
mock_var_manager = MagicMock()
|
|
|
|
mock_host = MagicMock()
|
2017-05-23 23:16:49 +02:00
|
|
|
mock_host.get_vars.return_value = dict()
|
2016-09-15 23:55:54 +02:00
|
|
|
mock_host.has_hostkey = True
|
|
|
|
mock_inventory = MagicMock()
|
2017-05-23 23:16:49 +02:00
|
|
|
mock_inventory.get.return_value = mock_host
|
2016-09-15 23:55:54 +02:00
|
|
|
mock_options = MagicMock()
|
|
|
|
mock_options.module_path = None
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2016-09-15 23:55:54 +02:00
|
|
|
tqm = TaskQueueManager(
|
|
|
|
inventory=mock_inventory,
|
|
|
|
variable_manager=mock_var_manager,
|
|
|
|
loader=fake_loader,
|
|
|
|
options=mock_options,
|
|
|
|
passwords=None,
|
|
|
|
)
|
|
|
|
tqm._initialize_processes(3)
|
|
|
|
tqm.hostvars = dict()
|
|
|
|
|
2018-01-15 16:49:58 +01:00
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task._uuid = 'abcd'
|
|
|
|
|
2016-09-15 23:55:54 +02:00
|
|
|
try:
|
|
|
|
strategy_base = StrategyBase(tqm=tqm)
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
|
2016-09-15 23:55:54 +02:00
|
|
|
self.assertEqual(strategy_base._cur_worker, 1)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 1)
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
|
2016-09-15 23:55:54 +02:00
|
|
|
self.assertEqual(strategy_base._cur_worker, 2)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 2)
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queue_task(host=mock_host, task=mock_task, task_vars=dict(), play_context=MagicMock())
|
2016-09-15 23:55:54 +02:00
|
|
|
self.assertEqual(strategy_base._cur_worker, 0)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 3)
|
|
|
|
finally:
|
|
|
|
tqm.cleanup()
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
def test_strategy_base_process_pending_results(self):
|
|
|
|
mock_tqm = MagicMock()
|
|
|
|
mock_tqm._terminated = False
|
|
|
|
mock_tqm._failed_hosts = dict()
|
|
|
|
mock_tqm._unreachable_hosts = dict()
|
|
|
|
mock_tqm.send_callback.return_value = None
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._notified_handlers = {}
|
|
|
|
mock_tqm._listening_handlers = {}
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
queue_items = []
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
def _queue_empty(*args, **kwargs):
|
|
|
|
return len(queue_items) == 0
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
def _queue_get(*args, **kwargs):
|
|
|
|
if len(queue_items) == 0:
|
|
|
|
raise Queue.Empty
|
|
|
|
else:
|
|
|
|
return queue_items.pop()
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_put(item, *args, **kwargs):
|
|
|
|
queue_items.append(item)
|
2016-07-14 22:37:35 +02:00
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_queue = MagicMock()
|
|
|
|
mock_queue.empty.side_effect = _queue_empty
|
|
|
|
mock_queue.get.side_effect = _queue_get
|
2016-09-16 07:14:53 +02:00
|
|
|
mock_queue.put.side_effect = _queue_put
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_tqm._final_q = mock_queue
|
|
|
|
|
|
|
|
mock_tqm._stats = MagicMock()
|
|
|
|
mock_tqm._stats.increment.return_value = None
|
2017-01-28 00:45:23 +01:00
|
|
|
|
2016-06-13 01:03:18 +02:00
|
|
|
mock_play = MagicMock()
|
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_host = MagicMock()
|
|
|
|
mock_host.name = 'test01'
|
|
|
|
mock_host.vars = dict()
|
2017-05-23 23:16:49 +02:00
|
|
|
mock_host.get_vars.return_value = dict()
|
2015-12-17 03:49:33 +01:00
|
|
|
mock_host.has_hostkey = True
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task._role = None
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_task._parent = None
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_task.ignore_errors = False
|
2018-08-23 17:41:02 +02:00
|
|
|
mock_task.ignore_unreachable = False
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_task._uuid = uuid.uuid4()
|
|
|
|
mock_task.loop = None
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_task.copy.return_value = mock_task
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2016-06-13 01:03:18 +02:00
|
|
|
mock_handler_task = MagicMock(Handler)
|
2016-07-12 23:59:30 +02:00
|
|
|
mock_handler_task.name = 'test handler'
|
2016-06-13 01:03:18 +02:00
|
|
|
mock_handler_task.action = 'foo'
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_handler_task._parent = None
|
2016-06-13 01:03:18 +02:00
|
|
|
mock_handler_task.get_name.return_value = "test handler"
|
|
|
|
mock_handler_task.has_triggered.return_value = False
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_handler_task._uuid = 'xxxxxxxxxxxxx'
|
|
|
|
mock_handler_task.copy.return_value = mock_handler_task
|
2016-06-13 01:03:18 +02:00
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_iterator = MagicMock()
|
|
|
|
mock_iterator._play = mock_play
|
|
|
|
mock_iterator.mark_host_failed.return_value = None
|
|
|
|
mock_iterator.get_next_task_for_host.return_value = (None, None)
|
|
|
|
|
2016-06-13 01:03:18 +02:00
|
|
|
mock_handler_block = MagicMock()
|
|
|
|
mock_handler_block.block = [mock_handler_task]
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_handler_block.rescue = []
|
|
|
|
mock_handler_block.always = []
|
2016-06-13 01:03:18 +02:00
|
|
|
mock_play.handlers = [mock_handler_block]
|
|
|
|
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_tqm._notified_handlers = {mock_handler_task._uuid: []}
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._listening_handlers = {}
|
2016-06-13 01:03:18 +02:00
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_group = MagicMock()
|
|
|
|
mock_group.add_host.return_value = None
|
|
|
|
|
|
|
|
def _get_host(host_name):
|
|
|
|
if host_name == 'test01':
|
|
|
|
return mock_host
|
|
|
|
return None
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
def _get_group(group_name):
|
|
|
|
if group_name in ('all', 'foo'):
|
|
|
|
return mock_group
|
|
|
|
return None
|
|
|
|
|
|
|
|
mock_inventory = MagicMock()
|
|
|
|
mock_inventory._hosts_cache = dict()
|
2017-05-23 23:16:49 +02:00
|
|
|
mock_inventory.hosts.return_value = mock_host
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_inventory.get_host.side_effect = _get_host
|
|
|
|
mock_inventory.get_group.side_effect = _get_group
|
|
|
|
mock_inventory.clear_pattern_cache.return_value = None
|
2016-01-18 21:08:07 +01:00
|
|
|
mock_inventory.get_host_vars.return_value = {}
|
2017-05-23 23:16:49 +02:00
|
|
|
mock_inventory.hosts.get.return_value = mock_host
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_var_mgr = MagicMock()
|
|
|
|
mock_var_mgr.set_host_variable.return_value = None
|
|
|
|
mock_var_mgr.set_host_facts.return_value = None
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_var_mgr.get_vars.return_value = dict()
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
strategy_base = StrategyBase(tqm=mock_tqm)
|
|
|
|
strategy_base._inventory = mock_inventory
|
|
|
|
strategy_base._variable_manager = mock_var_mgr
|
|
|
|
strategy_base._blocked_hosts = dict()
|
|
|
|
|
2016-07-14 22:37:35 +02:00
|
|
|
def _has_dead_workers():
|
2017-01-28 00:20:31 +01:00
|
|
|
return False
|
2016-07-14 22:37:35 +02:00
|
|
|
|
2016-12-20 15:43:39 +01:00
|
|
|
strategy_base._tqm.has_dead_workers.side_effect = _has_dead_workers
|
2015-05-21 09:03:38 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
|
|
|
self.assertEqual(len(results), 0)
|
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(changed=True))
|
|
|
|
queue_items.append(task_result)
|
2015-05-21 09:03:38 +02:00
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 20:50:07 +01:00
|
|
|
|
2018-01-15 16:49:58 +01:00
|
|
|
mock_queued_task_cache = {
|
|
|
|
(mock_host.name, mock_task._uuid): {
|
|
|
|
'task': mock_task,
|
|
|
|
'host': mock_host,
|
|
|
|
'task_vars': {},
|
|
|
|
'play_context': {},
|
|
|
|
}
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 20:50:07 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2015-05-21 09:03:38 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0], task_result)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"failed":true}')
|
|
|
|
queue_items.append(task_result)
|
2015-05-21 09:03:38 +02:00
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_iterator.is_failed.return_value = True
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2016-09-16 07:14:53 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
2015-05-21 09:03:38 +02:00
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0], task_result)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
2017-05-30 19:05:19 +02:00
|
|
|
# self.assertIn('test01', mock_tqm._failed_hosts)
|
|
|
|
# del mock_tqm._failed_hosts['test01']
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_iterator.is_failed.return_value = False
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"unreachable": true}')
|
|
|
|
queue_items.append(task_result)
|
2015-05-21 09:03:38 +02:00
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2015-05-21 09:03:38 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0], task_result)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
|
|
|
self.assertIn('test01', mock_tqm._unreachable_hosts)
|
|
|
|
del mock_tqm._unreachable_hosts['test01']
|
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"skipped": true}')
|
|
|
|
queue_items.append(task_result)
|
2015-05-21 09:03:38 +02:00
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2015-05-21 09:03:38 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0], task_result)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
queue_items.append(TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(add_host=dict(host_name='newhost01', new_groups=['foo']))))
|
2015-05-21 09:03:38 +02:00
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2016-09-16 07:14:53 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
2016-07-31 10:23:28 +02:00
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
queue_items.append(TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(add_group=dict(group_name='foo'))))
|
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2016-09-16 07:14:53 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
2016-07-31 10:23:28 +02:00
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
queue_items.append(TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(changed=True, _ansible_notify=['test handler'])))
|
|
|
|
strategy_base._blocked_hosts['test01'] = True
|
|
|
|
strategy_base._pending_results = 1
|
2018-01-15 16:49:58 +01:00
|
|
|
strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache)
|
2016-09-16 07:14:53 +02:00
|
|
|
results = strategy_base._wait_on_pending_results(iterator=mock_iterator)
|
2016-07-31 10:23:28 +02:00
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(strategy_base._pending_results, 0)
|
|
|
|
self.assertNotIn('test01', strategy_base._blocked_hosts)
|
2016-12-20 15:43:39 +01:00
|
|
|
self.assertIn(mock_handler_task._uuid, strategy_base._notified_handlers)
|
|
|
|
self.assertIn(mock_host, strategy_base._notified_handlers[mock_handler_task._uuid])
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
# queue_items.append(('set_host_var', mock_host, mock_task, None, 'foo', 'bar'))
|
|
|
|
# results = strategy_base._process_pending_results(iterator=mock_iterator)
|
|
|
|
# self.assertEqual(len(results), 0)
|
|
|
|
# self.assertEqual(strategy_base._pending_results, 1)
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
# queue_items.append(('set_host_facts', mock_host, mock_task, None, 'foo', dict()))
|
|
|
|
# results = strategy_base._process_pending_results(iterator=mock_iterator)
|
|
|
|
# self.assertEqual(len(results), 0)
|
|
|
|
# self.assertEqual(strategy_base._pending_results, 1)
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
# queue_items.append(('bad'))
|
|
|
|
# self.assertRaises(AnsibleError, strategy_base._process_pending_results, iterator=mock_iterator)
|
2016-09-16 07:14:53 +02:00
|
|
|
strategy_base.cleanup()
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
def test_strategy_base_load_included_file(self):
|
|
|
|
fake_loader = DictDataLoader({
|
|
|
|
"test.yml": """
|
|
|
|
- debug: msg='foo'
|
|
|
|
""",
|
|
|
|
"bad.yml": """
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
queue_items = []
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_empty(*args, **kwargs):
|
|
|
|
return len(queue_items) == 0
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_get(*args, **kwargs):
|
|
|
|
if len(queue_items) == 0:
|
|
|
|
raise Queue.Empty
|
|
|
|
else:
|
|
|
|
return queue_items.pop()
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2016-09-16 07:14:53 +02:00
|
|
|
def _queue_put(item, *args, **kwargs):
|
|
|
|
queue_items.append(item)
|
|
|
|
|
|
|
|
mock_queue = MagicMock()
|
|
|
|
mock_queue.empty.side_effect = _queue_empty
|
|
|
|
mock_queue.get.side_effect = _queue_get
|
|
|
|
mock_queue.put.side_effect = _queue_put
|
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_tqm = MagicMock()
|
2016-09-16 07:14:53 +02:00
|
|
|
mock_tqm._final_q = mock_queue
|
2016-04-20 15:06:53 +02:00
|
|
|
mock_tqm._notified_handlers = {}
|
|
|
|
mock_tqm._listening_handlers = {}
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
strategy_base = StrategyBase(tqm=mock_tqm)
|
|
|
|
strategy_base._loader = fake_loader
|
2016-09-16 07:14:53 +02:00
|
|
|
strategy_base.cleanup()
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
|
|
|
|
mock_block = MagicMock()
|
|
|
|
mock_block._play = mock_play
|
|
|
|
mock_block.vars = dict()
|
|
|
|
|
|
|
|
mock_task = MagicMock()
|
|
|
|
mock_task._block = mock_block
|
|
|
|
mock_task._role = None
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_task._parent = None
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2015-05-29 07:15:14 +02:00
|
|
|
mock_iterator = MagicMock()
|
|
|
|
mock_iterator.mark_host_failed.return_value = None
|
|
|
|
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_inc_file = MagicMock()
|
|
|
|
mock_inc_file._task = mock_task
|
|
|
|
|
|
|
|
mock_inc_file._filename = "test.yml"
|
2015-05-29 07:15:14 +02:00
|
|
|
res = strategy_base._load_included_file(included_file=mock_inc_file, iterator=mock_iterator)
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_inc_file._filename = "bad.yml"
|
2015-07-14 17:08:55 +02:00
|
|
|
res = strategy_base._load_included_file(included_file=mock_inc_file, iterator=mock_iterator)
|
|
|
|
self.assertEqual(res, [])
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2015-12-14 09:07:20 +01:00
|
|
|
@patch.object(WorkerProcess, 'run')
|
|
|
|
def test_strategy_base_run_handlers(self, mock_worker):
|
|
|
|
def fake_run(*args):
|
|
|
|
return
|
|
|
|
mock_worker.side_effect = fake_run
|
2015-07-21 18:12:22 +02:00
|
|
|
mock_play_context = MagicMock()
|
2015-05-21 09:03:38 +02:00
|
|
|
|
2015-12-14 09:07:20 +01:00
|
|
|
mock_handler_task = MagicMock(Handler)
|
|
|
|
mock_handler_task.action = 'foo'
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_handler_task.get_name.return_value = "test handler"
|
|
|
|
mock_handler_task.has_triggered.return_value = False
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_handler_task.listen = None
|
|
|
|
mock_handler_task._role = None
|
2016-12-20 15:43:39 +01:00
|
|
|
mock_handler_task._parent = None
|
|
|
|
mock_handler_task._uuid = 'xxxxxxxxxxxxxxxx'
|
2018-01-15 16:49:58 +01:00
|
|
|
mock_handler_task.copy.return_value = mock_handler_task
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_handler = MagicMock()
|
|
|
|
mock_handler.block = [mock_handler_task]
|
|
|
|
mock_handler.flag_for_host.return_value = False
|
|
|
|
|
|
|
|
mock_play = MagicMock()
|
|
|
|
mock_play.handlers = [mock_handler]
|
|
|
|
|
2015-12-14 09:07:20 +01:00
|
|
|
mock_host = MagicMock(Host)
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_host.name = "test01"
|
2015-12-17 03:49:33 +01:00
|
|
|
mock_host.has_hostkey = True
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_inventory = MagicMock()
|
|
|
|
mock_inventory.get_hosts.return_value = [mock_host]
|
2017-05-23 23:16:49 +02:00
|
|
|
mock_inventory.get.return_value = mock_host
|
2018-01-15 16:49:58 +01:00
|
|
|
mock_inventory.get_host.return_value = mock_host
|
2015-05-21 09:03:38 +02:00
|
|
|
|
|
|
|
mock_var_mgr = MagicMock()
|
|
|
|
mock_var_mgr.get_vars.return_value = dict()
|
|
|
|
|
2016-07-31 10:23:28 +02:00
|
|
|
mock_iterator = MagicMock()
|
2015-05-21 09:03:38 +02:00
|
|
|
mock_iterator._play = mock_play
|
|
|
|
|
2015-12-14 09:07:20 +01:00
|
|
|
fake_loader = DictDataLoader()
|
|
|
|
mock_options = MagicMock()
|
|
|
|
mock_options.module_path = None
|
|
|
|
|
|
|
|
tqm = TaskQueueManager(
|
|
|
|
inventory=mock_inventory,
|
|
|
|
variable_manager=mock_var_mgr,
|
|
|
|
loader=fake_loader,
|
|
|
|
options=mock_options,
|
|
|
|
passwords=None,
|
|
|
|
)
|
|
|
|
tqm._initialize_processes(3)
|
2016-06-13 01:03:18 +02:00
|
|
|
tqm._initialize_notified_handlers(mock_play)
|
2015-12-14 09:07:20 +01:00
|
|
|
tqm.hostvars = dict()
|
|
|
|
|
|
|
|
try:
|
|
|
|
strategy_base = StrategyBase(tqm=tqm)
|
|
|
|
|
|
|
|
strategy_base._inventory = mock_inventory
|
2016-12-20 15:43:39 +01:00
|
|
|
strategy_base._notified_handlers = {mock_handler_task._uuid: [mock_host]}
|
2015-12-14 09:07:20 +01:00
|
|
|
|
2018-01-15 16:49:58 +01:00
|
|
|
task_result = TaskResult(mock_host.name, mock_handler_task._uuid, dict(changed=False))
|
|
|
|
strategy_base._queued_task_cache = dict()
|
|
|
|
strategy_base._queued_task_cache[(mock_host.name, mock_handler_task._uuid)] = {
|
|
|
|
'task': mock_handler_task,
|
|
|
|
'host': mock_host,
|
Provide a way to explicitly invoke the debugger (#34006)
* Provide a way to explicitly invoke the debugger with in the debug strategy
* Merge the debugger strategy into StrategyBase
* Fix some logic, pin to a single result
* Make redo also continue
* Make sure that if the debug closure doesn't need to process the result, that we still return it
* Fix failing tests for the strategy
* Clean up messages from debugger and exit code to match bin/ansible
* Move the FieldAttribute higher, to apply at different levels
* make debugger a string, expand logic
* Better host state rollbacks
* More explicit debugger prompt
* ENABLE_TASK_DEBUGGER should be boolean, and better docs
* No bare except, add pprint, alias h, vars to task_vars
* _validate_debugger can ignore non-string, that can be caught later
* Address issue if there were no previous tasks/state, and use the correct key
* Update docs for changes to the debugger
* Guard against a stat going negative through use of decrement
* Add a few notes about using the debugger on the free strategy
* Add changelog entry for task debugger
* Add a few versionadded indicators and a note about vars -> task_vars
2018-01-09 20:50:07 +01:00
|
|
|
'task_vars': {},
|
|
|
|
'play_context': mock_play_context
|
|
|
|
}
|
2016-07-31 10:23:28 +02:00
|
|
|
tqm._final_q.put(task_result)
|
2015-12-14 09:07:20 +01:00
|
|
|
|
|
|
|
result = strategy_base.run_handlers(iterator=mock_iterator, play_context=mock_play_context)
|
|
|
|
finally:
|
2016-09-16 07:14:53 +02:00
|
|
|
strategy_base.cleanup()
|
2015-12-14 09:07:20 +01:00
|
|
|
tqm.cleanup()
|