async def test_run_tasks_cancel_right_before_claim_work(context, mocker): claim_work_called = False async def mock_claim_work(_): nonlocal claim_work_called claim_work_called = True mocker.patch('scriptworker.worker.claim_work', mock_claim_work) mock_prepare_task = mocker.patch('scriptworker.worker.prepare_to_run_task') mock_prepare_task.return_value = create_finished_future() mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_complete_task.return_value = create_finished_future() run_tasks = RunTasks() await run_tasks.cancel() await run_tasks.invoke(context) assert not claim_work_called mock_prepare_task.assert_not_called() mock_do_upload.assert_not_called() mock_complete_task.assert_not_called()
async def test_run_tasks_cancel_right_before_proc_created(context, mocker): mocker.patch("scriptworker.worker.do_upload", new=create_async(result=0)) mock_prepare_task = mocker.patch("scriptworker.worker.prepare_to_run_task") mock_complete_task = mocker.patch("scriptworker.worker.complete_task") mock_verify_chain_of_trust = mocker.patch( "scriptworker.worker.verify_chain_of_trust") mocker.patch("scriptworker.worker.claim_work", create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch("scriptworker.worker.ChainOfTrust", MockChainOfTrust) mocker.patch("scriptworker.worker.reclaim_task", noop_async) mocker.patch("scriptworker.worker.generate_cot", noop_sync) mocker.patch("scriptworker.worker.cleanup", noop_sync) mocker.patch("os.path.isfile", create_sync(True)) if not AT_LEAST_PY38: mock_prepare_task.return_value = create_finished_future() mock_complete_task.return_value = create_finished_future() mock_verify_chain_of_trust.return_value = create_finished_future() run_tasks = RunTasks() async def mock_do_run_task(_, __, to_cancellable_process): await run_tasks.cancel() task_process = MockTaskProcess() await to_cancellable_process(task_process) assert task_process.stopped_due_to_worker_shutdown raise WorkerShutdownDuringTask mocker.patch("scriptworker.worker.do_run_task", mock_do_run_task) await run_tasks.invoke(context) mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES["worker-shutdown"])
async def test_run_tasks_cancel_sleep(context, mocker): slow_function_called, slow_function = create_slow_async() mocker.patch.object(asyncio, 'sleep', slow_function) mocker.patch('scriptworker.worker.claim_work', create_async(_MOCK_CLAIM_WORK_NONE_RETURN)) mock_prepare_task = mocker.patch('scriptworker.worker.prepare_to_run_task') mock_prepare_task.return_value = create_finished_future() mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_complete_task.return_value = create_finished_future() run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await slow_function_called await run_tasks.cancel() await run_tasks_future mock_prepare_task.assert_not_called() mock_do_upload.assert_not_called() mock_complete_task.assert_not_called()
async def test_run_tasks_cancel_claim_work(context, mocker): slow_function_called, slow_function = create_slow_async() mocker.patch("scriptworker.worker.claim_work", slow_function) mock_sleep = mocker.patch.object(asyncio, "sleep") mock_sleep.return_value = create_finished_future() mock_prepare_task = mocker.patch("scriptworker.worker.prepare_to_run_task") mock_prepare_task.return_value = create_finished_future() mock_do_upload = mocker.patch("scriptworker.worker.do_upload") mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch("scriptworker.worker.complete_task") mock_complete_task.return_value = create_finished_future() run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await slow_function_called await run_tasks.cancel() await run_tasks_future mock_sleep.assert_not_called() mock_prepare_task.assert_not_called() mock_do_upload.assert_not_called() mock_complete_task.assert_not_called()
async def test_run_tasks_cancel_cot(context, mocker): context.config["verify_chain_of_trust"] = True slow_function_called, slow_function = create_slow_async() mocker.patch("scriptworker.worker.verify_chain_of_trust", slow_function) mocker.patch("scriptworker.worker.claim_work", create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch("scriptworker.worker.ChainOfTrust", MockChainOfTrust) mocker.patch("scriptworker.worker.reclaim_task", noop_async) mocker.patch("scriptworker.worker.run_task", noop_async) mocker.patch("scriptworker.worker.generate_cot", noop_sync) mocker.patch("scriptworker.worker.cleanup", noop_sync) mocker.patch("os.path.isfile", create_sync(True)) mock_prepare_task = mocker.patch("scriptworker.worker.prepare_to_run_task") mocker.patch("scriptworker.worker.do_upload", new=create_async(result=0)) mock_complete_task = mocker.patch("scriptworker.worker.complete_task") if not AT_LEAST_PY38: mock_prepare_task.return_value = create_finished_future() mock_complete_task.return_value = create_finished_future() run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await slow_function_called await run_tasks.cancel() await run_tasks_future mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES["worker-shutdown"])
async def test_run_tasks_no_cancel(context, mocker): expected_args = [(context, ["one", "public/two"]), None] async def fake_upload(*args, **kwargs): assert args == expected_args.pop(0) return 0 mocker.patch("scriptworker.worker.claim_work", create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch.object(asyncio, "sleep", noop_async) mocker.patch("scriptworker.worker.prepare_to_run_task", noop_sync) mocker.patch("scriptworker.worker.reclaim_task", noop_async) mocker.patch("scriptworker.worker.do_run_task", create_async(0)) mocker.patch("scriptworker.worker.cleanup", noop_sync) mocker.patch("scriptworker.worker.filepaths_in_dir", create_sync(["one", "public/two"])) mock_complete_task = mocker.patch("scriptworker.worker.complete_task") mocker.patch("scriptworker.worker.do_upload", new=fake_upload) future = asyncio.Future() future.set_result(None) mock_complete_task.return_value = future run_tasks = RunTasks() await run_tasks.invoke(context) mock_complete_task.assert_called_once_with(mock.ANY, 0)
async def test_run_tasks_cancel_right_before_cot(context, mocker): async def fake_upload(*args, **kwargs): assert args == (context, []) return 0 context.config["verify_chain_of_trust"] = True mock_prepare_task = mocker.patch("scriptworker.worker.prepare_to_run_task") mock_run_task = mocker.patch("scriptworker.worker.run_task") mock_complete_task = mocker.patch("scriptworker.worker.complete_task") mocker.patch("scriptworker.worker.do_upload", new=fake_upload) verify_cot_future = asyncio.Future() mock_verify_chain_of_trust = mocker.patch( "scriptworker.worker.verify_chain_of_trust") mock_verify_chain_of_trust.return_value = verify_cot_future mocker.patch("scriptworker.worker.claim_work", create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch("scriptworker.worker.ChainOfTrust", MockChainOfTrust) mocker.patch("scriptworker.worker.reclaim_task", noop_async) mocker.patch("scriptworker.worker.generate_cot", noop_sync) mocker.patch("scriptworker.worker.cleanup", noop_sync) if not AT_LEAST_PY38: mock_prepare_task.return_value = create_finished_future() mock_run_task.return_value = create_finished_future() mock_complete_task.return_value = create_finished_future() run_tasks = RunTasks() async def mock_do_run_task(*args, **kwargs): # Mock out do_run_task so we can cancel _right_ before cot happens # still call the underlying do_run_task function so we see how it behaves await run_tasks.cancel() return await do_run_task(*args, **kwargs) mocker.patch("scriptworker.worker.do_run_task", mock_do_run_task) await run_tasks.invoke(context) if AT_LEAST_PY38: assert mock_verify_chain_of_trust.cancelled() else: assert verify_cot_future.cancelled() mock_run_task.assert_not_called() mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES["worker-shutdown"])
async def test_run_tasks_cancel_claim_work(context, mocker): async def dont_call_me(*args, **kwargs): raise Exception("This should not have been called!") slow_function_called, slow_function = create_slow_async() mocker.patch("scriptworker.worker.claim_work", slow_function) mocker.patch.object(asyncio, "sleep", new=dont_call_me) mocker.patch("scriptworker.worker.prepare_to_run_task", new=dont_call_me) mocker.patch("scriptworker.worker.do_upload", new=dont_call_me) mocker.patch("scriptworker.worker.complete_task", new=dont_call_me) run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await slow_function_called await run_tasks.cancel() await run_tasks_future
async def test_run_tasks_cancel_right_before_cot(context, mocker): context.config['verify_chain_of_trust'] = True mock_prepare_task = mocker.patch('scriptworker.worker.prepare_to_run_task') mock_prepare_task.return_value = create_finished_future() mock_run_task = mocker.patch('scriptworker.worker.run_task') mock_run_task.return_value = create_finished_future() mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_complete_task.return_value = create_finished_future() verify_cot_future = asyncio.Future() mock_verify_chain_of_trust = mocker.patch( 'scriptworker.worker.verify_chain_of_trust') mock_verify_chain_of_trust.return_value = verify_cot_future mocker.patch('scriptworker.worker.claim_work', create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch('scriptworker.worker.ChainOfTrust', MockChainOfTrust) mocker.patch('scriptworker.worker.reclaim_task', noop_async) mocker.patch('scriptworker.worker.generate_cot', noop_sync) mocker.patch('scriptworker.worker.cleanup', noop_sync) run_tasks = RunTasks() async def mock_do_run_task(*args, **kwargs): # Mock out do_run_task so we can cancel _right_ before cot happens # still call the underlying do_run_task function so we see how it behaves await run_tasks.cancel() return await do_run_task(*args, **kwargs) mocker.patch('scriptworker.worker.do_run_task', mock_do_run_task) await run_tasks.invoke(context) assert verify_cot_future.cancelled() mock_run_task.assert_not_called() mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES['worker-shutdown']) mock_do_upload.assert_called_once_with(context, [])
async def test_run_tasks_cancel_run_tasks(context, mocker): mock_prepare_task = mocker.patch('scriptworker.worker.prepare_to_run_task') mock_prepare_task.return_value = create_finished_future() mocker.patch('scriptworker.worker.claim_work', create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch('scriptworker.worker.reclaim_task', noop_async) mocker.patch('scriptworker.worker.run_task', noop_async) mocker.patch('scriptworker.worker.generate_cot', noop_sync) mocker.patch('scriptworker.worker.cleanup', noop_sync) mocker.patch('os.path.isfile', create_sync(True)) mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_complete_task.return_value = create_finished_future() task_process = MockTaskProcess() run_task_called = asyncio.Future() async def mock_run_task(_, to_cancellable_process): await to_cancellable_process(task_process) run_task_called.set_result(None) await task_process._wait() raise WorkerShutdownDuringTask mocker.patch('scriptworker.worker.run_task', mock_run_task) run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await run_task_called await run_tasks.cancel() await run_tasks_future assert task_process.stopped_due_to_worker_shutdown mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES['worker-shutdown']) mock_do_upload.assert_called_once_with( context, ['public/logs/chain_of_trust.log', 'public/logs/live_backing.log'])
async def test_run_tasks_cancel_right_before_proc_created(context, mocker): mock_prepare_task = mocker.patch('scriptworker.worker.prepare_to_run_task') mock_prepare_task.return_value = create_finished_future() mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_complete_task.return_value = create_finished_future() mock_verify_chain_of_trust = mocker.patch( 'scriptworker.worker.verify_chain_of_trust') mock_verify_chain_of_trust.return_value = create_finished_future() mocker.patch('scriptworker.worker.claim_work', create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch('scriptworker.worker.ChainOfTrust', MockChainOfTrust) mocker.patch('scriptworker.worker.reclaim_task', noop_async) mocker.patch('scriptworker.worker.generate_cot', noop_sync) mocker.patch('scriptworker.worker.cleanup', noop_sync) mocker.patch('os.path.isfile', create_sync(True)) run_tasks = RunTasks() async def mock_do_run_task(_, __, to_cancellable_process): await run_tasks.cancel() task_process = MockTaskProcess() await to_cancellable_process(task_process) assert task_process.stopped_due_to_worker_shutdown raise WorkerShutdownDuringTask mocker.patch('scriptworker.worker.do_run_task', mock_do_run_task) await run_tasks.invoke(context) mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES['worker-shutdown']) mock_do_upload.assert_called_once_with( context, ['public/logs/chain_of_trust.log', 'public/logs/live_backing.log'])
async def test_run_tasks_no_cancel(context, mocker): mocker.patch('scriptworker.worker.claim_work', create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch.object(asyncio, 'sleep', noop_async) mocker.patch('scriptworker.worker.prepare_to_run_task', noop_sync) mocker.patch('scriptworker.worker.reclaim_task', noop_async) mocker.patch('scriptworker.worker.do_run_task', create_async(0)) mocker.patch('scriptworker.worker.cleanup', noop_sync) mocker.patch('scriptworker.worker.filepaths_in_dir', create_sync(['one', 'public/two'])) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) future = asyncio.Future() future.set_result(None) mock_complete_task.return_value = future run_tasks = RunTasks() await run_tasks.invoke(context) mock_complete_task.assert_called_once_with(mock.ANY, 0) mock_do_upload.assert_called_once_with(context, ['one', 'public/two'])
async def test_run_tasks_cancel_run_tasks(context, mocker): mocker.patch("scriptworker.worker.claim_work", create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch("scriptworker.worker.reclaim_task", noop_async) mocker.patch("scriptworker.worker.run_task", noop_async) mocker.patch("scriptworker.worker.generate_cot", noop_sync) mocker.patch("scriptworker.worker.cleanup", noop_sync) mocker.patch("os.path.isfile", create_sync(True)) mocker.patch("scriptworker.worker.do_upload", new=create_async(result=0)) mock_prepare_task = mocker.patch("scriptworker.worker.prepare_to_run_task") mock_complete_task = mocker.patch("scriptworker.worker.complete_task") if not AT_LEAST_PY38: mock_prepare_task.return_value = create_finished_future() mock_complete_task.return_value = create_finished_future() task_process = MockTaskProcess() run_task_called = asyncio.Future() async def mock_run_task(_, to_cancellable_process): await to_cancellable_process(task_process) run_task_called.set_result(None) await task_process._wait() raise WorkerShutdownDuringTask mocker.patch("scriptworker.worker.run_task", mock_run_task) run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await run_task_called await run_tasks.cancel() await run_tasks_future assert task_process.stopped_due_to_worker_shutdown mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES["worker-shutdown"])
async def test_run_tasks_cancel_cot(context, mocker): context.config['verify_chain_of_trust'] = True slow_function_called, slow_function = create_slow_async() mocker.patch('scriptworker.worker.verify_chain_of_trust', slow_function) mock_prepare_task = mocker.patch('scriptworker.worker.prepare_to_run_task') mock_prepare_task.return_value = create_finished_future() mocker.patch('scriptworker.worker.claim_work', create_async(_MOCK_CLAIM_WORK_RETURN)) mocker.patch('scriptworker.worker.ChainOfTrust', MockChainOfTrust) mocker.patch('scriptworker.worker.reclaim_task', noop_async) mocker.patch('scriptworker.worker.run_task', noop_async) mocker.patch('scriptworker.worker.generate_cot', noop_sync) mocker.patch('scriptworker.worker.cleanup', noop_sync) mocker.patch('os.path.isfile', create_sync(True)) mock_do_upload = mocker.patch('scriptworker.worker.do_upload') mock_do_upload.return_value = create_finished_future(0) mock_complete_task = mocker.patch('scriptworker.worker.complete_task') mock_complete_task.return_value = create_finished_future() run_tasks = RunTasks() run_tasks_future = asyncio.get_event_loop().create_task( run_tasks.invoke(context)) await slow_function_called await run_tasks.cancel() await run_tasks_future mock_prepare_task.assert_called_once() mock_complete_task.assert_called_once_with(mock.ANY, STATUSES['worker-shutdown']) mock_do_upload.assert_called_once_with( context, ['public/logs/chain_of_trust.log', 'public/logs/live_backing.log'])