def test_process_include_results(self): hostname = "testhost1" hostname2 = "testhost2" parent_task_ds = {'debug': 'msg=foo'} parent_task = Task() parent_task.load(parent_task_ds) task_ds = {'include': 'include_test.yml'} task_include = TaskInclude() loaded_task = task_include.load(task_ds, task_include=parent_task) child_task_ds = {'include': 'other_include_test.yml'} child_task_include = TaskInclude() loaded_child_task = child_task_include.load(child_task_ds, task_include=loaded_task) return_data = {'include': 'include_test.yml'} # The task in the TaskResult has to be a TaskInclude so it has a .static attr result1 = task_result.TaskResult(host=hostname, task=loaded_task, return_data=return_data) return_data = {'include': 'other_include_test.yml'} result2 = task_result.TaskResult(host=hostname2, task=loaded_child_task, return_data=return_data) results = [result1, result2] fake_loader = DictDataLoader({'include_test.yml': "", 'other_include_test.yml': ""}) mock_tqm = MagicMock(name='MockTaskQueueManager') mock_play = MagicMock(name='MockPlay') mock_iterator = MagicMock(name='MockIterator') mock_iterator._play = mock_play mock_inventory = MagicMock(name='MockInventory') mock_inventory._hosts_cache = dict() def _get_host(host_name): return None mock_inventory.get_host.side_effect = _get_host # TODO: can we use a real VariableManager? mock_variable_manager = MagicMock(name='MockVariableManager') mock_variable_manager.get_vars.return_value = dict() res = IncludedFile.process_include_results(results, mock_tqm, mock_iterator, mock_inventory, fake_loader, mock_variable_manager) self.assertIsInstance(res, list) self.assertEquals(res[0]._filename, os.path.join(os.getcwd(), 'include_test.yml')) self.assertEquals(res[1]._filename, os.path.join(os.getcwd(), 'other_include_test.yml')) self.assertEquals(res[0]._hosts, ['testhost1']) self.assertEquals(res[1]._hosts, ['testhost2']) self.assertEquals(res[0]._args, {}) self.assertEquals(res[1]._args, {})
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 mock_tqm._notified_handlers = {} mock_tqm._listening_handlers = {} queue_items = [] def _queue_empty(*args, **kwargs): return len(queue_items) == 0 def _queue_get(*args, **kwargs): if len(queue_items) == 0: raise Queue.Empty else: return queue_items.pop() 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 mock_tqm._final_q = mock_queue mock_tqm._stats = MagicMock() mock_tqm._stats.increment.return_value = None mock_play = MagicMock() mock_host = MagicMock() mock_host.name = 'test01' mock_host.vars = dict() mock_host.has_hostkey = True mock_task = MagicMock() mock_task._role = None mock_task._parent = None mock_task.ignore_errors = False mock_task._uuid = uuid.uuid4() mock_task.loop = None mock_task.copy.return_value = mock_task mock_handler_task = MagicMock(Handler) mock_handler_task.name = 'test handler' mock_handler_task.action = 'foo' mock_handler_task._parent = None mock_handler_task.get_name.return_value = "test handler" mock_handler_task.has_triggered.return_value = False mock_handler_task._uuid = 'xxxxxxxxxxxxx' mock_handler_task.copy.return_value = mock_handler_task 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) mock_iterator.get_original_task.return_value = mock_task mock_handler_block = MagicMock() mock_handler_block.block = [mock_handler_task] mock_handler_block.rescue = [] mock_handler_block.always = [] mock_play.handlers = [mock_handler_block] mock_tqm._notified_handlers = {mock_handler_task._uuid: []} mock_tqm._listening_handlers = {} mock_group = MagicMock() mock_group.add_host.return_value = None def _get_host(host_name): if host_name == 'test01': return mock_host return None def _get_group(group_name): if group_name in ('all', 'foo'): return mock_group return None mock_inventory = MagicMock() mock_inventory._hosts_cache = dict() mock_inventory.get_host.side_effect = _get_host mock_inventory.get_group.side_effect = _get_group mock_inventory.clear_pattern_cache.return_value = None mock_inventory.clear_group_dict_cache.return_value = None mock_inventory.get_host_vars.return_value = {} mock_var_mgr = MagicMock() mock_var_mgr.set_host_variable.return_value = None mock_var_mgr.set_host_facts.return_value = None mock_var_mgr.get_vars.return_value = dict() strategy_base = StrategyBase(tqm=mock_tqm) strategy_base._inventory = mock_inventory strategy_base._variable_manager = mock_var_mgr strategy_base._blocked_hosts = dict() def _has_dead_workers(): return False strategy_base._tqm.has_dead_workers.side_effect = _has_dead_workers results = strategy_base._wait_on_pending_results( iterator=mock_iterator) self.assertEqual(len(results), 0) task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(changed=True)) queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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) task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"failed":true}') queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 mock_iterator.is_failed.return_value = True 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._failed_hosts) #del mock_tqm._failed_hosts['test01'] mock_iterator.is_failed.return_value = False task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"unreachable": true}') queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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'] task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"skipped": true}') queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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) queue_items.append( TaskResult( host=mock_host.name, task=mock_task._uuid, return_data=dict( add_host=dict(host_name='newhost01', new_groups=['foo'])))) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 results = strategy_base._wait_on_pending_results( iterator=mock_iterator) self.assertEqual(len(results), 1) self.assertEqual(strategy_base._pending_results, 0) self.assertNotIn('test01', strategy_base._blocked_hosts) 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 results = strategy_base._wait_on_pending_results( iterator=mock_iterator) self.assertEqual(len(results), 1) self.assertEqual(strategy_base._pending_results, 0) self.assertNotIn('test01', strategy_base._blocked_hosts) 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 results = strategy_base._wait_on_pending_results( iterator=mock_iterator) self.assertEqual(len(results), 1) self.assertEqual(strategy_base._pending_results, 0) self.assertNotIn('test01', strategy_base._blocked_hosts) self.assertIn(mock_handler_task._uuid, strategy_base._notified_handlers) self.assertIn( mock_host, strategy_base._notified_handlers[mock_handler_task._uuid]) #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) #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) #queue_items.append(('bad')) #self.assertRaises(AnsibleError, strategy_base._process_pending_results, iterator=mock_iterator) strategy_base.cleanup()
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 mock_tqm._notified_handlers = {} mock_tqm._listening_handlers = {} queue_items = [] def _queue_empty(*args, **kwargs): return len(queue_items) == 0 def _queue_get(*args, **kwargs): if len(queue_items) == 0: raise Queue.Empty else: return queue_items.pop() mock_queue = MagicMock() mock_queue.empty.side_effect = _queue_empty mock_queue.get.side_effect = _queue_get mock_tqm._final_q = mock_queue mock_tqm._stats = MagicMock() mock_tqm._stats.increment.return_value = None mock_play = MagicMock() 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) mock_host = MagicMock() mock_host.name = 'test01' mock_host.vars = dict() mock_host.has_hostkey = True mock_task = MagicMock() mock_task._role = None mock_task.ignore_errors = False mock_handler_task = MagicMock(Handler) mock_handler_task.action = 'foo' mock_handler_task.get_name.return_value = "test handler" mock_handler_task.has_triggered.return_value = False mock_handler_block = MagicMock() mock_handler_block.block = [mock_handler_task] mock_play.handlers = [mock_handler_block] mock_tqm._notified_handlers = {mock_handler_task: []} mock_tqm._listening_handlers = {} mock_group = MagicMock() mock_group.add_host.return_value = None def _get_host(host_name): if host_name == 'test01': return mock_host return None def _get_group(group_name): if group_name in ('all', 'foo'): return mock_group return None mock_inventory = MagicMock() mock_inventory._hosts_cache = dict() mock_inventory.get_host.side_effect = _get_host mock_inventory.get_group.side_effect = _get_group mock_inventory.clear_pattern_cache.return_value = None mock_inventory.get_host_vars.return_value = {} mock_var_mgr = MagicMock() mock_var_mgr.set_host_variable.return_value = None mock_var_mgr.set_host_facts.return_value = None strategy_base = StrategyBase(tqm=mock_tqm) strategy_base._inventory = mock_inventory strategy_base._variable_manager = mock_var_mgr strategy_base._blocked_hosts = dict() results = strategy_base._wait_on_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) task_result = TaskResult(host=mock_host, task=mock_task, return_data=dict(changed=True)) queue_items.append(('host_task_ok', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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) task_result = TaskResult(host=mock_host, task=mock_task, return_data='{"failed":true}') queue_items.append(('host_task_failed', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 results = strategy_base._process_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._failed_hosts) del mock_tqm._failed_hosts['test01'] task_result = TaskResult(host=mock_host, task=mock_task, return_data='{}') queue_items.append(('host_unreachable', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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'] task_result = TaskResult(host=mock_host, task=mock_task, return_data='{}') queue_items.append(('host_task_skipped', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 queue_items.append(('add_host', dict(add_host=dict(host_name='newhost01', new_groups=['foo'])))) results = strategy_base._process_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) self.assertEqual(strategy_base._pending_results, 1) self.assertIn('test01', strategy_base._blocked_hosts) queue_items.append(('add_group', mock_host, dict(add_group=dict(group_name='foo')))) results = strategy_base._process_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) self.assertEqual(strategy_base._pending_results, 1) self.assertIn('test01', strategy_base._blocked_hosts) task_result = TaskResult(host=mock_host, task=mock_task, return_data=dict(changed=True)) queue_items.append(('notify_handler', task_result, 'test handler')) results = strategy_base._process_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) self.assertEqual(strategy_base._pending_results, 1) self.assertIn('test01', strategy_base._blocked_hosts) self.assertIn(mock_handler_task, strategy_base._notified_handlers) self.assertIn(mock_host, strategy_base._notified_handlers[mock_handler_task]) 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) 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) queue_items.append(('bad')) self.assertRaises(AnsibleError, strategy_base._process_pending_results, iterator=mock_iterator)
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 queue_items = [] def _queue_empty(*args, **kwargs): return len(queue_items) == 0 def _queue_get(*args, **kwargs): if len(queue_items) == 0: raise Queue.Empty else: return queue_items.pop() mock_queue = MagicMock() mock_queue.empty.side_effect = _queue_empty mock_queue.get.side_effect = _queue_get mock_tqm._final_q = mock_queue mock_tqm._stats = MagicMock() mock_tqm._stats.increment.return_value = None mock_iterator = MagicMock() mock_iterator.mark_host_failed.return_value = None mock_host = MagicMock() mock_host.name = 'test01' mock_host.vars = dict() mock_task = MagicMock() mock_task._role = None mock_task.ignore_errors = False mock_group = MagicMock() mock_group.add_host.return_value = None def _get_host(host_name): if host_name == 'test01': return mock_host return None def _get_group(group_name): if group_name in ('all', 'foo'): return mock_group return None mock_inventory = MagicMock() mock_inventory._hosts_cache = dict() mock_inventory.get_host.side_effect = _get_host mock_inventory.get_group.side_effect = _get_group mock_inventory.clear_pattern_cache.return_value = None mock_var_mgr = MagicMock() mock_var_mgr.set_host_variable.return_value = None mock_var_mgr.set_host_facts.return_value = None strategy_base = StrategyBase(tqm=mock_tqm) strategy_base._inventory = mock_inventory strategy_base._variable_manager = mock_var_mgr strategy_base._blocked_hosts = dict() strategy_base._notified_handlers = dict() results = strategy_base._wait_on_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) task_result = TaskResult(host=mock_host, task=mock_task, return_data=dict(changed=True)) queue_items.append(('host_task_ok', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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) task_result = TaskResult(host=mock_host, task=mock_task, return_data='{"failed":true}') queue_items.append(('host_task_failed', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 results = strategy_base._process_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._failed_hosts) del mock_tqm._failed_hosts['test01'] task_result = TaskResult(host=mock_host, task=mock_task, return_data='{}') queue_items.append(('host_unreachable', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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'] task_result = TaskResult(host=mock_host, task=mock_task, return_data='{}') queue_items.append(('host_task_skipped', task_result)) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 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) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 queue_items.append(('add_host', dict(add_host=dict(host_name='newhost01', new_groups=['foo'])))) results = strategy_base._process_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) self.assertEqual(strategy_base._pending_results, 1) self.assertIn('test01', strategy_base._blocked_hosts) queue_items.append(('add_group', mock_host, dict(add_group=dict(group_name='foo')))) results = strategy_base._process_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) self.assertEqual(strategy_base._pending_results, 1) self.assertIn('test01', strategy_base._blocked_hosts) task_result = TaskResult(host=mock_host, task=mock_task, return_data=dict(changed=True)) queue_items.append(('notify_handler', task_result, 'test handler')) results = strategy_base._process_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) self.assertEqual(strategy_base._pending_results, 1) self.assertIn('test01', strategy_base._blocked_hosts) self.assertIn('test handler', strategy_base._notified_handlers) self.assertIn(mock_host, strategy_base._notified_handlers['test handler']) 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) 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) queue_items.append(('bad')) self.assertRaises(AnsibleError, strategy_base._process_pending_results, iterator=mock_iterator)
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 mock_tqm._notified_handlers = {} mock_tqm._listening_handlers = {} queue_items = [] def _queue_empty(*args, **kwargs): return len(queue_items) == 0 def _queue_get(*args, **kwargs): if len(queue_items) == 0: raise Queue.Empty else: return queue_items.pop() 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 mock_tqm._final_q = mock_queue mock_tqm._stats = MagicMock() mock_tqm._stats.increment.return_value = None mock_play = MagicMock() mock_host = MagicMock() mock_host.name = 'test01' mock_host.vars = dict() mock_host.get_vars.return_value = dict() mock_host.has_hostkey = True mock_task = MagicMock() mock_task._role = None mock_task._parent = None mock_task.ignore_errors = False mock_task._uuid = uuid.uuid4() mock_task.loop = None mock_task.copy.return_value = mock_task mock_handler_task = MagicMock(Handler) mock_handler_task.name = 'test handler' mock_handler_task.action = 'foo' mock_handler_task._parent = None mock_handler_task.get_name.return_value = "test handler" mock_handler_task.has_triggered.return_value = False mock_handler_task._uuid = 'xxxxxxxxxxxxx' mock_handler_task.copy.return_value = mock_handler_task 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) mock_handler_block = MagicMock() mock_handler_block.block = [mock_handler_task] mock_handler_block.rescue = [] mock_handler_block.always = [] mock_play.handlers = [mock_handler_block] mock_tqm._notified_handlers = {mock_handler_task._uuid: []} mock_tqm._listening_handlers = {} mock_group = MagicMock() mock_group.add_host.return_value = None def _get_host(host_name): if host_name == 'test01': return mock_host return None def _get_group(group_name): if group_name in ('all', 'foo'): return mock_group return None mock_inventory = MagicMock() mock_inventory._hosts_cache = dict() mock_inventory.hosts.return_value = mock_host mock_inventory.get_host.side_effect = _get_host mock_inventory.get_group.side_effect = _get_group mock_inventory.clear_pattern_cache.return_value = None mock_inventory.get_host_vars.return_value = {} mock_inventory.hosts.get.return_value = mock_host mock_var_mgr = MagicMock() mock_var_mgr.set_host_variable.return_value = None mock_var_mgr.set_host_facts.return_value = None mock_var_mgr.get_vars.return_value = dict() strategy_base = StrategyBase(tqm=mock_tqm) strategy_base._inventory = mock_inventory strategy_base._variable_manager = mock_var_mgr strategy_base._blocked_hosts = dict() def _has_dead_workers(): return False strategy_base._tqm.has_dead_workers.side_effect = _has_dead_workers results = strategy_base._wait_on_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 0) task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(changed=True)) queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 mock_queued_task_cache = { (mock_host.name, mock_task._uuid): { 'task': mock_task, 'host': mock_host, 'task_vars': {}, 'play_context': {}, } } strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) 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) task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"failed":true}') queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 mock_iterator.is_failed.return_value = True strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) 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._failed_hosts) # del mock_tqm._failed_hosts['test01'] mock_iterator.is_failed.return_value = False task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"unreachable": true}') queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) 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'] task_result = TaskResult(host=mock_host.name, task=mock_task._uuid, return_data='{"skipped": true}') queue_items.append(task_result) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) 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) queue_items.append(TaskResult(host=mock_host.name, task=mock_task._uuid, return_data=dict(add_host=dict(host_name='newhost01', new_groups=['foo'])))) strategy_base._blocked_hosts['test01'] = True strategy_base._pending_results = 1 strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) results = strategy_base._wait_on_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 1) self.assertEqual(strategy_base._pending_results, 0) self.assertNotIn('test01', strategy_base._blocked_hosts) 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 strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) results = strategy_base._wait_on_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 1) self.assertEqual(strategy_base._pending_results, 0) self.assertNotIn('test01', strategy_base._blocked_hosts) 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 strategy_base._queued_task_cache = deepcopy(mock_queued_task_cache) results = strategy_base._wait_on_pending_results(iterator=mock_iterator) self.assertEqual(len(results), 1) self.assertEqual(strategy_base._pending_results, 0) self.assertNotIn('test01', strategy_base._blocked_hosts) self.assertIn(mock_handler_task._uuid, strategy_base._notified_handlers) self.assertIn(mock_host, strategy_base._notified_handlers[mock_handler_task._uuid]) # 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) # 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) # queue_items.append(('bad')) # self.assertRaises(AnsibleError, strategy_base._process_pending_results, iterator=mock_iterator) strategy_base.cleanup()
def test_process_include_results(self): hostname = "testhost1" hostname2 = "testhost2" parent_task_ds = {'debug': 'msg=foo'} parent_task = Task() parent_task.load(parent_task_ds) task_ds = {'include': 'include_test.yml'} task_include = TaskInclude() loaded_task = task_include.load(task_ds, task_include=parent_task) child_task_ds = {'include': 'other_include_test.yml'} child_task_include = TaskInclude() loaded_child_task = child_task_include.load(child_task_ds, task_include=loaded_task) return_data = {'include': 'include_test.yml'} # The task in the TaskResult has to be a TaskInclude so it has a .static attr result1 = task_result.TaskResult(host=hostname, task=loaded_task, return_data=return_data) return_data = {'include': 'other_include_test.yml'} result2 = task_result.TaskResult(host=hostname2, task=loaded_child_task, return_data=return_data) results = [result1, result2] fake_loader = DictDataLoader({ 'include_test.yml': "", 'other_include_test.yml': "" }) mock_tqm = MagicMock(name='MockTaskQueueManager') mock_play = MagicMock(name='MockPlay') mock_iterator = MagicMock(name='MockIterator') mock_iterator._play = mock_play mock_inventory = MagicMock(name='MockInventory') mock_inventory._hosts_cache = dict() def _get_host(host_name): return None mock_inventory.get_host.side_effect = _get_host # TODO: can we use a real VariableManager? mock_variable_manager = MagicMock(name='MockVariableManager') mock_variable_manager.get_vars.return_value = dict() res = IncludedFile.process_include_results(results, mock_tqm, mock_iterator, mock_inventory, fake_loader, mock_variable_manager) self.assertIsInstance(res, list) self.assertEquals(res[0]._filename, os.path.join(os.getcwd(), 'include_test.yml')) self.assertEquals(res[1]._filename, os.path.join(os.getcwd(), 'other_include_test.yml')) self.assertEquals(res[0]._hosts, ['testhost1']) self.assertEquals(res[1]._hosts, ['testhost2']) self.assertEquals(res[0]._args, {}) self.assertEquals(res[1]._args, {})