borgmatic/tests/unit/hooks/test_uptimekuma.py

166 lines
4.5 KiB
Python
Raw Normal View History

2024-06-22 11:19:34 +02:00
from flexmock import flexmock
import borgmatic.hooks.monitor
from borgmatic.hooks import uptimekuma as module
2024-06-24 11:34:52 +02:00
DEFAULT_PUSH_URL = 'https://example.uptime.kuma/api/push/abcd1234'
CUSTOM_PUSH_URL = 'https://uptime.example.com/api/push/efgh5678'
2024-06-22 11:19:34 +02:00
2024-06-22 11:46:17 +02:00
def test_push_monitor_hits_default_uptimekuma_on_fail():
2024-06-24 11:34:52 +02:00
hook_config = {}
2024-06-22 11:19:34 +02:00
flexmock(module.requests).should_receive('get').with_args(
2024-06-24 11:34:52 +02:00
f'{DEFAULT_PUSH_URL}?status=down&msg=fail'
2024-06-22 11:19:34 +02:00
).and_return(flexmock(ok=True)).once()
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
borgmatic.hooks.monitor.State.FAIL,
monitoring_log_level=1,
2024-06-22 11:46:17 +02:00
dry_run=False,
2024-06-22 11:19:34 +02:00
)
def test_push_monitor_hits_custom_uptimekuma_on_fail():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').with_args(
2024-06-24 11:34:52 +02:00
f'{CUSTOM_PUSH_URL}?status=down&msg=fail'
2024-06-22 11:19:34 +02:00
).and_return(flexmock(ok=True)).once()
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
borgmatic.hooks.monitor.State.FAIL,
monitoring_log_level=1,
dry_run=False,
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_custom_uptimekuma_on_start():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').with_args(
2024-06-24 11:34:52 +02:00
f'{CUSTOM_PUSH_URL}?status=up&msg=start'
2024-06-22 11:19:34 +02:00
).and_return(flexmock(ok=True)).once()
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
2024-06-22 11:46:17 +02:00
borgmatic.hooks.monitor.State.START,
2024-06-22 11:19:34 +02:00
monitoring_log_level=1,
dry_run=False,
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_custom_uptimekuma_on_finish():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').with_args(
2024-06-24 11:34:52 +02:00
f'{CUSTOM_PUSH_URL}?status=up&msg=finish'
2024-06-22 11:19:34 +02:00
).and_return(flexmock(ok=True)).once()
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
2024-06-22 11:46:17 +02:00
borgmatic.hooks.monitor.State.FINISH,
2024-06-22 11:19:34 +02:00
monitoring_log_level=1,
dry_run=False,
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_does_not_hit_custom_uptimekuma_on_fail_dry_run():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').never()
2024-06-22 11:19:34 +02:00
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
2024-06-22 11:46:17 +02:00
borgmatic.hooks.monitor.State.FAIL,
2024-06-22 11:19:34 +02:00
monitoring_log_level=1,
2024-06-22 11:46:17 +02:00
dry_run=True,
2024-06-22 11:19:34 +02:00
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_does_not_hit_custom_uptimekuma_on_start_dry_run():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').never()
2024-06-22 11:19:34 +02:00
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
2024-06-22 11:46:17 +02:00
borgmatic.hooks.monitor.State.START,
2024-06-22 11:19:34 +02:00
monitoring_log_level=1,
dry_run=True,
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_does_not_hit_custom_uptimekuma_on_finish_dry_run():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').never()
2024-06-22 11:19:34 +02:00
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
2024-06-22 11:46:17 +02:00
borgmatic.hooks.monitor.State.FINISH,
2024-06-22 11:19:34 +02:00
monitoring_log_level=1,
2024-06-22 11:46:17 +02:00
dry_run=True,
2024-06-22 11:19:34 +02:00
)
def test_push_monitor_with_connection_error_logs_warning():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:46:17 +02:00
flexmock(module.requests).should_receive('get').with_args(
2024-06-24 11:34:52 +02:00
f'{CUSTOM_PUSH_URL}?status=down&msg=fail'
2024-06-22 11:19:34 +02:00
).and_raise(module.requests.exceptions.ConnectionError)
flexmock(module.logger).should_receive('warning').once()
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
borgmatic.hooks.monitor.State.FAIL,
monitoring_log_level=1,
dry_run=False,
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_with_other_error_logs_warning():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
2024-06-22 11:19:34 +02:00
response = flexmock(ok=False)
response.should_receive('raise_for_status').and_raise(
module.requests.exceptions.RequestException
)
flexmock(module.requests).should_receive('post').with_args(
2024-06-24 11:34:52 +02:00
f'{CUSTOM_PUSH_URL}?status=down&msg=fail'
2024-06-22 11:19:34 +02:00
).and_return(response)
flexmock(module.logger).should_receive('warning').once()
module.push_monitor(
2024-06-22 11:19:34 +02:00
hook_config,
{},
'config.yaml',
borgmatic.hooks.monitor.State.FAIL,
monitoring_log_level=1,
dry_run=False,
)
2024-06-24 11:37:44 +02:00
def test_push_monitor_with_invalid_run_state():
2024-06-24 11:34:52 +02:00
hook_config = {'push_url': CUSTOM_PUSH_URL}
flexmock(module.requests).should_receive('get').never()
module.push_monitor(
hook_config,
{},
'config.yaml',
borgmatic.hooks.monitor.State.LOG,
monitoring_log_level=1,
dry_run=True,
)