def test_empty_queue(event_loop, context_function): with context_function(None) as context: with remember_cwd(): os.chdir(os.path.dirname(context.config['work_dir'])) status = event_loop.run_until_complete( worker.run_loop(context, creds_key="integration_credentials")) assert status is None
def test_mocker_run_loop(self, context, successful_queue, event_loop, mocker): task = { "foo": "bar", "credentials": { "a": "b" }, "task": { 'task_defn': True } } async def find_task(*args, **kwargs): return deepcopy(task) async def noop(*args, **kwargs): return context.queue = successful_queue mocker.patch.object(worker, "find_task", new=find_task) mocker.patch.object(worker, "reclaim_task", new=noop) mocker.patch.object(worker, "run_task", new=find_task) mocker.patch.object(worker, "upload_artifacts", new=noop) mocker.patch.object(worker, "complete_task", new=noop) status = event_loop.run_until_complete(worker.run_loop(context)) assert status == task
def test_mocker_run_loop(context, successful_queue, event_loop, verify_cot, mocker): task = { "foo": "bar", "credentials": { "a": "b" }, "task": { 'task_defn': True } } async def find_task(*args, **kwargs): return deepcopy(task) fake_cot = mock.MagicMock context.config['verify_chain_of_trust'] = verify_cot context.queue = successful_queue mocker.patch.object(worker, "find_task", new=find_task) mocker.patch.object(worker, "reclaim_task", new=noop_async) mocker.patch.object(worker, "run_task", new=find_task) mocker.patch.object(worker, "ChainOfTrust", new=fake_cot) mocker.patch.object(worker, "verify_chain_of_trust", new=noop_async) mocker.patch.object(worker, "generate_cot", new=noop_async) mocker.patch.object(worker, "upload_artifacts", new=noop_async) mocker.patch.object(worker, "complete_task", new=noop_async) status = event_loop.run_until_complete(worker.run_loop(context)) assert status == task
def test_mocker_run_loop_exception(context, successful_queue, event_loop, mocker, func_to_raise): """Raise an exception within the run_loop try/excepts and make sure the status is changed """ task = {"foo": "bar", "credentials": {"a": "b"}, "task": {'task_defn': True}} async def find_task(*args, **kwargs): return task async def exc(*args, **kwargs): raise ScriptWorkerException("foo") async def run_task(*args, **kwargs): return 0 context.queue = successful_queue mocker.patch.object(worker, "find_task", new=find_task) mocker.patch.object(worker, "reclaim_task", new=noop_async) if func_to_raise == "run_task": mocker.patch.object(worker, "run_task", new=exc) else: mocker.patch.object(worker, "run_task", new=run_task) mocker.patch.object(worker, "generate_cot", new=noop_sync) if func_to_raise == "upload_artifacts": mocker.patch.object(worker, "upload_artifacts", new=exc) else: mocker.patch.object(worker, "upload_artifacts", new=noop_async) mocker.patch.object(worker, "complete_task", new=noop_async) mocker.patch.object(worker, "read_worker_creds", new=noop_sync) status = event_loop.run_until_complete(worker.run_loop(context)) assert status == ScriptWorkerException.exit_code
def test_run_loop_exception(self, context, successful_queue, event_loop): context.queue = successful_queue async def raise_swe(*args, **kwargs): raise ScriptWorkerException("foo") with mock.patch.object(worker, 'find_task', new=raise_swe): status = event_loop.run_until_complete(worker.run_loop(context)) assert status is None
def test_mocker_run_loop_noop(context, successful_queue, event_loop, mocker): context.queue = successful_queue mocker.patch.object(worker, "find_task", new=noop_async) mocker.patch.object(worker, "reclaim_task", new=noop_async) mocker.patch.object(worker, "run_task", new=noop_async) mocker.patch.object(worker, "generate_cot", new=noop_sync) mocker.patch.object(worker, "upload_artifacts", new=noop_async) mocker.patch.object(worker, "complete_task", new=noop_async) status = event_loop.run_until_complete(worker.run_loop(context)) assert context.credentials is None assert status is None
def test_run_successful_task(event_loop, context_function): task_id = slugid.nice().decode('utf-8') task_group_id = slugid.nice().decode('utf-8') with context_function(None) as context: result = event_loop.run_until_complete( create_task(context, task_id, task_group_id)) assert result['status']['state'] == 'pending' with remember_cwd(): os.chdir(os.path.dirname(context.config['work_dir'])) status = event_loop.run_until_complete( worker.run_loop(context, creds_key="integration_credentials")) assert status == 1 result = event_loop.run_until_complete(task_status(context, task_id)) assert result['status']['state'] == 'failed'
def test_mocker_run_loop(self, context, successful_queue, event_loop, mocker): task = {"foo": "bar", "credentials": {"a": "b"}, "task": {'task_defn': True}} async def find_task(*args, **kwargs): return deepcopy(task) async def noop(*args, **kwargs): return context.queue = successful_queue mocker.patch.object(worker, "find_task", new=find_task) mocker.patch.object(worker, "reclaim_task", new=noop) mocker.patch.object(worker, "run_task", new=find_task) mocker.patch.object(worker, "upload_artifacts", new=noop) mocker.patch.object(worker, "complete_task", new=noop) status = event_loop.run_until_complete(worker.run_loop(context)) assert status == task
def test_mocker_run_loop_noop_update_creds(context, successful_queue, event_loop, mocker): new_creds = {"new_creds": "true"} def get_creds(*args, **kwargs): return deepcopy(new_creds) mocker.patch.object(worker, "find_task", new=noop_async) mocker.patch.object(worker, "reclaim_task", new=noop_async) mocker.patch.object(worker, "run_task", new=noop_async) mocker.patch.object(worker, "generate_cot", new=noop_sync) mocker.patch.object(worker, "upload_artifacts", new=noop_async) mocker.patch.object(worker, "complete_task", new=noop_async) mocker.patch.object(worker, "read_worker_creds", new=get_creds) context.queue = successful_queue status = event_loop.run_until_complete(worker.run_loop(context)) assert context.credentials == new_creds assert status is None
def test_mocker_run_loop_noop(self, context, successful_queue, event_loop, mocker): async def find_task(*args, **kwargs): return None async def noop(*args, **kwargs): return def noop_sync(*args, **kwargs): return context.queue = successful_queue mocker.patch.object(worker, "find_task", new=find_task) mocker.patch.object(worker, "reclaim_task", new=noop) mocker.patch.object(worker, "run_task", new=find_task) mocker.patch.object(worker, "upload_artifacts", new=noop) mocker.patch.object(worker, "complete_task", new=noop) mocker.patch.object(worker, "read_worker_creds", new=noop_sync) status = event_loop.run_until_complete(worker.run_loop(context)) assert context.credentials is None assert status is None
def test_run_maxtimeout(event_loop, context_function): task_id = slugid.nice().decode('utf-8') task_group_id = slugid.nice().decode('utf-8') partial_config = { 'task_max_timeout': 2, } with context_function(partial_config) as context: result = event_loop.run_until_complete( create_task(context, task_id, task_group_id)) assert result['status']['state'] == 'pending' with remember_cwd(): os.chdir(os.path.dirname(context.config['work_dir'])) with pytest.raises(RuntimeError): event_loop.run_until_complete( worker.run_loop(context, creds_key="integration_credentials")) # Because we're using asyncio to kill tasks in the loop, # we're going to hit a RuntimeError result = event_loop.run_until_complete(task_status(context, task_id)) # TODO We need to be able to ensure this is 'failed'. assert result['status']['state'] in ('failed', 'running')
def test_mocker_run_loop_exception(context, successful_queue, event_loop, mocker, func_to_raise, exc, expected): """Raise an exception within the run_loop try/excepts and make sure the status is changed """ task = { "foo": "bar", "credentials": { "a": "b" }, "task": { 'task_defn': True } } async def claim_work(*args, **kwargs): return {'tasks': [task]} async def fail(*args, **kwargs): raise exc("foo") async def run_task(*args, **kwargs): return 0 context.queue = successful_queue mocker.patch.object(worker, "claim_work", new=claim_work) mocker.patch.object(worker, "reclaim_task", new=noop_async) mocker.patch.object(worker, "prepare_to_run_task", new=noop_sync) if func_to_raise == "run_task": mocker.patch.object(worker, "run_task", new=fail) else: mocker.patch.object(worker, "run_task", new=run_task) mocker.patch.object(worker, "generate_cot", new=noop_sync) if func_to_raise == "upload_artifacts": mocker.patch.object(worker, "upload_artifacts", new=fail) else: mocker.patch.object(worker, "upload_artifacts", new=noop_async) mocker.patch.object(worker, "complete_task", new=noop_async) status = event_loop.run_until_complete(worker.run_loop(context)) assert status == expected
def test_run_maxtimeout(self, event_loop, context_function): task_id = slugid.nice().decode('utf-8') task_group_id = slugid.nice().decode('utf-8') partial_config = { 'task_max_timeout': 2, } with context_function(partial_config) as context: result = event_loop.run_until_complete( create_task(context, task_id, task_group_id) ) assert result['status']['state'] == 'pending' with remember_cwd(): os.chdir("integration") with pytest.raises(RuntimeError): event_loop.run_until_complete( worker.run_loop(context, creds_key="integration_credentials") ) # Because we're using asyncio to kill tasks in the loop, # we're going to hit a RuntimeError result = event_loop.run_until_complete(task_status(context, task_id)) # TODO We need to be able to ensure this is 'failed'. assert result['status']['state'] in ('failed', 'running')
def test_mocker_run_loop_noop_update_creds(self, context, successful_queue, event_loop, mocker): new_creds = {"new_creds": "true"} async def find_task(*args, **kwargs): return None async def noop(*args, **kwargs): return def get_creds(*args, **kwargs): return deepcopy(new_creds) context.queue = successful_queue mocker.patch.object(worker, "find_task", new=find_task) mocker.patch.object(worker, "reclaim_task", new=noop) mocker.patch.object(worker, "run_task", new=find_task) mocker.patch.object(worker, "upload_artifacts", new=noop) mocker.patch.object(worker, "complete_task", new=noop) mocker.patch.object(worker, "read_worker_creds", new=get_creds) status = event_loop.run_until_complete(worker.run_loop(context)) assert context.credentials == new_creds assert status is None