test_example.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import os
  2. import pytest
  3. from sentry.models import File, Release, ReleaseFile
  4. from sentry.testutils import RelayStoreHelper
  5. from sentry.testutils.helpers.datetime import before_now, iso_format
  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` to `system.internal-url-prefix`
  13. # inside `process_with_symbolicator` fixture inside `src/sentry/utils/pytest/fixtures.py`,
  14. # or add `127.0.0.1 host.docker.internal` 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(self, default_projectkey, default_project):
  24. self.project = default_project
  25. self.projectkey = default_projectkey
  26. self.project.update_option("sentry:scrape_javascript", False)
  27. @requires_symbolicator
  28. @pytest.mark.symbolicator
  29. def test_sourcemap_expansion(self, process_with_symbolicator):
  30. release = Release.objects.create(
  31. organization_id=self.project.organization_id, version="abc"
  32. )
  33. release.add_project(self.project)
  34. for file in ["test.min.js", "test.js", "test.min.js.map"]:
  35. with open(get_fixture_path(file), "rb") as f:
  36. f1 = File.objects.create(
  37. name=file,
  38. type="release.file",
  39. headers={},
  40. )
  41. f1.putfile(f)
  42. ReleaseFile.objects.create(
  43. name=f"http://example.com/{f1.name}",
  44. release_id=release.id,
  45. organization_id=self.project.organization_id,
  46. file=f1,
  47. )
  48. data = {
  49. "timestamp": iso_format(before_now(minutes=1)),
  50. "message": "hello",
  51. "platform": "javascript",
  52. "release": "abc",
  53. "exception": {
  54. "values": [
  55. {
  56. "type": "Error",
  57. "stacktrace": {
  58. "frames": [
  59. {
  60. "abs_path": "http://example.com/index.html",
  61. "filename": "index.html",
  62. "lineno": 6,
  63. "colno": 7,
  64. "function": "produceStack",
  65. },
  66. {
  67. "abs_path": "http://example.com/test.min.js",
  68. "filename": "test.min.js",
  69. "lineno": 1,
  70. "colno": 183,
  71. "function": "i",
  72. },
  73. {
  74. "abs_path": "http://example.com/test.min.js",
  75. "filename": "test.min.js",
  76. "lineno": 1,
  77. "colno": 136,
  78. "function": "r",
  79. },
  80. {
  81. "abs_path": "http://example.com/test.min.js",
  82. "filename": "test.min.js",
  83. "lineno": 1,
  84. "colno": 64,
  85. "function": "e",
  86. },
  87. ]
  88. },
  89. }
  90. ]
  91. },
  92. }
  93. event = self.post_and_retrieve_event(data)
  94. exception = event.interfaces["exception"]
  95. frame_list = exception.values[0].stacktrace.frames
  96. assert len(frame_list) == 4
  97. assert frame_list[0].function == "produceStack"
  98. assert frame_list[0].lineno == 6
  99. assert frame_list[0].filename == "index.html"
  100. assert frame_list[1].function == "test"
  101. assert frame_list[1].lineno == 20
  102. assert frame_list[1].filename == "test.js"
  103. assert frame_list[2].function == "invoke"
  104. assert frame_list[2].lineno == 15
  105. assert frame_list[2].filename == "test.js"
  106. assert frame_list[3].function == "onFailure"
  107. assert frame_list[3].lineno == 5
  108. assert frame_list[3].filename == "test.js"