test_sentry.py 4.7 KB

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