test_sdk.py 4.9 KB

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