test_minidump_full.py 8.5 KB

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