test_example.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_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. def get_fixture_path(name):
  17. return os.path.join(os.path.dirname(__file__), "fixtures/example", name)
  18. def load_fixture(name):
  19. with open(get_fixture_path(name)) as f:
  20. return f.read()
  21. @django_db_all(transaction=True)
  22. class TestExample(RelayStoreHelper):
  23. @pytest.fixture(autouse=True)
  24. def initialize(
  25. self, default_projectkey, default_project, request, set_sentry_option, live_server
  26. ):
  27. self.project = default_project
  28. self.projectkey = default_projectkey
  29. self.project.update_option("sentry:scrape_javascript", False)
  30. with set_sentry_option("system.url-prefix", live_server.url):
  31. # Run test case
  32. yield
  33. @requires_symbolicator
  34. @pytest.mark.symbolicator
  35. def test_sourcemap_expansion(self):
  36. release = Release.objects.create(
  37. organization_id=self.project.organization_id, version="abc"
  38. )
  39. release.add_project(self.project)
  40. for file in ["test.min.js", "test.js", "test.min.js.map"]:
  41. with open(get_fixture_path(file), "rb") as f:
  42. f1 = File.objects.create(
  43. name=file,
  44. type="release.file",
  45. headers={},
  46. )
  47. f1.putfile(f)
  48. ReleaseFile.objects.create(
  49. name=f"http://example.com/{f1.name}",
  50. release_id=release.id,
  51. organization_id=self.project.organization_id,
  52. file=f1,
  53. )
  54. data = {
  55. "timestamp": iso_format(before_now(minutes=1)),
  56. "message": "hello",
  57. "platform": "javascript",
  58. "release": "abc",
  59. "exception": {
  60. "values": [
  61. {
  62. "type": "Error",
  63. "stacktrace": {
  64. "frames": [
  65. {
  66. "abs_path": "http://example.com/index.html",
  67. "filename": "index.html",
  68. "lineno": 6,
  69. "colno": 7,
  70. "function": "produceStack",
  71. },
  72. {
  73. "abs_path": "http://example.com/test.min.js",
  74. "filename": "test.min.js",
  75. "lineno": 1,
  76. "colno": 183,
  77. "function": "i",
  78. },
  79. {
  80. "abs_path": "http://example.com/test.min.js",
  81. "filename": "test.min.js",
  82. "lineno": 1,
  83. "colno": 136,
  84. "function": "r",
  85. },
  86. {
  87. "abs_path": "http://example.com/test.min.js",
  88. "filename": "test.min.js",
  89. "lineno": 1,
  90. "colno": 64,
  91. "function": "e",
  92. },
  93. ]
  94. },
  95. }
  96. ]
  97. },
  98. }
  99. event = self.post_and_retrieve_event(data)
  100. exception = event.interfaces["exception"]
  101. frame_list = exception.values[0].stacktrace.frames
  102. assert len(frame_list) == 4
  103. assert frame_list[0].function == "produceStack"
  104. assert frame_list[0].lineno == 6
  105. assert frame_list[0].filename == "index.html"
  106. assert frame_list[1].function == "test"
  107. assert frame_list[1].lineno == 20
  108. assert frame_list[1].filename == "test.js"
  109. assert frame_list[2].function == "invoke"
  110. assert frame_list[2].lineno == 15
  111. assert frame_list[2].filename == "test.js"
  112. assert frame_list[3].function == "onFailure"
  113. assert frame_list[3].lineno == 5
  114. assert frame_list[3].filename == "test.js"