test_healthchecks.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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, timeout=int
  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, timeout=int
  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, timeout=int
  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, timeout=int
  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. timeout=int,
  137. ).and_return(flexmock(ok=True))
  138. module.ping_monitor(
  139. hook_config,
  140. {},
  141. 'config.yaml',
  142. state=module.monitor.State.FINISH,
  143. monitoring_log_level=1,
  144. dry_run=False,
  145. )
  146. def test_ping_monitor_skips_ssl_verification_when_verify_tls_false():
  147. hook_config = {'ping_url': 'https://example.com', 'verify_tls': False}
  148. payload = 'data'
  149. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  150. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  151. 'format_buffered_logs_for_payload'
  152. ).and_return(payload)
  153. flexmock(module.requests).should_receive('post').with_args(
  154. 'https://example.com', data=payload.encode('utf-8'), verify=False, timeout=int
  155. ).and_return(flexmock(ok=True))
  156. module.ping_monitor(
  157. hook_config,
  158. {},
  159. 'config.yaml',
  160. state=module.monitor.State.FINISH,
  161. monitoring_log_level=1,
  162. dry_run=False,
  163. )
  164. def test_ping_monitor_executes_ssl_verification_when_verify_tls_true():
  165. hook_config = {'ping_url': 'https://example.com', 'verify_tls': True}
  166. payload = 'data'
  167. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive('get_handler')
  168. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  169. 'format_buffered_logs_for_payload'
  170. ).and_return(payload)
  171. flexmock(module.requests).should_receive('post').with_args(
  172. 'https://example.com', data=payload.encode('utf-8'), verify=True, timeout=int
  173. ).and_return(flexmock(ok=True))
  174. module.ping_monitor(
  175. hook_config,
  176. {},
  177. 'config.yaml',
  178. state=module.monitor.State.FINISH,
  179. monitoring_log_level=1,
  180. dry_run=False,
  181. )
  182. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  183. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  184. 'Forgetful_buffering_handler'
  185. ).never()
  186. hook_config = {'ping_url': 'https://example.com'}
  187. flexmock(module.requests).should_receive('post').never()
  188. module.ping_monitor(
  189. hook_config,
  190. {},
  191. 'config.yaml',
  192. state=module.monitor.State.START,
  193. monitoring_log_level=1,
  194. dry_run=True,
  195. )
  196. def test_ping_monitor_does_not_hit_ping_url_when_states_not_matching():
  197. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  198. 'Forgetful_buffering_handler'
  199. ).never()
  200. hook_config = {'ping_url': 'https://example.com', 'states': ['finish']}
  201. flexmock(module.requests).should_receive('post').never()
  202. module.ping_monitor(
  203. hook_config,
  204. {},
  205. 'config.yaml',
  206. state=module.monitor.State.START,
  207. monitoring_log_level=1,
  208. dry_run=True,
  209. )
  210. def test_ping_monitor_hits_ping_url_when_states_matching():
  211. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  212. 'Forgetful_buffering_handler'
  213. ).never()
  214. hook_config = {'ping_url': 'https://example.com', 'states': ['start', 'finish']}
  215. flexmock(module.requests).should_receive('post').with_args(
  216. 'https://example.com/start', data=''.encode('utf-8'), verify=True, timeout=int
  217. ).and_return(flexmock(ok=True))
  218. module.ping_monitor(
  219. hook_config,
  220. {},
  221. 'config.yaml',
  222. state=module.monitor.State.START,
  223. monitoring_log_level=1,
  224. dry_run=False,
  225. )
  226. def test_ping_monitor_adds_create_query_parameter_when_create_slug_true():
  227. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  228. 'Forgetful_buffering_handler'
  229. ).never()
  230. hook_config = {'ping_url': 'https://example.com', 'create_slug': True}
  231. flexmock(module.requests).should_receive('post').with_args(
  232. 'https://example.com/start?create=1', data=''.encode('utf-8'), verify=True, timeout=int
  233. ).and_return(flexmock(ok=True))
  234. module.ping_monitor(
  235. hook_config,
  236. {},
  237. 'config.yaml',
  238. state=module.monitor.State.START,
  239. monitoring_log_level=1,
  240. dry_run=False,
  241. )
  242. def test_ping_monitor_does_not_add_create_query_parameter_when_create_slug_false():
  243. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  244. 'Forgetful_buffering_handler'
  245. ).never()
  246. hook_config = {'ping_url': 'https://example.com', 'create_slug': False}
  247. flexmock(module.requests).should_receive('post').with_args(
  248. 'https://example.com/start', data=''.encode('utf-8'), verify=True, timeout=int
  249. ).and_return(flexmock(ok=True))
  250. module.ping_monitor(
  251. hook_config,
  252. {},
  253. 'config.yaml',
  254. state=module.monitor.State.START,
  255. monitoring_log_level=1,
  256. dry_run=False,
  257. )
  258. def test_ping_monitor_does_not_add_create_query_parameter_when_ping_url_is_uuid():
  259. hook_config = {'ping_url': 'b3611b24-df9c-4d36-9203-fa292820bf2a', 'create_slug': True}
  260. flexmock(module.requests).should_receive('post').with_args(
  261. f"https://hc-ping.com/{hook_config['ping_url']}",
  262. data=''.encode('utf-8'),
  263. verify=True,
  264. timeout=int,
  265. ).and_return(flexmock(ok=True))
  266. module.ping_monitor(
  267. hook_config,
  268. {},
  269. 'config.yaml',
  270. state=module.monitor.State.FINISH,
  271. monitoring_log_level=1,
  272. dry_run=False,
  273. )
  274. def test_ping_monitor_issues_warning_when_ping_url_is_uuid_and_create_slug_true():
  275. hook_config = {'ping_url': 'b3611b24-df9c-4d36-9203-fa292820bf2a', 'create_slug': True}
  276. flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True))
  277. flexmock(module.logger).should_receive('warning').once()
  278. module.ping_monitor(
  279. hook_config,
  280. {},
  281. 'config.yaml',
  282. state=module.monitor.State.FINISH,
  283. monitoring_log_level=1,
  284. dry_run=False,
  285. )
  286. def test_ping_monitor_with_connection_error_logs_warning():
  287. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  288. 'Forgetful_buffering_handler'
  289. ).never()
  290. hook_config = {'ping_url': 'https://example.com'}
  291. flexmock(module.requests).should_receive('post').with_args(
  292. 'https://example.com/start', data=''.encode('utf-8'), verify=True, timeout=int
  293. ).and_raise(module.requests.exceptions.ConnectionError)
  294. flexmock(module.logger).should_receive('warning').once()
  295. module.ping_monitor(
  296. hook_config,
  297. {},
  298. 'config.yaml',
  299. state=module.monitor.State.START,
  300. monitoring_log_level=1,
  301. dry_run=False,
  302. )
  303. def test_ping_monitor_with_other_error_logs_warning():
  304. flexmock(module.borgmatic.hooks.monitoring.logs).should_receive(
  305. 'Forgetful_buffering_handler'
  306. ).never()
  307. hook_config = {'ping_url': 'https://example.com'}
  308. response = flexmock(ok=False)
  309. response.should_receive('raise_for_status').and_raise(
  310. module.requests.exceptions.RequestException
  311. )
  312. flexmock(module.requests).should_receive('post').with_args(
  313. 'https://example.com/start', data=''.encode('utf-8'), verify=True, timeout=int
  314. ).and_return(response)
  315. flexmock(module.logger).should_receive('warning').once()
  316. module.ping_monitor(
  317. hook_config,
  318. {},
  319. 'config.yaml',
  320. state=module.monitor.State.START,
  321. monitoring_log_level=1,
  322. dry_run=False,
  323. )