From d09b4c72a9bd77802297e42b83760e6a0ce84625 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Mon, 18 Nov 2024 20:32:17 -0800 Subject: [PATCH] Fix a few remaining Pushover issues from the PR. --- NEWS | 2 ++ borgmatic/config/schema.yaml | 1 - borgmatic/hooks/pushover.py | 9 +++++---- docs/how-to/monitor-your-backups.md | 2 +- tests/unit/hooks/test_pushover.py | 23 ++++------------------- 5 files changed, 12 insertions(+), 25 deletions(-) diff --git a/NEWS b/NEWS index aab16424..62439f4b 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,8 @@ file to support the new runtime and state directory logic. * #939: Fix borgmatic ignoring the "BORG_RELOCATED_REPO_ACCESS_IS_OK" and "BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK" environment variables. + * Add a Pushover monitoring hook. See the documentation for more information: + https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/#pushover-hook 1.9.1 * #928: Fix the user runtime directory location on macOS (and possibly Cygwin). diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 43a5f384..64c49f6f 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -1719,7 +1719,6 @@ properties: otherwise just the URL is shown. example: Pushover Link finish: - type: object type: object properties: message: diff --git a/borgmatic/hooks/pushover.py b/borgmatic/hooks/pushover.py index 65cc636d..739283bd 100644 --- a/borgmatic/hooks/pushover.py +++ b/borgmatic/hooks/pushover.py @@ -39,10 +39,10 @@ def ping_monitor(hook_config, config, config_filename, state, monitoring_log_lev if state_config.get('priority') == EMERGENCY_PRIORITY: if 'expire' not in state_config: - logger.info(f'{config_filename}: Setting expire to default (10min).') + logger.info(f'{config_filename}: Setting expire to default (10 min).') state_config['expire'] = 600 if 'retry' not in state_config: - logger.info(f'{config_filename}: Setting retry to default (30sec).') + logger.info(f'{config_filename}: Setting retry to default (30 sec).') state_config['retry'] = 30 else: if 'expire' in state_config or 'retry' in state_config: @@ -51,14 +51,15 @@ def ping_monitor(hook_config, config, config_filename, state, monitoring_log_lev ) state_config = { - key: (int(value) if key in 'html' else value) for key, value in state_config.items() + key: (int(value) if key == 'html' else value) for key, value in state_config.items() } data = dict( { 'token': token, 'user': user, - 'message': state.name.lower(), # default to state name. Can be overwritten in state_config loop below. + # Default to state name. Can be overwritten by state_config below. + 'message': state.name.lower(), }, **state_config, ) diff --git a/docs/how-to/monitor-your-backups.md b/docs/how-to/monitor-your-backups.md index daf1adc6..b180be12 100644 --- a/docs/how-to/monitor-your-backups.md +++ b/docs/how-to/monitor-your-backups.md @@ -338,7 +338,7 @@ pushover: fail: message: "Backup Failed" priority: 2 # Requests acknowledgement for messages. - expire: 1200 # Used only for priority 2. Default is 1200 seconds. + expire: 600 # Used only for priority 2. Default is 600 seconds. retry: 30 # Used only for priority 2. Default is 30 seconds. device: "pixel8" title: "Backup Failed" diff --git a/tests/unit/hooks/test_pushover.py b/tests/unit/hooks/test_pushover.py index 2654aac2..1cc451a4 100644 --- a/tests/unit/hooks/test_pushover.py +++ b/tests/unit/hooks/test_pushover.py @@ -222,7 +222,7 @@ def test_ping_monitor_start_state_backup_default_message_with_priority_emergency ) -def test_ping_monitor_start_state_backup_default_message_with_priority_high_declared_expire_and_retry_ignored_success(): +def test_ping_monitor_start_state_backup_default_message_with_priority_high_declared_expire_and_retry_raises(): ''' This simulates priority level 1, retry and expiry being set. Since expire and retry are only used for priority level 2, they should not be included @@ -238,6 +238,7 @@ def test_ping_monitor_start_state_backup_default_message_with_priority_high_decl flexmock(module.logger).should_receive('warning').never() flexmock(module.requests).should_receive('post').never() + with pytest.raises(ValueError): module.ping_monitor( hook_config, @@ -452,15 +453,7 @@ def test_ping_monitor_config_with_minimum_config_fail_state_backup_successfully_ ''' hook_config = {'token': 'ksdjfwoweijfvwoeifvjmwghagy92', 'user': '983hfe0of902lkjfa2amanfgui'} flexmock(module.logger).should_receive('warning').never() - flexmock(module.requests).should_receive('post').with_args( - 'https://api.pushover.net/1/messages.json', - headers={'Content-type': 'application/x-www-form-urlencoded'}, - data={ - 'token': 'ksdjfwoweijfvwoeifvjmwghagy92', - 'user': '983hfe0of902lkjfa2amanfgui', - 'message': 'fail', - }, - ).and_return(flexmock(ok=True)).never() + flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True)).never() module.ping_monitor( hook_config, @@ -481,15 +474,7 @@ def test_ping_monitor_config_incorrect_state_exit_early(): 'user': '983hfe0of902lkjfa2amanfgui', } flexmock(module.logger).should_receive('warning').never() - flexmock(module.requests).should_receive('post').with_args( - 'https://api.pushover.net/1/messages.json', - headers={'Content-type': 'application/x-www-form-urlencoded'}, - data={ - 'token': 'ksdjfwoweijfvwoeifvjmwghagy92', - 'user': '983hfe0of902lkjfa2amanfgui', - 'message': 'start', - }, - ).and_return(flexmock(ok=True)).never() + flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True)).never() module.ping_monitor( hook_config,