test_minidump_full.py 8.3 KB

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