2019-06-12 22:09:04 +02:00
|
|
|
import logging
|
|
|
|
|
2017-10-26 06:38:27 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
2019-06-13 19:09:16 +02:00
|
|
|
from borgmatic import hook as module
|
2017-10-26 06:38:27 +02:00
|
|
|
|
|
|
|
|
2019-10-01 21:23:16 +02:00
|
|
|
def test_interpolate_context_passes_through_command_without_variable():
|
|
|
|
assert module.interpolate_context('ls', {'foo': 'bar'}) == 'ls'
|
|
|
|
|
|
|
|
|
|
|
|
def test_interpolate_context_passes_through_command_with_unknown_variable():
|
|
|
|
assert module.interpolate_context('ls {baz}', {'foo': 'bar'}) == 'ls {baz}'
|
|
|
|
|
|
|
|
|
|
|
|
def test_interpolate_context_interpolates_variables():
|
|
|
|
context = {'foo': 'bar', 'baz': 'quux'}
|
|
|
|
|
|
|
|
assert module.interpolate_context('ls {foo}{baz} {baz}', context) == 'ls barquux quux'
|
|
|
|
|
|
|
|
|
2017-10-26 06:38:27 +02:00
|
|
|
def test_execute_hook_invokes_each_command():
|
2019-10-01 21:23:16 +02:00
|
|
|
flexmock(module).should_receive('interpolate_context').replace_with(
|
|
|
|
lambda command, context: command
|
|
|
|
)
|
2019-06-12 22:09:04 +02:00
|
|
|
flexmock(module.execute).should_receive('execute_command').with_args(
|
|
|
|
[':'], output_log_level=logging.WARNING, shell=True
|
|
|
|
).once()
|
2017-10-26 06:38:27 +02:00
|
|
|
|
2019-06-14 02:05:26 +02:00
|
|
|
module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
|
2017-10-26 07:32:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_execute_hook_with_multiple_commands_invokes_each_command():
|
2019-10-01 21:23:16 +02:00
|
|
|
flexmock(module).should_receive('interpolate_context').replace_with(
|
|
|
|
lambda command, context: command
|
|
|
|
)
|
2019-06-12 22:09:04 +02:00
|
|
|
flexmock(module.execute).should_receive('execute_command').with_args(
|
|
|
|
[':'], output_log_level=logging.WARNING, shell=True
|
|
|
|
).once()
|
|
|
|
flexmock(module.execute).should_receive('execute_command').with_args(
|
|
|
|
['true'], output_log_level=logging.WARNING, shell=True
|
|
|
|
).once()
|
2017-10-26 07:32:06 +02:00
|
|
|
|
2019-06-14 02:05:26 +02:00
|
|
|
module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_execute_hook_with_umask_sets_that_umask():
|
2019-10-01 21:23:16 +02:00
|
|
|
flexmock(module).should_receive('interpolate_context').replace_with(
|
|
|
|
lambda command, context: command
|
|
|
|
)
|
2019-06-14 02:05:26 +02:00
|
|
|
flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
|
|
|
|
flexmock(module.os).should_receive('umask').with_args(0o22).once()
|
|
|
|
flexmock(module.execute).should_receive('execute_command').with_args(
|
|
|
|
[':'], output_log_level=logging.WARNING, shell=True
|
|
|
|
)
|
|
|
|
|
|
|
|
module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
|
2019-05-08 01:06:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_execute_hook_with_dry_run_skips_commands():
|
2019-10-01 21:23:16 +02:00
|
|
|
flexmock(module).should_receive('interpolate_context').replace_with(
|
|
|
|
lambda command, context: command
|
|
|
|
)
|
2019-06-12 22:09:04 +02:00
|
|
|
flexmock(module.execute).should_receive('execute_command').never()
|
2019-05-08 01:06:31 +02:00
|
|
|
|
2019-06-14 02:05:26 +02:00
|
|
|
module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
|
2017-10-26 07:32:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_execute_hook_with_empty_commands_does_not_raise():
|
2019-06-14 02:05:26 +02:00
|
|
|
module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
|
2019-06-13 19:14:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_execute_hook_on_error_logs_as_error():
|
2019-10-01 21:23:16 +02:00
|
|
|
flexmock(module).should_receive('interpolate_context').replace_with(
|
|
|
|
lambda command, context: command
|
|
|
|
)
|
2019-06-13 19:14:16 +02:00
|
|
|
flexmock(module.execute).should_receive('execute_command').with_args(
|
|
|
|
[':'], output_log_level=logging.ERROR, shell=True
|
|
|
|
).once()
|
|
|
|
|
2019-06-14 02:05:26 +02:00
|
|
|
module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)
|
2019-10-15 19:49:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_ping_healthchecks_hits_ping_url():
|
|
|
|
ping_url = 'https://example.com'
|
|
|
|
flexmock(module.requests).should_receive('get').with_args(ping_url)
|
|
|
|
|
|
|
|
module.ping_healthchecks(ping_url, 'config.yaml', dry_run=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_healthchecks_without_ping_url_does_not_raise():
|
|
|
|
flexmock(module.requests).should_receive('get').never()
|
|
|
|
|
|
|
|
module.ping_healthchecks(ping_url_or_uuid=None, config_filename='config.yaml', dry_run=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_healthchecks_with_ping_uuid_hits_corresponding_url():
|
|
|
|
ping_uuid = 'abcd-efgh-ijkl-mnop'
|
|
|
|
flexmock(module.requests).should_receive('get').with_args(
|
|
|
|
'https://hc-ping.com/{}'.format(ping_uuid)
|
|
|
|
)
|
|
|
|
|
|
|
|
module.ping_healthchecks(ping_uuid, 'config.yaml', dry_run=False)
|
|
|
|
|
|
|
|
|
|
|
|
def test_ping_healthchecks_hits_ping_url_with_append():
|
|
|
|
ping_url = 'https://example.com'
|
|
|
|
append = 'failed-so-hard'
|
|
|
|
flexmock(module.requests).should_receive('get').with_args('{}/{}'.format(ping_url, append))
|
|
|
|
|
|
|
|
module.ping_healthchecks(ping_url, 'config.yaml', dry_run=False, append=append)
|