test_example.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import os
  2. import pytest
  3. from sentry.models.files.file import File
  4. from sentry.models.release import Release
  5. from sentry.models.releasefile import ReleaseFile
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. from sentry.testutils.pytest.fixtures import django_db_all
  8. from sentry.testutils.relay import RelayStoreHelper
  9. from sentry.testutils.skips import requires_kafka, requires_symbolicator
  10. # IMPORTANT:
  11. #
  12. # This test suite requires Symbolicator in order to run correctly.
  13. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  14. #
  15. # If you are using a local instance of Symbolicator, you need to either change `system.url-prefix`
  16. # to `system.internal-url-prefix` inside `initialize` method below, or add `127.0.0.1 host.docker.internal`
  17. # entry to your `/etc/hosts`
  18. pytestmark = [requires_symbolicator, requires_kafka]
  19. def get_fixture_path(name):
  20. return os.path.join(os.path.dirname(__file__), "fixtures/example", name)
  21. def load_fixture(name):
  22. with open(get_fixture_path(name)) as f:
  23. return f.read()
  24. @django_db_all(transaction=True)
  25. class TestExample(RelayStoreHelper):
  26. @pytest.fixture(autouse=True)
  27. def initialize(
  28. self, default_projectkey, default_project, request, set_sentry_option, live_server
  29. ):
  30. self.project = default_project
  31. self.projectkey = default_projectkey
  32. self.project.update_option("sentry:scrape_javascript", False)
  33. with set_sentry_option("system.url-prefix", live_server.url):
  34. # Run test case
  35. yield
  36. @requires_symbolicator
  37. @pytest.mark.symbolicator
  38. def test_sourcemap_expansion(self):
  39. release = Release.objects.create(
  40. organization_id=self.project.organization_id, version="abc"
  41. )
  42. release.add_project(self.project)
  43. for file in ["test.min.js", "test.js", "test.min.js.map"]:
  44. with open(get_fixture_path(file), "rb") as f:
  45. f1 = File.objects.create(
  46. name=file,
  47. type="release.file",
  48. headers={},
  49. )
  50. f1.putfile(f)
  51. ReleaseFile.objects.create(
  52. name=f"http://example.com/{f1.name}",
  53. release_id=release.id,
  54. organization_id=self.project.organization_id,
  55. file=f1,
  56. )
  57. data = {
  58. "timestamp": iso_format(before_now(minutes=1)),
  59. "message": "hello",
  60. "platform": "javascript",
  61. "release": "abc",
  62. "exception": {
  63. "values": [
  64. {
  65. "type": "Error",
  66. "stacktrace": {
  67. "frames": [
  68. {
  69. "abs_path": "http://example.com/index.html",
  70. "filename": "index.html",
  71. "lineno": 6,
  72. "colno": 7,
  73. "function": "produceStack",
  74. },
  75. {
  76. "abs_path": "http://example.com/test.min.js",
  77. "filename": "test.min.js",
  78. "lineno": 1,
  79. "colno": 183,
  80. "function": "i",
  81. },
  82. {
  83. "abs_path": "http://example.com/test.min.js",
  84. "filename": "test.min.js",
  85. "lineno": 1,
  86. "colno": 136,
  87. "function": "r",
  88. },
  89. {
  90. "abs_path": "http://example.com/test.min.js",
  91. "filename": "test.min.js",
  92. "lineno": 1,
  93. "colno": 64,
  94. "function": "e",
  95. },
  96. ]
  97. },
  98. }
  99. ]
  100. },
  101. }
  102. event = self.post_and_retrieve_event(data)
  103. exception = event.interfaces["exception"]
  104. frame_list = exception.values[0].stacktrace.frames
  105. assert len(frame_list) == 4
  106. assert frame_list[0].function == "produceStack"
  107. assert frame_list[0].lineno == 6
  108. assert frame_list[0].filename == "index.html"
  109. assert frame_list[1].function == "test"
  110. assert frame_list[1].lineno == 20
  111. assert frame_list[1].filename == "test.js"
  112. assert frame_list[2].function == "invoke"
  113. assert frame_list[2].lineno == 15
  114. assert frame_list[2].filename == "test.js"
  115. assert frame_list[3].function == "onFailure"
  116. assert frame_list[3].lineno == 5
  117. assert frame_list[3].filename == "test.js"