|
| 1 | +from sentry.models.group import GroupStatus |
| 2 | +from sentry.models.groupopenperiod import GroupOpenPeriod |
| 3 | +from sentry.types.group import PriorityLevel |
| 4 | +from sentry.users.services.user.service import user_service |
| 5 | +from sentry.workflow_engine.migration_helpers.alert_rule import ( |
| 6 | + migrate_alert_rule, |
| 7 | + migrate_metric_data_conditions, |
| 8 | +) |
| 9 | +from sentry.workflow_engine.models.data_condition import Condition |
| 10 | +from sentry.workflow_engine.types import DetectorPriorityLevel, WorkflowEventData |
| 11 | +from tests.sentry.workflow_engine.handlers.condition.test_base import ConditionTestCase |
| 12 | + |
| 13 | + |
| 14 | +class TestIssuePriorityGreaterOrEqualCondition(ConditionTestCase): |
| 15 | + condition = Condition.ISSUE_PRIORITY_DEESCALATING |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + super().setUp() |
| 19 | + self.event_data = WorkflowEventData(event=self.group_event) |
| 20 | + self.metric_alert = self.create_alert_rule() |
| 21 | + self.alert_rule_trigger_warning = self.create_alert_rule_trigger( |
| 22 | + alert_rule=self.metric_alert, label="warning" |
| 23 | + ) |
| 24 | + self.alert_rule_trigger_critical = self.create_alert_rule_trigger( |
| 25 | + alert_rule=self.metric_alert, label="critical" |
| 26 | + ) |
| 27 | + self.rpc_user = user_service.get_user(user_id=self.user.id) |
| 28 | + migrate_alert_rule(self.metric_alert, self.rpc_user) |
| 29 | + |
| 30 | + data_condition_warning_tuple = migrate_metric_data_conditions( |
| 31 | + self.alert_rule_trigger_warning |
| 32 | + ) |
| 33 | + data_condition_critical_tuple = migrate_metric_data_conditions( |
| 34 | + self.alert_rule_trigger_critical |
| 35 | + ) |
| 36 | + |
| 37 | + assert data_condition_warning_tuple is not None |
| 38 | + assert data_condition_critical_tuple is not None |
| 39 | + dc_warning = data_condition_warning_tuple[1] |
| 40 | + dc_critical = data_condition_critical_tuple[1] |
| 41 | + |
| 42 | + self.deescalating_dc_warning = self.create_data_condition( |
| 43 | + comparison={"priority": DetectorPriorityLevel.MEDIUM.value}, |
| 44 | + type=self.condition, |
| 45 | + condition_result=True, |
| 46 | + condition_group=dc_warning.condition_group, |
| 47 | + ) |
| 48 | + |
| 49 | + self.deescalating_dc_critical = self.create_data_condition( |
| 50 | + comparison={"priority": DetectorPriorityLevel.HIGH.value}, |
| 51 | + type=self.condition, |
| 52 | + condition_result=True, |
| 53 | + condition_group=dc_critical.condition_group, |
| 54 | + ) |
| 55 | + |
| 56 | + def update_group_and_open_period(self, priority: PriorityLevel) -> None: |
| 57 | + self.group.update(priority=priority) |
| 58 | + open_period = ( |
| 59 | + GroupOpenPeriod.objects.filter(group=self.group).order_by("-date_started").first() |
| 60 | + ) |
| 61 | + assert open_period is not None |
| 62 | + highest_seen_priority = open_period.data.get("highest_seen_priority", priority) |
| 63 | + open_period.data["highest_seen_priority"] = max(highest_seen_priority, priority) |
| 64 | + open_period.save() |
| 65 | + |
| 66 | + def test_warning(self): |
| 67 | + self.update_group_and_open_period(priority=PriorityLevel.MEDIUM) |
| 68 | + self.assert_does_not_pass(self.deescalating_dc_warning, self.event_data) |
| 69 | + |
| 70 | + self.update_group_and_open_period(priority=PriorityLevel.HIGH) |
| 71 | + self.assert_does_not_pass(self.deescalating_dc_warning, self.event_data) |
| 72 | + |
| 73 | + self.group.update(status=GroupStatus.RESOLVED) |
| 74 | + self.assert_passes(self.deescalating_dc_warning, self.event_data) |
| 75 | + |
| 76 | + def test_critical_threshold_not_breached(self): |
| 77 | + self.update_group_and_open_period(priority=PriorityLevel.MEDIUM) |
| 78 | + self.assert_does_not_pass(self.deescalating_dc_critical, self.event_data) |
| 79 | + |
| 80 | + self.group.update(status=GroupStatus.RESOLVED) |
| 81 | + self.assert_does_not_pass(self.deescalating_dc_critical, self.event_data) |
| 82 | + |
| 83 | + def test_critical(self): |
| 84 | + self.update_group_and_open_period(priority=PriorityLevel.MEDIUM) |
| 85 | + self.assert_does_not_pass(self.deescalating_dc_critical, self.event_data) |
| 86 | + |
| 87 | + self.update_group_and_open_period(priority=PriorityLevel.HIGH) |
| 88 | + self.assert_does_not_pass(self.deescalating_dc_critical, self.event_data) |
| 89 | + |
| 90 | + self.update_group_and_open_period(priority=PriorityLevel.MEDIUM) |
| 91 | + self.assert_passes(self.deescalating_dc_critical, self.event_data) |
| 92 | + |
| 93 | + self.group.update(status=GroupStatus.RESOLVED) |
| 94 | + self.assert_passes(self.deescalating_dc_critical, self.event_data) |
0 commit comments