Browse Source

test(hc): Have validate_silo_mode look at configurable default mode (#63916)

This will make it easier to swap the default mode from monolith to
region in the future without breaking `validate_silo_mode`.
Ryan Skonnord 1 year ago
parent
commit
534e56a10b
2 changed files with 16 additions and 8 deletions
  1. 5 0
      src/sentry/testutils/pytest/sentry.py
  2. 11 8
      tests/conftest.py

+ 5 - 0
src/sentry/testutils/pytest/sentry.py

@@ -16,6 +16,7 @@ from django.conf import settings
 from sentry_sdk import Hub
 
 from sentry.runner.importer import install_plugin_apps
+from sentry.silo import SiloMode
 from sentry.testutils.region import TestEnvRegionDirectory
 from sentry.testutils.silo import monkey_patch_single_process_silo_mode_state
 from sentry.types import region
@@ -49,7 +50,11 @@ def configure_split_db() -> None:
     settings.DATABASE_ROUTERS = ("sentry.db.router.SiloRouter",)
 
 
+DEFAULT_SILO_MODE_FOR_TEST_CASES = SiloMode.MONOLITH
+
+
 def _configure_test_env_regions() -> None:
+    settings.SILO_MODE = DEFAULT_SILO_MODE_FOR_TEST_CASES
 
     # Assign a random name on every test run, as a reminder that test setup and
     # assertions should not depend on this value. If you need to test behavior that

+ 11 - 8
tests/conftest.py

@@ -7,6 +7,7 @@ import responses
 from django.db import connections
 
 from sentry.silo import SiloMode
+from sentry.testutils.pytest.sentry import DEFAULT_SILO_MODE_FOR_TEST_CASES
 
 pytest_plugins = ["sentry.testutils.pytest"]
 
@@ -111,15 +112,17 @@ def validate_silo_mode():
     # during tests.  It depends upon `override_settings` using the correct contextmanager behaviors and correct
     # thread handling in acceptance tests.  If you hit one of these, it's possible either that cleanup logic has
     # a bug, or you may be using a contextmanager incorrectly.  Let us know and we can help!
-    if SiloMode.get_current_mode() != SiloMode.MONOLITH:
-        raise Exception(
-            "Possible test leak bug!  SiloMode was not reset to Monolith between tests.  Please read the comment for validate_silo_mode() in tests/conftest.py."
-        )
+    expected = DEFAULT_SILO_MODE_FOR_TEST_CASES
+    message = (
+        f"Possible test leak bug!  SiloMode was not reset to {expected} between tests.  "
+        "Please read the comment for validate_silo_mode() in tests/conftest.py."
+    )
+
+    if SiloMode.get_current_mode() != expected:
+        raise Exception(message)
     yield
-    if SiloMode.get_current_mode() != SiloMode.MONOLITH:
-        raise Exception(
-            "Possible test leak bug!  SiloMode was not reset to Monolith between tests.  Please read the comment for validate_silo_mode() in tests/conftest.py."
-        )
+    if SiloMode.get_current_mode() != expected:
+        raise Exception(message)
 
 
 @pytest.fixture(autouse=True)