test_sentry.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import pytest
  2. from flexmock import flexmock
  3. import borgmatic.hooks.monitoring.monitor
  4. from borgmatic.hooks.monitoring import sentry as module
  5. @pytest.mark.parametrize(
  6. 'state,configured_states,configured_environment,expected_status',
  7. (
  8. (borgmatic.hooks.monitoring.monitor.State.START, ['start'], None, 'in_progress'),
  9. (
  10. borgmatic.hooks.monitoring.monitor.State.START,
  11. ['start', 'finish', 'fail'],
  12. None,
  13. 'in_progress'
  14. ),
  15. (borgmatic.hooks.monitoring.monitor.State.START, None, 'production', 'in_progress'),
  16. (borgmatic.hooks.monitoring.monitor.State.FINISH, ['finish'], 'development', 'ok'),
  17. (borgmatic.hooks.monitoring.monitor.State.FAIL, ['fail'], 'another-environment', 'error'),
  18. ),
  19. )
  20. def test_ping_monitor_constructs_cron_url_and_pings_it(state, configured_states, configured_environment, expected_status):
  21. hook_config = {
  22. 'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
  23. 'monitor_slug': 'test',
  24. }
  25. if configured_states:
  26. hook_config['states'] = configured_states
  27. environment_query = ''
  28. if configured_environment:
  29. hook_config['environment'] = configured_environment
  30. environment_query = f'&environment={configured_environment}'
  31. flexmock(module.requests).should_receive('post').with_args(
  32. f'https://o294220.ingest.us.sentry.io/api/203069/cron/test/5f80ec/?status={expected_status}{environment_query}',
  33. timeout=int,
  34. headers={'User-Agent': 'borgmatic'},
  35. ).and_return(flexmock(ok=True)).once()
  36. module.ping_monitor(
  37. hook_config,
  38. {},
  39. 'config.yaml',
  40. state,
  41. monitoring_log_level=1,
  42. dry_run=False,
  43. )
  44. def test_ping_monitor_with_unconfigured_state_bails():
  45. hook_config = {
  46. 'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
  47. 'monitor_slug': 'test',
  48. 'states': ['fail'],
  49. }
  50. flexmock(module.requests).should_receive('post').never()
  51. module.ping_monitor(
  52. hook_config,
  53. {},
  54. 'config.yaml',
  55. borgmatic.hooks.monitoring.monitor.State.START,
  56. monitoring_log_level=1,
  57. dry_run=False,
  58. )
  59. @pytest.mark.parametrize(
  60. 'data_source_name_url',
  61. (
  62. '5f80ec@o294220.ingest.us.sentry.io/203069',
  63. 'https://o294220.ingest.us.sentry.io/203069',
  64. 'https://5f80ec@/203069',
  65. 'https://5f80ec@o294220.ingest.us.sentry.io',
  66. ),
  67. )
  68. def test_ping_monitor_with_invalid_data_source_name_url_bails(data_source_name_url):
  69. hook_config = {
  70. 'data_source_name_url': data_source_name_url,
  71. 'monitor_slug': 'test',
  72. }
  73. flexmock(module.requests).should_receive('post').never()
  74. module.ping_monitor(
  75. hook_config,
  76. {},
  77. 'config.yaml',
  78. borgmatic.hooks.monitoring.monitor.State.START,
  79. monitoring_log_level=1,
  80. dry_run=False,
  81. )
  82. def test_ping_monitor_with_invalid_sentry_state_bails():
  83. hook_config = {
  84. 'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
  85. 'monitor_slug': 'test',
  86. # This should never actually happen in practice, because the config schema prevents it.
  87. 'states': ['log'],
  88. }
  89. flexmock(module.requests).should_receive('post').never()
  90. module.ping_monitor(
  91. hook_config,
  92. {},
  93. 'config.yaml',
  94. borgmatic.hooks.monitoring.monitor.State.LOG,
  95. monitoring_log_level=1,
  96. dry_run=False,
  97. )
  98. def test_ping_monitor_with_dry_run_bails():
  99. hook_config = {
  100. 'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
  101. 'monitor_slug': 'test',
  102. }
  103. flexmock(module.requests).should_receive('post').never()
  104. module.ping_monitor(
  105. hook_config,
  106. {},
  107. 'config.yaml',
  108. borgmatic.hooks.monitoring.monitor.State.START,
  109. monitoring_log_level=1,
  110. dry_run=True,
  111. )
  112. def test_ping_monitor_with_network_error_does_not_raise():
  113. hook_config = {
  114. 'data_source_name_url': 'https://5f80ec@o294220.ingest.us.sentry.io/203069',
  115. 'monitor_slug': 'test',
  116. }
  117. response = flexmock(ok=False)
  118. response.should_receive('raise_for_status').and_raise(
  119. module.requests.exceptions.ConnectionError,
  120. )
  121. flexmock(module.requests).should_receive('post').with_args(
  122. 'https://o294220.ingest.us.sentry.io/api/203069/cron/test/5f80ec/?status=in_progress',
  123. timeout=int,
  124. headers={'User-Agent': 'borgmatic'},
  125. ).and_return(response).once()
  126. module.ping_monitor(
  127. hook_config,
  128. {},
  129. 'config.yaml',
  130. borgmatic.hooks.monitoring.monitor.State.START,
  131. monitoring_log_level=1,
  132. dry_run=False,
  133. )