test_unreal_full.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.cases import TransactionTestCase
  10. from sentry.testutils.factories import get_fixture_path
  11. from sentry.testutils.relay import RelayStoreHelper
  12. from sentry.testutils.skips import requires_kafka, requires_symbolicator
  13. from sentry.utils.safe import get_path
  14. from tests.symbolicator import normalize_native_exception
  15. # IMPORTANT:
  16. #
  17. # This test suite requires Symbolicator in order to run correctly.
  18. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  19. #
  20. # If you are using a local instance of Symbolicator, you need to
  21. # either change `system.url-prefix` option override inside `initialize` fixture to `system.internal-url-prefix`,
  22. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  23. pytestmark = [requires_symbolicator, requires_kafka]
  24. def get_unreal_crash_file():
  25. return get_fixture_path("native", "unreal_crash")
  26. def get_unreal_crash_apple_file():
  27. return get_fixture_path("native", "unreal_crash_apple")
  28. class SymbolicatorUnrealIntegrationTest(RelayStoreHelper, TransactionTestCase):
  29. @pytest.fixture(autouse=True)
  30. def initialize(self, live_server):
  31. self.project.update_option("sentry:builtin_symbol_sources", [])
  32. with patch("sentry.auth.system.is_internal_ip", return_value=True), self.options(
  33. {"system.url-prefix": live_server.url}
  34. ):
  35. # Run test case
  36. yield
  37. def upload_symbols(self):
  38. url = reverse(
  39. "sentry-api-0-dsym-files",
  40. kwargs={
  41. "organization_slug": self.project.organization.slug,
  42. "project_slug": self.project.slug,
  43. },
  44. )
  45. self.login_as(user=self.user)
  46. out = BytesIO()
  47. f = zipfile.ZipFile(out, "w")
  48. f.write(get_fixture_path("native", "unreal_crash.sym"), "crash.sym")
  49. f.close()
  50. response = self.client.post(
  51. url,
  52. {
  53. "file": SimpleUploadedFile(
  54. "symbols.zip", out.getvalue(), content_type="application/zip"
  55. )
  56. },
  57. format="multipart",
  58. )
  59. assert response.status_code == 201, response.content
  60. assert len(response.json()) == 1
  61. def unreal_crash_test_impl(self, filename):
  62. self.project.update_option("sentry:store_crash_reports", STORE_CRASH_REPORTS_ALL)
  63. self.upload_symbols()
  64. # attachments feature has to be on for the files extract stick around
  65. with self.feature("organizations:event-attachments"):
  66. with open(filename, "rb") as f:
  67. event = self.post_and_retrieve_unreal(f.read())
  68. self.insta_snapshot(
  69. {
  70. "contexts": event.data.get("contexts"),
  71. "exception": {
  72. "values": [
  73. normalize_native_exception(x)
  74. for x in get_path(event.data, "exception", "values") or ()
  75. ]
  76. },
  77. "stacktrace": event.data.get("stacktrace"),
  78. "threads": event.data.get("threads"),
  79. "extra": event.data.get("extra"),
  80. "sdk": event.data.get("sdk"),
  81. }
  82. )
  83. return sorted(EventAttachment.objects.filter(event_id=event.event_id), key=lambda x: x.name)
  84. def test_unreal_crash_with_attachments(self):
  85. attachments = self.unreal_crash_test_impl(get_unreal_crash_file())
  86. assert len(attachments) == 4
  87. context, config, minidump, log = attachments
  88. assert context.name == "CrashContext.runtime-xml"
  89. context_file = File.objects.get(id=context.file_id)
  90. assert context_file.type == "unreal.context"
  91. assert context_file.checksum == "835d3e10db5d1799dc625132c819c047261ddcfb"
  92. assert config.name == "CrashReportClient.ini"
  93. config_file = File.objects.get(id=config.file_id)
  94. assert config_file.type == "event.attachment"
  95. assert config_file.checksum == "5839c750bdde8cba4d2a979ea857b8154cffdab5"
  96. assert minidump.name == "UE4Minidump.dmp"
  97. minidump_file = File.objects.get(id=minidump.file_id)
  98. assert minidump_file.type == "event.minidump"
  99. assert minidump_file.checksum == "089d9fd3b5c0cc4426339ab46ec3835e4be83c0f"
  100. assert log.name == "YetAnother.log" # Log file is named after the project
  101. log_file = File.objects.get(id=log.file_id)
  102. assert log_file.type == "unreal.logs"
  103. assert log_file.checksum == "24d1c5f75334cd0912cc2670168d593d5fe6c081"
  104. def test_unreal_apple_crash_with_attachments(self):
  105. attachments = self.unreal_crash_test_impl(get_unreal_crash_apple_file())
  106. assert len(attachments) == 6
  107. context, config, diagnostics, log, info, minidump = attachments
  108. assert context.name == "CrashContext.runtime-xml"
  109. context_file = File.objects.get(id=context.file_id)
  110. assert context_file.type == "unreal.context"
  111. assert context_file.checksum == "5d2723a7d25111645702fcbbcb8e1d038db56c6e"
  112. assert config.name == "CrashReportClient.ini"
  113. config_file = File.objects.get(id=config.file_id)
  114. assert config_file.type == "event.attachment"
  115. assert config_file.checksum == "4d6a2736e3e4969a68b7adbe197b05c171c29ea0"
  116. assert diagnostics.name == "Diagnostics.txt"
  117. diagnostics_file = File.objects.get(id=diagnostics.file_id)
  118. assert diagnostics_file.type == "event.attachment"
  119. assert diagnostics_file.checksum == "aa271bf4e307a78005410234081945352e8fb236"
  120. assert log.name == "YetAnotherMac.log" # Log file is named after the project
  121. log_file = File.objects.get(id=log.file_id)
  122. assert log_file.type == "unreal.logs"
  123. assert log_file.checksum == "735e751a8b6b943dbc0abce0e6d096f4d48a0c1e"
  124. assert info.name == "info.txt"
  125. info_file = File.objects.get(id=info.file_id)
  126. assert info_file.type == "event.attachment"
  127. assert info_file.checksum == "279b27ac5d0e6792d088e0662ce1a18413b772bc"
  128. assert minidump.name == "minidump.dmp"
  129. minidump_file = File.objects.get(id=minidump.file_id)
  130. assert minidump_file.type == "event.applecrashreport"
  131. assert minidump_file.checksum == "728d0f4b09cf5a7942da3893b6db79ac842b701a"