test_sdk.py 4.7 KB

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