Skip to content

Commit

Permalink
feat(backup): Support alert rules (#52669)
Browse files Browse the repository at this point in the history
Adds a number of tests for exportable AlertRule* Sentry models, which
necessitates testing for SnubaQuery* and QuerySubscription models as
well, as these are included in AlertRule instances by default.

Issue: getsentry/team-ospo#156
azaslavsky authored Jul 14, 2023
1 parent e2bea9a commit dde6eb1
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/sentry/runner/commands/backup.py
Original file line number Diff line number Diff line change
@@ -150,6 +150,8 @@ def compare(self, on: InstanceID, left: JSONData, right: JSONData) -> Comparator
ComparatorList = List[JSONScrubbingComparator]
ComparatorMap = Dict[str, ComparatorList]
DEFAULT_COMPARATORS: ComparatorMap = {
"sentry.alertrule": [DateUpdatedComparator("date_modified")],
"sentry.querysubscription": [DateUpdatedComparator("date_updated")],
"sentry.userrole": [DateUpdatedComparator("date_updated")],
"sentry.userroleuser": [DateUpdatedComparator("date_updated")],
}
6 changes: 4 additions & 2 deletions src/sentry/testutils/factories.py
Original file line number Diff line number Diff line change
@@ -1302,11 +1302,13 @@ def create_alert_rule(

@staticmethod
@assume_test_silo_mode(SiloMode.REGION)
def create_alert_rule_trigger(alert_rule, label=None, alert_threshold=100):
def create_alert_rule_trigger(
alert_rule, label=None, alert_threshold=100, excluded_projects=None
):
if not label:
label = petname.generate(2, " ", letters=10).title()

return create_alert_rule_trigger(alert_rule, label, alert_threshold)
return create_alert_rule_trigger(alert_rule, label, alert_threshold, excluded_projects)

@staticmethod
@assume_test_silo_mode(SiloMode.REGION)
18 changes: 18 additions & 0 deletions tests/sentry/backup/test_models.py
Original file line number Diff line number Diff line change
@@ -65,6 +65,24 @@ def create_monitor(self):
config={"schedule": "* * * * *", "schedule_type": ScheduleType.CRONTAB},
)

def test_alert_rule(self):
self.create_alert_rule()
self.import_export_then_validate()

def test_alert_rule_excluded_projects(self):
user = self.create_user()
org = self.create_organization(owner=user)
excluded = self.create_project(organization=org)
self.create_alert_rule(include_all_projects=True, excluded_projects=[excluded])
self.import_export_then_validate()

def test_alert_rule_trigger(self):
excluded = self.create_project()
rule = self.create_alert_rule(include_all_projects=True)
trigger = self.create_alert_rule_trigger(alert_rule=rule, excluded_projects=[excluded])
self.create_alert_rule_trigger_action(alert_rule_trigger=trigger)
self.import_export_then_validate()

def test_environment(self):
self.create_environment()
self.import_export_then_validate()

0 comments on commit dde6eb1

Please sign in to comment.