conftest.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import os
  2. from typing import MutableMapping
  3. import pytest
  4. import responses
  5. from django.db import connections
  6. from sentry.silo import SiloMode
  7. pytest_plugins = ["sentry.testutils.pytest"]
  8. # XXX: The below code is vendored code from https://github.com/utgwkk/pytest-github-actions-annotate-failures
  9. # so that we can add support for pytest_rerunfailures
  10. # retried tests will no longer be annotated in GHA
  11. #
  12. # Reference:
  13. # https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks
  14. # https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example
  15. # https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_runtest_makereport
  16. #
  17. # Inspired by:
  18. # https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py
  19. @pytest.hookimpl(tryfirst=True, hookwrapper=True)
  20. def pytest_runtest_makereport(item, call):
  21. # execute all other hooks to obtain the report object
  22. outcome = yield
  23. report = outcome.get_result()
  24. # enable only in a workflow of GitHub Actions
  25. # ref: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
  26. if os.environ.get("GITHUB_ACTIONS") != "true":
  27. return
  28. # If we have the pytest_rerunfailures plugin,
  29. # and there are still retries to be run,
  30. # then do not return the error
  31. if hasattr(item, "execution_count"):
  32. import pytest_rerunfailures
  33. if item.execution_count <= pytest_rerunfailures.get_reruns_count(item):
  34. return
  35. if report.when == "call" and report.failed:
  36. # collect information to be annotated
  37. filesystempath, lineno, _ = report.location
  38. # try to convert to absolute path in GitHub Actions
  39. workspace = os.environ.get("GITHUB_WORKSPACE")
  40. if workspace:
  41. full_path = os.path.abspath(filesystempath)
  42. try:
  43. rel_path = os.path.relpath(full_path, workspace)
  44. except ValueError:
  45. # os.path.relpath() will raise ValueError on Windows
  46. # when full_path and workspace have different mount points.
  47. # https://github.com/utgwkk/pytest-github-actions-annotate-failures/issues/20
  48. rel_path = filesystempath
  49. if not rel_path.startswith(".."):
  50. filesystempath = rel_path
  51. if lineno is not None:
  52. # 0-index to 1-index
  53. lineno += 1
  54. # get the name of the current failed test, with parametrize info
  55. longrepr = report.head_line or item.name
  56. # get the error message and line number from the actual error
  57. try:
  58. longrepr += "\n\n" + report.longrepr.reprcrash.message
  59. lineno = report.longrepr.reprcrash.lineno
  60. except AttributeError:
  61. pass
  62. print(_error_workflow_command(filesystempath, lineno, longrepr)) # noqa: S002
  63. def _error_workflow_command(filesystempath, lineno, longrepr):
  64. # Build collection of arguments. Ordering is strict for easy testing
  65. details_dict = {"file": filesystempath}
  66. if lineno is not None:
  67. details_dict["line"] = lineno
  68. details = ",".join(f"{k}={v}" for k, v in details_dict.items())
  69. if longrepr is None:
  70. return f"\n::error {details}"
  71. else:
  72. longrepr = _escape(longrepr)
  73. return f"\n::error {details}::{longrepr}"
  74. def _escape(s):
  75. return s.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
  76. @pytest.fixture(autouse=True)
  77. def validate_silo_mode():
  78. # NOTE! Hybrid cloud uses many mechanisms to simulate multiple different configurations of the application
  79. # during tests. It depends upon `override_settings` using the correct contextmanager behaviors and correct
  80. # thread handling in acceptance tests. If you hit one of these, it's possible either that cleanup logic has
  81. # a bug, or you may be using a contextmanager incorrectly. Let us know and we can help!
  82. if SiloMode.get_current_mode() != SiloMode.MONOLITH:
  83. raise Exception(
  84. "Possible test leak bug! SiloMode was not reset to Monolith between tests. Please read the comment for validate_silo_mode() in tests/conftest.py."
  85. )
  86. yield
  87. if SiloMode.get_current_mode() != SiloMode.MONOLITH:
  88. raise Exception(
  89. "Possible test leak bug! SiloMode was not reset to Monolith between tests. Please read the comment for validate_silo_mode() in tests/conftest.py."
  90. )
  91. @pytest.fixture(autouse=True)
  92. def setup_simulate_on_commit(request):
  93. from sentry.testutils.hybrid_cloud import simulate_on_commit
  94. with simulate_on_commit(request):
  95. yield
  96. @pytest.fixture(autouse=True)
  97. def setup_enforce_monotonic_transactions(request):
  98. from sentry.testutils.hybrid_cloud import enforce_no_cross_transaction_interactions
  99. with enforce_no_cross_transaction_interactions():
  100. yield
  101. @pytest.fixture(autouse=True)
  102. def audit_hybrid_cloud_writes_and_deletes(request):
  103. """
  104. Ensure that write operations on hybrid cloud foreign keys are recorded
  105. alongside outboxes or use a context manager to indicate that the
  106. caller has considered outbox and didn't accidentally forget.
  107. Generally you can avoid assertion errors from these checks by:
  108. 1. Running deletion/write logic within an `outbox_context`.
  109. 2. Using Model.delete()/save methods that create outbox messages in the
  110. same transaction as a delete operation.
  111. Scenarios that are generally always unsafe are using
  112. `QuerySet.delete()`, `QuerySet.update()` or raw SQL to perform
  113. writes.
  114. The User.delete() method is a good example of how to safely
  115. delete records and generate outbox messages.
  116. """
  117. from sentry.testutils.silo import validate_protected_queries
  118. debug_cursor_state: MutableMapping[str, bool] = {}
  119. for conn in connections.all():
  120. debug_cursor_state[conn.alias] = conn.force_debug_cursor
  121. conn.queries_log.clear()
  122. conn.force_debug_cursor = True
  123. try:
  124. yield
  125. finally:
  126. for conn in connections.all():
  127. conn.force_debug_cursor = debug_cursor_state[conn.alias]
  128. validate_protected_queries(conn.queries)
  129. @pytest.fixture(autouse=True)
  130. def check_leaked_responses_mocks():
  131. yield
  132. leaked = responses.registered()
  133. if leaked:
  134. responses.reset()
  135. leaked_s = "".join(f"- {item}\n" for item in leaked)
  136. raise AssertionError(
  137. f"`responses` were leaked outside of the test context:\n{leaked_s}"
  138. f"(make sure to use `@responses.activate` or `with responses.mock:`)"
  139. )