fix PR comments

This commit is contained in:
Pim Kunis 2023-10-04 12:36:54 +02:00
parent 4763c323d0
commit 7a9625cd44
2 changed files with 39 additions and 28 deletions

View file

@ -21,7 +21,7 @@ def ping_monitor(hook_config, config, config_filename, state, monitoring_log_lev
try: try:
import apprise import apprise
from apprise import NotifyFormat, NotifyType from apprise import NotifyFormat, NotifyType
except ImportError: except ImportError: # pragma: no cover
logger.warning('Unable to import Apprise in monitoring hook') logger.warning('Unable to import Apprise in monitoring hook')
return return

View file

@ -5,14 +5,22 @@ from flexmock import flexmock
import borgmatic.hooks.monitor import borgmatic.hooks.monitor
from borgmatic.hooks import apprise as module from borgmatic.hooks import apprise as module
topic = 'borgmatic-unit-testing' TOPIC = 'borgmatic-unit-testing'
def mock_apprise():
apprise_mock = flexmock(
add=lambda servers: None, notify=lambda title, body, body_format, notify_type: None
)
flexmock(apprise.Apprise).new_instances(apprise_mock)
return apprise_mock
def test_ping_monitor_adheres_dry_run(): def test_ping_monitor_adheres_dry_run():
flexmock(apprise.Apprise).should_receive('notify').never() mock_apprise().should_receive('notify').never()
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}]}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]},
{}, {},
'config.yaml', 'config.yaml',
borgmatic.hooks.monitor.State.FAIL, borgmatic.hooks.monitor.State.FAIL,
@ -22,10 +30,10 @@ def test_ping_monitor_adheres_dry_run():
def test_ping_monitor_does_not_hit_with_no_states(): def test_ping_monitor_does_not_hit_with_no_states():
flexmock(apprise.Apprise).should_receive('notify').never() mock_apprise().should_receive('notify').never()
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': []}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': []},
{}, {},
'config.yaml', 'config.yaml',
borgmatic.hooks.monitor.State.FAIL, borgmatic.hooks.monitor.State.FAIL,
@ -35,11 +43,16 @@ def test_ping_monitor_does_not_hit_with_no_states():
def test_ping_monitor_hits_fail_by_default(): def test_ping_monitor_hits_fail_by_default():
flexmock(apprise.Apprise).should_receive('notify').once() mock_apprise().should_receive('notify').with_args(
title='A borgmatic FAIL event happened',
body='A borgmatic FAIL event happened',
body_format=NotifyFormat.TEXT,
notify_type=NotifyType.FAILURE,
).once()
for state in borgmatic.hooks.monitor.State: for state in borgmatic.hooks.monitor.State:
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}]}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}]},
{}, {},
'config.yaml', 'config.yaml',
state, state,
@ -49,7 +62,7 @@ def test_ping_monitor_hits_fail_by_default():
def test_ping_monitor_hits_with_finish_default_config(): def test_ping_monitor_hits_with_finish_default_config():
flexmock(apprise.Apprise).should_receive('notify').with_args( mock_apprise().should_receive('notify').with_args(
title='A borgmatic FINISH event happened', title='A borgmatic FINISH event happened',
body='A borgmatic FINISH event happened', body='A borgmatic FINISH event happened',
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
@ -57,7 +70,7 @@ def test_ping_monitor_hits_with_finish_default_config():
).once() ).once()
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['finish']}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['finish']},
{}, {},
'config.yaml', 'config.yaml',
borgmatic.hooks.monitor.State.FINISH, borgmatic.hooks.monitor.State.FINISH,
@ -67,7 +80,7 @@ def test_ping_monitor_hits_with_finish_default_config():
def test_ping_monitor_hits_with_start_default_config(): def test_ping_monitor_hits_with_start_default_config():
flexmock(apprise.Apprise).should_receive('notify').with_args( mock_apprise().should_receive('notify').with_args(
title='A borgmatic START event happened', title='A borgmatic START event happened',
body='A borgmatic START event happened', body='A borgmatic START event happened',
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
@ -75,7 +88,7 @@ def test_ping_monitor_hits_with_start_default_config():
).once() ).once()
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['start']}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['start']},
{}, {},
'config.yaml', 'config.yaml',
borgmatic.hooks.monitor.State.START, borgmatic.hooks.monitor.State.START,
@ -85,7 +98,7 @@ def test_ping_monitor_hits_with_start_default_config():
def test_ping_monitor_hits_with_fail_default_config(): def test_ping_monitor_hits_with_fail_default_config():
flexmock(apprise.Apprise).should_receive('notify').with_args( mock_apprise().should_receive('notify').with_args(
title='A borgmatic FAIL event happened', title='A borgmatic FAIL event happened',
body='A borgmatic FAIL event happened', body='A borgmatic FAIL event happened',
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
@ -93,7 +106,7 @@ def test_ping_monitor_hits_with_fail_default_config():
).once() ).once()
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['fail']}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['fail']},
{}, {},
'config.yaml', 'config.yaml',
borgmatic.hooks.monitor.State.FAIL, borgmatic.hooks.monitor.State.FAIL,
@ -103,7 +116,7 @@ def test_ping_monitor_hits_with_fail_default_config():
def test_ping_monitor_hits_with_log_default_config(): def test_ping_monitor_hits_with_log_default_config():
flexmock(apprise.Apprise).should_receive('notify').with_args( mock_apprise().should_receive('notify').with_args(
title='A borgmatic LOG event happened', title='A borgmatic LOG event happened',
body='A borgmatic LOG event happened', body='A borgmatic LOG event happened',
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
@ -111,7 +124,7 @@ def test_ping_monitor_hits_with_log_default_config():
).once() ).once()
module.ping_monitor( module.ping_monitor(
{'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'states': ['log']}, {'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}], 'states': ['log']},
{}, {},
'config.yaml', 'config.yaml',
borgmatic.hooks.monitor.State.LOG, borgmatic.hooks.monitor.State.LOG,
@ -120,8 +133,8 @@ def test_ping_monitor_hits_with_log_default_config():
) )
def test_ping_monitor_with_custom_message_title(): def test_ping_monitor_passes_through_custom_message_title():
flexmock(apprise.Apprise).should_receive('notify').with_args( mock_apprise().should_receive('notify').with_args(
title='foo', title='foo',
body='bar', body='bar',
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
@ -130,7 +143,7 @@ def test_ping_monitor_with_custom_message_title():
module.ping_monitor( module.ping_monitor(
{ {
'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}],
'states': ['fail'], 'states': ['fail'],
'fail': {'title': 'foo', 'body': 'bar'}, 'fail': {'title': 'foo', 'body': 'bar'},
}, },
@ -142,8 +155,8 @@ def test_ping_monitor_with_custom_message_title():
) )
def test_ping_monitor_with_custom_message_body(): def test_ping_monitor_passes_through_custom_message_body():
flexmock(apprise.Apprise).should_receive('notify').with_args( mock_apprise().should_receive('notify').with_args(
title='', title='',
body='baz', body='baz',
body_format=NotifyFormat.TEXT, body_format=NotifyFormat.TEXT,
@ -152,7 +165,7 @@ def test_ping_monitor_with_custom_message_body():
module.ping_monitor( module.ping_monitor(
{ {
'services': [{'url': f'ntfys://{topic}', 'label': 'ntfys'}], 'services': [{'url': f'ntfys://{TOPIC}', 'label': 'ntfys'}],
'states': ['fail'], 'states': ['fail'],
'fail': {'body': 'baz'}, 'fail': {'body': 'baz'},
}, },
@ -164,16 +177,14 @@ def test_ping_monitor_with_custom_message_body():
) )
def test_ping_monitor_multiple_services(): def test_ping_monitor_pings_multiple_services():
flexmock(apprise.Apprise).should_receive('add').with_args( mock_apprise().should_receive('add').with_args([f'ntfys://{TOPIC}', f'ntfy://{TOPIC}']).once()
[f'ntfys://{topic}', f'ntfy://{topic}']
).once()
module.ping_monitor( module.ping_monitor(
{ {
'services': [ 'services': [
{'url': f'ntfys://{topic}', 'label': 'ntfys'}, {'url': f'ntfys://{TOPIC}', 'label': 'ntfys'},
{'url': f'ntfy://{topic}', 'label': 'ntfy'}, {'url': f'ntfy://{TOPIC}', 'label': 'ntfy'},
] ]
}, },
{}, {},