test_healthchecks.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_ping_monitor_hits_ping_url_for_start_state():
  75. flexmock(module).should_receive('Forgetful_buffering_handler')
  76. hook_config = {'ping_url': 'https://example.com'}
  77. flexmock(module.requests).should_receive('post').with_args(
  78. 'https://example.com/start', data=''.encode('utf-8')
  79. )
  80. module.ping_monitor(
  81. hook_config,
  82. 'config.yaml',
  83. state=module.monitor.State.START,
  84. monitoring_log_level=1,
  85. dry_run=False,
  86. )
  87. def test_ping_monitor_hits_ping_url_for_finish_state():
  88. hook_config = {'ping_url': 'https://example.com'}
  89. payload = 'data'
  90. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  91. flexmock(module.requests).should_receive('post').with_args(
  92. 'https://example.com', data=payload.encode('utf-8')
  93. )
  94. module.ping_monitor(
  95. hook_config,
  96. 'config.yaml',
  97. state=module.monitor.State.FINISH,
  98. monitoring_log_level=1,
  99. dry_run=False,
  100. )
  101. def test_ping_monitor_hits_ping_url_for_fail_state():
  102. hook_config = {'ping_url': 'https://example.com'}
  103. payload = 'data'
  104. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  105. flexmock(module.requests).should_receive('post').with_args(
  106. 'https://example.com/fail', data=payload.encode('utf')
  107. )
  108. module.ping_monitor(
  109. hook_config,
  110. 'config.yaml',
  111. state=module.monitor.State.FAIL,
  112. monitoring_log_level=1,
  113. dry_run=False,
  114. )
  115. def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
  116. hook_config = {'ping_url': 'abcd-efgh-ijkl-mnop'}
  117. payload = 'data'
  118. flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
  119. flexmock(module.requests).should_receive('post').with_args(
  120. 'https://hc-ping.com/{}'.format(hook_config['ping_url']), data=payload.encode('utf-8')
  121. )
  122. module.ping_monitor(
  123. hook_config,
  124. 'config.yaml',
  125. state=module.monitor.State.FINISH,
  126. monitoring_log_level=1,
  127. dry_run=False,
  128. )
  129. def test_ping_monitor_dry_run_does_not_hit_ping_url():
  130. flexmock(module).should_receive('Forgetful_buffering_handler')
  131. hook_config = {'ping_url': 'https://example.com'}
  132. flexmock(module.requests).should_receive('post').never()
  133. module.ping_monitor(
  134. hook_config,
  135. 'config.yaml',
  136. state=module.monitor.State.START,
  137. monitoring_log_level=1,
  138. dry_run=True,
  139. )