123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import os
- from typing import MutableSet
- import pytest
- from django.db.transaction import get_connection
- from sentry.silo import SiloMode
- pytest_plugins = ["sentry.utils.pytest"]
- @pytest.hookimpl(tryfirst=True, hookwrapper=True)
- def pytest_runtest_makereport(item, call):
-
- outcome = yield
- report = outcome.get_result()
-
-
- if os.environ.get("GITHUB_ACTIONS") != "true":
- return
-
-
-
- if hasattr(item, "execution_count"):
- import pytest_rerunfailures
- if item.execution_count <= pytest_rerunfailures.get_reruns_count(item):
- return
- if report.when == "call" and report.failed:
-
- filesystempath, lineno, _ = report.location
-
- workspace = os.environ.get("GITHUB_WORKSPACE")
- if workspace:
- full_path = os.path.abspath(filesystempath)
- try:
- rel_path = os.path.relpath(full_path, workspace)
- except ValueError:
-
-
-
- rel_path = filesystempath
- if not rel_path.startswith(".."):
- filesystempath = rel_path
- if lineno is not None:
-
- lineno += 1
-
- longrepr = report.head_line or item.name
-
- try:
- longrepr += "\n\n" + report.longrepr.reprcrash.message
- lineno = report.longrepr.reprcrash.lineno
- except AttributeError:
- pass
- print(_error_workflow_command(filesystempath, lineno, longrepr))
- def _error_workflow_command(filesystempath, lineno, longrepr):
-
- details_dict = {"file": filesystempath}
- if lineno is not None:
- details_dict["line"] = lineno
- details = ",".join(f"{k}={v}" for k, v in details_dict.items())
- if longrepr is None:
- return f"\n::error {details}"
- else:
- longrepr = _escape(longrepr)
- return f"\n::error {details}::{longrepr}"
- def _escape(s):
- return s.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
- _MODEL_MANIFEST_FILE_PATH = "./model-manifest.json"
- _model_manifest = None
- @pytest.fixture(scope="session", autouse=True)
- def create_model_manifest_file():
- """Audit which models are touched by each test case and write it to file."""
-
-
- from sentry.testutils.modelmanifest import ModelManifest
- if _MODEL_MANIFEST_FILE_PATH:
- global _model_manifest
- _model_manifest = ModelManifest.open(_MODEL_MANIFEST_FILE_PATH)
- with _model_manifest.write():
- yield
- else:
- yield
- @pytest.fixture(scope="class", autouse=True)
- def register_class_in_model_manifest(request: pytest.FixtureRequest):
- if _model_manifest:
- with _model_manifest.register(request.node.nodeid):
- yield
- else:
- yield
- @pytest.fixture(autouse=True)
- def validate_silo_mode():
-
-
-
-
- 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."
- )
- 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."
- )
- @pytest.fixture(autouse=True)
- def setup_simulate_on_commit(request):
- from sentry.testutils.hybrid_cloud import simulate_on_commit
- with simulate_on_commit(request):
- yield
- @pytest.fixture(autouse=True)
- def protect_hybrid_cloud_writes_and_deletes(request):
- """
- Ensure the deletions on any hybrid cloud foreign keys would be recorded to an outbox
- by preventing any deletes that do not pass through a special 'connection'.
- This logic creates an additional database role which cannot make deletions on special
- restricted hybrid cloud objects, forcing code that would delete it in tests to explicitly
- escalate their role -- the hope being that only codepaths that are smart about outbox
- creation will do so.
- If you are running into issues with permissions to delete objects, consider whether
- you are deleting an object with a hybrid cloud foreign key pointing to it, and whether
- there is an 'expected' way to delete it (usually through the ORM .delete() method, but
- not the QuerySet.delete() or raw SQL delete).
- If you are certain you need to delete the objects in a new codepath, check out User.delete
- logic to see how to escalate the connection's role in tests. Make absolutely sure that you
- create Outbox objects in the same transaction that matches what you delete.
- """
- from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey
- from sentry.models import (
- Organization,
- OrganizationMapping,
- OrganizationMember,
- OrganizationMemberMapping,
- )
- from sentry.testutils.silo import iter_models, reset_test_role, restrict_role
- try:
- with get_connection().cursor() as conn:
- conn.execute("SET ROLE 'postgres'")
- except (RuntimeError, AssertionError) as e:
-
-
-
- if "Database access not allowed" in str(e) or "Database queries to" in str(e):
- yield
- return
- reset_test_role(role="postgres_unprivileged")
-
- seen_models: MutableSet[type] = set()
- for model in iter_models():
- for field in model._meta.fields:
- if not isinstance(field, HybridCloudForeignKey):
- continue
- fk_model = field.foreign_model
- if fk_model is None or fk_model in seen_models:
- continue
- seen_models.add(fk_model)
- restrict_role(role="postgres_unprivileged", model=fk_model, revocation_type="DELETE")
-
-
-
- restrict_role(role="postgres_unprivileged", model=OrganizationMember, revocation_type="INSERT")
- restrict_role(role="postgres_unprivileged", model=OrganizationMember, revocation_type="UPDATE")
- restrict_role(role="postgres_unprivileged", model=Organization, revocation_type="INSERT")
- restrict_role(role="postgres_unprivileged", model=Organization, revocation_type="UPDATE")
- restrict_role(role="postgres_unprivileged", model=OrganizationMapping, revocation_type="INSERT")
- restrict_role(role="postgres_unprivileged", model=OrganizationMapping, revocation_type="UPDATE")
-
-
- restrict_role(role="postgres_unprivileged", model=OrganizationMember, revocation_type="DELETE")
- restrict_role(
- role="postgres_unprivileged", model=OrganizationMemberMapping, revocation_type="INSERT"
- )
- with get_connection().cursor() as conn:
- conn.execute("SET ROLE 'postgres_unprivileged'")
- try:
- yield
- finally:
- with get_connection().cursor() as conn:
- conn.execute("SET ROLE 'postgres'")
|