2019-11-07 19:08:44 +01:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import cronhub as module
|
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_rewrites_ping_url_for_start_state():
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
2022-06-30 06:19:40 +02:00
|
|
|
flexmock(module.requests).should_receive('get').with_args(
|
|
|
|
'https://example.com/start/abcdef'
|
|
|
|
).and_return(flexmock(ok=True))
|
2019-11-07 19:08:44 +01:00
|
|
|
|
2020-01-23 00:10:47 +01:00
|
|
|
module.ping_monitor(
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config,
|
|
|
|
'config.yaml',
|
|
|
|
module.monitor.State.START,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2020-01-23 00:10:47 +01:00
|
|
|
)
|
2019-11-07 19:08:44 +01:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_rewrites_ping_url_and_state_for_start_state():
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config = {'ping_url': 'https://example.com/ping/abcdef'}
|
2022-06-30 06:19:40 +02:00
|
|
|
flexmock(module.requests).should_receive('get').with_args(
|
|
|
|
'https://example.com/start/abcdef'
|
|
|
|
).and_return(flexmock(ok=True))
|
2019-11-07 19:08:44 +01:00
|
|
|
|
2020-01-23 00:10:47 +01:00
|
|
|
module.ping_monitor(
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config,
|
|
|
|
'config.yaml',
|
|
|
|
module.monitor.State.START,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2020-01-23 00:10:47 +01:00
|
|
|
)
|
2019-11-07 19:08:44 +01:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_rewrites_ping_url_for_finish_state():
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
2022-06-30 06:19:40 +02:00
|
|
|
flexmock(module.requests).should_receive('get').with_args(
|
|
|
|
'https://example.com/finish/abcdef'
|
|
|
|
).and_return(flexmock(ok=True))
|
2019-11-13 00:31:07 +01:00
|
|
|
|
2020-01-23 00:10:47 +01:00
|
|
|
module.ping_monitor(
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config,
|
|
|
|
'config.yaml',
|
|
|
|
module.monitor.State.FINISH,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
2020-01-23 00:10:47 +01:00
|
|
|
)
|
2019-11-13 00:31:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_rewrites_ping_url_for_fail_state():
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
2022-06-30 06:19:40 +02:00
|
|
|
flexmock(module.requests).should_receive('get').with_args(
|
|
|
|
'https://example.com/fail/abcdef'
|
|
|
|
).and_return(flexmock(ok=True))
|
2019-11-07 19:08:44 +01:00
|
|
|
|
2020-01-23 00:10:47 +01:00
|
|
|
module.ping_monitor(
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config, 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False
|
2020-01-23 00:10:47 +01:00
|
|
|
)
|
2019-11-07 19:08:44 +01:00
|
|
|
|
|
|
|
|
2019-11-13 00:31:07 +01:00
|
|
|
def test_ping_monitor_dry_run_does_not_hit_ping_url():
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config = {'ping_url': 'https://example.com'}
|
2019-11-07 19:08:44 +01:00
|
|
|
flexmock(module.requests).should_receive('get').never()
|
|
|
|
|
2020-01-23 00:10:47 +01:00
|
|
|
module.ping_monitor(
|
2022-05-24 05:02:10 +02:00
|
|
|
hook_config, 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=True
|
2020-01-23 00:10:47 +01:00
|
|
|
)
|
2022-05-25 00:50:04 +02:00
|
|
|
|
|
|
|
|
2022-06-30 06:19:40 +02:00
|
|
|
def test_ping_monitor_with_connection_error_logs_warning():
|
2022-05-25 00:50:04 +02:00
|
|
|
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
|
|
|
flexmock(module.requests).should_receive('get').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,
|
|
|
|
'config.yaml',
|
|
|
|
module.monitor.State.START,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_with_other_error_logs_warning():
|
|
|
|
hook_config = {'ping_url': 'https://example.com/start/abcdef'}
|
|
|
|
response = flexmock(ok=False)
|
|
|
|
response.should_receive('raise_for_status').and_raise(
|
|
|
|
module.requests.exceptions.RequestException
|
|
|
|
)
|
|
|
|
flexmock(module.requests).should_receive('get').with_args(
|
|
|
|
'https://example.com/start/abcdef'
|
|
|
|
).and_return(response)
|
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
2022-05-25 00:50:04 +02:00
|
|
|
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config,
|
|
|
|
'config.yaml',
|
|
|
|
module.monitor.State.START,
|
|
|
|
monitoring_log_level=1,
|
|
|
|
dry_run=False,
|
|
|
|
)
|
2023-03-06 16:01:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_ping_monitor_with_unsupported_monitoring_state():
|
|
|
|
hook_config = {'ping_url': 'https://example.com'}
|
|
|
|
flexmock(module.logger).should_receive("debug").once().with_args(
|
|
|
|
'{}: Ignoring unsupported monitoring {} in Cronhub hook'.format("config.yaml", "log")
|
|
|
|
)
|
|
|
|
module.ping_monitor(
|
|
|
|
hook_config, 'config.yaml', module.monitor.State.LOG, monitoring_log_level=1, dry_run=False,
|
|
|
|
)
|