test_healthchecks.py 11 KB

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