Пример #1
0
def test_update_trigger_disable_status(starting_failure_count,
                                       starting_error_count, status,
                                       expected_reason, initialized_db):
    test_config = {
        "SUCCESSIVE_TRIGGER_FAILURE_DISABLE_THRESHOLD":
        TEST_FAIL_THRESHOLD,
        "SUCCESSIVE_TRIGGER_INTERNAL_ERROR_DISABLE_THRESHOLD":
        TEST_INTERNAL_ERROR_THRESHOLD,
    }

    trigger = model.build.list_build_triggers("devtable", "building")[0]
    trigger.successive_failure_count = starting_failure_count
    trigger.successive_internal_error_count = starting_error_count
    trigger.enabled = True
    trigger.save()

    with patch("data.model.config.app_config", test_config):
        update_trigger_disable_status(trigger, status)
        updated_trigger = RepositoryBuildTrigger.get(uuid=trigger.uuid)

        assert updated_trigger.enabled == (expected_reason is None)

        if expected_reason is not None:
            assert updated_trigger.disabled_reason.name == expected_reason
        else:
            assert updated_trigger.disabled_reason is None
            assert updated_trigger.successive_failure_count == 0
            assert updated_trigger.successive_internal_error_count == 0
Пример #2
0
def update_trigger_disable_status(trigger, final_phase):
    """
    Updates the disable status of the given build trigger.

    If the build trigger had a failure, then the counter is increased and, if we've reached the
    limit, the trigger is automatically disabled. Otherwise, if the trigger succeeded, it's counter
    is reset. This ensures that triggers that continue to error are eventually automatically
    disabled.
    """
    with db_transaction():
        try:
            trigger = RepositoryBuildTrigger.get(id=trigger.id)
        except RepositoryBuildTrigger.DoesNotExist:
            # Already deleted.
            return

        # If the build completed successfully, then reset the successive counters.
        if final_phase == BUILD_PHASE.COMPLETE:
            trigger.successive_failure_count = 0
            trigger.successive_internal_error_count = 0
            trigger.save()
            return

        # Otherwise, increment the counters and check for trigger disable.
        if final_phase == BUILD_PHASE.ERROR:
            trigger.successive_failure_count = trigger.successive_failure_count + 1
            trigger.successive_internal_error_count = 0
        elif final_phase == BUILD_PHASE.INTERNAL_ERROR:
            trigger.successive_internal_error_count = trigger.successive_internal_error_count + 1

        # Check if we need to disable the trigger.
        failure_threshold = config.app_config.get(
            "SUCCESSIVE_TRIGGER_FAILURE_DISABLE_THRESHOLD")
        error_threshold = config.app_config.get(
            "SUCCESSIVE_TRIGGER_INTERNAL_ERROR_DISABLE_THRESHOLD")

        if failure_threshold and trigger.successive_failure_count >= failure_threshold:
            toggle_build_trigger(trigger, False,
                                 TRIGGER_DISABLE_REASON.BUILD_FALURES)
        elif error_threshold and trigger.successive_internal_error_count >= error_threshold:
            toggle_build_trigger(trigger, False,
                                 TRIGGER_DISABLE_REASON.INTERNAL_ERRORS)
        else:
            # Save the trigger changes.
            trigger.save()