test_payload_full.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import re
  2. import zipfile
  3. from io import BytesIO
  4. from unittest.mock import patch
  5. import pytest
  6. from django.core.files.uploadedfile import SimpleUploadedFile
  7. from django.urls import reverse
  8. from sentry import eventstore
  9. from sentry.models import File, ProjectDebugFile
  10. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  11. from sentry.testutils.helpers.datetime import before_now, iso_format
  12. from tests.symbolicator import get_fixture_path, insta_snapshot_stacktrace_data
  13. # IMPORTANT:
  14. # For these tests to run, write `symbolicator.enabled: true` into your
  15. # `~/.sentry/config.yml` and run `sentry devservices up`
  16. REAL_RESOLVING_EVENT_DATA = {
  17. "platform": "cocoa",
  18. "debug_meta": {
  19. "images": [
  20. {
  21. "type": "apple",
  22. "arch": "x86_64",
  23. "uuid": "502fc0a5-1ec1-3e47-9998-684fa139dca7",
  24. "image_vmaddr": "0x0000000100000000",
  25. "image_size": 4096,
  26. "image_addr": "0x0000000100000000",
  27. "name": "Foo.app/Contents/Foo",
  28. }
  29. ],
  30. "sdk_info": {
  31. "dsym_type": "macho",
  32. "sdk_name": "macOS",
  33. "version_major": 10,
  34. "version_minor": 12,
  35. "version_patchlevel": 4,
  36. },
  37. },
  38. "exception": {
  39. "values": [
  40. {
  41. "stacktrace": {
  42. "frames": [
  43. {"platform": "foobar", "function": "hi"},
  44. {"function": "unknown", "instruction_addr": "0x0000000100000fa0"},
  45. ]
  46. },
  47. "type": "Fail",
  48. "value": "fail",
  49. }
  50. ]
  51. },
  52. "timestamp": iso_format(before_now(seconds=1)),
  53. }
  54. class SymbolicatorResolvingIntegrationTest(RelayStoreHelper, TransactionTestCase):
  55. # For these tests to run, write `symbolicator.enabled: true` into your
  56. # `~/.sentry/config.yml` and run `sentry devservices up`
  57. @pytest.fixture(autouse=True)
  58. def initialize(self, live_server):
  59. self.project.update_option("sentry:builtin_symbol_sources", [])
  60. new_prefix = live_server.url
  61. with patch("sentry.auth.system.is_internal_ip", return_value=True), self.options(
  62. {"system.url-prefix": new_prefix}
  63. ):
  64. # Run test case:
  65. yield
  66. def get_event(self, event_id):
  67. return eventstore.get_event_by_id(self.project.id, event_id)
  68. def test_real_resolving(self):
  69. url = reverse(
  70. "sentry-api-0-dsym-files",
  71. kwargs={
  72. "organization_slug": self.project.organization.slug,
  73. "project_slug": self.project.slug,
  74. },
  75. )
  76. self.login_as(user=self.user)
  77. out = BytesIO()
  78. f = zipfile.ZipFile(out, "w")
  79. f.write(get_fixture_path("hello.dsym"), "dSYM/hello")
  80. f.close()
  81. response = self.client.post(
  82. url,
  83. {
  84. "file": SimpleUploadedFile(
  85. "symbols.zip", out.getvalue(), content_type="application/zip"
  86. )
  87. },
  88. format="multipart",
  89. )
  90. assert response.status_code == 201, response.content
  91. assert len(response.data) == 1
  92. with self.feature({"organizations:images-loaded-v2": False}):
  93. event = self.post_and_retrieve_event(REAL_RESOLVING_EVENT_DATA)
  94. assert event.data["culprit"] == "main"
  95. insta_snapshot_stacktrace_data(self, event.data)
  96. def test_debug_id_resolving(self):
  97. file = File.objects.create(
  98. name="crash.pdb", type="default", headers={"Content-Type": "text/x-breakpad"}
  99. )
  100. path = get_fixture_path("windows.sym")
  101. with open(path, "rb") as f:
  102. file.putfile(f)
  103. ProjectDebugFile.objects.create(
  104. file=file,
  105. object_name="crash.pdb",
  106. cpu_name="x86",
  107. project_id=self.project.id,
  108. debug_id="3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  109. code_id="5AB380779000",
  110. )
  111. self.login_as(user=self.user)
  112. event_data = {
  113. "contexts": {
  114. "device": {"arch": "x86"},
  115. "os": {"build": "", "name": "Windows", "type": "os", "version": "10.0.14393"},
  116. },
  117. "debug_meta": {
  118. "images": [
  119. {
  120. "id": "3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  121. "image_addr": "0x2a0000",
  122. "image_size": 36864,
  123. "name": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  124. "type": "symbolic",
  125. }
  126. ]
  127. },
  128. "exception": {
  129. "stacktrace": {
  130. "frames": [
  131. {
  132. "function": "<unknown>",
  133. "instruction_addr": "0x2a2a3d",
  134. "package": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  135. }
  136. ]
  137. },
  138. "thread_id": 1636,
  139. "type": "EXCEPTION_ACCESS_VIOLATION_WRITE",
  140. "value": "Fatal Error: EXCEPTION_ACCESS_VIOLATION_WRITE",
  141. },
  142. "platform": "native",
  143. "timestamp": iso_format(before_now(seconds=1)),
  144. }
  145. with self.feature({"organizations:images-loaded-v2": False}):
  146. event = self.post_and_retrieve_event(event_data)
  147. assert event.data["culprit"] == "main"
  148. insta_snapshot_stacktrace_data(self, event.data)
  149. def test_missing_dsym(self):
  150. self.login_as(user=self.user)
  151. with self.feature({"organizations:images-loaded-v2": False}):
  152. event = self.post_and_retrieve_event(REAL_RESOLVING_EVENT_DATA)
  153. assert event.data["culprit"] == "unknown"
  154. insta_snapshot_stacktrace_data(self, event.data)
  155. def test_missing_debug_images(self):
  156. self.login_as(user=self.user)
  157. payload = dict(project=self.project.id, **REAL_RESOLVING_EVENT_DATA)
  158. del payload["debug_meta"]
  159. with self.feature({"organizations:images-loaded-v2": False}):
  160. event = self.post_and_retrieve_event(payload)
  161. assert event.data["culprit"] == "unknown"
  162. insta_snapshot_stacktrace_data(self, event.data)
  163. def test_resolving_with_candidates_sentry_source(self):
  164. # Checks the candidates with a sentry source URI for location
  165. file = File.objects.create(
  166. name="crash.pdb", type="default", headers={"Content-Type": "text/x-breakpad"}
  167. )
  168. path = get_fixture_path("windows.sym")
  169. with open(path, "rb") as f:
  170. file.putfile(f)
  171. ProjectDebugFile.objects.create(
  172. file=file,
  173. object_name="crash.pdb",
  174. cpu_name="x86",
  175. project_id=self.project.id,
  176. debug_id="3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  177. code_id="5AB380779000",
  178. )
  179. self.login_as(user=self.user)
  180. event_data = {
  181. "contexts": {
  182. "device": {"arch": "x86"},
  183. },
  184. "debug_meta": {
  185. "images": [
  186. {
  187. "id": "3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  188. "image_addr": "0x2a0000",
  189. "image_size": 36864,
  190. "name": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  191. "type": "symbolic",
  192. }
  193. ]
  194. },
  195. "exception": {
  196. "stacktrace": {
  197. "frames": [
  198. {
  199. "instruction_addr": "0x2a2a3d",
  200. }
  201. ]
  202. },
  203. "type": "EXCEPTION_ACCESS_VIOLATION_WRITE",
  204. "value": "Fatal Error: EXCEPTION_ACCESS_VIOLATION_WRITE",
  205. },
  206. "platform": "native",
  207. "timestamp": iso_format(before_now(seconds=1)),
  208. }
  209. with self.feature("organizations:images-loaded-v2"):
  210. event = self.post_and_retrieve_event(event_data)
  211. assert event.data["culprit"] == "main"
  212. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  213. redact_location(candidates)
  214. self.insta_snapshot(candidates)
  215. def redact_location(candidates):
  216. """Redacts the sentry location URI to be independent of the specific ID.
  217. This modifies the data passed in, returns None.
  218. """
  219. location_re = re.compile("^sentry://project_debug_file/[0-9]+$")
  220. for candidate in candidates:
  221. try:
  222. location = candidate["location"]
  223. except KeyError:
  224. continue
  225. else:
  226. if location_re.search(location):
  227. candidate["location"] = "sentry://project_debug_file/x"