test_unreal_full.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.lang.native.utils import STORE_CRASH_REPORTS_ALL
  8. from sentry.models import EventAttachment, File
  9. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  10. from sentry.testutils.factories import get_fixture_path
  11. from sentry.utils.safe import get_path
  12. from tests.symbolicator import normalize_native_exception
  13. # IMPORTANT:
  14. #
  15. # This test suite requires Symbolicator in order to run correctly.
  16. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  17. #
  18. # If you are using a local instance of Symbolicator, you need to
  19. # either change `system.url-prefix` option override inside `initialize` fixture to `system.internal-url-prefix`,
  20. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  21. def get_unreal_crash_file():
  22. return get_fixture_path("native", "unreal_crash")
  23. def get_unreal_crash_apple_file():
  24. return get_fixture_path("native", "unreal_crash_apple")
  25. class SymbolicatorUnrealIntegrationTest(RelayStoreHelper, TransactionTestCase):
  26. @pytest.fixture(autouse=True)
  27. def initialize(self, live_server):
  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", "unreal_crash.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.data) == 1
  58. def unreal_crash_test_impl(self, filename):
  59. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  60. self.upload_symbols()
  61. # attachments feature has to be on for the files extract stick around
  62. with self.feature("organizations:event-attachments"):
  63. with open(filename, "rb") as f:
  64. event = self.post_and_retrieve_unreal(f.read())
  65. self.insta_snapshot(
  66. {
  67. "contexts": event.data.get("contexts"),
  68. "exception": {
  69. "values": [
  70. normalize_native_exception(x)
  71. for x in get_path(event.data, "exception", "values") or ()
  72. ]
  73. },
  74. "stacktrace": event.data.get("stacktrace"),
  75. "threads": event.data.get("threads"),
  76. "extra": event.data.get("extra"),
  77. "sdk": event.data.get("sdk"),
  78. }
  79. )
  80. return sorted(EventAttachment.objects.filter(event_id=event.event_id), key=lambda x: x.name)
  81. def test_unreal_crash_with_attachments(self):
  82. attachments = self.unreal_crash_test_impl(get_unreal_crash_file())
  83. assert len(attachments) == 4
  84. context, config, minidump, log = attachments
  85. assert context.name == "CrashContext.runtime-xml"
  86. context_file = File.objects.get(id=context.file_id)
  87. assert context_file.type == "unreal.context"
  88. assert context_file.checksum == "835d3e10db5d1799dc625132c819c047261ddcfb"
  89. assert config.name == "CrashReportClient.ini"
  90. config_file = File.objects.get(id=config.file_id)
  91. assert config_file.type == "event.attachment"
  92. assert config_file.checksum == "5839c750bdde8cba4d2a979ea857b8154cffdab5"
  93. assert minidump.name == "UE4Minidump.dmp"
  94. minidump_file = File.objects.get(id=minidump.file_id)
  95. assert minidump_file.type == "event.minidump"
  96. assert minidump_file.checksum == "089d9fd3b5c0cc4426339ab46ec3835e4be83c0f"
  97. assert log.name == "YetAnother.log" # Log file is named after the project
  98. log_file = File.objects.get(id=log.file_id)
  99. assert log_file.type == "unreal.logs"
  100. assert log_file.checksum == "24d1c5f75334cd0912cc2670168d593d5fe6c081"
  101. def test_unreal_apple_crash_with_attachments(self):
  102. attachments = self.unreal_crash_test_impl(get_unreal_crash_apple_file())
  103. assert len(attachments) == 6
  104. context, config, diagnostics, log, info, minidump = attachments
  105. assert context.name == "CrashContext.runtime-xml"
  106. context_file = File.objects.get(id=context.file_id)
  107. assert context_file.type == "unreal.context"
  108. assert context_file.checksum == "5d2723a7d25111645702fcbbcb8e1d038db56c6e"
  109. assert config.name == "CrashReportClient.ini"
  110. config_file = File.objects.get(id=config.file_id)
  111. assert config_file.type == "event.attachment"
  112. assert config_file.checksum == "4d6a2736e3e4969a68b7adbe197b05c171c29ea0"
  113. assert diagnostics.name == "Diagnostics.txt"
  114. diagnostics_file = File.objects.get(id=diagnostics.file_id)
  115. assert diagnostics_file.type == "event.attachment"
  116. assert diagnostics_file.checksum == "aa271bf4e307a78005410234081945352e8fb236"
  117. assert log.name == "YetAnotherMac.log" # Log file is named after the project
  118. log_file = File.objects.get(id=log.file_id)
  119. assert log_file.type == "unreal.logs"
  120. assert log_file.checksum == "735e751a8b6b943dbc0abce0e6d096f4d48a0c1e"
  121. assert info.name == "info.txt"
  122. info_file = File.objects.get(id=info.file_id)
  123. assert info_file.type == "event.attachment"
  124. assert info_file.checksum == "279b27ac5d0e6792d088e0662ce1a18413b772bc"
  125. assert minidump.name == "minidump.dmp"
  126. minidump_file = File.objects.get(id=minidump.file_id)
  127. assert minidump_file.type == "event.applecrashreport"
  128. assert minidump_file.checksum == "728d0f4b09cf5a7942da3893b6db79ac842b701a"