2
0

test_healthchecks.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. {},
  103. 'config.yaml',
  104. state=module.monitor.State.START,
  105. monitoring_log_level=1,
  106. dry_run=False,
  107. )
  108. def test_ping_monitor_hits_ping_url_for_finish_state():
  109. hook_config = {'ping_url': 'https://example.com'}
  110. payload = 'data'
  111. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  112. flexmock(module.requests).should_receive('post').with_args(
  113. 'https://example.com', data=payload.encode('utf-8'), verify=True
  114. ).and_return(flexmock(ok=True))
  115. module.ping_monitor(
  116. hook_config,
  117. {},
  118. 'config.yaml',
  119. state=module.monitor.State.FINISH,
  120. monitoring_log_level=1,
  121. dry_run=False,
  122. )
  123. def test_ping_monitor_hits_ping_url_for_fail_state():
  124. hook_config = {'ping_url': 'https://example.com'}
  125. payload = 'data'
  126. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  127. flexmock(module.requests).should_receive('post').with_args(
  128. 'https://example.com/fail', data=payload.encode('utf'), verify=True
  129. ).and_return(flexmock(ok=True))
  130. module.ping_monitor(
  131. hook_config,
  132. {},
  133. 'config.yaml',
  134. state=module.monitor.State.FAIL,
  135. monitoring_log_level=1,
  136. dry_run=False,
  137. )
  138. def test_ping_monitor_hits_ping_url_for_log_state():
  139. hook_config = {'ping_url': 'https://example.com'}
  140. payload = 'data'
  141. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  142. flexmock(module.requests).should_receive('post').with_args(
  143. 'https://example.com/log', data=payload.encode('utf'), verify=True
  144. ).and_return(flexmock(ok=True))
  145. module.ping_monitor(
  146. hook_config,
  147. {},
  148. 'config.yaml',
  149. state=module.monitor.State.LOG,
  150. monitoring_log_level=1,
  151. dry_run=False,
  152. )
  153. def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
  154. hook_config = {'ping_url': 'abcd-efgh-ijkl-mnop'}
  155. payload = 'data'
  156. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  157. flexmock(module.requests).should_receive('post').with_args(
  158. f"https://hc-ping.com/{hook_config['ping_url']}",
  159. data=payload.encode('utf-8'),
  160. verify=True,
  161. ).and_return(flexmock(ok=True))
  162. module.ping_monitor(
  163. hook_config,
  164. {},
  165. 'config.yaml',
  166. state=module.monitor.State.FINISH,
  167. monitoring_log_level=1,
  168. dry_run=False,
  169. )
  170. def test_ping_monitor_skips_ssl_verification_when_verify_tls_false():
  171. hook_config = {'ping_url': 'https://example.com', 'verify_tls': False}
  172. payload = 'data'
  173. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  174. flexmock(module.requests).should_receive('post').with_args(
  175. 'https://example.com', data=payload.encode('utf-8'), verify=False
  176. ).and_return(flexmock(ok=True))
  177. module.ping_monitor(
  178. hook_config,
  179. {},
  180. 'config.yaml',
  181. state=module.monitor.State.FINISH,
  182. monitoring_log_level=1,
  183. dry_run=False,
  184. )
  185. def test_ping_monitor_executes_ssl_verification_when_verify_tls_true():
  186. hook_config = {'ping_url': 'https://example.com', 'verify_tls': True}
  187. payload = 'data'
  188. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  189. flexmock(module.requests).should_receive('post').with_args(
  190. 'https://example.com', data=payload.encode('utf-8'), verify=True
  191. ).and_return(flexmock(ok=True))
  192. module.ping_monitor(
  193. hook_config,
  194. {},
  195. 'config.yaml',
  196. state=module.monitor.State.FINISH,
  197. monitoring_log_level=1,
  198. dry_run=False,
  199. )
  200. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  201. flexmock(module).should_receive('Forgetful_buffering_handler')
  202. hook_config = {'ping_url': 'https://example.com'}
  203. flexmock(module.requests).should_receive('post').never()
  204. module.ping_monitor(
  205. hook_config,
  206. {},
  207. 'config.yaml',
  208. state=module.monitor.State.START,
  209. monitoring_log_level=1,
  210. dry_run=True,
  211. )
  212. def test_ping_monitor_does_not_hit_ping_url_when_states_not_matching():
  213. flexmock(module).should_receive('Forgetful_buffering_handler')
  214. hook_config = {'ping_url': 'https://example.com', 'states': ['finish']}
  215. flexmock(module.requests).should_receive('post').never()
  216. module.ping_monitor(
  217. hook_config,
  218. {},
  219. 'config.yaml',
  220. state=module.monitor.State.START,
  221. monitoring_log_level=1,
  222. dry_run=True,
  223. )
  224. def test_ping_monitor_hits_ping_url_when_states_matching():
  225. flexmock(module).should_receive('Forgetful_buffering_handler')
  226. hook_config = {'ping_url': 'https://example.com', 'states': ['start', 'finish']}
  227. flexmock(module.requests).should_receive('post').with_args(
  228. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  229. ).and_return(flexmock(ok=True))
  230. module.ping_monitor(
  231. hook_config,
  232. {},
  233. 'config.yaml',
  234. state=module.monitor.State.START,
  235. monitoring_log_level=1,
  236. dry_run=False,
  237. )
  238. def test_ping_monitor_with_connection_error_logs_warning():
  239. flexmock(module).should_receive('Forgetful_buffering_handler')
  240. hook_config = {'ping_url': 'https://example.com'}
  241. flexmock(module.requests).should_receive('post').with_args(
  242. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  243. ).and_raise(module.requests.exceptions.ConnectionError)
  244. flexmock(module.logger).should_receive('warning').once()
  245. module.ping_monitor(
  246. hook_config,
  247. {},
  248. 'config.yaml',
  249. state=module.monitor.State.START,
  250. monitoring_log_level=1,
  251. dry_run=False,
  252. )
  253. def test_ping_monitor_with_other_error_logs_warning():
  254. flexmock(module).should_receive('Forgetful_buffering_handler')
  255. hook_config = {'ping_url': 'https://example.com'}
  256. response = flexmock(ok=False)
  257. response.should_receive('raise_for_status').and_raise(
  258. module.requests.exceptions.RequestException
  259. )
  260. flexmock(module.requests).should_receive('post').with_args(
  261. 'https://example.com/start', data=''.encode('utf-8'), verify=True
  262. ).and_return(response)
  263. flexmock(module.logger).should_receive('warning').once()
  264. module.ping_monitor(
  265. hook_config,
  266. {},
  267. 'config.yaml',
  268. state=module.monitor.State.START,
  269. monitoring_log_level=1,
  270. dry_run=False,
  271. )