Beispiel #1
0
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_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'])
Beispiel #3
0
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_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()
Beispiel #5
0
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"])
Beispiel #6
0
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
Beispiel #7
0
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'])
Beispiel #8
0
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"])
Beispiel #9
0
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'])
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'])