test_uptimekuma.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. from enum import Enum
  2. from flexmock import flexmock
  3. import borgmatic.hooks.monitor
  4. from borgmatic.hooks import uptimekuma as module
  5. default_base_url = 'https://example.uptime.kuma'
  6. custom_base_url = 'https://uptime.example.com'
  7. push_code = 'abcd1234'
  8. def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
  9. hook_config = {'push_code': push_code}
  10. flexmock(module.requests).should_receive('get').with_args(
  11. f'{default_base_url}/api/push/{push_code}'
  12. ).and_return(flexmock(ok=True)).once()
  13. module.ping_monitor(
  14. hook_config,
  15. {},
  16. 'config.yaml',
  17. borgmatic.hooks.monitor.State.FAIL,
  18. monitoring_log_level=1,
  19. dry_run=True,
  20. )
  21. def test_ping_monitor_with_access_token_hits_hosted_ntfy_on_fail():
  22. hook_config = {
  23. 'topic': topic,
  24. 'access_token': 'abc123',
  25. }
  26. flexmock(module.requests).should_receive('post').with_args(
  27. f'{default_base_url}/{topic}',
  28. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  29. auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
  30. ).and_return(flexmock(ok=True)).once()
  31. module.ping_monitor(
  32. hook_config,
  33. {},
  34. 'config.yaml',
  35. borgmatic.hooks.monitor.State.FAIL,
  36. monitoring_log_level=1,
  37. dry_run=False,
  38. )
  39. def test_ping_monitor_with_username_password_and_access_token_ignores_username_password():
  40. hook_config = {
  41. 'topic': topic,
  42. 'username': 'testuser',
  43. 'password': 'fakepassword',
  44. 'access_token': 'abc123',
  45. }
  46. flexmock(module.requests).should_receive('post').with_args(
  47. f'{default_base_url}/{topic}',
  48. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  49. auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
  50. ).and_return(flexmock(ok=True)).once()
  51. flexmock(module.logger).should_receive('warning').once()
  52. module.ping_monitor(
  53. hook_config,
  54. {},
  55. 'config.yaml',
  56. borgmatic.hooks.monitor.State.FAIL,
  57. monitoring_log_level=1,
  58. dry_run=False,
  59. )
  60. def test_ping_monitor_with_username_password_hits_hosted_ntfy_on_fail():
  61. hook_config = {
  62. 'topic': topic,
  63. 'username': 'testuser',
  64. 'password': 'fakepassword',
  65. }
  66. flexmock(module.requests).should_receive('post').with_args(
  67. f'{default_base_url}/{topic}',
  68. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  69. auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
  70. ).and_return(flexmock(ok=True)).once()
  71. module.ping_monitor(
  72. hook_config,
  73. {},
  74. 'config.yaml',
  75. borgmatic.hooks.monitor.State.FAIL,
  76. monitoring_log_level=1,
  77. dry_run=False,
  78. )
  79. def test_ping_monitor_with_password_but_no_username_warns():
  80. hook_config = {'topic': topic, 'password': 'fakepassword'}
  81. flexmock(module.requests).should_receive('post').with_args(
  82. f'{default_base_url}/{topic}',
  83. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  84. auth=None,
  85. ).and_return(flexmock(ok=True)).once()
  86. flexmock(module.logger).should_receive('warning').once()
  87. module.ping_monitor(
  88. hook_config,
  89. {},
  90. 'config.yaml',
  91. borgmatic.hooks.monitor.State.FAIL,
  92. monitoring_log_level=1,
  93. dry_run=False,
  94. )
  95. def test_ping_monitor_with_username_but_no_password_warns():
  96. hook_config = {'topic': topic, 'username': 'testuser'}
  97. flexmock(module.requests).should_receive('post').with_args(
  98. f'{default_base_url}/{topic}',
  99. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  100. auth=None,
  101. ).and_return(flexmock(ok=True)).once()
  102. flexmock(module.logger).should_receive('warning').once()
  103. module.ping_monitor(
  104. hook_config,
  105. {},
  106. 'config.yaml',
  107. borgmatic.hooks.monitor.State.FAIL,
  108. monitoring_log_level=1,
  109. dry_run=False,
  110. )
  111. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
  112. hook_config = {'topic': topic}
  113. flexmock(module.requests).should_receive('post').never()
  114. module.ping_monitor(
  115. hook_config,
  116. {},
  117. 'config.yaml',
  118. borgmatic.hooks.monitor.State.START,
  119. monitoring_log_level=1,
  120. dry_run=False,
  121. )
  122. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
  123. hook_config = {'topic': topic}
  124. flexmock(module.requests).should_receive('post').never()
  125. module.ping_monitor(
  126. hook_config,
  127. {},
  128. 'config.yaml',
  129. borgmatic.hooks.monitor.State.FINISH,
  130. monitoring_log_level=1,
  131. dry_run=False,
  132. )
  133. def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
  134. hook_config = {'topic': topic, 'server': custom_base_url}
  135. flexmock(module.requests).should_receive('post').with_args(
  136. f'{custom_base_url}/{topic}',
  137. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  138. auth=None,
  139. ).and_return(flexmock(ok=True)).once()
  140. module.ping_monitor(
  141. hook_config,
  142. {},
  143. 'config.yaml',
  144. borgmatic.hooks.monitor.State.FAIL,
  145. monitoring_log_level=1,
  146. dry_run=False,
  147. )
  148. def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
  149. hook_config = {'topic': topic}
  150. flexmock(module.requests).should_receive('post').never()
  151. module.ping_monitor(
  152. hook_config,
  153. {},
  154. 'config.yaml',
  155. borgmatic.hooks.monitor.State.FAIL,
  156. monitoring_log_level=1,
  157. dry_run=True,
  158. )
  159. def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
  160. hook_config = {'topic': topic, 'fail': custom_message_config}
  161. flexmock(module.requests).should_receive('post').with_args(
  162. f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
  163. ).and_return(flexmock(ok=True)).once()
  164. module.ping_monitor(
  165. hook_config,
  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_custom_state_hits_hosted_ntfy_on_start():
  173. hook_config = {'topic': topic, 'states': ['start', 'fail']}
  174. flexmock(module.requests).should_receive('post').with_args(
  175. f'{default_base_url}/{topic}',
  176. headers=return_default_message_headers(borgmatic.hooks.monitor.State.START),
  177. auth=None,
  178. ).and_return(flexmock(ok=True)).once()
  179. module.ping_monitor(
  180. hook_config,
  181. {},
  182. 'config.yaml',
  183. borgmatic.hooks.monitor.State.START,
  184. monitoring_log_level=1,
  185. dry_run=False,
  186. )
  187. def test_ping_monitor_with_connection_error_logs_warning():
  188. hook_config = {'topic': topic}
  189. flexmock(module.requests).should_receive('post').with_args(
  190. f'{default_base_url}/{topic}',
  191. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  192. auth=None,
  193. ).and_raise(module.requests.exceptions.ConnectionError)
  194. flexmock(module.logger).should_receive('warning').once()
  195. module.ping_monitor(
  196. hook_config,
  197. {},
  198. 'config.yaml',
  199. borgmatic.hooks.monitor.State.FAIL,
  200. monitoring_log_level=1,
  201. dry_run=False,
  202. )
  203. def test_ping_monitor_with_other_error_logs_warning():
  204. hook_config = {'topic': topic}
  205. response = flexmock(ok=False)
  206. response.should_receive('raise_for_status').and_raise(
  207. module.requests.exceptions.RequestException
  208. )
  209. flexmock(module.requests).should_receive('post').with_args(
  210. f'{default_base_url}/{topic}',
  211. headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
  212. auth=None,
  213. ).and_return(response)
  214. flexmock(module.logger).should_receive('warning').once()
  215. module.ping_monitor(
  216. hook_config,
  217. {},
  218. 'config.yaml',
  219. borgmatic.hooks.monitor.State.FAIL,
  220. monitoring_log_level=1,
  221. dry_run=False,
  222. )