test_healthchecks.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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)
  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_forgets_log_records_when_capacity_reached():
  10. handler = module.Forgetful_buffering_handler(byte_capacity=len('foo\nbar\n'))
  11. handler.emit(flexmock(getMessage=lambda: 'foo'))
  12. assert handler.buffer == ['foo\n']
  13. handler.emit(flexmock(getMessage=lambda: 'bar'))
  14. assert handler.buffer == ['foo\n', 'bar\n']
  15. handler.emit(flexmock(getMessage=lambda: 'baz'))
  16. assert handler.buffer == ['bar\n', 'baz\n']
  17. handler.emit(flexmock(getMessage=lambda: 'quux'))
  18. assert handler.buffer == ['quux\n']
  19. assert handler.forgot
  20. def test_format_buffered_logs_for_payload_flattens_log_buffer():
  21. handler = module.Forgetful_buffering_handler(byte_capacity=100)
  22. handler.buffer = ['foo\n', 'bar\n']
  23. flexmock(module.logging).should_receive('getLogger').and_return(flexmock(handlers=[handler]))
  24. payload = module.format_buffered_logs_for_payload()
  25. assert payload == 'foo\nbar\n'
  26. def test_format_buffered_logs_for_payload_inserts_truncation_indicator_when_logs_forgotten():
  27. handler = module.Forgetful_buffering_handler(byte_capacity=100)
  28. handler.buffer = ['foo\n', 'bar\n']
  29. handler.forgot = True
  30. flexmock(module.logging).should_receive('getLogger').and_return(flexmock(handlers=[handler]))
  31. payload = module.format_buffered_logs_for_payload()
  32. assert payload == '...\nfoo\nbar\n'
  33. def test_format_buffered_logs_for_payload_without_handler_produces_empty_payload():
  34. flexmock(module.logging).should_receive('getLogger').and_return(
  35. flexmock(handlers=[module.logging.Handler()])
  36. )
  37. payload = module.format_buffered_logs_for_payload()
  38. assert payload == ''
  39. def test_ping_monitor_hits_ping_url_for_start_state():
  40. flexmock(module).should_receive('Forgetful_buffering_handler')
  41. ping_url = 'https://example.com'
  42. flexmock(module.requests).should_receive('post').with_args(
  43. '{}/{}'.format(ping_url, 'start'), data=''.encode('utf-8')
  44. )
  45. module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.START, dry_run=False)
  46. def test_ping_monitor_hits_ping_url_for_finish_state():
  47. ping_url = 'https://example.com'
  48. payload = 'data'
  49. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  50. flexmock(module.requests).should_receive('post').with_args(
  51. ping_url, data=payload.encode('utf-8')
  52. )
  53. module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.FINISH, dry_run=False)
  54. def test_ping_monitor_hits_ping_url_for_fail_state():
  55. ping_url = 'https://example.com'
  56. payload = 'data'
  57. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  58. flexmock(module.requests).should_receive('post').with_args(
  59. '{}/{}'.format(ping_url, 'fail'), data=payload.encode('utf')
  60. )
  61. module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.FAIL, dry_run=False)
  62. def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
  63. ping_uuid = 'abcd-efgh-ijkl-mnop'
  64. payload = 'data'
  65. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  66. flexmock(module.requests).should_receive('post').with_args(
  67. 'https://hc-ping.com/{}'.format(ping_uuid), data=payload.encode('utf-8')
  68. )
  69. module.ping_monitor(ping_uuid, 'config.yaml', state=module.monitor.State.FINISH, dry_run=False)
  70. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  71. flexmock(module).should_receive('Forgetful_buffering_handler')
  72. ping_url = 'https://example.com'
  73. flexmock(module.requests).should_receive('post').never()
  74. module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.START, dry_run=True)