123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- from enum import Enum
- from flexmock import flexmock
- import borgmatic.hooks.monitoring.monitor
- from borgmatic.hooks.monitoring import ntfy as module
- default_base_url = 'https://ntfy.sh'
- custom_base_url = 'https://ntfy.example.com'
- topic = 'borgmatic-unit-testing'
- custom_message_config = {
- 'title': 'borgmatic unit testing',
- 'message': 'borgmatic unit testing',
- '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 = {
- 'X-Title': f'A borgmatic {state.name} event happened',
- 'X-Message': f'A borgmatic {state.name} event happened',
- '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.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=None,
- ).and_return(flexmock(ok=True)).once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_access_token_hits_hosted_ntfy_on_fail():
- hook_config = {
- 'topic': topic,
- 'access_token': 'abc123',
- }
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
- ).and_return(flexmock(ok=True)).once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_username_password_and_access_token_ignores_username_password():
- hook_config = {
- 'topic': topic,
- 'username': 'testuser',
- 'password': 'fakepassword',
- 'access_token': 'abc123',
- }
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=module.requests.auth.HTTPBasicAuth('', 'abc123'),
- ).and_return(flexmock(ok=True)).once()
- flexmock(module.logger).should_receive('warning').once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_username_password_hits_hosted_ntfy_on_fail():
- hook_config = {
- 'topic': topic,
- 'username': 'testuser',
- 'password': 'fakepassword',
- }
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
- ).and_return(flexmock(ok=True)).once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_password_but_no_username_warns():
- hook_config = {'topic': topic, 'password': 'fakepassword'}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=None,
- ).and_return(flexmock(ok=True)).once()
- flexmock(module.logger).should_receive('warning').once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_username_but_no_password_warns():
- hook_config = {'topic': topic, 'username': 'testuser'}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=None,
- ).and_return(flexmock(ok=True)).once()
- flexmock(module.logger).should_receive('warning').once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
- hook_config = {'topic': topic}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').never()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.START,
- 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.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').never()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FINISH,
- 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.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{custom_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=None,
- ).and_return(flexmock(ok=True)).once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
- hook_config = {'topic': topic}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').never()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=True,
- )
- def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
- hook_config = {'topic': topic, 'fail': custom_message_config}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
- ).and_return(flexmock(ok=True)).once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
- hook_config = {'topic': topic, 'states': ['start', 'fail']}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.START),
- auth=None,
- ).and_return(flexmock(ok=True)).once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.START,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_connection_error_logs_warning():
- hook_config = {'topic': topic}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.requests).should_receive('post').with_args(
- f'{default_base_url}/{topic}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=None,
- ).and_raise(module.requests.exceptions.ConnectionError)
- flexmock(module.logger).should_receive('warning').once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_credential_error_logs_warning():
- hook_config = {'topic': topic}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).and_raise(ValueError)
- flexmock(module.requests).should_receive('post').never()
- flexmock(module.logger).should_receive('warning').once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
- def test_ping_monitor_with_other_error_logs_warning():
- hook_config = {'topic': topic}
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- 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}',
- headers=return_default_message_headers(borgmatic.hooks.monitoring.monitor.State.FAIL),
- auth=None,
- ).and_return(response)
- flexmock(module.logger).should_receive('warning').once()
- module.ping_monitor(
- hook_config,
- {},
- 'config.yaml',
- borgmatic.hooks.monitoring.monitor.State.FAIL,
- monitoring_log_level=1,
- dry_run=False,
- )
|