test_payload_full.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.models import File, ProjectDebugFile
  9. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  10. from sentry.testutils.factories import get_fixture_path
  11. from sentry.testutils.helpers.datetime import before_now, iso_format
  12. from tests.symbolicator import insta_snapshot_native_stacktrace_data, redact_location
  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("native", "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. event = self.post_and_retrieve_event(REAL_RESOLVING_EVENT_DATA)
  93. assert event.data["culprit"] == "main"
  94. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  95. redact_location(candidates)
  96. event.data["debug_meta"]["images"][0]["candidates"] = candidates
  97. insta_snapshot_native_stacktrace_data(self, event.data)
  98. def test_debug_id_resolving(self):
  99. file = File.objects.create(
  100. name="crash.pdb", type="default", headers={"Content-Type": "text/x-breakpad"}
  101. )
  102. path = get_fixture_path("native", "windows.sym")
  103. with open(path, "rb") as f:
  104. file.putfile(f)
  105. ProjectDebugFile.objects.create(
  106. file=file,
  107. object_name="crash.pdb",
  108. cpu_name="x86",
  109. project_id=self.project.id,
  110. debug_id="3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  111. code_id="5AB380779000",
  112. )
  113. self.login_as(user=self.user)
  114. event_data = {
  115. "contexts": {
  116. "device": {"arch": "x86"},
  117. "os": {"build": "", "name": "Windows", "type": "os", "version": "10.0.14393"},
  118. },
  119. "debug_meta": {
  120. "images": [
  121. {
  122. "id": "3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  123. "image_addr": "0x2a0000",
  124. "image_size": 36864,
  125. "name": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  126. "type": "symbolic",
  127. }
  128. ]
  129. },
  130. "exception": {
  131. "stacktrace": {
  132. "frames": [
  133. {
  134. "function": "<unknown>",
  135. "instruction_addr": "0x2a2a3d",
  136. "package": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  137. }
  138. ]
  139. },
  140. "thread_id": 1636,
  141. "type": "EXCEPTION_ACCESS_VIOLATION_WRITE",
  142. "value": "Fatal Error: EXCEPTION_ACCESS_VIOLATION_WRITE",
  143. },
  144. "platform": "native",
  145. "timestamp": iso_format(before_now(seconds=1)),
  146. }
  147. event = self.post_and_retrieve_event(event_data)
  148. assert event.data["culprit"] == "main"
  149. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  150. redact_location(candidates)
  151. event.data["debug_meta"]["images"][0]["candidates"] = candidates
  152. insta_snapshot_native_stacktrace_data(self, event.data)
  153. def test_missing_dsym(self):
  154. self.login_as(user=self.user)
  155. event = self.post_and_retrieve_event(REAL_RESOLVING_EVENT_DATA)
  156. assert event.data["culprit"] == "unknown"
  157. insta_snapshot_native_stacktrace_data(self, event.data)
  158. def test_missing_debug_images(self):
  159. self.login_as(user=self.user)
  160. payload = dict(project=self.project.id, **REAL_RESOLVING_EVENT_DATA)
  161. del payload["debug_meta"]
  162. event = self.post_and_retrieve_event(payload)
  163. assert event.data["culprit"] == "unknown"
  164. insta_snapshot_native_stacktrace_data(self, event.data)
  165. def test_resolving_with_candidates_sentry_source(self):
  166. # Checks the candidates with a sentry source URI for location
  167. file = File.objects.create(
  168. name="crash.pdb", type="default", headers={"Content-Type": "text/x-breakpad"}
  169. )
  170. path = get_fixture_path("native", "windows.sym")
  171. with open(path, "rb") as f:
  172. file.putfile(f)
  173. ProjectDebugFile.objects.create(
  174. file=file,
  175. object_name="crash.pdb",
  176. cpu_name="x86",
  177. project_id=self.project.id,
  178. debug_id="3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  179. code_id="5AB380779000",
  180. )
  181. self.login_as(user=self.user)
  182. event_data = {
  183. "contexts": {
  184. "device": {"arch": "x86"},
  185. },
  186. "debug_meta": {
  187. "images": [
  188. {
  189. "id": "3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  190. "image_addr": "0x2a0000",
  191. "image_size": 36864,
  192. "name": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  193. "type": "symbolic",
  194. }
  195. ]
  196. },
  197. "exception": {
  198. "stacktrace": {
  199. "frames": [
  200. {
  201. "instruction_addr": "0x2a2a3d",
  202. }
  203. ]
  204. },
  205. "type": "EXCEPTION_ACCESS_VIOLATION_WRITE",
  206. "value": "Fatal Error: EXCEPTION_ACCESS_VIOLATION_WRITE",
  207. },
  208. "platform": "native",
  209. "timestamp": iso_format(before_now(seconds=1)),
  210. }
  211. event = self.post_and_retrieve_event(event_data)
  212. assert event.data["culprit"] == "main"
  213. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  214. redact_location(candidates)
  215. self.insta_snapshot(candidates)