test_healthchecks.py 14 KB

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