test_sdk.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import uuid
  2. from unittest import mock
  3. import pytest
  4. import sentry_sdk
  5. from django.test.utils import override_settings
  6. from sentry_sdk import Hub, push_scope
  7. from sentry import eventstore
  8. from sentry.eventstore.models import Event
  9. from sentry.models.userrole import manage_default_super_admin_role
  10. from sentry.receivers import create_default_projects
  11. from sentry.silo.base import SiloMode
  12. from sentry.testutils.asserts import assert_mock_called_once_with_partial
  13. from sentry.testutils.pytest.fixtures import django_db_all
  14. from sentry.testutils.pytest.relay import adjust_settings_for_relay_tests
  15. from sentry.testutils.silo import assume_test_silo_mode, no_silo_test
  16. from sentry.testutils.skips import requires_kafka
  17. from sentry.utils.sdk import bind_organization_context, configure_sdk
  18. pytestmark = [requires_kafka]
  19. @pytest.fixture(autouse=True)
  20. def setup_fixtures():
  21. with assume_test_silo_mode(SiloMode.CONTROL):
  22. manage_default_super_admin_role()
  23. create_default_projects()
  24. @pytest.fixture
  25. def post_event_with_sdk(settings, relay_server, wait_for_ingest_consumer):
  26. adjust_settings_for_relay_tests(settings)
  27. settings.SENTRY_ENDPOINT = relay_server["url"]
  28. settings.SENTRY_PROJECT = 1
  29. configure_sdk()
  30. current_scope = sentry_sdk.Scope()
  31. isolation_scope = sentry_sdk.Scope()
  32. wait_for_ingest_consumer = wait_for_ingest_consumer(settings)
  33. def inner(*args, **kwargs):
  34. event_id = sentry_sdk.capture_event(*args, **kwargs)
  35. sentry_sdk.flush()
  36. with sentry_sdk.scope.use_scope(current_scope):
  37. with sentry_sdk.scope.use_isolation_scope(isolation_scope):
  38. return wait_for_ingest_consumer(
  39. lambda: eventstore.backend.get_event_by_id(settings.SENTRY_PROJECT, event_id)
  40. )
  41. yield inner
  42. @no_silo_test
  43. @override_settings(SENTRY_PROJECT=1)
  44. @django_db_all
  45. def test_simple(settings, post_event_with_sdk):
  46. event = post_event_with_sdk({"message": "internal client test"})
  47. assert event
  48. assert event.data["project"] == settings.SENTRY_PROJECT
  49. assert event.data["logentry"]["formatted"] == "internal client test"
  50. @no_silo_test
  51. @override_settings(SENTRY_PROJECT=1)
  52. @django_db_all
  53. def test_recursion_breaker(settings, post_event_with_sdk):
  54. # If this test terminates at all then we avoided recursion.
  55. settings.SENTRY_INGEST_CONSUMER_APM_SAMPLING = 1.0
  56. settings.SENTRY_PROJECT = 1
  57. event_id = uuid.uuid4().hex
  58. with mock.patch(
  59. "sentry.event_manager.EventManager.save", spec=Event, side_effect=ValueError("oh no!")
  60. ) as save:
  61. with pytest.raises(Exception):
  62. post_event_with_sdk({"message": "internal client test", "event_id": event_id})
  63. assert_mock_called_once_with_partial(save, settings.SENTRY_PROJECT, cache_key=f"e:{event_id}:1")
  64. @no_silo_test
  65. @django_db_all
  66. @override_settings(SENTRY_PROJECT=1)
  67. def test_encoding(settings, post_event_with_sdk):
  68. class NotJSONSerializable:
  69. pass
  70. with push_scope() as scope:
  71. scope.set_extra("request", NotJSONSerializable())
  72. event = post_event_with_sdk({"message": "check the req"})
  73. assert event.data["project"] == settings.SENTRY_PROJECT
  74. assert event.data["logentry"]["formatted"] == "check the req"
  75. assert "NotJSONSerializable" in event.data["extra"]["request"]
  76. @no_silo_test
  77. @override_settings(SENTRY_PROJECT=1)
  78. @django_db_all
  79. def test_bind_organization_context(default_organization):
  80. configure_sdk()
  81. bind_organization_context(default_organization)
  82. assert Hub.current.scope._tags["organization"] == default_organization.id
  83. assert Hub.current.scope._tags["organization.slug"] == default_organization.slug
  84. assert Hub.current.scope._contexts["organization"] == {
  85. "id": default_organization.id,
  86. "slug": default_organization.slug,
  87. }
  88. @no_silo_test
  89. @override_settings(SENTRY_PROJECT=1)
  90. @django_db_all
  91. def test_bind_organization_context_with_callback(default_organization):
  92. create_default_projects()
  93. configure_sdk()
  94. def add_context(scope, organization, **kwargs):
  95. scope.set_tag("organization.test", "1")
  96. with override_settings(SENTRY_ORGANIZATION_CONTEXT_HELPER=add_context):
  97. bind_organization_context(default_organization)
  98. assert Hub.current.scope._tags["organization.test"] == "1"
  99. @no_silo_test
  100. @override_settings(SENTRY_PROJECT=1)
  101. @django_db_all
  102. def test_bind_organization_context_with_callback_error(default_organization):
  103. configure_sdk()
  104. def add_context(scope, organization, **kwargs):
  105. 1 / 0
  106. with override_settings(SENTRY_ORGANIZATION_CONTEXT_HELPER=add_context):
  107. bind_organization_context(default_organization)
  108. assert Hub.current.scope._tags["organization"] == default_organization.id