def test_filter_fails(self): # setup a simple alert rule with 1 condition and 1 filter that doesn't pass self.event = self.store_event(data={}, project_id=self.project.id) filter_data = { "id": "tests.sentry.rules.test_processor.MockFilterFalse" } Rule.objects.filter(project=self.event.project).delete() self.rule = Rule.objects.create( project=self.event.project, data={ "conditions": [EVERY_EVENT_COND_DATA, filter_data], "actions": [EMAIL_ACTION_DATA], }, ) # patch the rule registry to contain the mocked rules with patch("sentry.rules.processor.rules", init_registry()): rp = RuleProcessor( self.event, is_new=True, is_regression=True, is_new_group_environment=True, has_reappeared=True, ) results = list(rp.apply()) assert len(results) == 0
def test_slow_conditions_evaluate_last(self): # Make sure slow/expensive conditions are evaluated last, so that we can skip evaluating # them if cheaper conditions satisfy the rule. self.rule.update(data={ "conditions": [ { "id": "sentry.rules.conditions.event_frequency.EventFrequencyCondition" }, { "id": "tests.sentry.rules.test_processor.MockConditionTrue" }, ], "action_match": "any", "actions": [EMAIL_ACTION_DATA], }, ) with patch("sentry.rules.processor.rules", init_registry()), patch( "sentry.rules.conditions.event_frequency.BaseEventFrequencyCondition.passes" ) as passes: rp = RuleProcessor( self.event, is_new=True, is_regression=True, is_new_group_environment=True, has_reappeared=True, ) results = rp.apply() assert len(results) == 1 # We should never call `passes` on the frequency condition since we should run the cheap # mock condition first. assert passes.call_count == 0
def test_rule_processor_buffer_values(self): # Test that pending buffer values for `times_seen` are applied to the group and that alerts # fire as expected from sentry.models import Rule MOCK_RULES = ( "sentry.rules.filters.issue_occurrences.IssueOccurrencesFilter", ) redis_buffer = RedisBuffer() with mock.patch("sentry.buffer.get", redis_buffer.get), mock.patch( "sentry.buffer.incr", redis_buffer.incr), patch( "sentry.constants._SENTRY_RULES", MOCK_RULES), patch("sentry.rules.processor.rules", init_registry()) as rules: MockAction = mock.Mock() MockAction.rule_type = "action/event" MockAction.id = "tests.sentry.tasks.post_process.tests.MockAction" MockAction.return_value.after.return_value = [] rules.add(MockAction) conditions = [ { "id": "sentry.rules.filters.issue_occurrences.IssueOccurrencesFilter", "value": 10, }, ] actions = [{ "id": "tests.sentry.tasks.post_process.tests.MockAction" }] Rule.objects.filter(project=self.project).delete() Rule.objects.create(project=self.project, data={ "conditions": conditions, "actions": actions }) event = self.store_event(data={ "message": "testing", "fingerprint": ["group-1"] }, project_id=self.project.id) event_2 = self.store_event(data={ "message": "testing", "fingerprint": ["group-1"] }, project_id=self.project.id) cache_key = write_event_to_cache(event) post_process_group( is_new=True, is_regression=False, is_new_group_environment=True, cache_key=cache_key, group_id=event.group_id, ) event.group.update(times_seen=2) assert MockAction.return_value.after.call_count == 0 cache_key = write_event_to_cache(event_2) buffer.incr(Group, {"times_seen": 15}, filters={"pk": event.group.id}) post_process_group( is_new=True, is_regression=False, is_new_group_environment=True, cache_key=cache_key, group_id=event_2.group_id, ) assert MockAction.return_value.after.call_count == 1