123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- import logging
- import subprocess
- import pytest
- from flexmock import flexmock
- from borgmatic.hooks import command as module
- def test_interpolate_context_passes_through_command_without_variable():
- assert module.interpolate_context('pre-backup', 'ls', {'foo': 'bar'}) == 'ls'
- def test_interpolate_context_warns_and_passes_through_command_with_unknown_variable():
- command = 'ls {baz}' # noqa: FS003
- flexmock(module.logger).should_receive('warning').once()
- assert module.interpolate_context('pre-backup', command, {'foo': 'bar'}) == command
- def test_interpolate_context_does_not_warn_and_passes_through_command_with_unknown_variable_matching_borg_placeholder():
- command = 'ls {hostname}' # noqa: FS003
- flexmock(module.logger).should_receive('warning').never()
- assert module.interpolate_context('pre-backup', command, {'foo': 'bar'}) == command
- def test_interpolate_context_interpolates_variables():
- command = 'ls {foo}{baz} {baz}' # noqa: FS003
- context = {'foo': 'bar', 'baz': 'quux'}
- assert module.interpolate_context('pre-backup', command, context) == 'ls barquux quux'
- def test_interpolate_context_escapes_interpolated_variables():
- command = 'ls {foo} {inject}' # noqa: FS003
- context = {'foo': 'bar', 'inject': 'hi; naughty-command'}
- assert (
- module.interpolate_context('pre-backup', command, context) == "ls bar 'hi; naughty-command'"
- )
- def test_make_environment_without_pyinstaller_does_not_touch_environment():
- assert module.make_environment({}, sys_module=flexmock()) == {}
- def test_make_environment_with_pyinstaller_clears_LD_LIBRARY_PATH():
- assert module.make_environment({}, sys_module=flexmock(frozen=True, _MEIPASS='yup')) == {
- 'LD_LIBRARY_PATH': ''
- }
- def test_make_environment_with_pyinstaller_and_LD_LIBRARY_PATH_ORIG_copies_it_into_LD_LIBRARY_PATH():
- assert module.make_environment(
- {'LD_LIBRARY_PATH_ORIG': '/lib/lib/lib'}, sys_module=flexmock(frozen=True, _MEIPASS='yup')
- ) == {'LD_LIBRARY_PATH_ORIG': '/lib/lib/lib', 'LD_LIBRARY_PATH': '/lib/lib/lib'}
- @pytest.mark.parametrize(
- 'hooks,filters,expected_hooks',
- (
- (
- (
- {
- 'before': 'action',
- 'run': ['foo'],
- },
- {
- 'after': 'action',
- 'run': ['bar'],
- },
- {
- 'before': 'repository',
- 'run': ['baz'],
- },
- ),
- {},
- (
- {
- 'before': 'action',
- 'run': ['foo'],
- },
- {
- 'after': 'action',
- 'run': ['bar'],
- },
- {
- 'before': 'repository',
- 'run': ['baz'],
- },
- ),
- ),
- (
- (
- {
- 'before': 'action',
- 'run': ['foo'],
- },
- {
- 'after': 'action',
- 'run': ['bar'],
- },
- {
- 'before': 'repository',
- 'run': ['baz'],
- },
- ),
- {
- 'before': 'action',
- },
- (
- {
- 'before': 'action',
- 'run': ['foo'],
- },
- ),
- ),
- (
- (
- {
- 'after': 'action',
- 'run': ['foo'],
- },
- {
- 'before': 'action',
- 'run': ['bar'],
- },
- {
- 'after': 'repository',
- 'run': ['baz'],
- },
- ),
- {
- 'after': 'action',
- },
- (
- {
- 'after': 'action',
- 'run': ['foo'],
- },
- ),
- ),
- (
- (
- {
- 'before': 'action',
- 'run': ['foo'],
- },
- {
- 'before': 'action',
- 'run': ['bar'],
- },
- {
- 'before': 'action',
- 'run': ['baz'],
- },
- ),
- {
- 'before': 'action',
- 'action_names': ['create', 'compact', 'extract'],
- },
- (
- {
- 'before': 'action',
- 'run': ['foo'],
- },
- {
- 'before': 'action',
- 'run': ['bar'],
- },
- {
- 'before': 'action',
- 'run': ['baz'],
- },
- ),
- ),
- (
- (
- {
- 'before': 'action',
- 'states': ['finish'], # Not actually valid; only valid for "after".
- 'run': ['foo'],
- },
- {
- 'after': 'action',
- 'run': ['bar'],
- },
- {
- 'after': 'action',
- 'states': ['finish'],
- 'run': ['baz'],
- },
- {
- 'after': 'action',
- 'states': ['fail'],
- 'run': ['quux'],
- },
- ),
- {
- 'after': 'action',
- 'state_names': ['finish'],
- },
- (
- {
- 'after': 'action',
- 'run': ['bar'],
- },
- {
- 'after': 'action',
- 'states': ['finish'],
- 'run': ['baz'],
- },
- ),
- ),
- ),
- )
- def test_filter_hooks(hooks, filters, expected_hooks):
- assert module.filter_hooks(hooks, **filters) == expected_hooks
- LOGGING_ANSWER = flexmock()
- def test_execute_hooks_invokes_each_hook_and_command():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').replace_with(
- lambda hook_description, command, context: command
- )
- flexmock(module).should_receive('make_environment').and_return({})
- for command in ('foo', 'bar', 'baz'):
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- [command],
- output_log_level=LOGGING_ANSWER,
- shell=True,
- environment={},
- working_directory=None,
- ).once()
- module.execute_hooks(
- [{'before': 'create', 'run': ['foo']}, {'before': 'create', 'run': ['bar', 'baz']}],
- umask=None,
- working_directory=None,
- dry_run=False,
- )
- def test_execute_hooks_with_umask_sets_that_umask():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').replace_with(
- lambda hook_description, command, context: command
- )
- 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).should_receive('make_environment').and_return({})
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- ['foo'],
- output_log_level=logging.ANSWER,
- shell=True,
- environment={},
- working_directory=None,
- )
- module.execute_hooks(
- [{'before': 'create', 'run': ['foo']}], umask=77, working_directory=None, dry_run=False
- )
- def test_execute_hooks_with_working_directory_executes_command_with_it():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').replace_with(
- lambda hook_description, command, context: command
- )
- flexmock(module).should_receive('make_environment').and_return({})
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- ['foo'],
- output_log_level=logging.ANSWER,
- shell=True,
- environment={},
- working_directory='/working',
- )
- module.execute_hooks(
- [{'before': 'create', 'run': ['foo']}],
- umask=None,
- working_directory='/working',
- dry_run=False,
- )
- def test_execute_hooks_with_dry_run_skips_commands():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').replace_with(
- lambda hook_description, command, context: command
- )
- flexmock(module).should_receive('make_environment').and_return({})
- flexmock(module.borgmatic.execute).should_receive('execute_command').never()
- module.execute_hooks(
- [{'before': 'create', 'run': ['foo']}], umask=None, working_directory=None, dry_run=True
- )
- def test_execute_hooks_with_empty_commands_does_not_raise():
- module.execute_hooks([], umask=None, working_directory=None, dry_run=True)
- def test_execute_hooks_with_error_logs_as_error():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').replace_with(
- lambda hook_description, command, context: command
- )
- flexmock(module).should_receive('make_environment').and_return({})
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- ['foo'],
- output_log_level=logging.ERROR,
- shell=True,
- environment={},
- working_directory=None,
- ).once()
- module.execute_hooks(
- [{'after': 'error', 'run': ['foo']}], umask=None, working_directory=None, dry_run=False
- )
- def test_execute_hooks_with_before_or_after_raises():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').never()
- flexmock(module).should_receive('make_environment').never()
- flexmock(module.borgmatic.execute).should_receive('execute_command').never()
- with pytest.raises(ValueError):
- module.execute_hooks(
- [
- {'erstwhile': 'create', 'run': ['foo']},
- {'erstwhile': 'create', 'run': ['bar', 'baz']},
- ],
- umask=None,
- working_directory=None,
- dry_run=False,
- )
- def test_execute_hooks_without_commands_to_run_does_not_raise():
- flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
- flexmock(module.logging).ANSWER = LOGGING_ANSWER
- flexmock(module).should_receive('interpolate_context').replace_with(
- lambda hook_description, command, context: command
- )
- flexmock(module).should_receive('make_environment').and_return({})
- for command in ('foo', 'bar'):
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- [command],
- output_log_level=LOGGING_ANSWER,
- shell=True,
- environment={},
- working_directory=None,
- ).once()
- module.execute_hooks(
- [{'before': 'create', 'run': []}, {'before': 'create', 'run': ['foo', 'bar']}],
- umask=None,
- working_directory=None,
- dry_run=False,
- )
- def test_before_after_hooks_calls_command_hooks():
- commands = [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- before='action',
- action_names=['create'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- after='action',
- action_names=['create'],
- state_names=['finish'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('execute_hooks').twice()
- with module.Before_after_hooks(
- command_hooks=commands,
- before_after='action',
- umask=1234,
- working_directory='/working',
- dry_run=False,
- action_names=['create'],
- context1='stuff',
- context2='such',
- ):
- pass
- def test_before_after_hooks_with_before_error_runs_after_hook_and_raises():
- commands = [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- before='action',
- action_names=['create'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- after='action',
- action_names=['create'],
- state_names=['fail'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('execute_hooks').and_raise(OSError).and_return(None)
- flexmock(module).should_receive('considered_soft_failure').and_return(False)
- with pytest.raises(ValueError):
- with module.Before_after_hooks(
- command_hooks=commands,
- before_after='action',
- umask=1234,
- working_directory='/working',
- dry_run=False,
- action_names=['create'],
- context1='stuff',
- context2='such',
- ):
- assert False # This should never get called.
- def test_before_after_hooks_with_before_soft_failure_raises():
- commands = [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- before='action',
- action_names=['create'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- after='action',
- action_names=['create'],
- state_names=['finish'],
- ).never()
- flexmock(module).should_receive('execute_hooks').and_raise(OSError)
- flexmock(module).should_receive('considered_soft_failure').and_return(True)
- with pytest.raises(OSError):
- with module.Before_after_hooks(
- command_hooks=commands,
- before_after='action',
- umask=1234,
- working_directory='/working',
- dry_run=False,
- action_names=['create'],
- context1='stuff',
- context2='such',
- ):
- pass
- def test_before_after_hooks_with_wrapped_code_error_runs_after_hook_and_raises():
- commands = [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- before='action',
- action_names=['create'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- after='action',
- action_names=['create'],
- state_names=['fail'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('execute_hooks').twice()
- with pytest.raises(ValueError):
- with module.Before_after_hooks(
- command_hooks=commands,
- before_after='action',
- umask=1234,
- working_directory='/working',
- dry_run=False,
- action_names=['create'],
- context1='stuff',
- context2='such',
- ):
- raise ValueError()
- def test_before_after_hooks_with_after_error_raises():
- commands = [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- before='action',
- action_names=['create'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- after='action',
- action_names=['create'],
- state_names=['finish'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('execute_hooks').and_return(None).and_raise(OSError)
- flexmock(module).should_receive('considered_soft_failure').and_return(False)
- with pytest.raises(ValueError):
- with module.Before_after_hooks(
- command_hooks=commands,
- before_after='action',
- umask=1234,
- working_directory='/working',
- dry_run=False,
- action_names=['create'],
- context1='stuff',
- context2='such',
- ):
- pass
- def test_before_after_hooks_with_after_soft_failure_raises():
- commands = [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- before='action',
- action_names=['create'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('filter_hooks').with_args(
- commands,
- after='action',
- action_names=['create'],
- state_names=['finish'],
- ).and_return(flexmock()).once()
- flexmock(module).should_receive('execute_hooks').and_return(None).and_raise(OSError)
- flexmock(module).should_receive('considered_soft_failure').and_return(True)
- with pytest.raises(OSError):
- with module.Before_after_hooks(
- command_hooks=commands,
- before_after='action',
- umask=1234,
- working_directory='/working',
- dry_run=False,
- action_names=['create'],
- context1='stuff',
- context2='such',
- ):
- pass
- 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(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(error)
- def test_considered_soft_failure_does_not_treat_other_exception_type_as_soft_fail():
- assert not module.considered_soft_failure(Exception())
- def test_considered_soft_failure_caches_results_and_only_logs_once():
- error = subprocess.CalledProcessError(module.SOFT_FAIL_EXIT_CODE, 'try again')
- flexmock(module.logger).should_receive('info').once()
- assert module.considered_soft_failure(error)
- assert module.considered_soft_failure(error)
|