test_integration.py 10 KB

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