test_minidump_full.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import zipfile
  2. from io import BytesIO
  3. from unittest.mock import patch
  4. import pytest
  5. from django.core.files.uploadedfile import SimpleUploadedFile
  6. from django.urls import reverse
  7. from sentry import eventstore
  8. from sentry.lang.native.utils import STORE_CRASH_REPORTS_ALL
  9. from sentry.models import EventAttachment, File
  10. from sentry.testutils.cases import TransactionTestCase
  11. from sentry.testutils.factories import get_fixture_path
  12. from sentry.testutils.helpers.task_runner import BurstTaskRunner
  13. from sentry.testutils.relay import RelayStoreHelper
  14. from sentry.testutils.skips import requires_kafka, requires_symbolicator
  15. from sentry.utils.safe import get_path
  16. from tests.symbolicator import insta_snapshot_native_stacktrace_data, redact_location
  17. # IMPORTANT:
  18. #
  19. # This test suite requires Symbolicator in order to run correctly.
  20. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  21. #
  22. # If you are using a local instance of Symbolicator, you need to
  23. # either change `system.url-prefix` option override inside `initialize` fixture to `system.internal-url-prefix`,
  24. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  25. pytestmark = [requires_symbolicator, requires_kafka]
  26. @pytest.mark.snuba
  27. class SymbolicatorMinidumpIntegrationTest(RelayStoreHelper, TransactionTestCase):
  28. @pytest.fixture(autouse=True)
  29. def initialize(self, live_server, reset_snuba):
  30. self.project.update_option("sentry:builtin_symbol_sources", [])
  31. with patch("sentry.auth.system.is_internal_ip", return_value=True), self.options(
  32. {"system.url-prefix": live_server.url}
  33. ):
  34. # Run test case
  35. yield
  36. def upload_symbols(self):
  37. url = reverse(
  38. "sentry-api-0-dsym-files",
  39. kwargs={
  40. "organization_slug": self.project.organization.slug,
  41. "project_slug": self.project.slug,
  42. },
  43. )
  44. self.login_as(user=self.user)
  45. out = BytesIO()
  46. f = zipfile.ZipFile(out, "w")
  47. f.write(get_fixture_path("native", "windows.sym"), "crash.sym")
  48. f.close()
  49. response = self.client.post(
  50. url,
  51. {
  52. "file": SimpleUploadedFile(
  53. "symbols.zip", out.getvalue(), content_type="application/zip"
  54. )
  55. },
  56. format="multipart",
  57. )
  58. assert response.status_code == 201, response.content
  59. assert len(response.json()) == 1
  60. _FEATURES = {
  61. "organizations:event-attachments": True,
  62. "organizations:symbol-sources": False,
  63. "organizations:custom-symbol-sources": False,
  64. }
  65. def test_full_minidump(self):
  66. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  67. self.upload_symbols()
  68. with self.feature(self._FEATURES):
  69. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  70. event = self.post_and_retrieve_minidump(
  71. {
  72. "upload_file_minidump": f,
  73. "some_file": ("hello.txt", BytesIO(b"Hello World!")),
  74. },
  75. {
  76. "sentry[logger]": "test-logger",
  77. "sentry[level]": "error",
  78. },
  79. )
  80. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  81. redact_location(candidates)
  82. event.data["debug_meta"]["images"][0]["candidates"] = candidates
  83. insta_snapshot_native_stacktrace_data(self, event.data)
  84. assert event.data.get("logger") == "test-logger"
  85. assert event.data.get("level") == "error"
  86. # assert event.data.get("extra") == {"foo": "bar"}
  87. attachments = sorted(
  88. EventAttachment.objects.filter(event_id=event.event_id), key=lambda x: x.name
  89. )
  90. hello, minidump = attachments
  91. assert hello.name == "hello.txt"
  92. hello_file = File.objects.get(id=hello.file_id)
  93. assert hello_file.type == "event.attachment"
  94. assert hello_file.checksum == "2ef7bde608ce5404e97d5f042f95f89f1c232871"
  95. assert minidump.name == "windows.dmp"
  96. minidump_file = File.objects.get(id=minidump.file_id)
  97. assert minidump_file.type == "event.minidump"
  98. assert minidump_file.checksum == "74bb01c850e8d65d3ffbc5bad5cabc4668fce247"
  99. def test_full_minidump_json_extra(self):
  100. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  101. self.upload_symbols()
  102. with self.feature("organizations:event-attachments"):
  103. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  104. event = self.post_and_retrieve_minidump(
  105. {"upload_file_minidump": f},
  106. {"sentry": '{"logger":"test-logger"}', "foo": "bar"},
  107. )
  108. assert event.data.get("logger") == "test-logger"
  109. assert event.data.get("extra") == {"foo": "bar"}
  110. # Other assertions are performed by `test_full_minidump`
  111. def test_full_minidump_invalid_extra(self):
  112. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  113. self.upload_symbols()
  114. with self.feature("organizations:event-attachments"):
  115. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  116. event = self.post_and_retrieve_minidump(
  117. {"upload_file_minidump": f},
  118. {"sentry": "{{{{", "foo": "bar"}, # invalid sentry JSON
  119. )
  120. assert not event.data.get("logger")
  121. assert event.data.get("extra") == {"foo": "bar"}
  122. # Other assertions are performed by `test_full_minidump`
  123. def test_missing_dsym(self):
  124. with self.feature(self._FEATURES):
  125. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  126. event = self.post_and_retrieve_minidump(
  127. {"upload_file_minidump": f}, {"sentry[logger]": "test-logger"}
  128. )
  129. insta_snapshot_native_stacktrace_data(self, event.data)
  130. assert not EventAttachment.objects.filter(event_id=event.event_id)
  131. def test_reprocessing(self):
  132. # NOTE:
  133. # When running this test against a local symbolicator instance,
  134. # make sure that instance has its caches disabled. This test assumes
  135. # that a symbol upload has immediate effect, whereas in reality the
  136. # negative cache needs to expire first.
  137. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  138. features = dict(self._FEATURES)
  139. features["organizations:reprocessing-v2"] = True
  140. with self.feature(features):
  141. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  142. event = self.post_and_retrieve_minidump(
  143. {"upload_file_minidump": f}, {"sentry[logger]": "test-logger"}
  144. )
  145. insta_snapshot_native_stacktrace_data(self, event.data, subname="initial")
  146. self.upload_symbols()
  147. from sentry.tasks.reprocessing2 import reprocess_group
  148. with BurstTaskRunner() as burst:
  149. reprocess_group.delay(project_id=self.project.id, group_id=event.group_id)
  150. burst(max_jobs=100)
  151. new_event = eventstore.backend.get_event_by_id(self.project.id, event.event_id)
  152. assert new_event is not None
  153. assert new_event.event_id == event.event_id
  154. candidates = new_event.data["debug_meta"]["images"][0]["candidates"]
  155. redact_location(candidates)
  156. new_event.data["debug_meta"]["images"][0]["candidates"] = candidates
  157. insta_snapshot_native_stacktrace_data(self, new_event.data, subname="reprocessed")
  158. for event_id in (event.event_id, new_event.event_id):
  159. (minidump,) = sorted(
  160. EventAttachment.objects.filter(event_id=new_event.event_id), key=lambda x: x.name
  161. )
  162. assert minidump.name == "windows.dmp"
  163. minidump_file = File.objects.get(id=minidump.file_id)
  164. assert minidump_file.type == "event.minidump"
  165. assert minidump_file.checksum == "74bb01c850e8d65d3ffbc5bad5cabc4668fce247"
  166. def test_minidump_threadnames(self):
  167. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  168. with self.feature(self._FEATURES):
  169. with open(get_fixture_path("native", "threadnames.dmp"), "rb") as f:
  170. event = self.post_and_retrieve_minidump({"upload_file_minidump": f}, {})
  171. thread_name = get_path(event.data, "threads", "values", 1, "name")
  172. assert thread_name == "sentry-http"