test_example.py 5.0 KB

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