test_minidump_full.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_stacktrace_data
  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. insta_snapshot_stacktrace_data(self, event.data)
  72. assert event.data.get("logger") == "test-logger"
  73. # assert event.data.get("extra") == {"foo": "bar"}
  74. attachments = sorted(
  75. EventAttachment.objects.filter(event_id=event.event_id), key=lambda x: x.name
  76. )
  77. hello, minidump = attachments
  78. assert hello.name == "hello.txt"
  79. hello_file = File.objects.get(id=hello.file_id)
  80. assert hello_file.type == "event.attachment"
  81. assert hello_file.checksum == "2ef7bde608ce5404e97d5f042f95f89f1c232871"
  82. assert minidump.name == "windows.dmp"
  83. minidump_file = File.objects.get(id=minidump.file_id)
  84. assert minidump_file.type == "event.minidump"
  85. assert minidump_file.checksum == "74bb01c850e8d65d3ffbc5bad5cabc4668fce247"
  86. def test_full_minidump_json_extra(self):
  87. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  88. self.upload_symbols()
  89. with self.feature("organizations:event-attachments"):
  90. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  91. event = self.post_and_retrieve_minidump(
  92. {"upload_file_minidump": f},
  93. {"sentry": '{"logger":"test-logger"}', "foo": "bar"},
  94. )
  95. assert event.data.get("logger") == "test-logger"
  96. assert event.data.get("extra") == {"foo": "bar"}
  97. # Other assertions are performed by `test_full_minidump`
  98. def test_full_minidump_invalid_extra(self):
  99. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  100. self.upload_symbols()
  101. with self.feature("organizations:event-attachments"):
  102. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  103. event = self.post_and_retrieve_minidump(
  104. {"upload_file_minidump": f},
  105. {"sentry": "{{{{", "foo": "bar"}, # invalid sentry JSON
  106. )
  107. assert not event.data.get("logger")
  108. assert event.data.get("extra") == {"foo": "bar"}
  109. # Other assertions are performed by `test_full_minidump`
  110. def test_missing_dsym(self):
  111. with self.feature(self._FEATURES):
  112. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  113. event = self.post_and_retrieve_minidump(
  114. {"upload_file_minidump": f}, {"sentry[logger]": "test-logger"}
  115. )
  116. insta_snapshot_stacktrace_data(self, event.data)
  117. assert not EventAttachment.objects.filter(event_id=event.event_id)
  118. def test_reprocessing(self):
  119. # NOTE:
  120. # When running this test against a local symbolicator instance,
  121. # make sure that instance has its caches disabled. This test assumes
  122. # that a symbol upload has immediate effect, whereas in reality the
  123. # negative cache needs to expire first.
  124. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  125. features = dict(self._FEATURES)
  126. features["organizations:reprocessing-v2"] = True
  127. with self.feature(features):
  128. with open(get_fixture_path("native", "windows.dmp"), "rb") as f:
  129. event = self.post_and_retrieve_minidump(
  130. {"upload_file_minidump": f}, {"sentry[logger]": "test-logger"}
  131. )
  132. insta_snapshot_stacktrace_data(self, event.data, subname="initial")
  133. self.upload_symbols()
  134. from sentry.tasks.reprocessing2 import reprocess_group
  135. with BurstTaskRunner() as burst:
  136. reprocess_group.delay(project_id=self.project.id, group_id=event.group_id)
  137. burst(max_jobs=100)
  138. new_event = eventstore.get_event_by_id(self.project.id, event.event_id)
  139. assert new_event is not None
  140. assert new_event.event_id == event.event_id
  141. insta_snapshot_stacktrace_data(self, new_event.data, subname="reprocessed")
  142. for event_id in (event.event_id, new_event.event_id):
  143. (minidump,) = sorted(
  144. EventAttachment.objects.filter(event_id=new_event.event_id), key=lambda x: x.name
  145. )
  146. assert minidump.name == "windows.dmp"
  147. minidump_file = File.objects.get(id=minidump.file_id)
  148. assert minidump_file.type == "event.minidump"
  149. assert minidump_file.checksum == "74bb01c850e8d65d3ffbc5bad5cabc4668fce247"
  150. def test_minidump_threadnames(self):
  151. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  152. with self.feature(self._FEATURES):
  153. with open(get_fixture_path("native", "threadnames.dmp"), "rb") as f:
  154. event = self.post_and_retrieve_minidump({"upload_file_minidump": f}, {})
  155. thread_name = get_path(event.data, "threads", "values", 1, "name")
  156. assert thread_name == "sentry-http"