test_ntfy.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from enum import Enum
  2. from flexmock import flexmock
  3. from borgmatic.hooks import ntfy as module
  4. default_base_url = 'https://ntfy.sh'
  5. custom_base_url = 'https://ntfy.example.com'
  6. topic = 'borgmatic-unit-testing'
  7. custom_message_config = {
  8. 'title': 'Borgmatic unit testing',
  9. 'message': 'Borgmatic unit testing',
  10. 'priority': 'min',
  11. 'tags': '+1',
  12. }
  13. custom_message_headers = {
  14. 'X-Title': custom_message_config['title'],
  15. 'X-Message': custom_message_config['message'],
  16. 'X-Priority': custom_message_config['priority'],
  17. 'X-Tags': custom_message_config['tags'],
  18. }
  19. def return_default_message_headers(state=Enum):
  20. headers = {
  21. 'X-Title': f'A Borgmatic {state.name} event happened',
  22. 'X-Message': f'A Borgmatic {state.name} event happened',
  23. 'X-Priority': 'default',
  24. 'X-Tags': 'borgmatic',
  25. }
  26. return headers
  27. def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
  28. hook_config = {'topic': topic}
  29. flexmock(module.requests).should_receive('post').with_args(
  30. f'{default_base_url}/{topic}',
  31. headers=return_default_message_headers(module.monitor.State.FAIL),
  32. ).and_return(flexmock(ok=True)).once()
  33. module.ping_monitor(
  34. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  35. )
  36. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
  37. hook_config = {'topic': topic}
  38. flexmock(module.requests).should_receive('post').never()
  39. module.ping_monitor(
  40. hook_config,
  41. 'config.yaml',
  42. module.monitor.State.START,
  43. monitoring_log_level=1,
  44. dry_run=False,
  45. )
  46. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
  47. hook_config = {'topic': topic}
  48. flexmock(module.requests).should_receive('post').never()
  49. module.ping_monitor(
  50. hook_config,
  51. 'config.yaml',
  52. module.monitor.State.FINISH,
  53. monitoring_log_level=1,
  54. dry_run=False,
  55. )
  56. def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
  57. hook_config = {'topic': topic, 'server': custom_base_url}
  58. flexmock(module.requests).should_receive('post').with_args(
  59. f'{custom_base_url}/{topic}',
  60. headers=return_default_message_headers(module.monitor.State.FAIL),
  61. ).and_return(flexmock(ok=True)).once()
  62. module.ping_monitor(
  63. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  64. )
  65. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
  66. hook_config = {'topic': topic}
  67. flexmock(module.requests).should_receive('post').never()
  68. module.ping_monitor(
  69. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=True
  70. )
  71. def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
  72. hook_config = {'topic': topic, 'fail': custom_message_config}
  73. flexmock(module.requests).should_receive('post').with_args(
  74. f'{default_base_url}/{topic}', headers=custom_message_headers,
  75. ).and_return(flexmock(ok=True)).once()
  76. module.ping_monitor(
  77. hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
  78. )
  79. def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
  80. hook_config = {'topic': topic, 'states': ['start', 'fail']}
  81. flexmock(module.requests).should_receive('post').with_args(
  82. f'{default_base_url}/{topic}',
  83. headers=return_default_message_headers(module.monitor.State.START),
  84. ).and_return(flexmock(ok=True)).once()
  85. module.ping_monitor(
  86. hook_config,
  87. 'config.yaml',
  88. module.monitor.State.START,
  89. monitoring_log_level=1,
  90. dry_run=False,
  91. )
  92. def test_ping_monitor_with_connection_error_logs_warning():
  93. hook_config = {'topic': topic}
  94. flexmock(module.requests).should_receive('post').with_args(
  95. f'{default_base_url}/{topic}',
  96. headers=return_default_message_headers(module.monitor.State.FAIL),
  97. ).and_raise(module.requests.exceptions.ConnectionError)
  98. flexmock(module.logger).should_receive('warning').once()
  99. module.ping_monitor(
  100. hook_config,
  101. 'config.yaml',
  102. module.monitor.State.FAIL,
  103. monitoring_log_level=1,
  104. dry_run=False,
  105. )
  106. def test_ping_monitor_with_other_error_logs_warning():
  107. hook_config = {'topic': topic}
  108. response = flexmock(ok=False)
  109. response.should_receive('raise_for_status').and_raise(
  110. module.requests.exceptions.RequestException
  111. )
  112. flexmock(module.requests).should_receive('post').with_args(
  113. f'{default_base_url}/{topic}',
  114. headers=return_default_message_headers(module.monitor.State.FAIL),
  115. ).and_return(response)
  116. flexmock(module.logger).should_receive('warning').once()
  117. module.ping_monitor(
  118. hook_config,
  119. 'config.yaml',
  120. module.monitor.State.FAIL,
  121. monitoring_log_level=1,
  122. dry_run=False,
  123. )