def test_task_result_no_log(self): mock_host = MagicMock() mock_task = MagicMock() # no_log should remove secrets tr = TaskResult(mock_host, mock_task, dict(_assible_no_log=True, secret='DONTSHOWME')) clean = tr.clean_copy() self.assertTrue('secret' not in clean._result)
def test_task_result_basic(self): mock_host = MagicMock() mock_task = MagicMock() # test loading a result with a dict tr = TaskResult(mock_host, mock_task, dict()) # test loading a result with a JSON string with patch('assible.parsing.dataloader.DataLoader.load') as p: tr = TaskResult(mock_host, mock_task, '{}')
def test_task_result_no_log_preserve(self): mock_host = MagicMock() mock_task = MagicMock() # no_log should not remove presrved keys tr = TaskResult( mock_host, mock_task, dict( _assible_no_log=True, retries=5, attempts=5, changed=False, foo='bar', ) ) clean = tr.clean_copy() self.assertTrue('retries' in clean._result) self.assertTrue('attempts' in clean._result) self.assertTrue('changed' in clean._result) self.assertTrue('foo' not in clean._result)
def test_strategy_base_run_handlers(self, mock_worker): def fake_run(*args): return mock_worker.side_effect = fake_run mock_play_context = MagicMock() mock_handler_task = Handler() mock_handler_task.action = 'foo' mock_handler_task.cached_name = False mock_handler_task.name = "test handler" mock_handler_task.listen = [] mock_handler_task._role = None mock_handler_task._parent = None mock_handler_task._uuid = 'xxxxxxxxxxxxxxxx' 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] mock_host = MagicMock(Host) mock_host.name = "test01" mock_host.has_hostkey = True mock_inventory = MagicMock() mock_inventory.get_hosts.return_value = [mock_host] mock_inventory.get.return_value = mock_host mock_inventory.get_host.return_value = mock_host mock_var_mgr = MagicMock() mock_var_mgr.get_vars.return_value = dict() mock_iterator = MagicMock() mock_iterator._play = mock_play fake_loader = DictDataLoader() tqm = TaskQueueManager( inventory=mock_inventory, variable_manager=mock_var_mgr, loader=fake_loader, passwords=None, forks=5, ) tqm._initialize_processes(3) tqm.hostvars = dict() try: strategy_base = StrategyBase(tqm=tqm) strategy_base._inventory = mock_inventory 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, 'task_vars': {}, 'play_context': mock_play_context } tqm._final_q.put(task_result) result = strategy_base.run_handlers(iterator=mock_iterator, play_context=mock_play_context) finally: strategy_base.cleanup() tqm.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 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.ignore_unreachable = False mock_task._uuid = uuid.uuid4() mock_task.loop = None mock_task.copy.return_value = mock_task mock_handler_task = Handler() mock_handler_task.name = 'test handler' mock_handler_task.action = 'foo' mock_handler_task._parent = None mock_handler_task._uuid = 'xxxxxxxxxxxxx' 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_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, _assible_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.assertTrue(mock_handler_task.is_host_notified(mock_host)) # 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(AssibleError, strategy_base._process_pending_results, iterator=mock_iterator) strategy_base.cleanup()
def test_task_result_is_unreachable(self): mock_host = MagicMock() mock_task = MagicMock() # test with no unreachable in result tr = TaskResult(mock_host, mock_task, dict()) self.assertFalse(tr.is_unreachable()) # test with unreachable in the result tr = TaskResult(mock_host, mock_task, dict(unreachable=True)) self.assertTrue(tr.is_unreachable()) # test with multiple results but none unreachable mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True])) self.assertFalse(tr.is_unreachable()) # test with multiple results and one unreachable mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(unreachable=False), dict(unreachable=True), dict(some_key=False)])) self.assertTrue(tr.is_unreachable())
def test_task_result_is_skipped(self): mock_host = MagicMock() mock_task = MagicMock() # test with no skipped in result tr = TaskResult(mock_host, mock_task, dict()) self.assertFalse(tr.is_skipped()) # test with skipped in the result tr = TaskResult(mock_host, mock_task, dict(skipped=True)) self.assertTrue(tr.is_skipped()) # test with multiple results but none skipped mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True])) self.assertFalse(tr.is_skipped()) # test with multiple results and one skipped mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(skipped=False), dict(skipped=True), dict(some_key=False)])) self.assertFalse(tr.is_skipped()) # test with multiple results and all skipped mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(skipped=True), dict(skipped=True), dict(skipped=True)])) self.assertTrue(tr.is_skipped()) # test with multiple squashed results (list of strings) # first with the main result having skipped=False mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=["a", "b", "c"], skipped=False)) self.assertFalse(tr.is_skipped()) # then with the main result having skipped=True tr = TaskResult(mock_host, mock_task, dict(results=["a", "b", "c"], skipped=True)) self.assertTrue(tr.is_skipped())
def test_task_result_is_changed(self): mock_host = MagicMock() mock_task = MagicMock() # test with no changed in result tr = TaskResult(mock_host, mock_task, dict()) self.assertFalse(tr.is_changed()) # test with changed in the result tr = TaskResult(mock_host, mock_task, dict(changed=True)) self.assertTrue(tr.is_changed()) # test with multiple results but none changed mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(foo='bar'), dict(bam='baz'), True])) self.assertFalse(tr.is_changed()) # test with multiple results and one changed mock_task.loop = 'foo' tr = TaskResult(mock_host, mock_task, dict(results=[dict(changed=False), dict(changed=True), dict(some_key=False)])) self.assertTrue(tr.is_changed())
def test_task_result_is_failed(self): mock_host = MagicMock() mock_task = MagicMock() # test with no failed in result tr = TaskResult(mock_host, mock_task, dict()) self.assertFalse(tr.is_failed()) # test failed result with rc values (should not matter) tr = TaskResult(mock_host, mock_task, dict(rc=0)) self.assertFalse(tr.is_failed()) tr = TaskResult(mock_host, mock_task, dict(rc=1)) self.assertFalse(tr.is_failed()) # test with failed in result tr = TaskResult(mock_host, mock_task, dict(failed=True)) self.assertTrue(tr.is_failed()) # test with failed_when in result tr = TaskResult(mock_host, mock_task, dict(failed_when_result=True)) self.assertTrue(tr.is_failed())
def send_task_result(self, *args, **kwargs): if isinstance(args[0], TaskResult): tr = args[0] else: tr = TaskResult(*args, **kwargs) self.put(tr, block=False)