Browse Source

test(hc): Various fixes to unannotated test cases (#63454)

Fixes for an arbitrary cross-section of test cases that fail if run in
region mode by default. Add test decorators and mode exemptions where
appropriate.
Ryan Skonnord 1 year ago
parent
commit
aacb43569a

+ 5 - 2
tests/integration/test_sso.py

@@ -1,17 +1,20 @@
 from sentry.models.authidentity import AuthIdentity
 from sentry.models.authprovider import AuthProvider
+from sentry.models.organizationmember import OrganizationMember
 from sentry.testutils.cases import AuthProviderTestCase
+from sentry.testutils.silo import assume_test_silo_mode_of, control_silo_test
 from sentry.utils.auth import SsoSession
 
 
-# @control_silo_test
+@control_silo_test
 class OrganizationAuthLoginTest(AuthProviderTestCase):
     def test_sso_auth_required(self):
         user = self.create_user("foo@example.com", is_superuser=False)
         organization = self.create_organization(name="foo")
         member = self.create_member(user=user, organization=organization)
         setattr(member.flags, "sso:linked", True)
-        member.save()
+        with assume_test_silo_mode_of(OrganizationMember):
+            member.save()
 
         auth_provider = AuthProvider.objects.create(
             organization_id=organization.id, provider="dummy", flags=0

+ 1 - 1
tests/sentry/api/endpoints/test_user_roles.py

@@ -3,6 +3,7 @@ from sentry.testutils.cases import APITestCase
 from sentry.testutils.silo import control_silo_test
 
 
+@control_silo_test
 class UserUserRolesTest(APITestCase):
     endpoint = "sentry-api-0-user-userroles"
 
@@ -31,7 +32,6 @@ class UserUserRolesTest(APITestCase):
         assert resp.status_code == 403
 
 
-@control_silo_test
 class UserUserRolesGetTest(UserUserRolesTest):
     def test_lookup_self(self):
         role = UserRole.objects.create(name="support", permissions=["broadcasts.admin"])

+ 8 - 6
tests/sentry/event_manager/test_event_manager.py

@@ -67,6 +67,7 @@ from sentry.models.grouplink import GroupLink
 from sentry.models.grouprelease import GroupRelease
 from sentry.models.groupresolution import GroupResolution
 from sentry.models.grouptombstone import GroupTombstone
+from sentry.models.integrations import Integration
 from sentry.models.integrations.external_issue import ExternalIssue
 from sentry.models.project import Project
 from sentry.models.pullrequest import PullRequest, PullRequestCommit
@@ -77,7 +78,6 @@ from sentry.models.releaseprojectenvironment import ReleaseProjectEnvironment
 from sentry.models.userreport import UserReport
 from sentry.options import set
 from sentry.projectoptions.defaults import DEFAULT_GROUPING_CONFIG, LEGACY_GROUPING_CONFIG
-from sentry.silo import SiloMode
 from sentry.spans.grouping.utils import hash_values
 from sentry.testutils.asserts import assert_mock_called_once_with_partial
 from sentry.testutils.cases import (
@@ -91,7 +91,7 @@ from sentry.testutils.helpers.datetime import before_now, freeze_time, iso_forma
 from sentry.testutils.outbox import outbox_runner
 from sentry.testutils.performance_issues.event_generators import get_event
 from sentry.testutils.pytest.fixtures import django_db_all
-from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
+from sentry.testutils.silo import assume_test_silo_mode_of, region_silo_test
 from sentry.testutils.skips import requires_snuba
 from sentry.tsdb.base import TSDBModel
 from sentry.types.activity import ActivityType
@@ -2230,7 +2230,7 @@ class EventManagerTest(TestCase, SnubaTestCase, EventManagerTestMixin, Performan
             assert project.get_option("sentry:grouping_config") == DEFAULT_GROUPING_CONFIG
 
             # and we should see an audit log record.
-            with assume_test_silo_mode(SiloMode.CONTROL):
+            with assume_test_silo_mode_of(AuditLogEntry):
                 record = AuditLogEntry.objects.first()
             assert record.event == audit_log.get_event_id("PROJECT_EDIT")
             assert record.data["sentry:grouping_config"] == DEFAULT_GROUPING_CONFIG
@@ -2552,14 +2552,16 @@ class EventManagerTest(TestCase, SnubaTestCase, EventManagerTestMixin, Performan
             assert "grouping.in_app_frame_mix" not in metrics_logged
 
 
+@region_silo_test
 class AutoAssociateCommitTest(TestCase, EventManagerTestMixin):
     def setUp(self):
         super().setUp()
         self.repo_name = "example"
         self.project = self.create_project(name="foo")
-        self.org_integration = self.integration.add_organization(
-            self.project.organization, self.user
-        )
+        with assume_test_silo_mode_of(Integration):
+            self.org_integration = self.integration.add_organization(
+                self.project.organization, self.user
+            )
         self.repo = self.create_repo(
             project=self.project,
             name=self.repo_name,

+ 43 - 37
tests/sentry/incidents/action_handlers/test_email.py

@@ -31,6 +31,7 @@ from sentry.snuba.models import SnubaQuery
 from sentry.testutils.cases import TestCase
 from sentry.testutils.helpers.datetime import freeze_time
 from sentry.testutils.helpers.features import with_feature
+from sentry.testutils.silo import assume_test_silo_mode_of, region_silo_test
 
 from . import FireTest
 
@@ -75,6 +76,7 @@ class EmailActionHandlerTest(FireTest):
         )
 
 
+@region_silo_test
 class EmailActionHandlerGetTargetsTest(TestCase):
     @cached_property
     def incident(self):
@@ -108,13 +110,14 @@ class EmailActionHandlerGetTargetsTest(TestCase):
         assert handler.get_targets() == []
 
     def test_user_alerts_disabled(self):
-        NotificationSettingOption.objects.create(
-            user_id=self.user.id,
-            scope_type="project",
-            scope_identifier=self.project.id,
-            type="alerts",
-            value="never",
-        )
+        with assume_test_silo_mode_of(NotificationSettingOption):
+            NotificationSettingOption.objects.create(
+                user_id=self.user.id,
+                scope_type="project",
+                scope_identifier=self.project.id,
+                type="alerts",
+                value="never",
+            )
         action = self.create_alert_rule_trigger_action(
             target_type=AlertRuleTriggerAction.TargetType.USER,
             target_identifier=str(self.user.id),
@@ -160,21 +163,22 @@ class EmailActionHandlerGetTargetsTest(TestCase):
         assert handler.get_targets() == []
 
     def test_team_alert_disabled(self):
-        NotificationSettingOption.objects.create(
-            user_id=self.user.id,
-            scope_type="project",
-            scope_identifier=self.project.id,
-            type="alerts",
-            value="never",
-        )
-        disabled_user = self.create_user()
-        NotificationSettingOption.objects.create(
-            user_id=disabled_user.id,
-            scope_type="user",
-            scope_identifier=disabled_user.id,
-            type="alerts",
-            value="never",
-        )
+        with assume_test_silo_mode_of(NotificationSettingOption):
+            NotificationSettingOption.objects.create(
+                user_id=self.user.id,
+                scope_type="project",
+                scope_identifier=self.project.id,
+                type="alerts",
+                value="never",
+            )
+            disabled_user = self.create_user()
+            NotificationSettingOption.objects.create(
+                user_id=disabled_user.id,
+                scope_type="user",
+                scope_identifier=disabled_user.id,
+                type="alerts",
+                value="never",
+            )
 
         new_user = self.create_user()
         self.create_team_membership(team=self.team, user=new_user)
@@ -187,13 +191,14 @@ class EmailActionHandlerGetTargetsTest(TestCase):
 
     def test_user_email_routing(self):
         new_email = "marcos@sentry.io"
-        UserOption.objects.create(
-            user=self.user, project_id=self.project.id, key="mail:email", value=new_email
-        )
+        with assume_test_silo_mode_of(UserOption):
+            UserOption.objects.create(
+                user=self.user, project_id=self.project.id, key="mail:email", value=new_email
+            )
 
-        useremail = UserEmail.objects.get(email=self.user.email)
-        useremail.email = new_email
-        useremail.save()
+            useremail = UserEmail.objects.get(email=self.user.email)
+            useremail.email = new_email
+            useremail.save()
 
         action = self.create_alert_rule_trigger_action(
             target_type=AlertRuleTriggerAction.TargetType.USER,
@@ -207,16 +212,17 @@ class EmailActionHandlerGetTargetsTest(TestCase):
 
         new_user = self.create_user(new_email)
 
-        useremail = UserEmail.objects.get(email=self.user.email)
-        useremail.email = new_email
-        useremail.save()
+        with assume_test_silo_mode_of(UserEmail):
+            useremail = UserEmail.objects.get(email=self.user.email)
+            useremail.email = new_email
+            useremail.save()
 
-        UserOption.objects.create(
-            user=self.user, project_id=self.project.id, key="mail:email", value=new_email
-        )
-        UserOption.objects.create(
-            user=new_user, project_id=self.project.id, key="mail:email", value=new_email
-        )
+            UserOption.objects.create(
+                user=self.user, project_id=self.project.id, key="mail:email", value=new_email
+            )
+            UserOption.objects.create(
+                user=new_user, project_id=self.project.id, key="mail:email", value=new_email
+            )
 
         self.create_team_membership(team=self.team, user=new_user)
         action = self.create_alert_rule_trigger_action(

+ 7 - 2
tests/sentry/integrations/msteams/test_notify_action.py

@@ -5,14 +5,17 @@ from unittest import mock
 import responses
 
 from sentry.integrations.msteams import MsTeamsNotifyServiceAction
+from sentry.models.integrations import Integration
 from sentry.testutils.cases import PerformanceIssueTestCase, RuleTestCase
 from sentry.testutils.helpers.notifications import TEST_ISSUE_OCCURRENCE, TEST_PERF_ISSUE_OCCURRENCE
+from sentry.testutils.silo import assume_test_silo_mode_of, region_silo_test
 from sentry.testutils.skips import requires_snuba
 from sentry.utils import json
 
 pytestmark = [requires_snuba]
 
 
+@region_silo_test
 class MsTeamsNotifyActionTest(RuleTestCase, PerformanceIssueTestCase):
     rule_cls = MsTeamsNotifyServiceAction
 
@@ -29,7 +32,8 @@ class MsTeamsNotifyActionTest(RuleTestCase, PerformanceIssueTestCase):
                 "expires_at": int(time.time()) + 86400,
             },
         )
-        self.integration.add_organization(event.project.organization, self.user)
+        with assume_test_silo_mode_of(Integration):
+            self.integration.add_organization(event.project.organization, self.user)
 
     def assert_form_valid(self, form, expected_channel_id, expected_channel):
         assert form.is_valid()
@@ -178,7 +182,8 @@ class MsTeamsNotifyActionTest(RuleTestCase, PerformanceIssueTestCase):
         assert rule.render_label() == "Send a notification to the Galactic Empire Team to Tatooine"
 
     def test_render_label_without_integration(self):
-        self.integration.delete()
+        with assume_test_silo_mode_of(Integration):
+            self.integration.delete()
 
         rule = self.get_rule(data={"team": self.integration.id, "channel": "Coruscant"})
 

+ 81 - 43
tests/sentry/integrations/opsgenie/test_integration.py

@@ -14,7 +14,7 @@ from sentry.tasks.integrations.migrate_opsgenie_plugins import (
     ALERT_LEGACY_INTEGRATIONS_WITH_NAME,
 )
 from sentry.testutils.cases import APITestCase, IntegrationTestCase
-from sentry.testutils.silo import control_silo_test
+from sentry.testutils.silo import assume_test_silo_mode_of, control_silo_test, region_silo_test
 from sentry_plugins.opsgenie.plugin import OpsGeniePlugin
 
 EXTERNAL_ID = "test-app"
@@ -174,6 +174,7 @@ class OpsgenieIntegrationTest(IntegrationTestCase):
         }
 
 
+@region_silo_test
 class OpsgenieMigrationIntegrationTest(APITestCase):
     @cached_property
     def integration(self):
@@ -192,7 +193,8 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         self.plugin.set_option("enabled", True, self.project)
         self.plugin.set_option("alert_url", "https://api.opsgenie.com/v2/alerts/", self.project)
         self.plugin.set_option("api_key", "123-key", self.project)
-        self.installation = self.integration.get_installation(self.organization.id)
+        with assume_test_silo_mode_of(Integration):
+            self.installation = self.integration.get_installation(self.organization.id)
         self.login_as(self.user)
 
     @patch("sentry.tasks.integrations.migrate_opsgenie_plugins.metrics")
@@ -201,9 +203,12 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         Test that 2 projects with the Opsgenie plugin activated that have one alert rule each
         and distinct API keys are successfully migrated.
         """
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        org_integration.config = {"team_table": []}
-        org_integration.save()
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+            org_integration.config = {"team_table": []}
+            org_integration.save()
 
         project2 = self.create_project(
             name="thinkies", organization=self.organization, teams=[self.team]
@@ -228,7 +233,10 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         with self.tasks():
             self.installation.schedule_migrate_opsgenie_plugin()
 
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
         id1 = str(self.organization_integration.id) + "-thonk"
         id2 = str(self.organization_integration.id) + "-thinkies"
         assert org_integration.config == {
@@ -275,9 +283,12 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         """
         Keys should not be migrated twice.
         """
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        org_integration.config = {"team_table": []}
-        org_integration.save()
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+            org_integration.config = {"team_table": []}
+            org_integration.save()
 
         project2 = self.create_project(
             name="thinkies", organization=self.organization, teams=[self.team]
@@ -290,7 +301,10 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         with self.tasks():
             self.installation.schedule_migrate_opsgenie_plugin()
 
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
         id1 = str(self.organization_integration.id) + "-thonk"
 
         assert org_integration.config == {
@@ -303,17 +317,20 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         """
         Test that migration works if a key has already been added to config.
         """
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        org_integration.config = {
-            "team_table": [
-                {
-                    "id": str(self.organization_integration.id) + "-pikachu",
-                    "team": "pikachu",
-                    "integration_key": "123-key",
-                },
-            ]
-        }
-        org_integration.save()
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+            org_integration.config = {
+                "team_table": [
+                    {
+                        "id": str(self.organization_integration.id) + "-pikachu",
+                        "team": "pikachu",
+                        "integration_key": "123-key",
+                    },
+                ]
+            }
+            org_integration.save()
 
         Rule.objects.create(
             label="rule",
@@ -323,7 +340,10 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         with self.tasks():
             self.installation.schedule_migrate_opsgenie_plugin()
 
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
         assert org_integration.config == {
             "team_table": [
                 {
@@ -352,9 +372,12 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         """
         Test multiple rules, some of which send notifications to legacy integrations.
         """
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        org_integration.config = {"team_table": []}
-        org_integration.save()
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+            org_integration.config = {"team_table": []}
+            org_integration.save()
 
         Rule.objects.create(
             label="rule",
@@ -371,8 +394,11 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         with self.tasks():
             self.installation.schedule_migrate_opsgenie_plugin()
 
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        id1 = str(self.organization_integration.id) + "-thonk"
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+        id1 = str(org_integration.id) + "-thonk"
         rule_updated = Rule.objects.get(
             label="rule",
             project=self.project,
@@ -398,17 +424,20 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         """
         Don't add a new recipient from an API key if the recipient already exists.
         """
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        org_integration.config = {
-            "team_table": [
-                {
-                    "id": str(self.organization_integration.id) + "-pikachu",
-                    "team": "pikachu",
-                    "integration_key": "123-key",
-                },
-            ]
-        }
-        org_integration.save()
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+            org_integration.config = {
+                "team_table": [
+                    {
+                        "id": str(self.organization_integration.id) + "-pikachu",
+                        "team": "pikachu",
+                        "integration_key": "123-key",
+                    },
+                ]
+            }
+            org_integration.save()
 
         Rule.objects.create(
             label="rule",
@@ -428,7 +457,10 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         with self.tasks():
             self.installation.schedule_migrate_opsgenie_plugin()
 
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
         assert org_integration.config == {
             "team_table": [
                 {
@@ -457,9 +489,12 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         """
         Test that the Opsgenie plugin is migrated correctly if the legacy alert action has a name field.
         """
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
-        org_integration.config = {"team_table": []}
-        org_integration.save()
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
+            org_integration.config = {"team_table": []}
+            org_integration.save()
 
         Rule.objects.create(
             label="rule",
@@ -470,7 +505,10 @@ class OpsgenieMigrationIntegrationTest(APITestCase):
         with self.tasks():
             self.installation.schedule_migrate_opsgenie_plugin()
 
-        org_integration = OrganizationIntegration.objects.get(integration_id=self.integration.id)
+        with assume_test_silo_mode_of(OrganizationIntegration):
+            org_integration = OrganizationIntegration.objects.get(
+                integration_id=self.integration.id
+            )
         id1 = str(self.organization_integration.id) + "-thonk"
         assert org_integration.config == {
             "team_table": [

+ 1 - 0
tests/sentry/sentry_apps/test_sentry_app_creator.py

@@ -159,6 +159,7 @@ class TestCreator(TestCase):
         assert self.creator.run(user=self.user)
 
 
+@control_silo_test
 class TestInternalCreator(TestCase):
     def setUp(self):
         self.user = self.create_user()