2023-07-13 11:22:59 +02:00
|
|
|
from unittest.mock import Mock, PropertyMock, call, patch
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
import pytest
|
2023-03-17 11:14:08 +01:00
|
|
|
import requests
|
|
|
|
|
from django.test import override_settings
|
2022-06-03 08:09:47 -06:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
2023-12-18 13:13:18 -03:00
|
|
|
from apps.alerts.models import EscalationPolicy
|
2024-11-19 15:23:15 -07:00
|
|
|
from apps.alerts.tasks import escalate_alert_group
|
2023-03-17 11:14:08 +01:00
|
|
|
from apps.alerts.tasks.check_escalation_finished import (
|
|
|
|
|
AlertGroupEscalationPolicyExecutionAuditException,
|
|
|
|
|
audit_alert_group_escalation,
|
2024-01-10 15:54:27 -03:00
|
|
|
check_alert_group_personal_notifications_task,
|
2023-03-17 11:14:08 +01:00
|
|
|
check_escalation_finished_task,
|
2023-12-18 13:13:18 -03:00
|
|
|
check_personal_notifications_task,
|
2024-11-19 15:23:15 -07:00
|
|
|
retry_audited_alert_group,
|
2023-03-17 11:14:08 +01:00
|
|
|
send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
)
|
2023-12-18 13:13:18 -03:00
|
|
|
from apps.base.models import UserNotificationPolicy, UserNotificationPolicyLogRecord
|
2024-01-17 14:46:18 -03:00
|
|
|
from apps.twilioapp.models import TwilioSMS, TwilioSMSstatuses
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
MOCKED_HEARTBEAT_URL = "https://hello.com/lsdjjkf"
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
now = timezone.now()
|
|
|
|
|
yesterday = now - timezone.timedelta(days=1)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def make_alert_group_that_started_at_specific_date(make_alert_group):
|
2023-12-07 13:03:41 -03:00
|
|
|
def _make_alert_group_that_started_at_specific_date(
|
|
|
|
|
alert_receive_channel, started_at=yesterday, received_delta=1, **kwargs
|
|
|
|
|
):
|
2023-07-13 11:22:59 +02:00
|
|
|
# we can't simply pass started_at to the fixture because started_at is being "auto-set" on the Model
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel, **kwargs)
|
2023-12-14 15:25:34 -03:00
|
|
|
if received_delta is not None:
|
|
|
|
|
alert_group.received_at = started_at - timezone.timedelta(seconds=received_delta)
|
2023-07-13 11:22:59 +02:00
|
|
|
alert_group.started_at = started_at
|
|
|
|
|
alert_group.save()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
return alert_group
|
|
|
|
|
|
|
|
|
|
return _make_alert_group_that_started_at_specific_date
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_not_called_with(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
https://stackoverflow.com/a/54838760
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
self.assert_called_with(*args, **kwargs)
|
|
|
|
|
except AssertionError:
|
|
|
|
|
return
|
|
|
|
|
raise AssertionError("Expected %s to not have been called." % self._format_mock_call_signature(args, kwargs))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Mock.assert_not_called_with = assert_not_called_with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.requests")
|
|
|
|
|
def test_send_alert_group_escalation_auditor_task_heartbeat_does_not_call_the_heartbeat_url_if_one_is_not_configured(
|
|
|
|
|
mock_requests,
|
|
|
|
|
):
|
|
|
|
|
send_alert_group_escalation_auditor_task_heartbeat()
|
|
|
|
|
mock_requests.get.assert_not_called()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.requests")
|
2023-03-17 11:14:08 +01:00
|
|
|
@override_settings(ALERT_GROUP_ESCALATION_AUDITOR_CELERY_TASK_HEARTBEAT_URL=MOCKED_HEARTBEAT_URL)
|
2023-07-13 11:22:59 +02:00
|
|
|
def test_send_alert_group_escalation_auditor_task_heartbeat_calls_the_heartbeat_url_if_one_is_configured(mock_requests):
|
|
|
|
|
send_alert_group_escalation_auditor_task_heartbeat()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mock_requests.get.assert_called_once_with(MOCKED_HEARTBEAT_URL)
|
|
|
|
|
mock_requests.get.return_value.raise_for_status.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.requests")
|
2023-03-17 11:14:08 +01:00
|
|
|
@override_settings(ALERT_GROUP_ESCALATION_AUDITOR_CELERY_TASK_HEARTBEAT_URL=MOCKED_HEARTBEAT_URL)
|
2023-07-13 11:22:59 +02:00
|
|
|
def test_send_alert_group_escalation_auditor_task_heartbeat_raises_an_exception_if_the_heartbeat_url_request_fails(
|
|
|
|
|
mock_requests,
|
|
|
|
|
):
|
|
|
|
|
mock_response = Mock()
|
|
|
|
|
mock_response.status_code = 500
|
|
|
|
|
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mock_requests.get.return_value = mock_response
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
with pytest.raises(requests.exceptions.HTTPError):
|
|
|
|
|
send_alert_group_escalation_auditor_task_heartbeat()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mock_requests.get.assert_called_once_with(MOCKED_HEARTBEAT_URL)
|
|
|
|
|
mock_requests.get.return_value.raise_for_status.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-17 16:04:53 +02:00
|
|
|
@pytest.mark.django_db
|
2023-12-18 13:28:55 +01:00
|
|
|
def test_audit_alert_group_escalation_skips_validation_if_the_alert_group_does_not_have_an_escalation_chain_snapshot(
|
2023-07-17 16:04:53 +02:00
|
|
|
make_organization_and_user,
|
|
|
|
|
make_alert_receive_channel,
|
|
|
|
|
make_alert_group,
|
|
|
|
|
):
|
|
|
|
|
organization, _ = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
alert_group = make_alert_group(alert_receive_channel)
|
|
|
|
|
|
2023-12-18 13:28:55 +01:00
|
|
|
alert_group.raw_escalation_snapshot = {"escalation_chain_snapshot": None}
|
2023-07-17 16:04:53 +02:00
|
|
|
alert_group.save()
|
|
|
|
|
|
2023-12-18 13:28:55 +01:00
|
|
|
assert alert_group.raw_escalation_snapshot["escalation_chain_snapshot"] is None
|
2023-07-17 16:04:53 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
audit_alert_group_escalation(alert_group)
|
|
|
|
|
except AlertGroupEscalationPolicyExecutionAuditException:
|
|
|
|
|
pytest.fail()
|
|
|
|
|
|
|
|
|
|
|
2023-03-17 11:14:08 +01:00
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_audit_alert_group_escalation_raises_exception_if_the_alert_group_does_not_have_an_escalation_snapshot(
|
|
|
|
|
escalation_snapshot_test_setup,
|
|
|
|
|
):
|
|
|
|
|
alert_group, _, _, _ = escalation_snapshot_test_setup
|
2023-12-18 13:28:55 +01:00
|
|
|
alert_group.raw_escalation_snapshot = None
|
|
|
|
|
alert_group.save()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
with pytest.raises(AlertGroupEscalationPolicyExecutionAuditException):
|
|
|
|
|
audit_alert_group_escalation(alert_group)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_audit_alert_group_escalation_skips_further_validation_if_the_escalation_policies_snapshots_is_empty(
|
|
|
|
|
escalation_snapshot_test_setup,
|
|
|
|
|
):
|
|
|
|
|
alert_group, _, _, _ = escalation_snapshot_test_setup
|
|
|
|
|
|
|
|
|
|
alert_group.escalation_snapshot.escalation_policies_snapshots = []
|
2023-12-18 13:28:55 +01:00
|
|
|
alert_group.raw_escalation_snapshot = {"escalation_policies_snapshots": []}
|
|
|
|
|
alert_group.save()
|
2023-03-17 11:14:08 +01:00
|
|
|
audit_alert_group_escalation(alert_group)
|
|
|
|
|
|
2023-12-18 13:28:55 +01:00
|
|
|
alert_group.raw_escalation_snapshot["escalation_policies_snapshots"] = None
|
|
|
|
|
alert_group.save()
|
2023-03-17 11:14:08 +01:00
|
|
|
audit_alert_group_escalation(alert_group)
|
|
|
|
|
|
|
|
|
|
|
2023-12-18 13:28:55 +01:00
|
|
|
@patch("apps.alerts.escalation_snapshot.escalation_snapshot_mixin.EscalationSnapshotMixin.next_step_eta_is_valid")
|
2023-03-17 11:14:08 +01:00
|
|
|
@pytest.mark.django_db
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"next_step_eta_is_valid_return_value,raises_exception",
|
|
|
|
|
[
|
|
|
|
|
(None, False),
|
|
|
|
|
(True, False),
|
|
|
|
|
(False, True),
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
def test_audit_alert_group_escalation_next_step_eta_validation(
|
2023-07-13 11:22:59 +02:00
|
|
|
mock_next_step_eta_is_valid, escalation_snapshot_test_setup, next_step_eta_is_valid_return_value, raises_exception
|
2023-03-17 11:14:08 +01:00
|
|
|
):
|
2023-07-13 11:22:59 +02:00
|
|
|
mock_next_step_eta_is_valid.return_value = next_step_eta_is_valid_return_value
|
2023-03-17 11:14:08 +01:00
|
|
|
alert_group, _, _, _ = escalation_snapshot_test_setup
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
if raises_exception:
|
|
|
|
|
with pytest.raises(AlertGroupEscalationPolicyExecutionAuditException):
|
|
|
|
|
audit_alert_group_escalation(alert_group)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
audit_alert_group_escalation(alert_group)
|
|
|
|
|
except AlertGroupEscalationPolicyExecutionAuditException:
|
|
|
|
|
pytest.fail()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mock_next_step_eta_is_valid.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch(
|
2023-12-18 13:28:55 +01:00
|
|
|
"apps.alerts.escalation_snapshot.escalation_snapshot_mixin.EscalationSnapshotMixin.last_active_escalation_policy_order",
|
2023-07-13 11:22:59 +02:00
|
|
|
new_callable=PropertyMock,
|
|
|
|
|
)
|
2023-03-17 11:14:08 +01:00
|
|
|
@pytest.mark.django_db
|
2023-07-13 11:22:59 +02:00
|
|
|
def test_audit_alert_group_escalation_no_executed_escalation_policy_snapshots(
|
2023-12-18 13:28:55 +01:00
|
|
|
mock_last_active_escalation_policy_order, escalation_snapshot_test_setup
|
2023-07-13 11:22:59 +02:00
|
|
|
):
|
2023-03-17 11:14:08 +01:00
|
|
|
alert_group, _, _, _ = escalation_snapshot_test_setup
|
|
|
|
|
|
2023-12-18 13:28:55 +01:00
|
|
|
mock_last_active_escalation_policy_order.return_value = None
|
2023-07-13 11:22:59 +02:00
|
|
|
audit_alert_group_escalation(alert_group)
|
2023-12-18 13:28:55 +01:00
|
|
|
mock_last_active_escalation_policy_order.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# # see TODO: comment in engine/apps/alerts/tasks/check_escalation_finished.py
|
|
|
|
|
# @pytest.mark.django_db
|
|
|
|
|
# def test_audit_alert_group_escalation_all_executed_escalation_policy_snapshots_have_triggered_log_records(
|
|
|
|
|
# escalation_snapshot_test_setup,
|
|
|
|
|
# make_organization_and_user,
|
|
|
|
|
# make_alert_group_log_record,
|
|
|
|
|
# ):
|
|
|
|
|
# _, user = make_organization_and_user()
|
|
|
|
|
# alert_group, _, _, _ = escalation_snapshot_test_setup
|
|
|
|
|
# escalation_policies_snapshots = alert_group.escalation_snapshot.escalation_policies_snapshots
|
|
|
|
|
|
|
|
|
|
# for escalation_policy_snapshot in escalation_policies_snapshots:
|
|
|
|
|
# escalation_policy = EscalationPolicy.objects.get(id=escalation_policy_snapshot.id)
|
|
|
|
|
# log_record_type = _get_relevant_log_record_type()
|
|
|
|
|
|
|
|
|
|
# make_alert_group_log_record(alert_group, log_record_type, user, escalation_policy=escalation_policy)
|
|
|
|
|
|
|
|
|
|
# with patch(
|
|
|
|
|
# "apps.alerts.escalation_snapshot.snapshot_classes.escalation_snapshot.EscalationSnapshot.executed_escalation_policy_snapshots",
|
|
|
|
|
# new_callable=PropertyMock,
|
|
|
|
|
# ) as mock_executed_escalation_policy_snapshots:
|
|
|
|
|
# mock_executed_escalation_policy_snapshots.return_value = escalation_policies_snapshots
|
|
|
|
|
# audit_alert_group_escalation(alert_group)
|
|
|
|
|
# mock_executed_escalation_policy_snapshots.assert_called_once_with()
|
|
|
|
|
|
|
|
|
|
# see TODO: comment in engine/apps/alerts/tasks/check_escalation_finished.py
|
|
|
|
|
# @pytest.mark.django_db
|
|
|
|
|
# def test_audit_alert_group_escalation_one_executed_escalation_policy_snapshot_does_not_have_a_triggered_log_record(
|
|
|
|
|
# escalation_snapshot_test_setup,
|
|
|
|
|
# make_organization_and_user,
|
|
|
|
|
# make_alert_group_log_record,
|
|
|
|
|
# ):
|
|
|
|
|
# _, user = make_organization_and_user()
|
|
|
|
|
# alert_group, _, _, _ = escalation_snapshot_test_setup
|
|
|
|
|
# escalation_policies_snapshots = alert_group.escalation_snapshot.escalation_policies_snapshots
|
|
|
|
|
|
|
|
|
|
# # let's skip creating a relevant alert group log record for the first executed escalation policy
|
|
|
|
|
# for idx, escalation_policy_snapshot in enumerate(escalation_policies_snapshots):
|
|
|
|
|
# if idx != 0:
|
|
|
|
|
# escalation_policy = EscalationPolicy.objects.get(id=escalation_policy_snapshot.id)
|
|
|
|
|
# make_alert_group_log_record(
|
|
|
|
|
# alert_group, _get_relevant_log_record_type(), user, escalation_policy=escalation_policy
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
# with patch(
|
|
|
|
|
# "apps.alerts.escalation_snapshot.snapshot_classes.escalation_snapshot.EscalationSnapshot.executed_escalation_policy_snapshots",
|
|
|
|
|
# new_callable=PropertyMock,
|
|
|
|
|
# ) as mock_executed_escalation_policy_snapshots:
|
|
|
|
|
# mock_executed_escalation_policy_snapshots.return_value = escalation_policies_snapshots
|
|
|
|
|
|
|
|
|
|
# with pytest.raises(AlertGroupEscalationPolicyExecutionAuditException):
|
|
|
|
|
# audit_alert_group_escalation(alert_group)
|
|
|
|
|
# mock_executed_escalation_policy_snapshots.assert_called_once_with()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.audit_alert_group_escalation")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
2022-06-03 08:09:47 -06:00
|
|
|
@pytest.mark.django_db
|
2023-03-17 11:14:08 +01:00
|
|
|
def test_check_escalation_finished_task_queries_doesnt_grab_alert_groups_outside_of_date_range(
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
mocked_audit_alert_group_escalation,
|
2022-06-03 08:09:47 -06:00
|
|
|
make_organization_and_user,
|
|
|
|
|
make_alert_receive_channel,
|
2023-07-13 11:22:59 +02:00
|
|
|
make_alert_group_that_started_at_specific_date,
|
2022-06-03 08:09:47 -06:00
|
|
|
):
|
2023-03-17 11:14:08 +01:00
|
|
|
organization, _ = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel)
|
|
|
|
|
make_alert_group_that_started_at_specific_date(alert_receive_channel, now - timezone.timedelta(days=5))
|
|
|
|
|
make_alert_group_that_started_at_specific_date(alert_receive_channel, now + timezone.timedelta(days=5))
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
check_escalation_finished_task()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_called_once_with(alert_group1)
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.audit_alert_group_escalation")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_check_escalation_finished_task_calls_audit_alert_group_escalation_for_every_alert_group(
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
mocked_audit_alert_group_escalation,
|
|
|
|
|
make_organization_and_user,
|
|
|
|
|
make_alert_receive_channel,
|
|
|
|
|
make_alert_group_that_started_at_specific_date,
|
|
|
|
|
):
|
|
|
|
|
organization, _ = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
|
|
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel)
|
|
|
|
|
alert_group2 = make_alert_group_that_started_at_specific_date(alert_receive_channel)
|
|
|
|
|
alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
check_escalation_finished_task()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_any_call(alert_group1)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_any_call(alert_group2)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_any_call(alert_group3)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.audit_alert_group_escalation")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
2023-03-17 11:14:08 +01:00
|
|
|
@pytest.mark.django_db
|
2023-07-13 11:22:59 +02:00
|
|
|
def test_check_escalation_finished_task_filters_the_right_alert_groups(
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
mocked_audit_alert_group_escalation,
|
2023-03-17 11:14:08 +01:00
|
|
|
make_organization_and_user,
|
|
|
|
|
make_alert_receive_channel,
|
2023-07-13 11:22:59 +02:00
|
|
|
make_alert_group_that_started_at_specific_date,
|
2023-03-17 11:14:08 +01:00
|
|
|
):
|
|
|
|
|
organization, _ = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel)
|
|
|
|
|
|
|
|
|
|
silenced_for_one_hour_alert_group = make_alert_group_that_started_at_specific_date(
|
|
|
|
|
alert_receive_channel, silenced=True, silenced_until=(now + timezone.timedelta(hours=1))
|
|
|
|
|
)
|
|
|
|
|
silenced_forever = make_alert_group_that_started_at_specific_date(
|
|
|
|
|
alert_receive_channel, silenced=True, silenced_until=None
|
|
|
|
|
)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
in_maintenance = make_alert_group_that_started_at_specific_date(alert_receive_channel, maintenance_uuid="asdfasdf")
|
|
|
|
|
escalation_finished = make_alert_group_that_started_at_specific_date(
|
|
|
|
|
alert_receive_channel, is_escalation_finished=True
|
|
|
|
|
)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
resolved = make_alert_group_that_started_at_specific_date(alert_receive_channel, is_escalation_finished=True)
|
|
|
|
|
acknowledged = make_alert_group_that_started_at_specific_date(alert_receive_channel, is_escalation_finished=True)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
root_alert_group = make_alert_group_that_started_at_specific_date(
|
|
|
|
|
alert_receive_channel, root_alert_group=alert_group1
|
|
|
|
|
)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
check_escalation_finished_task()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_has_calls(
|
|
|
|
|
[
|
|
|
|
|
call(alert_group1),
|
|
|
|
|
call(silenced_for_one_hour_alert_group),
|
|
|
|
|
],
|
|
|
|
|
any_order=True,
|
|
|
|
|
)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_not_called_with(in_maintenance)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_not_called_with(escalation_finished)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_not_called_with(silenced_forever)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_not_called_with(resolved)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_not_called_with(acknowledged)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_not_called_with(root_alert_group)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.audit_alert_group_escalation")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
2023-03-17 11:14:08 +01:00
|
|
|
@pytest.mark.django_db
|
2023-07-13 11:22:59 +02:00
|
|
|
def test_check_escalation_finished_task_simply_calls_heartbeat_when_no_alert_groups_found(
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
mocked_audit_alert_group_escalation,
|
|
|
|
|
):
|
|
|
|
|
check_escalation_finished_task()
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_not_called()
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat.assert_called_once_with()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
|
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.audit_alert_group_escalation")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
2023-03-17 11:14:08 +01:00
|
|
|
@pytest.mark.django_db
|
2023-11-10 16:39:13 +08:00
|
|
|
def test_check_escalation_finished_task_calls_audit_alert_group_escalation_for_every_alert_group_even_if_one_fails_and_returns_success_ratio(
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
mocked_audit_alert_group_escalation,
|
2023-03-17 11:14:08 +01:00
|
|
|
make_organization_and_user,
|
|
|
|
|
make_alert_receive_channel,
|
2023-07-13 11:22:59 +02:00
|
|
|
make_alert_group_that_started_at_specific_date,
|
2023-11-10 16:39:13 +08:00
|
|
|
caplog,
|
2023-03-17 11:14:08 +01:00
|
|
|
):
|
|
|
|
|
organization, _ = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
|
2023-12-07 13:03:41 -03:00
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=1)
|
|
|
|
|
alert_group2 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=5)
|
|
|
|
|
alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=12)
|
2023-12-14 15:25:34 -03:00
|
|
|
alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=None)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
def _mocked_audit_alert_group_escalation(alert_group):
|
|
|
|
|
if not alert_group.id == alert_group3.id:
|
|
|
|
|
raise AlertGroupEscalationPolicyExecutionAuditException("asdfasdf")
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.side_effect = _mocked_audit_alert_group_escalation
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
with pytest.raises(AlertGroupEscalationPolicyExecutionAuditException) as exc:
|
|
|
|
|
check_escalation_finished_task()
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-08-28 14:13:01 +02:00
|
|
|
error_msg = str(exc.value)
|
|
|
|
|
|
|
|
|
|
assert "The following alert group id(s) failed auditing:" in error_msg
|
|
|
|
|
assert str(alert_group1.id) in error_msg
|
|
|
|
|
assert str(alert_group2.id) in error_msg
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-12-07 13:03:41 -03:00
|
|
|
assert "Alert group ingestion/creation avg delta seconds: 6" in caplog.text
|
|
|
|
|
assert "Alert group ingestion/creation max delta seconds: 12" in caplog.text
|
2023-12-14 15:25:34 -03:00
|
|
|
assert "Alert group notifications success ratio: 25.00" in caplog.text
|
2023-11-10 16:39:13 +08:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_audit_alert_group_escalation.assert_any_call(alert_group1)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_any_call(alert_group2)
|
|
|
|
|
mocked_audit_alert_group_escalation.assert_any_call(alert_group3)
|
2023-03-17 11:14:08 +01:00
|
|
|
|
2023-07-13 11:22:59 +02:00
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat.assert_not_called()
|
2023-12-18 13:13:18 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_check_escalation_finished_task_calls_audit_alert_group_personal_notifications(
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
make_organization_and_user,
|
2024-04-23 08:40:24 -03:00
|
|
|
make_user_for_organization,
|
2023-12-18 13:13:18 -03:00
|
|
|
make_user_notification_policy,
|
|
|
|
|
make_escalation_chain,
|
|
|
|
|
make_escalation_policy,
|
|
|
|
|
make_channel_filter,
|
|
|
|
|
make_alert_receive_channel,
|
|
|
|
|
make_alert_group_that_started_at_specific_date,
|
|
|
|
|
make_user_notification_policy_log_record,
|
2024-01-17 14:46:18 -03:00
|
|
|
make_sms_record,
|
2023-12-18 13:13:18 -03:00
|
|
|
caplog,
|
|
|
|
|
):
|
|
|
|
|
organization, user = make_organization_and_user()
|
|
|
|
|
user_notification_policy = make_user_notification_policy(
|
|
|
|
|
user=user,
|
|
|
|
|
step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
notify_by=UserNotificationPolicy.NotificationChannel.SLACK,
|
|
|
|
|
)
|
2024-04-23 08:40:24 -03:00
|
|
|
user2 = make_user_for_organization(organization)
|
|
|
|
|
user_notification_policy2 = make_user_notification_policy(
|
|
|
|
|
user=user2,
|
|
|
|
|
step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
notify_by=UserNotificationPolicy.NotificationChannel.PHONE_CALL,
|
|
|
|
|
)
|
|
|
|
|
# the previous one will be deleted later, we need to have an extra one (policy cannot be empty)
|
|
|
|
|
make_user_notification_policy(
|
|
|
|
|
user=user2,
|
|
|
|
|
step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
notify_by=UserNotificationPolicy.NotificationChannel.SLACK,
|
|
|
|
|
)
|
2023-12-18 13:13:18 -03:00
|
|
|
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
escalation_chain = make_escalation_chain(organization)
|
|
|
|
|
channel_filter = make_channel_filter(alert_receive_channel, escalation_chain=escalation_chain)
|
|
|
|
|
notify_to_multiple_users_step = make_escalation_policy(
|
|
|
|
|
escalation_chain=channel_filter.escalation_chain,
|
|
|
|
|
escalation_policy_step=EscalationPolicy.STEP_NOTIFY_MULTIPLE_USERS,
|
|
|
|
|
)
|
|
|
|
|
notify_to_multiple_users_step.notify_to_users_queue.set([user])
|
|
|
|
|
|
|
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
|
|
|
|
alert_group2 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
|
|
|
|
alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
|
|
|
|
alert_group4 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
2024-04-23 08:40:24 -03:00
|
|
|
alert_group5 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
|
|
|
|
alert_groups = [alert_group1, alert_group2, alert_group3, alert_group4, alert_group5]
|
2023-12-18 13:13:18 -03:00
|
|
|
for alert_group in alert_groups:
|
|
|
|
|
alert_group.raw_escalation_snapshot = alert_group.build_raw_escalation_snapshot()
|
|
|
|
|
alert_group.raw_escalation_snapshot["last_active_escalation_policy_order"] = 1
|
|
|
|
|
alert_group.save()
|
|
|
|
|
|
|
|
|
|
now = timezone.now()
|
|
|
|
|
# alert_group1: wait, notify user, notification successful
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group1,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.WAIT,
|
|
|
|
|
)
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group1,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
)
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group1,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_SUCCESS,
|
|
|
|
|
)
|
|
|
|
|
# records created > 5 mins ago
|
|
|
|
|
alert_group1.personal_log_records.update(created_at=now - timezone.timedelta(minutes=7))
|
|
|
|
|
|
2024-02-01 16:42:43 +01:00
|
|
|
# alert_group2: notify user, notification failed; triggered 2 sms, sent and accepted statuses
|
2023-12-18 13:13:18 -03:00
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group2,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
)
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group2,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_FAILED,
|
|
|
|
|
)
|
2024-01-17 14:46:18 -03:00
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group2,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
)
|
2024-02-01 16:42:43 +01:00
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group2,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
)
|
2024-01-17 14:46:18 -03:00
|
|
|
# no failed or succeed record, but SMS was sent (without Twilio delivered confirmation yet)
|
|
|
|
|
sms_record = make_sms_record(
|
|
|
|
|
receiver=user,
|
|
|
|
|
represents_alert_group=alert_group2,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
)
|
|
|
|
|
sent_sms = TwilioSMS.objects.create(sid="someid", sms_record=sms_record, status=TwilioSMSstatuses.SENT)
|
2024-02-01 16:42:43 +01:00
|
|
|
# no failed or succeed record, but SMS has status ACCEPTED from Twilio (without Twilio delivered confirmation yet)
|
|
|
|
|
sms_record2 = make_sms_record(
|
|
|
|
|
receiver=user,
|
|
|
|
|
represents_alert_group=alert_group2,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
)
|
|
|
|
|
accepted_sms = TwilioSMS.objects.create(sid="someid2", sms_record=sms_record2, status=TwilioSMSstatuses.ACCEPTED)
|
2023-12-18 13:13:18 -03:00
|
|
|
# records created > 5 mins ago
|
|
|
|
|
alert_group2.personal_log_records.update(created_at=now - timezone.timedelta(minutes=7))
|
2024-01-17 14:46:18 -03:00
|
|
|
sent_sms.created_at = now - timezone.timedelta(minutes=6)
|
|
|
|
|
sent_sms.save()
|
2024-02-01 16:42:43 +01:00
|
|
|
accepted_sms.created_at = now - timezone.timedelta(minutes=6)
|
|
|
|
|
accepted_sms.save()
|
2023-12-18 13:13:18 -03:00
|
|
|
|
|
|
|
|
# alert_group3: notify user, missing completion
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
alert_group=alert_group3,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
)
|
|
|
|
|
# record created > 5 mins ago
|
|
|
|
|
alert_group3.personal_log_records.update(created_at=now - timezone.timedelta(minutes=7))
|
|
|
|
|
|
|
|
|
|
# alert_group4: notify user created > 5 mins ago, missing completion
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user,
|
|
|
|
|
created_at=now - timezone.timedelta(minutes=3),
|
|
|
|
|
alert_group=alert_group3,
|
|
|
|
|
notification_policy=user_notification_policy,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
)
|
|
|
|
|
# record created < 5 mins ago
|
|
|
|
|
alert_group4.personal_log_records.update(created_at=now - timezone.timedelta(minutes=2))
|
|
|
|
|
|
2024-04-23 08:40:24 -03:00
|
|
|
# alert_group5: notification triggered but policy is deleted before completion (should be ignored)
|
|
|
|
|
make_user_notification_policy_log_record(
|
|
|
|
|
author=user2,
|
|
|
|
|
alert_group=alert_group5,
|
|
|
|
|
notification_policy=user_notification_policy2,
|
|
|
|
|
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_TRIGGERED,
|
|
|
|
|
notification_step=UserNotificationPolicy.Step.WAIT,
|
|
|
|
|
)
|
|
|
|
|
user_notification_policy2.delete()
|
|
|
|
|
|
2023-12-18 13:13:18 -03:00
|
|
|
# trigger task
|
2024-01-10 15:54:27 -03:00
|
|
|
with patch(
|
|
|
|
|
"apps.alerts.tasks.check_escalation_finished.check_alert_group_personal_notifications_task"
|
|
|
|
|
) as mock_check_notif:
|
2023-12-18 13:13:18 -03:00
|
|
|
check_escalation_finished_task()
|
|
|
|
|
|
|
|
|
|
for alert_group in alert_groups:
|
|
|
|
|
mock_check_notif.apply_async.assert_any_call((alert_group.id,))
|
2024-01-10 15:54:27 -03:00
|
|
|
check_alert_group_personal_notifications_task(alert_group.id)
|
2023-12-18 13:13:18 -03:00
|
|
|
if alert_group == alert_group3:
|
|
|
|
|
assert f"Alert group {alert_group3.id} has (1) uncompleted personal notifications" in caplog.text
|
|
|
|
|
else:
|
|
|
|
|
assert f"Alert group {alert_group.id} personal notifications check passed" in caplog.text
|
|
|
|
|
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat.assert_called()
|
2024-01-10 15:54:27 -03:00
|
|
|
|
|
|
|
|
# also trigger the general personal notification checker
|
|
|
|
|
check_personal_notifications_task()
|
|
|
|
|
|
2024-02-01 16:42:43 +01:00
|
|
|
assert "personal_notifications_triggered=6 personal_notifications_completed=2" in caplog.text
|
2024-11-19 15:23:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.audit_alert_group_escalation")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.retry_audited_alert_group")
|
|
|
|
|
@patch("apps.alerts.tasks.check_escalation_finished.send_alert_group_escalation_auditor_task_heartbeat")
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_invoke_retry_from_check_escalation_finished_task(
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat,
|
|
|
|
|
mocked_retry_audited_alert_group,
|
|
|
|
|
mocked_audit_alert_group_escalation,
|
|
|
|
|
make_organization_and_user,
|
|
|
|
|
make_alert_receive_channel,
|
|
|
|
|
make_alert_group_that_started_at_specific_date,
|
|
|
|
|
):
|
|
|
|
|
organization, _ = make_organization_and_user()
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
|
|
|
|
|
# Pass audit (should not be counted in final message or go to retry function)
|
|
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=1)
|
|
|
|
|
# Fail audit but not retrying (should be counted in final message)
|
|
|
|
|
alert_group2 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=5)
|
|
|
|
|
# Fail audit but retry (should not be counted in final message)
|
|
|
|
|
alert_group3 = make_alert_group_that_started_at_specific_date(alert_receive_channel, received_delta=10)
|
|
|
|
|
|
|
|
|
|
def _mocked_audit_alert_group_escalation(alert_group):
|
|
|
|
|
if alert_group.id == alert_group2.id or alert_group.id == alert_group3.id:
|
|
|
|
|
raise AlertGroupEscalationPolicyExecutionAuditException(f"{alert_group2.id} failed audit")
|
|
|
|
|
|
|
|
|
|
mocked_audit_alert_group_escalation.side_effect = _mocked_audit_alert_group_escalation
|
|
|
|
|
|
|
|
|
|
def _mocked_retry_audited_alert_group(alert_group):
|
|
|
|
|
if alert_group.id == alert_group2.id:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
mocked_retry_audited_alert_group.side_effect = _mocked_retry_audited_alert_group
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AlertGroupEscalationPolicyExecutionAuditException) as exc:
|
|
|
|
|
check_escalation_finished_task()
|
|
|
|
|
|
|
|
|
|
error_msg = str(exc.value)
|
|
|
|
|
|
|
|
|
|
assert "The following alert group id(s) failed auditing:" in error_msg
|
|
|
|
|
assert str(alert_group1.id) not in error_msg
|
|
|
|
|
assert str(alert_group2.id) in error_msg
|
|
|
|
|
assert str(alert_group3.id) not in error_msg
|
|
|
|
|
|
|
|
|
|
assert mocked_retry_audited_alert_group.call_count == 2
|
|
|
|
|
mocked_send_alert_group_escalation_auditor_task_heartbeat.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@patch.object(escalate_alert_group, "apply_async")
|
|
|
|
|
@override_settings(AUDITED_ALERT_GROUP_MAX_RETRIES=1)
|
|
|
|
|
@pytest.mark.django_db
|
|
|
|
|
def test_retry_audited_alert_group(
|
|
|
|
|
mocked_escalate_alert_group,
|
|
|
|
|
make_organization_and_user,
|
|
|
|
|
make_user_for_organization,
|
|
|
|
|
make_user_notification_policy,
|
|
|
|
|
make_escalation_chain,
|
|
|
|
|
make_escalation_policy,
|
|
|
|
|
make_channel_filter,
|
|
|
|
|
make_alert_receive_channel,
|
|
|
|
|
make_alert_group_that_started_at_specific_date,
|
|
|
|
|
):
|
|
|
|
|
organization, user = make_organization_and_user()
|
|
|
|
|
make_user_notification_policy(
|
|
|
|
|
user=user,
|
|
|
|
|
step=UserNotificationPolicy.Step.NOTIFY,
|
|
|
|
|
notify_by=UserNotificationPolicy.NotificationChannel.SLACK,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
alert_receive_channel = make_alert_receive_channel(organization)
|
|
|
|
|
escalation_chain = make_escalation_chain(organization)
|
|
|
|
|
channel_filter = make_channel_filter(alert_receive_channel, escalation_chain=escalation_chain)
|
|
|
|
|
notify_to_multiple_users_step = make_escalation_policy(
|
|
|
|
|
escalation_chain=channel_filter.escalation_chain,
|
|
|
|
|
escalation_policy_step=EscalationPolicy.STEP_NOTIFY_MULTIPLE_USERS,
|
|
|
|
|
)
|
|
|
|
|
notify_to_multiple_users_step.notify_to_users_queue.set([user])
|
|
|
|
|
|
|
|
|
|
alert_group1 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
|
|
|
|
alert_group1.raw_escalation_snapshot = alert_group1.build_raw_escalation_snapshot()
|
|
|
|
|
alert_group1.raw_escalation_snapshot["last_active_escalation_policy_order"] = 1
|
|
|
|
|
alert_group1.save()
|
|
|
|
|
|
|
|
|
|
# Retry should occur
|
|
|
|
|
is_retrying = retry_audited_alert_group(alert_group1)
|
|
|
|
|
assert is_retrying
|
|
|
|
|
mocked_escalate_alert_group.assert_called()
|
|
|
|
|
mocked_escalate_alert_group.reset_mock()
|
|
|
|
|
|
|
|
|
|
# No retry as attempts == max
|
|
|
|
|
is_retrying = retry_audited_alert_group(alert_group1)
|
|
|
|
|
assert not is_retrying
|
|
|
|
|
mocked_escalate_alert_group.assert_not_called()
|
|
|
|
|
mocked_escalate_alert_group.reset_mock()
|
|
|
|
|
|
|
|
|
|
alert_group2 = make_alert_group_that_started_at_specific_date(alert_receive_channel, channel_filter=channel_filter)
|
|
|
|
|
# No retry because no escalation snapshot
|
|
|
|
|
is_retrying = retry_audited_alert_group(alert_group2)
|
|
|
|
|
assert not is_retrying
|
|
|
|
|
mocked_escalate_alert_group.assert_not_called()
|
|
|
|
|
mocked_escalate_alert_group.reset_mock()
|
|
|
|
|
|
|
|
|
|
alert_group3 = make_alert_group_that_started_at_specific_date(
|
|
|
|
|
alert_receive_channel,
|
|
|
|
|
channel_filter=channel_filter,
|
|
|
|
|
silenced=True,
|
|
|
|
|
silenced_at=timezone.now(),
|
|
|
|
|
silenced_by_user=user,
|
|
|
|
|
silenced_until=(now + timezone.timedelta(hours=1)),
|
|
|
|
|
)
|
|
|
|
|
alert_group3.raw_escalation_snapshot = alert_group1.build_raw_escalation_snapshot()
|
|
|
|
|
alert_group3.raw_escalation_snapshot["last_active_escalation_policy_order"] = 1
|
|
|
|
|
alert_group3.save()
|
|
|
|
|
|
|
|
|
|
# No retry because alert group silenced
|
|
|
|
|
is_retrying = retry_audited_alert_group(alert_group3)
|
|
|
|
|
assert not is_retrying
|
|
|
|
|
mocked_escalate_alert_group.assert_not_called()
|