Exemplo n.º 1
0
def test_bounce_task(task_type, configure_model, pymongo_connection,
                     freeze_time):
    new_task = task.Task(task_type, pytest.faux.gen_uuid())
    new_task.create()

    new_task.bounce()

    assert new_task.bounced == 1
    assert int(freeze_time.return_value) + task.BOUNCE_TIMEOUT \
        <= new_task.time_bounced \
        <= int(freeze_time.return_value) + 2 * task.BOUNCE_TIMEOUT

    new_task.bounce()

    assert new_task.bounced == 2
    assert int(freeze_time.return_value) + task.BOUNCE_TIMEOUT \
        <= new_task.time_bounced \
        <= int(freeze_time.return_value) + new_task.bounced \
        * task.BOUNCE_TIMEOUT

    db_task = pymongo_connection.db.task.find_one({"_id": new_task._id})
    assert db_task
    assert task.TTL_FIELDNAME not in db_task

    assert db_task == new_task.get_state()
Exemplo n.º 2
0
def test_create_task_watchable(task_type, configure_model, no_sleep,
                               clean_tasks):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id)
    new_task.create()

    iterator = task.Task.watch(exit_on_empty=True)
    assert new_task._id == next(iterator)._id
Exemplo n.º 3
0
def test_cancel_not_started_task(task_type, configure_model, freeze_time):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id).create()

    new_task.cancel()

    assert not new_task.time_started
    assert new_task.time_cancelled == int(freeze_time.return_value)
Exemplo n.º 4
0
def test_fail_task_error_message(task_type, configure_model):
    executor_id = pytest.faux.gen_uuid()
    message = pytest.faux.gen_iplum()
    new_task = task.Task(task_type, executor_id).create()
    new_task.start()
    new_task.fail(message)

    assert new_task.error == message
Exemplo n.º 5
0
def test_finish_finished_task(task_type, how_to_finish, how_to_finish_again,
                              configure_model, freeze_time):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id).create()
    new_task.start()
    getattr(new_task, how_to_finish)()

    with pytest.raises(exceptions.DecapodError):
        getattr(new_task, how_to_finish_again)()
Exemplo n.º 6
0
def test_get_by_execution_id(task_type, configure_model):
    executor_id = pytest.faux.gen_uuid()

    assert task.Task.get_by_execution_id(executor_id, task_type) is None

    new_task = task.Task(task_type, executor_id)
    new_task.create()

    found = task.Task.get_by_execution_id(executor_id, task_type)
    assert found._id == new_task._id
Exemplo n.º 7
0
def test_process_task_list(task_type, task_watch, mainloop_possible_to_process,
                           mainloop_process_task, configure_model):
    tsk1 = task.Task(task_type, pytest.faux.gen_uuid())
    tsk2 = task.Task(task_type, pytest.faux.gen_uuid())
    tsk1.create()
    tsk2.create()

    task_watch.return_value = [tsk1, tsk2]
    mainloop_possible_to_process.return_value = True

    mainloop.main()

    for tsk in tsk1, tsk2:
        mainloop_possible_to_process.assert_any_call(tsk)
        mainloop_process_task.assert_any_call(tsk)

    assert len(mainloop_possible_to_process.mock_calls) == 2
    assert len(mainloop_process_task.mock_calls) == 2
    assert mainloop.SHUTDOWN_EVENT.is_set()
Exemplo n.º 8
0
def test_fail_task_error_message(task_type, configure_model,
                                 pymongo_connection):
    executor_id = pytest.faux.gen_uuid()
    message = pytest.faux.gen_iplum()
    new_task = task.Task(task_type, executor_id).create()
    new_task.start()
    new_task.fail(message)

    assert new_task.error == message

    db_task = pymongo_connection.db.task.find_one({"_id": new_task._id})
    assert db_task
    assert task.TTL_FIELDNAME in db_task
Exemplo n.º 9
0
def test_restart_task(task_type, finish_action, configure_model, freeze_time):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id).create()

    new_task.start()
    assert new_task.time_started == int(freeze_time.return_value)

    with pytest.raises(exceptions.CannotStartTaskError):
        new_task.start()

    getattr(new_task, finish_action)()
    with pytest.raises(exceptions.CannotStartTaskError):
        new_task.start()
Exemplo n.º 10
0
def test_cancel_not_started_task(task_type, configure_model, freeze_time,
                                 pymongo_connection):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id).create()

    new_task.cancel()

    assert not new_task.time_started
    assert new_task.time_cancelled == int(freeze_time.return_value)

    db_task = pymongo_connection.db.task.find_one({"_id": new_task._id})
    assert db_task
    assert task.TTL_FIELDNAME in db_task
