test_payload_full.py 8.5 KB

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