|
| 1 | +from hashlib import md5 |
| 2 | + |
| 3 | +from sentry.models.group import Group |
| 4 | +from sentry.models.grouphash import GroupHash |
| 5 | +from sentry.testutils.cases import TestMigrations, UptimeTestCaseMixin |
| 6 | +from sentry.uptime.grouptype import UptimeDomainCheckFailure |
| 7 | +from sentry.uptime.issue_platform import ( |
| 8 | + build_detector_fingerprint_component, |
| 9 | + create_issue_platform_occurrence, |
| 10 | +) |
| 11 | +from sentry.uptime.types import DATA_SOURCE_UPTIME_SUBSCRIPTION, ProjectUptimeSubscriptionMode |
| 12 | +from sentry.workflow_engine.models import DataSource, DataSourceDetector |
| 13 | + |
| 14 | + |
| 15 | +class TestUptimeBackfillDetectorGrouphash(TestMigrations, UptimeTestCaseMixin): |
| 16 | + app = "uptime" |
| 17 | + migrate_from = "0039_uptime_drop_project_subscription_uptime_status_db" |
| 18 | + migrate_to = "0040_uptime_backfill_detector_grouphash" |
| 19 | + |
| 20 | + def setup_before_migration(self, apps): |
| 21 | + self.proj_sub_no_hash = self.create_project_uptime_subscription(project=self.project) |
| 22 | + self.proj_sub_one_hash = self.create_project_uptime_subscription(project=self.project) |
| 23 | + self.proj_sub_both_hash = self.create_project_uptime_subscription(project=self.project) |
| 24 | + |
| 25 | + DataSource.objects.all().delete() |
| 26 | + self.data_source_no_hash = self.create_data_source( |
| 27 | + type=DATA_SOURCE_UPTIME_SUBSCRIPTION, |
| 28 | + source_id=str(self.proj_sub_no_hash.uptime_subscription_id), |
| 29 | + ) |
| 30 | + self.detector_no_hash = self.create_detector( |
| 31 | + type=UptimeDomainCheckFailure.slug, |
| 32 | + config={"mode": ProjectUptimeSubscriptionMode.MANUAL, "environment": None}, |
| 33 | + project=self.project, |
| 34 | + ) |
| 35 | + DataSourceDetector.objects.create( |
| 36 | + data_source=self.data_source_no_hash, detector=self.detector_no_hash |
| 37 | + ) |
| 38 | + |
| 39 | + self.data_source_one_hash = self.create_data_source( |
| 40 | + type=DATA_SOURCE_UPTIME_SUBSCRIPTION, |
| 41 | + source_id=str(self.proj_sub_one_hash.uptime_subscription_id), |
| 42 | + ) |
| 43 | + self.detector_one_hash = self.create_detector( |
| 44 | + type=UptimeDomainCheckFailure.slug, |
| 45 | + config={"mode": ProjectUptimeSubscriptionMode.MANUAL, "environment": None}, |
| 46 | + project=self.project, |
| 47 | + ) |
| 48 | + DataSourceDetector.objects.create( |
| 49 | + data_source=self.data_source_one_hash, detector=self.detector_one_hash |
| 50 | + ) |
| 51 | + |
| 52 | + self.data_source_both_hash = self.create_data_source( |
| 53 | + type=DATA_SOURCE_UPTIME_SUBSCRIPTION, |
| 54 | + source_id=str(self.proj_sub_both_hash.uptime_subscription_id), |
| 55 | + ) |
| 56 | + self.detector_both_hash = self.create_detector( |
| 57 | + type=UptimeDomainCheckFailure.slug, |
| 58 | + config={"mode": ProjectUptimeSubscriptionMode.MANUAL, "environment": None}, |
| 59 | + project=self.project, |
| 60 | + ) |
| 61 | + DataSourceDetector.objects.create( |
| 62 | + data_source=self.data_source_both_hash, detector=self.detector_both_hash |
| 63 | + ) |
| 64 | + |
| 65 | + with self.tasks(), self.feature(UptimeDomainCheckFailure.build_ingest_feature_name()): |
| 66 | + create_issue_platform_occurrence(self.create_uptime_result(), self.detector_both_hash) |
| 67 | + create_issue_platform_occurrence(self.create_uptime_result(), self.detector_one_hash) |
| 68 | + |
| 69 | + # Remove the hash for the detector |
| 70 | + self.group_one_hash = Group.objects.last() |
| 71 | + GroupHash.objects.filter( |
| 72 | + group=self.group_one_hash, |
| 73 | + hash=md5( |
| 74 | + build_detector_fingerprint_component(self.detector_one_hash).encode("utf-8") |
| 75 | + ).hexdigest(), |
| 76 | + ).delete() |
| 77 | + |
| 78 | + def test(self): |
| 79 | + assert GroupHash.objects.filter( |
| 80 | + group=self.group_one_hash, |
| 81 | + hash=md5( |
| 82 | + build_detector_fingerprint_component(self.detector_one_hash).encode("utf-8") |
| 83 | + ).hexdigest(), |
| 84 | + ).exists() |
| 85 | + assert not GroupHash.objects.filter( |
| 86 | + project=self.detector_no_hash.project, |
| 87 | + hash=md5( |
| 88 | + build_detector_fingerprint_component(self.detector_no_hash).encode("utf-8") |
| 89 | + ).hexdigest(), |
| 90 | + ).exists() |
| 91 | + both_group = GroupHash.objects.get( |
| 92 | + project=self.detector_both_hash.project, |
| 93 | + hash=md5( |
| 94 | + build_detector_fingerprint_component(self.detector_both_hash).encode("utf-8") |
| 95 | + ).hexdigest(), |
| 96 | + ).group |
| 97 | + assert GroupHash.objects.filter(group=both_group).count() == 2 |
0 commit comments