Browse Source

fix(hc): Fix some silo modes during test setup (#55962)

These test cases are not yet stable, these changes help move the
failures closer to the actual source.
Matt Duncan 1 year ago
parent
commit
18512876e3

+ 6 - 4
tests/sentry/api/endpoints/test_project_stacktrace_link.py

@@ -9,8 +9,9 @@ from sentry import options
 from sentry.api.endpoints.project_stacktrace_link import get_code_mapping_configs
 from sentry.integrations.example.integration import ExampleIntegration
 from sentry.models import Integration, OrganizationIntegration
+from sentry.silo import SiloMode
 from sentry.testutils.cases import APITestCase
-from sentry.testutils.silo import region_silo_test
+from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
 
 example_base_url = "https://example.com/getsentry/sentry/blob/master"
 git_blame = [
@@ -81,9 +82,10 @@ class BaseProjectStacktraceLink(APITestCase):
     endpoint = "sentry-api-0-project-stacktrace-link"
 
     def setUp(self):
-        self.integration = Integration.objects.create(provider="example", name="Example")
-        self.integration.add_organization(self.organization, self.user)
-        self.oi = OrganizationIntegration.objects.get(integration_id=self.integration.id)
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            self.integration = Integration.objects.create(provider="example", name="Example")
+            self.integration.add_organization(self.organization, self.user)
+            self.oi = OrganizationIntegration.objects.get(integration_id=self.integration.id)
 
         self.repo = self.create_repo(
             project=self.project,

+ 50 - 45
tests/sentry/api/endpoints/test_team_notification_settings.py

@@ -2,8 +2,11 @@ from rest_framework import status
 
 from sentry.models import NotificationSetting
 from sentry.notifications.types import NotificationSettingOptionValues, NotificationSettingTypes
+from sentry.services.hybrid_cloud.actor import RpcActor
+from sentry.services.hybrid_cloud.notifications import notifications_service
+from sentry.silo import SiloMode
 from sentry.testutils.cases import APITestCase
-from sentry.testutils.silo import region_silo_test
+from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
 from sentry.types.integrations import ExternalProviders
 
 
@@ -20,28 +23,28 @@ class TeamNotificationSettingsGetTest(TeamNotificationSettingsTestBase):
     def test_simple(self):
         _ = self.project  # HACK to force creation.
 
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.EMAIL,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
-            project=self.project,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.EMAIL,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
+            project_id=self.project.id,
             organization_id_for_team=self.organization.id,
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.EMAIL,
-            NotificationSettingTypes.DEPLOY,
-            NotificationSettingOptionValues.NEVER,
-            team_id=self.team.id,
-            organization=self.organization,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.EMAIL,
+            notification_type=NotificationSettingTypes.DEPLOY,
+            setting_option=NotificationSettingOptionValues.NEVER,
+            actor=RpcActor.from_object(self.team),
+            organization_id=self.organization.id,
             organization_id_for_team=self.organization.id,
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.WORKFLOW,
-            NotificationSettingOptionValues.DEFAULT,
-            team_id=self.team.id,
-            project=self.project,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.WORKFLOW,
+            setting_option=NotificationSettingOptionValues.DEFAULT,
+            actor=RpcActor.from_object(self.team),
+            project_id=self.project.id,
             organization_id_for_team=self.organization.id,
         )
 
@@ -55,18 +58,18 @@ class TeamNotificationSettingsGetTest(TeamNotificationSettingsTestBase):
         assert not response.data["workflow"]
 
     def test_type_querystring(self):
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.EMAIL,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.EMAIL,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.WORKFLOW,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.WORKFLOW,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
         response = self.get_success_response(
@@ -105,15 +108,16 @@ class TeamNotificationSettingsTest(TeamNotificationSettingsTestBase):
     method = "put"
 
     def test_simple(self):
-        assert (
-            NotificationSetting.objects.get_settings(
-                provider=ExternalProviders.SLACK,
-                type=NotificationSettingTypes.ISSUE_ALERTS,
-                team_id=self.team.id,
-                project=self.project,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            assert (
+                NotificationSetting.objects.get_settings(
+                    provider=ExternalProviders.SLACK,
+                    type=NotificationSettingTypes.ISSUE_ALERTS,
+                    team_id=self.team.id,
+                    project=self.project,
+                )
+                == NotificationSettingOptionValues.DEFAULT
             )
-            == NotificationSettingOptionValues.DEFAULT
-        )
 
         self.get_success_response(
             self.organization.slug,
@@ -122,15 +126,16 @@ class TeamNotificationSettingsTest(TeamNotificationSettingsTestBase):
             status_code=status.HTTP_204_NO_CONTENT,
         )
 
-        assert (
-            NotificationSetting.objects.get_settings(
-                provider=ExternalProviders.SLACK,
-                type=NotificationSettingTypes.ISSUE_ALERTS,
-                team_id=self.team.id,
-                project=self.project,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            assert (
+                NotificationSetting.objects.get_settings(
+                    provider=ExternalProviders.SLACK,
+                    type=NotificationSettingTypes.ISSUE_ALERTS,
+                    team_id=self.team.id,
+                    project=self.project,
+                )
+                == NotificationSettingOptionValues.ALWAYS
             )
-            == NotificationSettingOptionValues.ALWAYS
-        )
 
     def test_empty_payload(self):
         self.get_error_response(

+ 128 - 109
tests/sentry/integrations/slack/notifications/test_issue_alert.py

@@ -14,7 +14,6 @@ from sentry.models import (
     Identity,
     IdentityProvider,
     IdentityStatus,
-    NotificationSetting,
     OrganizationIntegration,
     Rule,
 )
@@ -29,11 +28,14 @@ from sentry.ownership.grammar import Matcher, Owner
 from sentry.ownership.grammar import Rule as GrammarRule
 from sentry.ownership.grammar import dump_schema
 from sentry.plugins.base import Notification
+from sentry.services.hybrid_cloud.actor import RpcActor
+from sentry.services.hybrid_cloud.notifications import notifications_service
+from sentry.silo import SiloMode
 from sentry.tasks.digests import deliver_digest
 from sentry.testutils.cases import PerformanceIssueTestCase, SlackActivityNotificationTest
 from sentry.testutils.helpers.notifications import TEST_ISSUE_OCCURRENCE, TEST_PERF_ISSUE_OCCURRENCE
 from sentry.testutils.helpers.slack import get_attachment, send_notification
-from sentry.testutils.silo import region_silo_test
+from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
 from sentry.types.integrations import ExternalProviders
 from sentry.utils import json
 
@@ -139,10 +141,10 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
     @responses.activate
     @mock.patch("sentry.notifications.notify.notify", side_effect=send_notification)
     def test_disabled_org_integration_for_user(self, mock_func):
-
-        OrganizationIntegration.objects.filter(integration=self.integration).update(
-            status=ObjectStatus.DISABLED
-        )
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            OrganizationIntegration.objects.filter(integration=self.integration).update(
+                status=ObjectStatus.DISABLED
+            )
 
         event = self.store_event(
             data={"message": "Hello world", "level": "error"}, project_id=self.project.id
@@ -205,19 +207,22 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
         # sent once (to the team, and not to each individual user)
         user2 = self.create_user(is_superuser=False)
         self.create_member(teams=[self.team], user=user2, organization=self.organization)
-        self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX2", config={})
-        self.identity = Identity.objects.create(
-            external_id="UXXXXXXX2",
-            idp=self.idp,
-            user=user2,
-            status=IdentityStatus.VALID,
-            scopes=[],
-        )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            user_id=user2.id,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            self.idp = IdentityProvider.objects.create(
+                type="slack", external_id="TXXXXXXX2", config={}
+            )
+            self.identity = Identity.objects.create(
+                external_id="UXXXXXXX2",
+                idp=self.idp,
+                user=user2,
+                status=IdentityStatus.VALID,
+                scopes=[],
+            )
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(user2),
         )
         # update the team's notification settings
         ExternalActor.objects.create(
@@ -229,11 +234,11 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
             external_name="goma",
             external_id="CXXXXXXX2",
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
 
@@ -302,17 +307,18 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
             external_name="goma",
             external_id="CXXXXXXX2",
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
 
-        OrganizationIntegration.objects.filter(integration=self.integration).update(
-            status=ObjectStatus.DISABLED
-        )
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            OrganizationIntegration.objects.filter(integration=self.integration).update(
+                status=ObjectStatus.DISABLED
+            )
 
         rule = GrammarRule(Matcher("path", "*"), [Owner("team", self.team.slug)])
         ProjectOwnership.objects.create(
@@ -366,29 +372,32 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
         # turn off the user's issue alert notification settings
         # there was a bug where issue alerts to a team's Slack channel
         # were only firing if this was set to ALWAYS
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.NEVER,
-            user_id=self.user.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.NEVER,
+            actor=RpcActor.from_object(self.user),
         )
         # add a second user to the team so we can be sure it's only
         # sent once (to the team, and not to each individual user)
         user2 = self.create_user(is_superuser=False)
         self.create_member(teams=[self.team], user=user2, organization=self.organization)
-        self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX2", config={})
-        self.identity = Identity.objects.create(
-            external_id="UXXXXXXX2",
-            idp=self.idp,
-            user=user2,
-            status=IdentityStatus.VALID,
-            scopes=[],
-        )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.NEVER,
-            user_id=user2.id,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            self.idp = IdentityProvider.objects.create(
+                type="slack", external_id="TXXXXXXX2", config={}
+            )
+            self.identity = Identity.objects.create(
+                external_id="UXXXXXXX2",
+                idp=self.idp,
+                user=user2,
+                status=IdentityStatus.VALID,
+                scopes=[],
+            )
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.NEVER,
+            actor=RpcActor.from_object(self.user),
         )
         # update the team's notification settings
         ExternalActor.objects.create(
@@ -400,11 +409,11 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
             external_name="goma",
             external_id="CXXXXXXX2",
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
 
@@ -464,25 +473,31 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
         """Test that issue alerts are sent to a team in Slack."""
         # add a second organization
         org = self.create_organization(owner=self.user)
-        OrganizationIntegration.objects.create(organization_id=org.id, integration=self.integration)
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            OrganizationIntegration.objects.create(
+                organization_id=org.id, integration=self.integration
+            )
 
         # add a second user to the team so we can be sure it's only
         # sent once (to the team, and not to each individual user)
         user2 = self.create_user(is_superuser=False)
         self.create_member(teams=[self.team], user=user2, organization=self.organization)
-        self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX2", config={})
-        self.identity = Identity.objects.create(
-            external_id="UXXXXXXX2",
-            idp=self.idp,
-            user=user2,
-            status=IdentityStatus.VALID,
-            scopes=[],
-        )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            user_id=user2.id,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            self.idp = IdentityProvider.objects.create(
+                type="slack", external_id="TXXXXXXX2", config={}
+            )
+            self.identity = Identity.objects.create(
+                external_id="UXXXXXXX2",
+                idp=self.idp,
+                user=user2,
+                status=IdentityStatus.VALID,
+                scopes=[],
+            )
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(user2),
         )
         # update the team's notification settings
         ExternalActor.objects.create(
@@ -494,11 +509,11 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
             external_name="goma",
             external_id="CXXXXXXX2",
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
 
@@ -548,19 +563,22 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
         # sent once (to the team, and not to each individual user)
         user2 = self.create_user(is_superuser=False)
         self.create_member(teams=[self.team], user=user2, organization=self.organization)
-        self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX2", config={})
-        self.identity = Identity.objects.create(
-            external_id="UXXXXXXX2",
-            idp=self.idp,
-            user=user2,
-            status=IdentityStatus.VALID,
-            scopes=[],
-        )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            user_id=user2.id,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            self.idp = IdentityProvider.objects.create(
+                type="slack", external_id="TXXXXXXX2", config={}
+            )
+            self.identity = Identity.objects.create(
+                external_id="UXXXXXXX2",
+                idp=self.idp,
+                user=user2,
+                status=IdentityStatus.VALID,
+                scopes=[],
+            )
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(user2),
         )
         # update the team's notification settings
         ExternalActor.objects.create(
@@ -572,11 +590,11 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
             external_name="goma",
             external_id="CXXXXXXX2",
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
         # add a new project
@@ -636,11 +654,11 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
             external_name="goma",
             external_id="CXXXXXXX2",
         )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            team_id=self.team.id,
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(self.team),
             organization_id_for_team=self.organization.id,
         )
         # remove the project from the team
@@ -676,18 +694,19 @@ class SlackIssueAlertNotificationTest(SlackActivityNotificationTest, Performance
 
         user2 = self.create_user(is_superuser=False)
         self.create_member(teams=[self.team], user=user2, organization=self.organization)
-        self.identity = Identity.objects.create(
-            external_id="UXXXXXXX2",
-            idp=self.idp,
-            user=user2,
-            status=IdentityStatus.VALID,
-            scopes=[],
-        )
-        NotificationSetting.objects.update_settings(
-            ExternalProviders.SLACK,
-            NotificationSettingTypes.ISSUE_ALERTS,
-            NotificationSettingOptionValues.ALWAYS,
-            user_id=user2.id,
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            self.identity = Identity.objects.create(
+                external_id="UXXXXXXX2",
+                idp=self.idp,
+                user=user2,
+                status=IdentityStatus.VALID,
+                scopes=[],
+            )
+        notifications_service.update_settings(
+            external_provider=ExternalProviders.SLACK,
+            notification_type=NotificationSettingTypes.ISSUE_ALERTS,
+            setting_option=NotificationSettingOptionValues.ALWAYS,
+            actor=RpcActor.from_object(user2),
         )
 
         event = self.store_event(

+ 6 - 4
tests/sentry/integrations/test_issues.py

@@ -203,8 +203,9 @@ class IssueDefaultTest(TestCase):
         }
 
     def test_store_issue_last_defaults_for_user_multiple_providers(self):
-        other_integration = Integration.objects.create(provider=AliasedIntegrationProvider.key)
-        other_integration.add_organization(self.organization, self.user)
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            other_integration = Integration.objects.create(provider=AliasedIntegrationProvider.key)
+            other_integration.add_organization(self.organization, self.user)
         other_installation = other_integration.get_installation(self.organization.id)
 
         self.installation.store_issue_last_defaults(
@@ -230,8 +231,9 @@ class IssueDefaultTest(TestCase):
             self.group.id: [f'<a href="{link}">{label}</a>']
         }
 
-        integration = Integration.objects.create(provider="example", external_id="4444")
-        integration.add_organization(self.group.organization, self.user)
+        with assume_test_silo_mode(SiloMode.CONTROL):
+            integration = Integration.objects.create(provider="example", external_id="4444")
+            integration.add_organization(self.group.organization, self.user)
         installation = integration.get_installation(self.group.organization.id)
 
         assert installation.get_annotations_for_group_list([self.group]) == {self.group.id: []}