conftest.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import os
  2. from collections.abc import MutableMapping
  3. from typing import Any
  4. import psutil
  5. import pytest
  6. import responses
  7. from django.core.cache import cache
  8. from django.db import connections
  9. from sentry.silo.base import SiloMode
  10. from sentry.testutils.pytest.sentry import get_default_silo_mode_for_test_cases
  11. pytest_plugins = ["sentry.testutils.pytest"]
  12. # XXX: The below code is vendored code from https://github.com/utgwkk/pytest-github-actions-annotate-failures
  13. # so that we can add support for pytest_rerunfailures
  14. # retried tests will no longer be annotated in GHA
  15. #
  16. # Reference:
  17. # https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks
  18. # https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example
  19. # https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_runtest_makereport
  20. #
  21. # Inspired by:
  22. # https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py
  23. @pytest.fixture(autouse=True)
  24. def unclosed_files():
  25. fds = frozenset(psutil.Process().open_files())
  26. yield
  27. assert frozenset(psutil.Process().open_files()) == fds
  28. @pytest.fixture(autouse=True)
  29. def validate_silo_mode():
  30. # NOTE! Hybrid cloud uses many mechanisms to simulate multiple different configurations of the application
  31. # during tests. It depends upon `override_settings` using the correct contextmanager behaviors and correct
  32. # thread handling in acceptance tests. If you hit one of these, it's possible either that cleanup logic has
  33. # a bug, or you may be using a contextmanager incorrectly. Let us know and we can help!
  34. expected = get_default_silo_mode_for_test_cases()
  35. message = (
  36. f"Possible test leak bug! SiloMode was not reset to {expected} between tests. "
  37. "Please read the comment for validate_silo_mode() in tests/conftest.py."
  38. )
  39. if SiloMode.get_current_mode() != expected:
  40. raise Exception(message)
  41. yield
  42. if SiloMode.get_current_mode() != expected:
  43. raise Exception(message)
  44. @pytest.fixture(autouse=True)
  45. def setup_simulate_on_commit(request):
  46. from sentry.testutils.hybrid_cloud import simulate_on_commit
  47. with simulate_on_commit(request):
  48. yield
  49. @pytest.fixture(autouse=True)
  50. def setup_enforce_monotonic_transactions(request):
  51. from sentry.testutils.hybrid_cloud import enforce_no_cross_transaction_interactions
  52. with enforce_no_cross_transaction_interactions():
  53. yield
  54. @pytest.fixture(autouse=True)
  55. def audit_hybrid_cloud_writes_and_deletes(request):
  56. """
  57. Ensure that write operations on hybrid cloud foreign keys are recorded
  58. alongside outboxes or use a context manager to indicate that the
  59. caller has considered outbox and didn't accidentally forget.
  60. Generally you can avoid assertion errors from these checks by:
  61. 1. Running deletion/write logic within an `outbox_context`.
  62. 2. Using Model.delete()/save methods that create outbox messages in the
  63. same transaction as a delete operation.
  64. Scenarios that are generally always unsafe are using
  65. `QuerySet.delete()`, `QuerySet.update()` or raw SQL to perform
  66. writes.
  67. The User.delete() method is a good example of how to safely
  68. delete records and generate outbox messages.
  69. """
  70. from sentry.testutils.silo import validate_protected_queries
  71. debug_cursor_state: MutableMapping[str, bool] = {}
  72. for conn in connections.all():
  73. debug_cursor_state[conn.alias] = conn.force_debug_cursor
  74. conn.queries_log.clear()
  75. conn.force_debug_cursor = True
  76. try:
  77. yield
  78. finally:
  79. for conn in connections.all():
  80. conn.force_debug_cursor = debug_cursor_state[conn.alias]
  81. validate_protected_queries(conn.queries)
  82. @pytest.fixture(autouse=True)
  83. def clear_caches():
  84. yield
  85. cache.clear()
  86. @pytest.fixture(autouse=True)
  87. def check_leaked_responses_mocks():
  88. yield
  89. leaked = responses.registered()
  90. if leaked:
  91. responses.reset()
  92. leaked_s = "".join(f"- {item}\n" for item in leaked)
  93. raise AssertionError(
  94. f"`responses` were leaked outside of the test context:\n{leaked_s}"
  95. f"(make sure to use `@responses.activate` or `with responses.mock:`)"
  96. )
  97. def pytest_collection_modifyitems(
  98. session: pytest.Session, config: Any, items: list[pytest.Item]
  99. ) -> None:
  100. """
  101. Enable the use of `@pytest.mark.only` for running just the tests with this decorator. Works
  102. best when running tests in a single file.
  103. Note: Should only be used in development!
  104. Inspired by https://stackoverflow.com/a/70607961
  105. """
  106. # There's a lint rule in place to keep people from committing a `@pytest.mark.only`
  107. # decorator (and to keep CI from passing if it finds one), but just in case...
  108. if os.environ.get("GITHUB_ACTIONS"):
  109. return
  110. tests_with_only_marker = [i for i in items if i.get_closest_marker("only")]
  111. if tests_with_only_marker:
  112. print( # noqa: S002
  113. f"\nFound `pytest.mark.only`. Running {len(tests_with_only_marker)} of {len(items)} tests."
  114. )
  115. items[:] = tests_with_only_marker