Exemplo n.º 11
0
def test_do_not_process_impossible_tasks(
    task_type, task_watch, mainloop_possible_to_process, mainloop_process_task,
    configure_model
):
    tsk = task.Task(task_type, pytest.faux.gen_uuid())
    tsk.create()

    task_watch.return_value = [tsk]
    mainloop_possible_to_process.return_value = False

    mainloop.main()

    mainloop_possible_to_process.assert_called_once_with(tsk)
    mainloop_process_task.assert_not_called()

    assert mainloop.SHUTDOWN_EVENT.is_set()
Exemplo n.º 12
0
def test_watch_after_bounce(task_type, freeze_time,
                            configure_model, no_sleep, clean_tasks):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id)
    new_task.create()

    iterator = task.Task.watch(exit_on_empty=True)
    assert new_task._id == next(iterator)._id
    iterator = task.Task.watch(exit_on_empty=True)
    assert new_task._id == next(iterator)._id

    new_task.bounce()
    iterator = task.Task.watch(exit_on_empty=True)
    assert not list(iterator)

    freeze_time.return_value += task.BOUNCE_TIMEOUT * 100
    iterator = task.Task.watch(exit_on_empty=True)
    assert new_task._id == next(iterator)._id
Exemplo n.º 13
0
def test_raise_exception_on_watch(
    task_type, failed_func, task_watch, mainloop_process_task,
    mainloop_possible_to_process, configure_model
):
    tsk = task.Task(task_type, pytest.faux.gen_uuid())
    tsk.create()

    if failed_func == "process_task":
        mainloop_process_task.side_effect = OSError
    elif failed_func == "possible_to_process":
        mainloop_possible_to_process.side_effect = OSError
    else:
        task_watch.side_effect = OSError
    task_watch.return_value = [tsk]

    with pytest.raises(OSError):
        mainloop.main()

    assert mainloop.SHUTDOWN_EVENT.is_set()
Exemplo n.º 14
0
def test_create_task_in_db(task_type, configure_model, pymongo_connection,
                           freeze_time):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id)

    assert new_task.task_type == task_type
    assert new_task._id is None
    assert new_task.time_started == 0
    assert new_task.time_created == 0
    assert new_task.time_completed == 0
    assert new_task.time_cancelled == 0
    assert new_task.time_updated == 0
    assert new_task.time_failed == 0
    assert new_task.time_bounced == 0
    assert new_task.execution_id == executor_id
    assert new_task.update_marker == ""
    assert new_task.executor_host == ""
    assert new_task.bounced == 0
    assert new_task.executor_pid == 0
    assert new_task.data == {}

    new_task.create()

    assert new_task.task_type == task_type
    assert new_task._id is not None
    assert new_task.time_started == 0
    assert new_task.time_created == int(freeze_time.return_value)
    assert new_task.time_completed == 0
    assert new_task.time_cancelled == 0
    assert new_task.time_updated == int(freeze_time.return_value)
    assert new_task.time_failed == 0
    assert new_task.time_bounced == 0
    assert new_task.execution_id == executor_id
    assert new_task.update_marker != ""
    assert new_task.executor_host == ""
    assert new_task.bounced == 0
    assert new_task.executor_pid == 0
    assert new_task.data == {}

    db_task = pymongo_connection.db.task.find_one({"_id": new_task._id})
    assert db_task
    assert task.TTL_FIELDNAME not in db_task
    assert db_task == new_task.get_state()
Exemplo n.º 15
0
def test_set_executor_data(task_type, finish_action, configure_model):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id)
    new_task.create()

    with pytest.raises(exceptions.CannotSetExecutorError):
        new_task.set_executor_data("host", 10)

    new_task.start()
    new_task.set_executor_data("host2", 20)

    assert new_task.executor_host == "host2"
    assert new_task.executor_pid == 20

    with pytest.raises(exceptions.CannotSetExecutorError):
        new_task.set_executor_data("host", 10)

    getattr(new_task, finish_action)()

    with pytest.raises(exceptions.CannotSetExecutorError):
        new_task.set_executor_data("host", 10)
Exemplo n.º 16
0
def test_create_task_for_same_exec_id(task_type, configure_model):
    executor_id = pytest.faux.gen_uuid()
    task.Task(task_type, executor_id).create()

    with pytest.raises(exceptions.UniqueConstraintViolationError):
        task.Task(task_type, executor_id).create()
Exemplo n.º 17
0
def test_create_task_with_unknown_type(task_type, configure_model):
    with pytest.raises(ValueError):
        task.Task(task_type, "1")
Exemplo n.º 18
0
def test_finish_not_started_task(task_type, finish_action, configure_model):
    executor_id = pytest.faux.gen_uuid()
    new_task = task.Task(task_type, executor_id).create()

    with pytest.raises(exceptions.DecapodError):
        getattr(new_task, finish_action)()
Exemplo n.º 19
0
def new_task(new_execution):
    created = task.Task(task.TaskType.playbook, new_execution.model_id)
    created.create()

    return created