test_healthchecks.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 test_initialize_monitor_creates_log_handler_with_ping_body_limit():
  50. ping_body_limit = 100
  51. monitoring_log_level = 1
  52. flexmock(module).should_receive('Forgetful_buffering_handler').with_args(
  53. ping_body_limit - len(module.PAYLOAD_TRUNCATION_INDICATOR), monitoring_log_level
  54. ).once()
  55. module.initialize_monitor(
  56. {'ping_body_limit': ping_body_limit}, 'test.yaml', monitoring_log_level, dry_run=False
  57. )
  58. def test_initialize_monitor_creates_log_handler_with_default_ping_body_limit():
  59. monitoring_log_level = 1
  60. flexmock(module).should_receive('Forgetful_buffering_handler').with_args(
  61. module.DEFAULT_PING_BODY_LIMIT_BYTES - len(module.PAYLOAD_TRUNCATION_INDICATOR),
  62. monitoring_log_level,
  63. ).once()
  64. module.initialize_monitor({}, 'test.yaml', monitoring_log_level, dry_run=False)
  65. def test_initialize_monitor_creates_log_handler_with_zero_ping_body_limit():
  66. ping_body_limit = 0
  67. monitoring_log_level = 1
  68. flexmock(module).should_receive('Forgetful_buffering_handler').with_args(
  69. ping_body_limit, monitoring_log_level
  70. ).once()
  71. module.initialize_monitor(
  72. {'ping_body_limit': ping_body_limit}, 'test.yaml', monitoring_log_level, dry_run=False
  73. )
  74. def test_initialize_monitor_creates_log_handler_when_send_logs_true():
  75. flexmock(module).should_receive('Forgetful_buffering_handler').once()
  76. module.initialize_monitor(
  77. {'send_logs': True}, 'test.yaml', monitoring_log_level=1, dry_run=False
  78. )
  79. def test_initialize_monitor_bails_when_send_logs_false():
  80. flexmock(module).should_receive('Forgetful_buffering_handler').never()
  81. module.initialize_monitor(
  82. {'send_logs': False}, 'test.yaml', monitoring_log_level=1, dry_run=False
  83. )
  84. def test_ping_monitor_hits_ping_url_for_start_state():
  85. flexmock(module).should_receive('Forgetful_buffering_handler')
  86. hook_config = {'ping_url': 'https://example.com'}
  87. flexmock(module.requests).should_receive('post').with_args(
  88. 'https://example.com/start', data=''.encode('utf-8')
  89. )
  90. module.ping_monitor(
  91. hook_config,
  92. 'config.yaml',
  93. state=module.monitor.State.START,
  94. monitoring_log_level=1,
  95. dry_run=False,
  96. )
  97. def test_ping_monitor_hits_ping_url_for_finish_state():
  98. hook_config = {'ping_url': 'https://example.com'}
  99. payload = 'data'
  100. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  101. flexmock(module.requests).should_receive('post').with_args(
  102. 'https://example.com', data=payload.encode('utf-8')
  103. )
  104. module.ping_monitor(
  105. hook_config,
  106. 'config.yaml',
  107. state=module.monitor.State.FINISH,
  108. monitoring_log_level=1,
  109. dry_run=False,
  110. )
  111. def test_ping_monitor_hits_ping_url_for_fail_state():
  112. hook_config = {'ping_url': 'https://example.com'}
  113. payload = 'data'
  114. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  115. flexmock(module.requests).should_receive('post').with_args(
  116. 'https://example.com/fail', data=payload.encode('utf')
  117. )
  118. module.ping_monitor(
  119. hook_config,
  120. 'config.yaml',
  121. state=module.monitor.State.FAIL,
  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).should_receive('format_buffered_logs_for_payload').and_return(payload)
  129. flexmock(module.requests).should_receive('post').with_args(
  130. 'https://hc-ping.com/{}'.format(hook_config['ping_url']), data=payload.encode('utf-8')
  131. )
  132. module.ping_monitor(
  133. hook_config,
  134. 'config.yaml',
  135. state=module.monitor.State.FINISH,
  136. monitoring_log_level=1,
  137. dry_run=False,
  138. )
  139. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  140. flexmock(module).should_receive('Forgetful_buffering_handler')
  141. hook_config = {'ping_url': 'https://example.com'}
  142. flexmock(module.requests).should_receive('post').never()
  143. module.ping_monitor(
  144. hook_config,
  145. 'config.yaml',
  146. state=module.monitor.State.START,
  147. monitoring_log_level=1,
  148. dry_run=True,
  149. )
  150. def test_ping_monitor_does_not_hit_ping_url_when_states_not_matching():
  151. flexmock(module).should_receive('Forgetful_buffering_handler')
  152. hook_config = {'ping_url': 'https://example.com', 'states': ['finish']}
  153. flexmock(module.requests).should_receive('post').never()
  154. module.ping_monitor(
  155. hook_config,
  156. 'config.yaml',
  157. state=module.monitor.State.START,
  158. monitoring_log_level=1,
  159. dry_run=True,
  160. )
  161. def test_ping_monitor_hits_ping_url_when_states_matching():
  162. flexmock(module).should_receive('Forgetful_buffering_handler')
  163. hook_config = {'ping_url': 'https://example.com', 'states': ['start', 'finish']}
  164. flexmock(module.requests).should_receive('post').with_args(
  165. 'https://example.com/start', data=''.encode('utf-8')
  166. )
  167. module.ping_monitor(
  168. hook_config,
  169. 'config.yaml',
  170. state=module.monitor.State.START,
  171. monitoring_log_level=1,
  172. dry_run=False,
  173. )
  174. def test_ping_monitor_with_connection_error_does_not_raise():
  175. flexmock(module).should_receive('Forgetful_buffering_handler')
  176. flexmock(module.logger).should_receive('warning')
  177. hook_config = {'ping_url': 'https://example.com'}
  178. flexmock(module.requests).should_receive('post').with_args(
  179. 'https://example.com/start', data=''.encode('utf-8')
  180. ).and_raise(module.requests.exceptions.ConnectionError)
  181. module.ping_monitor(
  182. hook_config,
  183. 'config.yaml',
  184. state=module.monitor.State.START,
  185. monitoring_log_level=1,
  186. dry_run=False,
  187. )