test_healthchecks.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_forgets_log_records_when_capacity_reached():
  10. handler = module.Forgetful_buffering_handler(byte_capacity=len('foo\nbar\n'), log_level=1)
  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, log_level=1)
  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, log_level=1)
  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(
  46. ping_url,
  47. 'config.yaml',
  48. state=module.monitor.State.START,
  49. monitoring_log_level=1,
  50. dry_run=False,
  51. )
  52. def test_ping_monitor_hits_ping_url_for_finish_state():
  53. ping_url = 'https://example.com'
  54. payload = 'data'
  55. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  56. flexmock(module.requests).should_receive('post').with_args(
  57. ping_url, data=payload.encode('utf-8')
  58. )
  59. module.ping_monitor(
  60. ping_url,
  61. 'config.yaml',
  62. state=module.monitor.State.FINISH,
  63. monitoring_log_level=1,
  64. dry_run=False,
  65. )
  66. def test_ping_monitor_hits_ping_url_for_fail_state():
  67. ping_url = 'https://example.com'
  68. payload = 'data'
  69. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  70. flexmock(module.requests).should_receive('post').with_args(
  71. '{}/{}'.format(ping_url, 'fail'), data=payload.encode('utf')
  72. )
  73. module.ping_monitor(
  74. ping_url,
  75. 'config.yaml',
  76. state=module.monitor.State.FAIL,
  77. monitoring_log_level=1,
  78. dry_run=False,
  79. )
  80. def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
  81. ping_uuid = 'abcd-efgh-ijkl-mnop'
  82. payload = 'data'
  83. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  84. flexmock(module.requests).should_receive('post').with_args(
  85. 'https://hc-ping.com/{}'.format(ping_uuid), data=payload.encode('utf-8')
  86. )
  87. module.ping_monitor(
  88. ping_uuid,
  89. 'config.yaml',
  90. state=module.monitor.State.FINISH,
  91. monitoring_log_level=1,
  92. dry_run=False,
  93. )
  94. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  95. flexmock(module).should_receive('Forgetful_buffering_handler')
  96. ping_url = 'https://example.com'
  97. flexmock(module.requests).should_receive('post').never()
  98. module.ping_monitor(
  99. ping_url,
  100. 'config.yaml',
  101. state=module.monitor.State.START,
  102. monitoring_log_level=1,
  103. dry_run=True,
  104. )