test_healthchecks.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. from flexmock import flexmock
  2. from borgmatic.hooks.monitoring import healthchecks as module
  3. def test_initialize_monitor_creates_log_handler_with_ping_body_limit():
  4. ping_body_limit = 100
  5. monitoring_log_level = 1
  6. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  7. 'Forgetful_buffering_handler'
  8. ).with_args(
  9. module.HANDLER_IDENTIFIER,
  10. ping_body_limit - len(module.borgmatic.hooks.monitoring.logs.PAYLOAD_TRUNCATION_INDICATOR),
  11. monitoring_log_level,
  12. ).once()
  13. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('add_handler')
  14. module.initialize_monitor(
  15. {'ping_body_limit': ping_body_limit}, {}, 'test.yaml', monitoring_log_level, dry_run=False
  16. )
  17. def test_initialize_monitor_creates_log_handler_with_default_ping_body_limit():
  18. monitoring_log_level = 1
  19. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  20. 'Forgetful_buffering_handler'
  21. ).with_args(
  22. module.HANDLER_IDENTIFIER,
  23. module.DEFAULT_PING_BODY_LIMIT_BYTES
  24. - len(module.borgmatic.hooks.monitoring.logs.PAYLOAD_TRUNCATION_INDICATOR),
  25. monitoring_log_level,
  26. ).once()
  27. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('add_handler')
  28. module.initialize_monitor({}, {}, 'test.yaml', monitoring_log_level, dry_run=False)
  29. def test_initialize_monitor_creates_log_handler_with_zero_ping_body_limit():
  30. ping_body_limit = 0
  31. monitoring_log_level = 1
  32. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  33. 'Forgetful_buffering_handler'
  34. ).with_args(module.HANDLER_IDENTIFIER, ping_body_limit, monitoring_log_level).once()
  35. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('add_handler')
  36. module.initialize_monitor(
  37. {'ping_body_limit': ping_body_limit}, {}, 'test.yaml', monitoring_log_level, dry_run=False
  38. )
  39. def test_initialize_monitor_creates_log_handler_when_send_logs_true():
  40. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  41. 'Forgetful_buffering_handler'
  42. ).once()
  43. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('add_handler')
  44. module.initialize_monitor(
  45. {'send_logs': True}, {}, 'test.yaml', monitoring_log_level=1, dry_run=False
  46. )
  47. def test_initialize_monitor_bails_when_send_logs_false():
  48. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  49. 'Forgetful_buffering_handler'
  50. ).never()
  51. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('add_handler')
  52. module.initialize_monitor(
  53. {'send_logs': False}, {}, 'test.yaml', monitoring_log_level=1, dry_run=False
  54. )
  55. def test_ping_monitor_hits_ping_url_for_start_state():
  56. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  57. 'Forgetful_buffering_handler'
  58. ).never()
  59. hook_config = {'ping_url': 'https://example.com'}
  60. flexmock(module.requests).should_receive('post').with_args(
  61. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  62. ).and_return(flexmock(ok=True))
  63. module.ping_monitor(
  64. hook_config,
  65. {},
  66. 'config.yaml',
  67. state=module.monitor.State.START,
  68. monitoring_log_level=1,
  69. dry_run=False,
  70. )
  71. def test_ping_monitor_hits_ping_url_for_finish_state():
  72. hook_config = {'ping_url': 'https://example.com'}
  73. payload = 'data'
  74. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  75. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  76. 'format_buffered_logs_for_payload'
  77. ).and_return(payload)
  78. flexmock(module.requests).should_receive('post').with_args(
  79. 'https://example.com', data=payload.encode('utf-8'), verify=True
  80. ).and_return(flexmock(ok=True))
  81. module.ping_monitor(
  82. hook_config,
  83. {},
  84. 'config.yaml',
  85. state=module.monitor.State.FINISH,
  86. monitoring_log_level=1,
  87. dry_run=False,
  88. )
  89. def test_ping_monitor_hits_ping_url_for_fail_state():
  90. hook_config = {'ping_url': 'https://example.com'}
  91. payload = 'data'
  92. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  93. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  94. 'format_buffered_logs_for_payload'
  95. ).and_return(payload)
  96. flexmock(module.requests).should_receive('post').with_args(
  97. 'https://example.com/fail', data=payload.encode('utf'), verify=True
  98. ).and_return(flexmock(ok=True))
  99. module.ping_monitor(
  100. hook_config,
  101. {},
  102. 'config.yaml',
  103. state=module.monitor.State.FAIL,
  104. monitoring_log_level=1,
  105. dry_run=False,
  106. )
  107. def test_ping_monitor_hits_ping_url_for_log_state():
  108. hook_config = {'ping_url': 'https://example.com'}
  109. payload = 'data'
  110. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  111. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  112. 'format_buffered_logs_for_payload'
  113. ).and_return(payload)
  114. flexmock(module.requests).should_receive('post').with_args(
  115. 'https://example.com/log', data=payload.encode('utf'), verify=True
  116. ).and_return(flexmock(ok=True))
  117. module.ping_monitor(
  118. hook_config,
  119. {},
  120. 'config.yaml',
  121. state=module.monitor.State.LOG,
  122. monitoring_log_level=1,
  123. dry_run=False,
  124. )
  125. def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
  126. hook_config = {'ping_url': 'abcd-efgh-ijkl-mnop'}
  127. payload = 'data'
  128. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  129. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  130. 'format_buffered_logs_for_payload'
  131. ).and_return(payload)
  132. flexmock(module.requests).should_receive('post').with_args(
  133. f"https://hc-ping.com/{hook_config['ping_url']}",
  134. data=payload.encode('utf-8'),
  135. verify=True,
  136. ).and_return(flexmock(ok=True))
  137. module.ping_monitor(
  138. hook_config,
  139. {},
  140. 'config.yaml',
  141. state=module.monitor.State.FINISH,
  142. monitoring_log_level=1,
  143. dry_run=False,
  144. )
  145. def test_ping_monitor_skips_ssl_verification_when_verify_tls_false():
  146. hook_config = {'ping_url': 'https://example.com', 'verify_tls': False}
  147. payload = 'data'
  148. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  149. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  150. 'format_buffered_logs_for_payload'
  151. ).and_return(payload)
  152. flexmock(module.requests).should_receive('post').with_args(
  153. 'https://example.com', data=payload.encode('utf-8'), verify=False
  154. ).and_return(flexmock(ok=True))
  155. module.ping_monitor(
  156. hook_config,
  157. {},
  158. 'config.yaml',
  159. state=module.monitor.State.FINISH,
  160. monitoring_log_level=1,
  161. dry_run=False,
  162. )
  163. def test_ping_monitor_executes_ssl_verification_when_verify_tls_true():
  164. hook_config = {'ping_url': 'https://example.com', 'verify_tls': True}
  165. payload = 'data'
  166. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  167. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  168. 'format_buffered_logs_for_payload'
  169. ).and_return(payload)
  170. flexmock(module.requests).should_receive('post').with_args(
  171. 'https://example.com', data=payload.encode('utf-8'), verify=True
  172. ).and_return(flexmock(ok=True))
  173. module.ping_monitor(
  174. hook_config,
  175. {},
  176. 'config.yaml',
  177. state=module.monitor.State.FINISH,
  178. monitoring_log_level=1,
  179. dry_run=False,
  180. )
  181. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  182. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  183. 'Forgetful_buffering_handler'
  184. ).never()
  185. hook_config = {'ping_url': 'https://example.com'}
  186. flexmock(module.requests).should_receive('post').never()
  187. module.ping_monitor(
  188. hook_config,
  189. {},
  190. 'config.yaml',
  191. state=module.monitor.State.START,
  192. monitoring_log_level=1,
  193. dry_run=True,
  194. )
  195. def test_ping_monitor_does_not_hit_ping_url_when_states_not_matching():
  196. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  197. 'Forgetful_buffering_handler'
  198. ).never()
  199. hook_config = {'ping_url': 'https://example.com', 'states': ['finish']}
  200. flexmock(module.requests).should_receive('post').never()
  201. module.ping_monitor(
  202. hook_config,
  203. {},
  204. 'config.yaml',
  205. state=module.monitor.State.START,
  206. monitoring_log_level=1,
  207. dry_run=True,
  208. )
  209. def test_ping_monitor_hits_ping_url_when_states_matching():
  210. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  211. 'Forgetful_buffering_handler'
  212. ).never()
  213. hook_config = {'ping_url': 'https://example.com', 'states': ['start', 'finish']}
  214. flexmock(module.requests).should_receive('post').with_args(
  215. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  216. ).and_return(flexmock(ok=True))
  217. module.ping_monitor(
  218. hook_config,
  219. {},
  220. 'config.yaml',
  221. state=module.monitor.State.START,
  222. monitoring_log_level=1,
  223. dry_run=False,
  224. )
  225. def test_ping_monitor_adds_create_query_parameter_when_create_slug_true():
  226. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  227. 'Forgetful_buffering_handler'
  228. ).never()
  229. hook_config = {'ping_url': 'https://example.com', 'create_slug': True}
  230. flexmock(module.requests).should_receive('post').with_args(
  231. 'https://example.com/start?create=1', data=''.encode('utf-8'), verify=True
  232. ).and_return(flexmock(ok=True))
  233. module.ping_monitor(
  234. hook_config,
  235. {},
  236. 'config.yaml',
  237. state=module.monitor.State.START,
  238. monitoring_log_level=1,
  239. dry_run=False,
  240. )
  241. def test_ping_monitor_does_not_add_create_query_parameter_when_create_slug_false():
  242. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  243. 'Forgetful_buffering_handler'
  244. ).never()
  245. hook_config = {'ping_url': 'https://example.com', 'create_slug': False}
  246. flexmock(module.requests).should_receive('post').with_args(
  247. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  248. ).and_return(flexmock(ok=True))
  249. module.ping_monitor(
  250. hook_config,
  251. {},
  252. 'config.yaml',
  253. state=module.monitor.State.START,
  254. monitoring_log_level=1,
  255. dry_run=False,
  256. )
  257. def test_ping_monitor_does_not_add_create_query_parameter_when_ping_url_is_uuid():
  258. hook_config = {'ping_url': 'b3611b24-df9c-4d36-9203-fa292820bf2a', 'create_slug': True}
  259. flexmock(module.requests).should_receive('post').with_args(
  260. f"https://hc-ping.com/{hook_config['ping_url']}",
  261. data=''.encode('utf-8'),
  262. verify=True,
  263. ).and_return(flexmock(ok=True))
  264. module.ping_monitor(
  265. hook_config,
  266. {},
  267. 'config.yaml',
  268. state=module.monitor.State.FINISH,
  269. monitoring_log_level=1,
  270. dry_run=False,
  271. )
  272. def test_ping_monitor_issues_warning_when_ping_url_is_uuid_and_create_slug_true():
  273. hook_config = {'ping_url': 'b3611b24-df9c-4d36-9203-fa292820bf2a', 'create_slug': True}
  274. flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True))
  275. flexmock(module.logger).should_receive('warning').once()
  276. module.ping_monitor(
  277. hook_config,
  278. {},
  279. 'config.yaml',
  280. state=module.monitor.State.FINISH,
  281. monitoring_log_level=1,
  282. dry_run=False,
  283. )
  284. def test_ping_monitor_with_connection_error_logs_warning():
  285. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  286. 'Forgetful_buffering_handler'
  287. ).never()
  288. hook_config = {'ping_url': 'https://example.com'}
  289. flexmock(module.requests).should_receive('post').with_args(
  290. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  291. ).and_raise(module.requests.exceptions.ConnectionError)
  292. flexmock(module.logger).should_receive('warning').once()
  293. module.ping_monitor(
  294. hook_config,
  295. {},
  296. 'config.yaml',
  297. state=module.monitor.State.START,
  298. monitoring_log_level=1,
  299. dry_run=False,
  300. )
  301. def test_ping_monitor_with_other_error_logs_warning():
  302. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  303. 'Forgetful_buffering_handler'
  304. ).never()
  305. hook_config = {'ping_url': 'https://example.com'}
  306. response = flexmock(ok=False)
  307. response.should_receive('raise_for_status').and_raise(
  308. module.requests.exceptions.RequestException
  309. )
  310. flexmock(module.requests).should_receive('post').with_args(
  311. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  312. ).and_return(response)
  313. flexmock(module.logger).should_receive('warning').once()
  314. module.ping_monitor(
  315. hook_config,
  316. {},
  317. 'config.yaml',
  318. state=module.monitor.State.START,
  319. monitoring_log_level=1,
  320. dry_run=False,
  321. )