2019-10-22 00:52:14 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import healthchecks as module
|
|
|
|
|
|
|
|
|
2019-11-18 01:54:27 +01:00
|
|
|
def test_forgetful_buffering_handler_emit_collects_log_records():
|
|
|
|
handler = module.Forgetful_buffering_handler(byte_capacity=100)
|
|
|
|
handler.emit(flexmock(getMessage=lambda: 'foo'))
|
|
|
|
handler.emit(flexmock(getMessage=lambda: 'bar'))
|
|
|
|
|
|
|
|
assert handler.buffer == ['foo\n', 'bar\n']
|
|
|
|
assert not handler.forgot
|
|
|
|
|
|
|
|
|
|
|
|
def test_forgetful_buffering_handler_emit_forgets_log_records_when_capacity_reached():
|
|
|
|
handler = module.Forgetful_buffering_handler(byte_capacity=len('foo\nbar\n'))
|
|
|
|
handler.emit(flexmock(getMessage=lambda: 'foo'))
|
|
|
|
assert handler.buffer == ['foo\n']
|
|
|
|
handler.emit(flexmock(getMessage=lambda: 'bar'))
|
|
|
|
assert handler.buffer == ['foo\n', 'bar\n']
|
|
|
|
handler.emit(flexmock(getMessage=lambda: 'baz'))
|
|
|
|
assert handler.buffer == ['bar\n', 'baz\n']
|
|
|
|
handler.emit(flexmock(getMessage=lambda: 'quux'))
|
|
|
|
assert handler.buffer == ['quux\n']
|
|
|
|
assert handler.forgot
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_buffered_logs_for_payload_flattens_log_buffer():
|
|
|
|
handler = module.Forgetful_buffering_handler(byte_capacity=100)
|
|
|
|
handler.buffer = ['foo\n', 'bar\n']
|
|
|
|
flexmock(module.logging).should_receive('getLogger').and_return(flexmock(handlers=[handler]))
|
|
|
|
|
|
|
|
payload = module.format_buffered_logs_for_payload()
|
|
|
|
|
|
|
|
assert payload == 'foo\nbar\n'
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_buffered_logs_for_payload_inserts_truncation_indicator_when_logs_forgotten():
|
|
|
|
handler = module.Forgetful_buffering_handler(byte_capacity=100)
|
|
|
|
handler.buffer = ['foo\n', 'bar\n']
|
|
|
|
handler.forgot = True
|
|
|
|
flexmock(module.logging).should_receive('getLogger').and_return(flexmock(handlers=[handler]))
|
|
|
|
|
|
|
|
payload = module.format_buffered_logs_for_payload()
|
|
|
|
|
|
|
|
assert payload == '...\nfoo\nbar\n'
|
|
|
|
|
|
|
|
|
2019-11-18 03:55:40 +01:00
|
|
|
def test_format_buffered_logs_for_payload_without_handler_produces_empty_payload():
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module.logging).should_receive('getLogger').and_return(
|
|
|
|
flexmock(handlers=[module.logging.Handler()])
|
|
|
|
)
|
|
|
|
|
|
|
|
payload = module.format_buffered_logs_for_payload()
|
|
|
|
|
2019-11-18 03:55:40 +01:00
|
|
|
assert payload == ''
|
2019-11-18 01:54:27 +01:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_hits_ping_url_for_start_state():
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module).should_receive('Forgetful_buffering_handler')
|
2019-11-13 00:31:07 +01:00
|
|
|
ping_url = 'https://example.com'
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
2019-11-30 23:51:32 +01:00
|
|
|
'{}/{}'.format(ping_url, 'start'), data=''.encode('utf-8')
|
2019-11-18 01:54:27 +01:00
|
|
|
)
|
2019-11-13 00:31:07 +01:00
|
|
|
|
|
|
|
module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.START, dry_run=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_hits_ping_url_for_finish_state():
|
2019-10-22 00:52:14 +02:00
|
|
|
ping_url = 'https://example.com'
|
2019-11-30 23:51:32 +01:00
|
|
|
payload = 'data'
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
|
2019-11-30 23:51:32 +01:00
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
ping_url, data=payload.encode('utf-8')
|
|
|
|
)
|
2019-10-22 00:52:14 +02:00
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.FINISH, dry_run=False)
|
2019-10-22 00:52:14 +02:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_hits_ping_url_for_fail_state():
|
|
|
|
ping_url = 'https://example.com'
|
2019-11-30 23:51:32 +01:00
|
|
|
payload = 'data'
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
2019-11-30 23:51:32 +01:00
|
|
|
'{}/{}'.format(ping_url, 'fail'), data=payload.encode('utf')
|
2019-11-18 01:54:27 +01:00
|
|
|
)
|
2019-10-22 00:52:14 +02:00
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.FAIL, dry_run=False)
|
2019-10-22 00:52:14 +02:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_with_ping_uuid_hits_corresponding_url():
|
2019-10-22 00:52:14 +02:00
|
|
|
ping_uuid = 'abcd-efgh-ijkl-mnop'
|
2019-11-30 23:51:32 +01:00
|
|
|
payload = 'data'
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module).should_receive('format_buffered_logs_for_payload').and_return(payload)
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
2019-11-30 23:51:32 +01:00
|
|
|
'https://hc-ping.com/{}'.format(ping_uuid), data=payload.encode('utf-8')
|
2019-10-22 00:52:14 +02:00
|
|
|
)
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
module.ping_monitor(ping_uuid, 'config.yaml', state=module.monitor.State.FINISH, dry_run=False)
|
2019-11-07 19:08:44 +01:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_dry_run_does_not_hit_ping_url():
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module).should_receive('Forgetful_buffering_handler')
|
2019-11-07 19:08:44 +01:00
|
|
|
ping_url = 'https://example.com'
|
2019-11-18 01:54:27 +01:00
|
|
|
flexmock(module.requests).should_receive('post').never()
|
2019-11-07 19:08:44 +01:00
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
module.ping_monitor(ping_url, 'config.yaml', state=module.monitor.State.START, dry_run=True)
|