2
0

test_apprise.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import apprise
  2. from apprise import NotifyFormat, NotifyType
  3. from flexmock import flexmock
  4. import borgmatic.hooks.monitor
  5. from borgmatic.hooks import apprise as module
  6. TOPIC = 'borgmatic-unit-testing'
  7. def mock_apprise():
  8. apprise_mock = flexmock(
  9. add=lambda servers: None, notify=lambda title, body, body_format, notify_type: None
  10. )
  11. flexmock(apprise.Apprise).new_instances(apprise_mock)
  12. return apprise_mock
  13. def test_ping_monitor_adheres_dry_run():
  14. mock_apprise().should_receive('notify').never()
  15. module.ping_monitor(
  16. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]},
  17. {},
  18. 'config.yaml',
  19. borgmatic.hooks.monitor.State.FAIL,
  20. monitoring_log_level=1,
  21. dry_run=True,
  22. )
  23. def test_ping_monitor_does_not_hit_with_no_states():
  24. mock_apprise().should_receive('notify').never()
  25. module.ping_monitor(
  26. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': []},
  27. {},
  28. 'config.yaml',
  29. borgmatic.hooks.monitor.State.FAIL,
  30. monitoring_log_level=1,
  31. dry_run=True,
  32. )
  33. def test_ping_monitor_hits_fail_by_default():
  34. mock_apprise().should_receive('notify').with_args(
  35. title='A borgmatic FAIL event happened',
  36. body='A borgmatic FAIL event happened',
  37. body_format=NotifyFormat.TEXT,
  38. notify_type=NotifyType.FAILURE,
  39. ).once()
  40. for state in borgmatic.hooks.monitor.State:
  41. module.ping_monitor(
  42. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]},
  43. {},
  44. 'config.yaml',
  45. state,
  46. monitoring_log_level=1,
  47. dry_run=False,
  48. )
  49. def test_ping_monitor_hits_with_finish_default_config():
  50. mock_apprise().should_receive('notify').with_args(
  51. title='A borgmatic FINISH event happened',
  52. body='A borgmatic FINISH event happened',
  53. body_format=NotifyFormat.TEXT,
  54. notify_type=NotifyType.SUCCESS,
  55. ).once()
  56. module.ping_monitor(
  57. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['finish']},
  58. {},
  59. 'config.yaml',
  60. borgmatic.hooks.monitor.State.FINISH,
  61. monitoring_log_level=1,
  62. dry_run=False,
  63. )
  64. def test_ping_monitor_hits_with_start_default_config():
  65. mock_apprise().should_receive('notify').with_args(
  66. title='A borgmatic START event happened',
  67. body='A borgmatic START event happened',
  68. body_format=NotifyFormat.TEXT,
  69. notify_type=NotifyType.INFO,
  70. ).once()
  71. module.ping_monitor(
  72. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['start']},
  73. {},
  74. 'config.yaml',
  75. borgmatic.hooks.monitor.State.START,
  76. monitoring_log_level=1,
  77. dry_run=False,
  78. )
  79. def test_ping_monitor_hits_with_fail_default_config():
  80. mock_apprise().should_receive('notify').with_args(
  81. title='A borgmatic FAIL event happened',
  82. body='A borgmatic FAIL event happened',
  83. body_format=NotifyFormat.TEXT,
  84. notify_type=NotifyType.FAILURE,
  85. ).once()
  86. module.ping_monitor(
  87. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['fail']},
  88. {},
  89. 'config.yaml',
  90. borgmatic.hooks.monitor.State.FAIL,
  91. monitoring_log_level=1,
  92. dry_run=False,
  93. )
  94. def test_ping_monitor_hits_with_log_default_config():
  95. mock_apprise().should_receive('notify').with_args(
  96. title='A borgmatic LOG event happened',
  97. body='A borgmatic LOG event happened',
  98. body_format=NotifyFormat.TEXT,
  99. notify_type=NotifyType.INFO,
  100. ).once()
  101. module.ping_monitor(
  102. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['log']},
  103. {},
  104. 'config.yaml',
  105. borgmatic.hooks.monitor.State.LOG,
  106. monitoring_log_level=1,
  107. dry_run=False,
  108. )
  109. def test_ping_monitor_passes_through_custom_message_title():
  110. mock_apprise().should_receive('notify').with_args(
  111. title='foo',
  112. body='bar',
  113. body_format=NotifyFormat.TEXT,
  114. notify_type=NotifyType.FAILURE,
  115. ).once()
  116. module.ping_monitor(
  117. {
  118. 'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}],
  119. 'states': ['fail'],
  120. 'fail': {'title': 'foo', 'body': 'bar'},
  121. },
  122. {},
  123. 'config.yaml',
  124. borgmatic.hooks.monitor.State.FAIL,
  125. monitoring_log_level=1,
  126. dry_run=False,
  127. )
  128. def test_ping_monitor_passes_through_custom_message_body():
  129. mock_apprise().should_receive('notify').with_args(
  130. title='',
  131. body='baz',
  132. body_format=NotifyFormat.TEXT,
  133. notify_type=NotifyType.FAILURE,
  134. ).once()
  135. module.ping_monitor(
  136. {
  137. 'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}],
  138. 'states': ['fail'],
  139. 'fail': {'body': 'baz'},
  140. },
  141. {},
  142. 'config.yaml',
  143. borgmatic.hooks.monitor.State.FAIL,
  144. monitoring_log_level=1,
  145. dry_run=False,
  146. )
  147. def test_ping_monitor_pings_multiple_services():
  148. mock_apprise().should_receive('add').with_args([f'ntfys://{TOPIC}', f'ntfy://{TOPIC}']).once()
  149. module.ping_monitor(
  150. {
  151. 'services': [
  152. {'url': f'ntfys://{TOPIC}', 'label': 'ntfys'},
  153. {'url': f'ntfy://{TOPIC}', 'label': 'ntfy'},
  154. ]
  155. },
  156. {},
  157. 'config.yaml',
  158. borgmatic.hooks.monitor.State.FAIL,
  159. monitoring_log_level=1,
  160. dry_run=False,
  161. )
  162. def test_ping_monitor_logs_info_for_no_services():
  163. flexmock(module.logger).should_receive('info').once()
  164. module.ping_monitor(
  165. {'services': []},
  166. {},
  167. 'config.yaml',
  168. borgmatic.hooks.monitor.State.FAIL,
  169. monitoring_log_level=1,
  170. dry_run=False,
  171. )
  172. def test_ping_monitor_logs_warning_when_notify_fails():
  173. mock_apprise().should_receive('notify').and_return(False)
  174. flexmock(module.logger).should_receive('warning').once()
  175. for state in borgmatic.hooks.monitor.State:
  176. module.ping_monitor(
  177. {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]},
  178. {},
  179. 'config.yaml',
  180. state,
  181. monitoring_log_level=1,
  182. dry_run=False,
  183. )