test_healthchecks.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. from flexmock import flexmock
  2. from borgmatic.hooks import healthchecks as module
  3. def mock_logger():
  4. logger = flexmock()
  5. logger.should_receive('addHandler')
  6. logger.should_receive('removeHandler')
  7. flexmock(module.logging).should_receive('getLogger').and_return(logger)
  8. def test_initialize_monitor_creates_log_handler_with_ping_body_limit():
  9. ping_body_limit = 100
  10. monitoring_log_level = 1
  11. mock_logger()
  12. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').with_args(
  13. module.HANDLER_IDENTIFIER,
  14. ping_body_limit - len(module.borgmatic.hooks.logs.PAYLOAD_TRUNCATION_INDICATOR),
  15. monitoring_log_level,
  16. ).once()
  17. module.initialize_monitor(
  18. {'ping_body_limit': ping_body_limit}, {}, 'test.yaml', monitoring_log_level, dry_run=False
  19. )
  20. def test_initialize_monitor_creates_log_handler_with_default_ping_body_limit():
  21. monitoring_log_level = 1
  22. mock_logger()
  23. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').with_args(
  24. module.HANDLER_IDENTIFIER,
  25. module.DEFAULT_PING_BODY_LIMIT_BYTES
  26. - len(module.borgmatic.hooks.logs.PAYLOAD_TRUNCATION_INDICATOR),
  27. monitoring_log_level,
  28. ).once()
  29. module.initialize_monitor({}, {}, 'test.yaml', monitoring_log_level, dry_run=False)
  30. def test_initialize_monitor_creates_log_handler_with_zero_ping_body_limit():
  31. ping_body_limit = 0
  32. monitoring_log_level = 1
  33. mock_logger()
  34. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').with_args(
  35. module.HANDLER_IDENTIFIER, ping_body_limit, monitoring_log_level
  36. ).once()
  37. module.initialize_monitor(
  38. {'ping_body_limit': ping_body_limit}, {}, 'test.yaml', monitoring_log_level, dry_run=False
  39. )
  40. def test_initialize_monitor_creates_log_handler_when_send_logs_true():
  41. mock_logger()
  42. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').once()
  43. module.initialize_monitor(
  44. {'send_logs': True}, {}, 'test.yaml', monitoring_log_level=1, dry_run=False
  45. )
  46. def test_initialize_monitor_bails_when_send_logs_false():
  47. mock_logger()
  48. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  49. module.initialize_monitor(
  50. {'send_logs': False}, {}, 'test.yaml', monitoring_log_level=1, dry_run=False
  51. )
  52. def test_ping_monitor_hits_ping_url_for_start_state():
  53. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  54. hook_config = {'ping_url': 'https://example.com'}
  55. flexmock(module.requests).should_receive('post').with_args(
  56. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  57. ).and_return(flexmock(ok=True))
  58. module.ping_monitor(
  59. hook_config,
  60. {},
  61. 'config.yaml',
  62. state=module.monitor.State.START,
  63. monitoring_log_level=1,
  64. dry_run=False,
  65. )
  66. def test_ping_monitor_hits_ping_url_for_finish_state():
  67. hook_config = {'ping_url': 'https://example.com'}
  68. payload = 'data'
  69. flexmock(module.borgmatic.hooks.logs).should_receive('get_handler')
  70. flexmock(module.borgmatic.hooks.logs).should_receive(
  71. 'format_buffered_logs_for_payload'
  72. ).and_return(payload)
  73. flexmock(module.requests).should_receive('post').with_args(
  74. 'https://example.com', data=payload.encode('utf-8'), verify=True
  75. ).and_return(flexmock(ok=True))
  76. module.ping_monitor(
  77. hook_config,
  78. {},
  79. 'config.yaml',
  80. state=module.monitor.State.FINISH,
  81. monitoring_log_level=1,
  82. dry_run=False,
  83. )
  84. def test_ping_monitor_hits_ping_url_for_fail_state():
  85. hook_config = {'ping_url': 'https://example.com'}
  86. payload = 'data'
  87. flexmock(module.borgmatic.hooks.logs).should_receive('get_handler')
  88. flexmock(module.borgmatic.hooks.logs).should_receive(
  89. 'format_buffered_logs_for_payload'
  90. ).and_return(payload)
  91. flexmock(module.requests).should_receive('post').with_args(
  92. 'https://example.com/fail', data=payload.encode('utf'), verify=True
  93. ).and_return(flexmock(ok=True))
  94. module.ping_monitor(
  95. hook_config,
  96. {},
  97. 'config.yaml',
  98. state=module.monitor.State.FAIL,
  99. monitoring_log_level=1,
  100. dry_run=False,
  101. )
  102. def test_ping_monitor_hits_ping_url_for_log_state():
  103. hook_config = {'ping_url': 'https://example.com'}
  104. payload = 'data'
  105. flexmock(module.borgmatic.hooks.logs).should_receive('get_handler')
  106. flexmock(module.borgmatic.hooks.logs).should_receive(
  107. 'format_buffered_logs_for_payload'
  108. ).and_return(payload)
  109. flexmock(module.requests).should_receive('post').with_args(
  110. 'https://example.com/log', data=payload.encode('utf'), verify=True
  111. ).and_return(flexmock(ok=True))
  112. module.ping_monitor(
  113. hook_config,
  114. {},
  115. 'config.yaml',
  116. state=module.monitor.State.LOG,
  117. monitoring_log_level=1,
  118. dry_run=False,
  119. )
  120. def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
  121. hook_config = {'ping_url': 'abcd-efgh-ijkl-mnop'}
  122. payload = 'data'
  123. flexmock(module.borgmatic.hooks.logs).should_receive('get_handler')
  124. flexmock(module.borgmatic.hooks.logs).should_receive(
  125. 'format_buffered_logs_for_payload'
  126. ).and_return(payload)
  127. flexmock(module.requests).should_receive('post').with_args(
  128. f"https://hc-ping.com/{hook_config['ping_url']}",
  129. data=payload.encode('utf-8'),
  130. verify=True,
  131. ).and_return(flexmock(ok=True))
  132. module.ping_monitor(
  133. hook_config,
  134. {},
  135. 'config.yaml',
  136. state=module.monitor.State.FINISH,
  137. monitoring_log_level=1,
  138. dry_run=False,
  139. )
  140. def test_ping_monitor_skips_ssl_verification_when_verify_tls_false():
  141. hook_config = {'ping_url': 'https://example.com', 'verify_tls': False}
  142. payload = 'data'
  143. flexmock(module.borgmatic.hooks.logs).should_receive('get_handler')
  144. flexmock(module.borgmatic.hooks.logs).should_receive(
  145. 'format_buffered_logs_for_payload'
  146. ).and_return(payload)
  147. flexmock(module.requests).should_receive('post').with_args(
  148. 'https://example.com', data=payload.encode('utf-8'), verify=False
  149. ).and_return(flexmock(ok=True))
  150. module.ping_monitor(
  151. hook_config,
  152. {},
  153. 'config.yaml',
  154. state=module.monitor.State.FINISH,
  155. monitoring_log_level=1,
  156. dry_run=False,
  157. )
  158. def test_ping_monitor_executes_ssl_verification_when_verify_tls_true():
  159. hook_config = {'ping_url': 'https://example.com', 'verify_tls': True}
  160. payload = 'data'
  161. flexmock(module.borgmatic.hooks.logs).should_receive('get_handler')
  162. flexmock(module.borgmatic.hooks.logs).should_receive(
  163. 'format_buffered_logs_for_payload'
  164. ).and_return(payload)
  165. flexmock(module.requests).should_receive('post').with_args(
  166. 'https://example.com', data=payload.encode('utf-8'), verify=True
  167. ).and_return(flexmock(ok=True))
  168. module.ping_monitor(
  169. hook_config,
  170. {},
  171. 'config.yaml',
  172. state=module.monitor.State.FINISH,
  173. monitoring_log_level=1,
  174. dry_run=False,
  175. )
  176. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  177. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  178. hook_config = {'ping_url': 'https://example.com'}
  179. flexmock(module.requests).should_receive('post').never()
  180. module.ping_monitor(
  181. hook_config,
  182. {},
  183. 'config.yaml',
  184. state=module.monitor.State.START,
  185. monitoring_log_level=1,
  186. dry_run=True,
  187. )
  188. def test_ping_monitor_does_not_hit_ping_url_when_states_not_matching():
  189. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  190. hook_config = {'ping_url': 'https://example.com', 'states': ['finish']}
  191. flexmock(module.requests).should_receive('post').never()
  192. module.ping_monitor(
  193. hook_config,
  194. {},
  195. 'config.yaml',
  196. state=module.monitor.State.START,
  197. monitoring_log_level=1,
  198. dry_run=True,
  199. )
  200. def test_ping_monitor_hits_ping_url_when_states_matching():
  201. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  202. hook_config = {'ping_url': 'https://example.com', 'states': ['start', 'finish']}
  203. flexmock(module.requests).should_receive('post').with_args(
  204. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  205. ).and_return(flexmock(ok=True))
  206. module.ping_monitor(
  207. hook_config,
  208. {},
  209. 'config.yaml',
  210. state=module.monitor.State.START,
  211. monitoring_log_level=1,
  212. dry_run=False,
  213. )
  214. def test_ping_monitor_with_connection_error_logs_warning():
  215. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  216. hook_config = {'ping_url': 'https://example.com'}
  217. flexmock(module.requests).should_receive('post').with_args(
  218. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  219. ).and_raise(module.requests.exceptions.ConnectionError)
  220. flexmock(module.logger).should_receive('warning').once()
  221. module.ping_monitor(
  222. hook_config,
  223. {},
  224. 'config.yaml',
  225. state=module.monitor.State.START,
  226. monitoring_log_level=1,
  227. dry_run=False,
  228. )
  229. def test_ping_monitor_with_other_error_logs_warning():
  230. flexmock(module.borgmatic.hooks.logs).should_receive('Forgetful_buffering_handler').never()
  231. hook_config = {'ping_url': 'https://example.com'}
  232. response = flexmock(ok=False)
  233. response.should_receive('raise_for_status').and_raise(
  234. module.requests.exceptions.RequestException
  235. )
  236. flexmock(module.requests).should_receive('post').with_args(
  237. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  238. ).and_return(response)
  239. flexmock(module.logger).should_receive('warning').once()
  240. module.ping_monitor(
  241. hook_config,
  242. {},
  243. 'config.yaml',
  244. state=module.monitor.State.START,
  245. monitoring_log_level=1,
  246. dry_run=False,
  247. )