test_example.py 4.8 KB

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