forked from borgmatic-collective/borgmatic
Compare commits
2 Commits
1c7ad67c8c
...
95575c3450
Author | SHA1 | Date |
---|---|---|
Tom Hubrecht | 95575c3450 | |
Tom Hubrecht | 9b071ff92f |
|
@ -59,11 +59,18 @@ def ping_monitor(hook_config, config_filename, state, monitoring_log_level, dry_
|
||||||
username = hook_config.get('username')
|
username = hook_config.get('username')
|
||||||
password = hook_config.get('password')
|
password = hook_config.get('password')
|
||||||
|
|
||||||
auth = (
|
auth = None
|
||||||
requests.auth.HTTPBasicAuth(username, password)
|
if (username and password) is not None:
|
||||||
if (username and password) is not None
|
auth = requests.auth.HTTPBasicAuth(username, password)
|
||||||
else None
|
logger.info(f'{config_filename}: Using basic auth with user {username} for Ntfy')
|
||||||
)
|
elif username is not None:
|
||||||
|
logger.warn(
|
||||||
|
f'{config_filename}: Password missing for Ntfy authentication, defaulting to no auth'
|
||||||
|
)
|
||||||
|
elif password is not None:
|
||||||
|
logger.warn(
|
||||||
|
f'{config_filename}: Username missing for Ntfy authentication, defaulting to no auth'
|
||||||
|
)
|
||||||
|
|
||||||
if not dry_run:
|
if not dry_run:
|
||||||
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
||||||
|
|
|
@ -38,6 +38,7 @@ def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
|
||||||
flexmock(module.requests).should_receive('post').with_args(
|
flexmock(module.requests).should_receive('post').with_args(
|
||||||
f'{default_base_url}/{topic}',
|
f'{default_base_url}/{topic}',
|
||||||
headers=return_default_message_headers(module.monitor.State.FAIL),
|
headers=return_default_message_headers(module.monitor.State.FAIL),
|
||||||
|
auth=None,
|
||||||
).and_return(flexmock(ok=True)).once()
|
).and_return(flexmock(ok=True)).once()
|
||||||
|
|
||||||
module.ping_monitor(
|
module.ping_monitor(
|
||||||
|
@ -45,6 +46,51 @@ def test_ping_monitor_minimal_config_hits_hosted_ntfy_on_fail():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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}',
|
||||||
|
headers=return_default_message_headers(module.monitor.State.FAIL),
|
||||||
|
auth=module.requests.auth.HTTPBasicAuth('testuser', 'fakepassword'),
|
||||||
|
).and_return(flexmock(ok=True)).once()
|
||||||
|
|
||||||
|
module.ping_monitor(
|
||||||
|
hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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}',
|
||||||
|
headers=return_default_message_headers(module.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', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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}',
|
||||||
|
headers=return_default_message_headers(module.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', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
|
def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_start():
|
||||||
hook_config = {'topic': topic}
|
hook_config = {'topic': topic}
|
||||||
flexmock(module.requests).should_receive('post').never()
|
flexmock(module.requests).should_receive('post').never()
|
||||||
|
@ -76,6 +122,7 @@ def test_ping_monitor_minimal_config_hits_selfhosted_ntfy_on_fail():
|
||||||
flexmock(module.requests).should_receive('post').with_args(
|
flexmock(module.requests).should_receive('post').with_args(
|
||||||
f'{custom_base_url}/{topic}',
|
f'{custom_base_url}/{topic}',
|
||||||
headers=return_default_message_headers(module.monitor.State.FAIL),
|
headers=return_default_message_headers(module.monitor.State.FAIL),
|
||||||
|
auth=None,
|
||||||
).and_return(flexmock(ok=True)).once()
|
).and_return(flexmock(ok=True)).once()
|
||||||
|
|
||||||
module.ping_monitor(
|
module.ping_monitor(
|
||||||
|
@ -95,7 +142,7 @@ def test_ping_monitor_minimal_config_does_not_hit_hosted_ntfy_on_fail_dry_run():
|
||||||
def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
|
def test_ping_monitor_custom_message_hits_hosted_ntfy_on_fail():
|
||||||
hook_config = {'topic': topic, 'fail': custom_message_config}
|
hook_config = {'topic': topic, 'fail': custom_message_config}
|
||||||
flexmock(module.requests).should_receive('post').with_args(
|
flexmock(module.requests).should_receive('post').with_args(
|
||||||
f'{default_base_url}/{topic}', headers=custom_message_headers,
|
f'{default_base_url}/{topic}', headers=custom_message_headers, auth=None
|
||||||
).and_return(flexmock(ok=True)).once()
|
).and_return(flexmock(ok=True)).once()
|
||||||
|
|
||||||
module.ping_monitor(
|
module.ping_monitor(
|
||||||
|
@ -108,6 +155,7 @@ def test_ping_monitor_custom_state_hits_hosted_ntfy_on_start():
|
||||||
flexmock(module.requests).should_receive('post').with_args(
|
flexmock(module.requests).should_receive('post').with_args(
|
||||||
f'{default_base_url}/{topic}',
|
f'{default_base_url}/{topic}',
|
||||||
headers=return_default_message_headers(module.monitor.State.START),
|
headers=return_default_message_headers(module.monitor.State.START),
|
||||||
|
auth=None,
|
||||||
).and_return(flexmock(ok=True)).once()
|
).and_return(flexmock(ok=True)).once()
|
||||||
|
|
||||||
module.ping_monitor(
|
module.ping_monitor(
|
||||||
|
@ -124,6 +172,7 @@ def test_ping_monitor_with_connection_error_logs_warning():
|
||||||
flexmock(module.requests).should_receive('post').with_args(
|
flexmock(module.requests).should_receive('post').with_args(
|
||||||
f'{default_base_url}/{topic}',
|
f'{default_base_url}/{topic}',
|
||||||
headers=return_default_message_headers(module.monitor.State.FAIL),
|
headers=return_default_message_headers(module.monitor.State.FAIL),
|
||||||
|
auth=None,
|
||||||
).and_raise(module.requests.exceptions.ConnectionError)
|
).and_raise(module.requests.exceptions.ConnectionError)
|
||||||
flexmock(module.logger).should_receive('warning').once()
|
flexmock(module.logger).should_receive('warning').once()
|
||||||
|
|
||||||
|
@ -145,6 +194,7 @@ def test_ping_monitor_with_other_error_logs_warning():
|
||||||
flexmock(module.requests).should_receive('post').with_args(
|
flexmock(module.requests).should_receive('post').with_args(
|
||||||
f'{default_base_url}/{topic}',
|
f'{default_base_url}/{topic}',
|
||||||
headers=return_default_message_headers(module.monitor.State.FAIL),
|
headers=return_default_message_headers(module.monitor.State.FAIL),
|
||||||
|
auth=None,
|
||||||
).and_return(response)
|
).and_return(response)
|
||||||
flexmock(module.logger).should_receive('warning').once()
|
flexmock(module.logger).should_receive('warning').once()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue