test_command.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import logging
  2. import subprocess
  3. from flexmock import flexmock
  4. from borgmatic.hooks import command as module
  5. def test_interpolate_context_passes_through_command_without_variable():
  6. assert module.interpolate_context('pre-backup', 'ls', {'foo': 'bar'}) == 'ls'
  7. def test_interpolate_context_passes_through_command_with_unknown_variable():
  8. command = 'ls {baz}' # noqa: FS003
  9. assert module.interpolate_context('pre-backup', command, {'foo': 'bar'}) == command
  10. def test_interpolate_context_interpolates_variables():
  11. command = 'ls {foo}{baz} {baz}' # noqa: FS003
  12. context = {'foo': 'bar', 'baz': 'quux'}
  13. assert module.interpolate_context('pre-backup', command, context) == 'ls barquux quux'
  14. def test_interpolate_context_escapes_interpolated_variables():
  15. command = 'ls {foo} {inject}' # noqa: FS003
  16. context = {'foo': 'bar', 'inject': 'hi; naughty-command'}
  17. assert (
  18. module.interpolate_context('pre-backup', command, context) == "ls bar 'hi; naughty-command'"
  19. )
  20. def test_make_environment_without_pyinstaller_does_not_touch_environment():
  21. assert module.make_environment({}, sys_module=flexmock()) == {}
  22. def test_make_environment_with_pyinstaller_clears_LD_LIBRARY_PATH():
  23. assert module.make_environment({}, sys_module=flexmock(frozen=True, _MEIPASS='yup')) == {
  24. 'LD_LIBRARY_PATH': ''
  25. }
  26. def test_make_environment_with_pyinstaller_and_LD_LIBRARY_PATH_ORIG_copies_it_into_LD_LIBRARY_PATH():
  27. assert module.make_environment(
  28. {'LD_LIBRARY_PATH_ORIG': '/lib/lib/lib'}, sys_module=flexmock(frozen=True, _MEIPASS='yup')
  29. ) == {'LD_LIBRARY_PATH_ORIG': '/lib/lib/lib', 'LD_LIBRARY_PATH': '/lib/lib/lib'}
  30. LOGGING_ANSWER = flexmock()
  31. def test_execute_hooks_invokes_each_hook_and_command():
  32. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  33. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  34. flexmock(module).should_receive('interpolate_context').replace_with(
  35. lambda hook_description, command, context: command
  36. )
  37. flexmock(module).should_receive('make_environment').and_return({})
  38. for command in ('foo', 'bar', 'baz'):
  39. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  40. [command],
  41. output_log_level=LOGGING_ANSWER,
  42. shell=True,
  43. environment={},
  44. ).once()
  45. module.execute_hooks(
  46. [{'before': 'create', 'run': ['foo']}, {'before': 'create', 'run': ['bar', 'baz']}],
  47. umask=None,
  48. dry_run=False,
  49. )
  50. def test_execute_hooks_with_umask_sets_that_umask():
  51. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  52. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  53. flexmock(module).should_receive('interpolate_context').replace_with(
  54. lambda hook_description, command, context: command
  55. )
  56. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  57. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  58. flexmock(module).should_receive('make_environment').and_return({})
  59. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  60. ['foo'],
  61. output_log_level=logging.ANSWER,
  62. shell=True,
  63. environment={},
  64. )
  65. module.execute_hooks([{'before': 'create', 'run': ['foo']}], umask=77, dry_run=False)
  66. def test_execute_hooks_with_dry_run_skips_commands():
  67. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  68. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  69. flexmock(module).should_receive('interpolate_context').replace_with(
  70. lambda hook_description, command, context: command
  71. )
  72. flexmock(module).should_receive('make_environment').and_return({})
  73. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  74. module.execute_hooks([{'before': 'create', 'run': ['foo']}], umask=None, dry_run=True)
  75. def test_execute_hooks_with_empty_commands_does_not_raise():
  76. module.execute_hooks([], umask=None, dry_run=True)
  77. def test_execute_hooks_with_error_logs_as_error():
  78. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  79. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  80. flexmock(module).should_receive('interpolate_context').replace_with(
  81. lambda hook_description, command, context: command
  82. )
  83. flexmock(module).should_receive('make_environment').and_return({})
  84. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  85. ['foo'],
  86. output_log_level=logging.ERROR,
  87. shell=True,
  88. environment={},
  89. ).once()
  90. module.execute_hooks([{'after': 'error', 'run': ['foo']}], umask=None, dry_run=False)
  91. def test_considered_soft_failure_treats_soft_fail_exit_code_as_soft_fail():
  92. error = subprocess.CalledProcessError(module.SOFT_FAIL_EXIT_CODE, 'try again')
  93. assert module.considered_soft_failure(error)
  94. def test_considered_soft_failure_does_not_treat_other_exit_code_as_soft_fail():
  95. error = subprocess.CalledProcessError(1, 'error')
  96. assert not module.considered_soft_failure(error)
  97. def test_considered_soft_failure_does_not_treat_other_exception_type_as_soft_fail():
  98. assert not module.considered_soft_failure(Exception())