test_integration.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. from io import BytesIO
  2. from unittest import mock
  3. from uuid import uuid4
  4. import pytest
  5. from sentry.models.eventattachment import EventAttachment
  6. from sentry.spans.grouping.utils import hash_values
  7. from sentry.tasks.relay import invalidate_project_config
  8. from sentry.testutils.cases import TransactionTestCase
  9. from sentry.testutils.helpers.datetime import before_now, iso_format, timestamp_format
  10. from sentry.testutils.relay import RelayStoreHelper
  11. from sentry.testutils.silo import region_silo_test
  12. from sentry.testutils.skips import requires_kafka
  13. pytestmark = [requires_kafka]
  14. @region_silo_test
  15. class SentryRemoteTest(RelayStoreHelper, TransactionTestCase):
  16. # used to be test_ungzipped_data
  17. def test_simple_data(self):
  18. event_data = {"message": "hello", "timestamp": iso_format(before_now(seconds=1))}
  19. event = self.post_and_retrieve_event(event_data)
  20. assert event.message == "hello"
  21. def test_csp(self):
  22. event_data = {
  23. "csp-report": {
  24. "document-uri": "https://example.com/foo/bar",
  25. "referrer": "https://www.google.com/",
  26. "violated-directive": "default-src self",
  27. "original-policy": "default-src self; report-uri /csp-hotline.php",
  28. "blocked-uri": "http://evilhackerscripts.com",
  29. }
  30. }
  31. event = self.post_and_retrieve_security_report(event_data)
  32. assert event.message == "Blocked 'default-src' from 'evilhackerscripts.com'"
  33. def test_hpkp(self):
  34. event_data = {
  35. "date-time": "2014-04-06T13:00:50Z",
  36. "hostname": "www.example.com",
  37. "port": 443,
  38. "effective-expiration-date": "2014-05-01T12:40:50Z",
  39. "include-subdomains": False,
  40. "served-certificate-chain": [
  41. "-----BEGIN CERTIFICATE-----\n MIIEBDCCAuygBQUAMEIxCzAJBgNVBAYTAlVT\n -----END CERTIFICATE-----"
  42. ],
  43. "validated-certificate-chain": [
  44. "-----BEGIN CERTIFICATE-----\n MIIEBDCCAuygAwIBAgIDCzAJBgNVBAYTAlVT\n -----END CERTIFICATE-----"
  45. ],
  46. "known-pins": [
  47. 'pin-sha256="d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM="',
  48. 'pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="',
  49. ],
  50. }
  51. event = self.post_and_retrieve_security_report(event_data)
  52. assert event.message == "Public key pinning validation failed for 'www.example.com'"
  53. assert event.group.title == "Public key pinning validation failed for 'www.example.com'"
  54. def test_expect_ct(self):
  55. event_data = {
  56. "expect-ct-report": {
  57. "date-time": "2014-04-06T13:00:50Z",
  58. "hostname": "www.example.com",
  59. "port": 443,
  60. "effective-expiration-date": "2014-05-01T12:40:50Z",
  61. "served-certificate-chain": [
  62. "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----"
  63. ],
  64. "validated-certificate-chain": [
  65. "-----BEGIN CERTIFICATE-----\nCDE\n-----END CERTIFICATE-----"
  66. ],
  67. "scts": [
  68. {
  69. "version": 1,
  70. "status": "invalid",
  71. "source": "embedded",
  72. "serialized_sct": "ABCD==",
  73. }
  74. ],
  75. }
  76. }
  77. event = self.post_and_retrieve_security_report(event_data)
  78. assert event.message == "Expect-CT failed for 'www.example.com'"
  79. assert event.group.title == "Expect-CT failed for 'www.example.com'"
  80. def test_expect_staple(self):
  81. event_data = {
  82. "expect-staple-report": {
  83. "date-time": "2014-04-06T13:00:50Z",
  84. "hostname": "www.example.com",
  85. "port": 443,
  86. "response-status": "ERROR_RESPONSE",
  87. "cert-status": "REVOKED",
  88. "effective-expiration-date": "2014-05-01T12:40:50Z",
  89. "served-certificate-chain": [
  90. "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----"
  91. ],
  92. "validated-certificate-chain": [
  93. "-----BEGIN CERTIFICATE-----\nCDE\n-----END CERTIFICATE-----"
  94. ],
  95. }
  96. }
  97. event = self.post_and_retrieve_security_report(event_data)
  98. assert event.message == "Expect-Staple failed for 'www.example.com'"
  99. assert event.group.title == "Expect-Staple failed for 'www.example.com'"
  100. def test_standalone_attachment(self):
  101. event_id = uuid4().hex
  102. # First, ingest the attachment and ensure it is saved
  103. files = {"some_file": ("hello.txt", BytesIO(b"Hello World!"))}
  104. self.post_and_retrieve_attachment(event_id, files)
  105. # Next, ingest an error event
  106. event = self.post_and_retrieve_event({"event_id": event_id, "message": "my error"})
  107. assert event.event_id == event_id
  108. assert event.group_id
  109. # Finally, fetch the updated attachment and compare the group id
  110. attachment = EventAttachment.objects.get(project_id=self.project.id, event_id=event_id)
  111. assert attachment.group_id == event.group_id
  112. def test_blob_only_attachment(self):
  113. event_id1 = uuid4().hex
  114. event_id2 = uuid4().hex
  115. files = {"some_file": ("hello.txt", BytesIO(b"Hello World! default"))}
  116. self.post_and_retrieve_attachment(event_id1, files)
  117. # Again, but using direct blob storage
  118. files = {"some_file": ("hello.txt", BytesIO(b"Hello World! direct"))}
  119. with self.options(
  120. {
  121. "eventattachments.store-blobs.sample-rate": 1,
  122. }
  123. ):
  124. self.post_and_retrieve_attachment(event_id2, files)
  125. attachments = EventAttachment.objects.filter(project_id=self.project.id)
  126. assert len(attachments) == 2
  127. attachment1 = EventAttachment.objects.get(event_id=event_id1)
  128. with attachment1.getfile() as blob:
  129. assert blob.read() == b"Hello World! default"
  130. assert attachment1.file_id is not None
  131. attachment2 = EventAttachment.objects.get(event_id=event_id2)
  132. with attachment2.getfile() as blob:
  133. assert blob.read() == b"Hello World! direct"
  134. assert attachment2.blob_path is not None
  135. def test_transaction(self):
  136. event_data = {
  137. "event_id": "d2132d31b39445f1938d7e21b6bf0ec4",
  138. "type": "transaction",
  139. "transaction": "/organizations/:orgId/performance/:eventSlug/",
  140. "start_timestamp": iso_format(before_now(minutes=1, milliseconds=500)),
  141. "timestamp": iso_format(before_now(minutes=1)),
  142. "contexts": {
  143. "trace": {
  144. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  145. "span_id": "8f5a2b8768cafb4e",
  146. "type": "trace",
  147. }
  148. },
  149. "spans": [
  150. {
  151. "description": "<OrganizationContext>",
  152. "op": "react.mount",
  153. "parent_span_id": "8f5a2b8768cafb4e",
  154. "span_id": "bd429c44b67a3eb4",
  155. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=250)),
  156. "timestamp": timestamp_format(before_now(minutes=1)),
  157. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  158. },
  159. {
  160. "description": "browser span",
  161. "op": "browser",
  162. "parent_span_id": "bd429c44b67a3eb4",
  163. "span_id": "a99fd04e79e17631",
  164. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  165. "timestamp": timestamp_format(before_now(minutes=1)),
  166. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  167. },
  168. {
  169. "description": "resource span",
  170. "op": "resource",
  171. "parent_span_id": "bd429c44b67a3eb4",
  172. "span_id": "a71a5e67db5ce938",
  173. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  174. "timestamp": timestamp_format(before_now(minutes=1)),
  175. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  176. },
  177. {
  178. "description": "http span",
  179. "op": "http",
  180. "parent_span_id": "a99fd04e79e17631",
  181. "span_id": "abe79ad9292b90a9",
  182. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  183. "timestamp": timestamp_format(before_now(minutes=1)),
  184. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  185. },
  186. {
  187. "description": "db span",
  188. "op": "db",
  189. "parent_span_id": "abe79ad9292b90a9",
  190. "span_id": "9c045ea336297177",
  191. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  192. "timestamp": timestamp_format(before_now(minutes=1)),
  193. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  194. },
  195. ],
  196. }
  197. event = self.post_and_retrieve_event(event_data)
  198. raw_event = event.get_raw_data()
  199. exclusive_times = [
  200. pytest.approx(50, abs=2),
  201. pytest.approx(0, abs=2),
  202. pytest.approx(200, abs=2),
  203. pytest.approx(0, abs=2),
  204. pytest.approx(200, abs=2),
  205. ]
  206. for actual, expected, exclusive_time in zip(
  207. raw_event["spans"], event_data["spans"], exclusive_times
  208. ):
  209. assert actual == dict(
  210. expected,
  211. exclusive_time=exclusive_time,
  212. hash=hash_values([expected["description"]]),
  213. )
  214. assert raw_event["breakdowns"] == {
  215. "span_ops": {
  216. "ops.browser": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  217. "ops.resource": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  218. "ops.http": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  219. "ops.db": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  220. "total.time": {"unit": "millisecond", "value": pytest.approx(1050, abs=2)},
  221. }
  222. }
  223. def test_project_config_compression(self):
  224. # Populate redis cache with compressed config:
  225. invalidate_project_config(public_key=self.projectkey, trigger="test")
  226. # Disable project config endpoint, to make sure Relay gets its data
  227. # from redis:
  228. with mock.patch(
  229. "sentry.api.endpoints.relay.project_configs.RelayProjectConfigsEndpoint.post"
  230. ):
  231. event_data = {"message": "hello", "timestamp": iso_format(before_now(seconds=1))}
  232. event = self.post_and_retrieve_event(event_data)
  233. assert event.message == "hello"