test_minidump_full.py 8.1 KB

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