2022-06-09 22:10:34 +02:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
from flexmock import flexmock
|
|
|
|
|
2023-03-08 00:45:23 +01:00
|
|
|
import borgmatic.hooks.monitor
|
2022-06-09 22:10:34 +02:00
|
|
|
from borgmatic.hooks import ntfy as module
|
|
|
|
|
|
|
|
default_base_url = 'https://ntfy.sh'
|
|
|
|
custom_base_url = 'https://ntfy.example.com'
|
|
|
|
topic = 'borgmatic-unit-testing'
|
|
|
|
|
|
|
|
custom_message_config = {
|
2023-07-27 08:38:08 +02:00
|
|
|
'title': 'borgmatic unit testing',
|
|
|
|
'message': 'borgmatic unit testing',
|
2022-06-09 22:10:34 +02:00
|
|
|
'priority': 'min',
|
|
|
|
'tags': '+1',
|
|
|
|
}
|
|
|
|
|
|
|
|
custom_message_headers = {
|
|
|
|
'X-Title': custom_message_config['title'],
|
|
|
|
'X-Message': custom_message_config['message'],
|
|
|
|
'X-Priority': custom_message_config['priority'],
|
|
|
|
'X-Tags': custom_message_config['tags'],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def return_default_message_headers(state=Enum):
|
|
|
|
headers = {
|
2023-07-27 08:38:08 +02:00
|
|
|
'X-Title': f'A borgmatic {state.name} event happened',
|
|
|
|
'X-Message': f'A borgmatic {state.name} event happened',
|
2022-06-09 22:10:34 +02:00
|
|
|
'X-Priority': 'default',
|
|
|
|
'X-Tags': 'borgmatic',
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
|
|
|
|
hook_config = {'topic': topic}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
2022-06-30 06:19:40 +02:00
|
|
|
).and_return(flexmock(ok=True)).once()
|
2022-06-09 22:10:34 +02:00
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2022-06-09 22:10:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-25 20:03:51 +01:00
|
|
|
def test_ping_monitor_with_auth_hits_hosted_ntfy_on_fail():
|
|
|
|
hook_config = {
|
|
|
|
'topic': topic,
|
|
|
|
'username': 'testuser',
|
|
|
|
'password': 'fakepassword',
|
|
|
|
}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
|
|
|
|
).and_return(flexmock(ok=True)).once()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2023-02-25 20:03:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_auth_with_no_username_warning():
|
|
|
|
hook_config = {'topic': topic, 'password': 'fakepassword'}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
|
|
|
).and_return(flexmock(ok=True)).once()
|
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2023-02-25 20:03:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_auth_with_no_password_warning():
|
|
|
|
hook_config = {'topic': topic, 'username': 'testuser'}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
|
|
|
).and_return(flexmock(ok=True)).once()
|
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2023-02-25 20:03:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-09 22:10:34 +02:00
|
|
|
def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
|
|
|
|
hook_config = {'topic': topic}
|
|
|
|
flexmock(module.requests).should_receive('post').never()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2022-06-09 22:10:34 +02:00
|
|
|
'config.yaml',
|
2023-03-08 00:45:23 +01:00
|
|
|
borgmatic.hooks.monitor.State.START,
|
2022-06-09 22:10:34 +02:00
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_finish():
|
|
|
|
hook_config = {'topic': topic}
|
|
|
|
flexmock(module.requests).should_receive('post').never()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2022-06-09 22:10:34 +02:00
|
|
|
'config.yaml',
|
2023-03-08 00:45:23 +01:00
|
|
|
borgmatic.hooks.monitor.State.FINISH,
|
2022-06-09 22:10:34 +02:00
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
|
|
|
|
hook_config = {'topic': topic, 'server': custom_base_url}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{custom_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
2022-06-30 06:19:40 +02:00
|
|
|
).and_return(flexmock(ok=True)).once()
|
2022-06-09 22:10:34 +02:00
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2022-06-09 22:10:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
|
|
|
|
hook_config = {'topic': topic}
|
|
|
|
flexmock(module.requests).should_receive('post').never()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=True,
|
2022-06-09 22:10:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
|
|
|
|
hook_config = {'topic': topic, 'fail': custom_message_config}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
2023-02-25 20:03:51 +01:00
|
|
|
f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
|
2022-06-30 06:19:40 +02:00
|
|
|
).and_return(flexmock(ok=True)).once()
|
2022-06-09 22:10:34 +02:00
|
|
|
|
|
|
|
module.ping_monitor(
|
2023-03-08 00:45:23 +01:00
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2023-03-08 00:45:23 +01:00
|
|
|
'config.yaml',
|
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2022-06-09 22:10:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
|
|
|
|
hook_config = {'topic': topic, 'states': ['start', 'fail']}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.START),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
2022-06-30 06:19:40 +02:00
|
|
|
).and_return(flexmock(ok=True)).once()
|
2022-06-09 22:10:34 +02:00
|
|
|
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2022-06-09 22:10:34 +02:00
|
|
|
'config.yaml',
|
2023-03-08 00:45:23 +01:00
|
|
|
borgmatic.hooks.monitor.State.START,
|
2022-06-09 22:10:34 +02:00
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-30 06:19:40 +02:00
|
|
|
def test_ping_monitor_with_connection_error_logs_warning():
|
2022-06-09 22:10:34 +02:00
|
|
|
hook_config = {'topic': topic}
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
2022-06-09 22:10:34 +02:00
|
|
|
).and_raise(module.requests.exceptions.ConnectionError)
|
2022-06-30 06:19:40 +02:00
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
|
|
|
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2022-06-30 06:19:40 +02:00
|
|
|
'config.yaml',
|
2023-03-08 00:45:23 +01:00
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
2022-06-30 06:19:40 +02:00
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_with_other_error_logs_warning():
|
|
|
|
hook_config = {'topic': topic}
|
|
|
|
response = flexmock(ok=False)
|
|
|
|
response.should_receive('raise_for_status').and_raise(
|
|
|
|
module.requests.exceptions.RequestException
|
|
|
|
)
|
|
|
|
flexmock(module.requests).should_receive('post').with_args(
|
|
|
|
f'{default_base_url}/{topic}',
|
2023-03-08 00:45:23 +01:00
|
|
|
headers=return_default_message_headers(borgmatic.hooks.monitor.State.FAIL),
|
2023-02-25 20:03:51 +01:00
|
|
|
auth=None,
|
2022-06-30 06:19:40 +02:00
|
|
|
).and_return(response)
|
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
2022-06-09 22:10:34 +02:00
|
|
|
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config,
|
2023-07-09 08:14:30 +02:00
|
|
|
{},
|
2022-06-09 22:10:34 +02:00
|
|
|
'config.yaml',
|
2023-03-08 00:45:23 +01:00
|
|
|
borgmatic.hooks.monitor.State.FAIL,
|
2022-06-09 22:10:34 +02:00
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|