test_sdk.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from __future__ import absolute_import, print_function
  2. import uuid
  3. import pytest
  4. import sentry_sdk
  5. from sentry_sdk import Hub, push_scope
  6. from django.conf import settings
  7. from sentry.utils.sdk import configure_sdk, bind_organization_context
  8. from sentry.utils.compat import mock
  9. from sentry import eventstore
  10. from sentry.testutils import assert_mock_called_once_with_partial
  11. from sentry.utils.pytest.relay import adjust_settings_for_relay_tests
  12. @pytest.fixture
  13. def post_event_with_sdk(settings, relay_server, wait_for_ingest_consumer):
  14. adjust_settings_for_relay_tests(settings)
  15. settings.SENTRY_ENDPOINT = relay_server["url"]
  16. configure_sdk()
  17. wait_for_ingest_consumer = wait_for_ingest_consumer(settings)
  18. def inner(*args, **kwargs):
  19. event_id = sentry_sdk.capture_event(*args, **kwargs)
  20. Hub.current.client.flush()
  21. with push_scope():
  22. return wait_for_ingest_consumer(
  23. lambda: eventstore.get_event_by_id(settings.SENTRY_PROJECT, event_id)
  24. )
  25. return inner
  26. @pytest.mark.django_db
  27. def test_simple(post_event_with_sdk):
  28. event = post_event_with_sdk({"message": "internal client test"})
  29. assert event
  30. assert event.data["project"] == settings.SENTRY_PROJECT
  31. assert event.data["logentry"]["formatted"] == "internal client test"
  32. @pytest.mark.django_db
  33. def test_recursion_breaker(settings, post_event_with_sdk):
  34. # If this test terminates at all then we avoided recursion.
  35. settings.SENTRY_INGEST_CONSUMER_APM_SAMPLING = 1.0
  36. event_id = uuid.uuid4().hex
  37. with mock.patch(
  38. "sentry.event_manager.EventManager.save", side_effect=ValueError("oh no!")
  39. ) as save:
  40. with pytest.raises(ValueError):
  41. post_event_with_sdk({"message": "internal client test", "event_id": event_id})
  42. assert_mock_called_once_with_partial(
  43. save, settings.SENTRY_PROJECT, cache_key=u"e:{}:1".format(event_id)
  44. )
  45. @pytest.mark.django_db
  46. def test_encoding(post_event_with_sdk):
  47. class NotJSONSerializable:
  48. pass
  49. with push_scope() as scope:
  50. scope.set_extra("request", NotJSONSerializable())
  51. event = post_event_with_sdk({"message": "check the req"})
  52. assert event.data["project"] == settings.SENTRY_PROJECT
  53. assert event.data["logentry"]["formatted"] == "check the req"
  54. assert "NotJSONSerializable" in event.data["extra"]["request"]
  55. @pytest.mark.django_db
  56. def test_bind_organization_context(default_organization):
  57. configure_sdk()
  58. bind_organization_context(default_organization)
  59. assert Hub.current.scope._tags["organization"] == default_organization.id
  60. assert Hub.current.scope._tags["organization.slug"] == default_organization.slug
  61. assert Hub.current.scope._contexts["organization"] == {
  62. "id": default_organization.id,
  63. "slug": default_organization.slug,
  64. }
  65. @pytest.mark.django_db
  66. def test_bind_organization_context_with_callback(settings, default_organization):
  67. configure_sdk()
  68. def add_context(scope, organization, **kwargs):
  69. scope.set_tag("organization.test", "1")
  70. settings.SENTRY_ORGANIZATION_CONTEXT_HELPER = add_context
  71. bind_organization_context(default_organization)
  72. assert Hub.current.scope._tags["organization.test"] == "1"
  73. @pytest.mark.django_db
  74. def test_bind_organization_context_with_callback_error(settings, default_organization):
  75. configure_sdk()
  76. def add_context(scope, organization, **kwargs):
  77. 1 / 0
  78. settings.SENTRY_ORGANIZATION_CONTEXT_HELPER = add_context
  79. bind_organization_context(default_organization)
  80. assert Hub.current.scope._tags["organization"] == default_organization.id