2019-06-12 22:09:04 +02:00
|
|
|
import logging
|
2020-01-25 05:52:48 +01:00
|
|
|
import subprocess
|
2019-06-12 22:09:04 +02:00
|
|
|
|
2017-10-26 06:38:27 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
2019-10-22 00:52:14 +02:00
|
|
|
from borgmatic.hooks import command 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():
|
2022-05-24 00:27:54 +02:00
|
|
|
assert module.interpolate_context('test.yaml', 'pre-backup', 'ls', {'foo': 'bar'}) == 'ls'
|
2019-10-01 21:23:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_interpolate_context_passes_through_command_with_unknown_variable():
|
2023-03-24 07:11:14 +01:00
|
|
|
command = 'ls {baz}' # noqa: FS003
|
|
|
|
|
|
|
|
assert module.interpolate_context('test.yaml', 'pre-backup', command, {'foo': 'bar'}) == command
|
2019-10-01 21:23:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_interpolate_context_interpolates_variables():
|
2023-03-24 07:11:14 +01:00
|
|
|
command = 'ls {foo}{baz} {baz}' # noqa: FS003
|
2019-10-01 21:23:16 +02:00
|
|
|
context = {'foo': 'bar', 'baz': 'quux'}
|
|
|
|
|
2022-05-24 00:27:54 +02:00
|
|
|
assert (
|
2023-03-24 07:11:14 +01:00
|
|
|
module.interpolate_context('test.yaml', 'pre-backup', command, context) == 'ls barquux quux'
|
2022-05-24 00:27:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 19:21:49 +01:00
|
|
|
def test_interpolate_context_escapes_interpolated_variables():
|
|
|
|
command = 'ls {foo} {inject}' # noqa: FS003
|
|
|
|
context = {'foo': 'bar', 'inject': 'hi; naughty-command'}
|
|
|
|
|
|
|
|
assert (
|
|
|
|
module.interpolate_context('test.yaml', 'pre-backup', command, context)
|
|
|
|
== "ls bar 'hi; naughty-command'"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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(
|
2022-05-24 00:27:54 +02:00
|
|
|
lambda config_file, hook_description, command, context: command
|
2019-10-01 21:23:16 +02:00
|
|
|
)
|
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(
|
2022-05-24 00:27:54 +02:00
|
|
|
lambda config_file, hook_description, command, context: command
|
2019-10-01 21:23:16 +02:00
|
|
|
)
|
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(
|
2022-05-24 00:27:54 +02:00
|
|
|
lambda config_file, hook_description, command, context: command
|
2019-10-01 21:23:16 +02:00
|
|
|
)
|
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(
|
2022-05-24 00:27:54 +02:00
|
|
|
lambda config_file, hook_description, command, context: command
|
2019-10-01 21:23:16 +02:00
|
|
|
)
|
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(
|
2022-05-24 00:27:54 +02:00
|
|
|
lambda config_file, hook_description, command, context: command
|
2019-10-01 21:23:16 +02:00
|
|
|
)
|
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)
|
2020-01-25 05:52:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_considered_soft_failure_treats_soft_fail_exit_code_as_soft_fail():
|
|
|
|
error = subprocess.CalledProcessError(module.SOFT_FAIL_EXIT_CODE, 'try again')
|
|
|
|
|
|
|
|
assert module.considered_soft_failure('config.yaml', error)
|
|
|
|
|
|
|
|
|
|
|
|
def test_considered_soft_failure_does_not_treat_other_exit_code_as_soft_fail():
|
|
|
|
error = subprocess.CalledProcessError(1, 'error')
|
|
|
|
|
|
|
|
assert not module.considered_soft_failure('config.yaml', error)
|
|
|
|
|
|
|
|
|
|
|
|
def test_considered_soft_failure_does_not_treat_other_exception_type_as_soft_fail():
|
|
|
|
assert not module.considered_soft_failure('config.yaml', Exception())
|