test_command.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 (
  14. module.interpolate_context('pre-backup', command, context) == 'ls barquux quux'
  15. )
  16. def test_interpolate_context_escapes_interpolated_variables():
  17. command = 'ls {foo} {inject}' # noqa: FS003
  18. context = {'foo': 'bar', 'inject': 'hi; naughty-command'}
  19. assert (
  20. module.interpolate_context('pre-backup', command, context)
  21. == "ls bar 'hi; naughty-command'"
  22. )
  23. def test_make_environment_without_pyinstaller_does_not_touch_environment():
  24. assert module.make_environment({}, sys_module=flexmock()) == {}
  25. def test_make_environment_with_pyinstaller_clears_LD_LIBRARY_PATH():
  26. assert module.make_environment({}, sys_module=flexmock(frozen=True, _MEIPASS='yup')) == {
  27. 'LD_LIBRARY_PATH': ''
  28. }
  29. def test_make_environment_with_pyinstaller_and_LD_LIBRARY_PATH_ORIG_copies_it_into_LD_LIBRARY_PATH():
  30. assert module.make_environment(
  31. {'LD_LIBRARY_PATH_ORIG': '/lib/lib/lib'}, sys_module=flexmock(frozen=True, _MEIPASS='yup')
  32. ) == {'LD_LIBRARY_PATH': '/lib/lib/lib'}
  33. def test_execute_hook_invokes_each_command():
  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. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  39. [':'],
  40. output_log_level=logging.WARNING,
  41. shell=True,
  42. extra_environment={},
  43. ).once()
  44. module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
  45. def test_execute_hook_with_multiple_commands_invokes_each_command():
  46. flexmock(module).should_receive('interpolate_context').replace_with(
  47. lambda hook_description, command, context: command
  48. )
  49. flexmock(module).should_receive('make_environment').and_return({})
  50. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  51. [':'],
  52. output_log_level=logging.WARNING,
  53. shell=True,
  54. extra_environment={},
  55. ).once()
  56. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  57. ['true'],
  58. output_log_level=logging.WARNING,
  59. shell=True,
  60. extra_environment={},
  61. ).once()
  62. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
  63. def test_execute_hook_with_umask_sets_that_umask():
  64. flexmock(module).should_receive('interpolate_context').replace_with(
  65. lambda hook_description, command, context: command
  66. )
  67. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  68. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  69. flexmock(module).should_receive('make_environment').and_return({})
  70. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  71. [':'],
  72. output_log_level=logging.WARNING,
  73. shell=True,
  74. extra_environment={},
  75. )
  76. module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
  77. def test_execute_hook_with_dry_run_skips_commands():
  78. flexmock(module).should_receive('interpolate_context').replace_with(
  79. lambda hook_description, command, context: command
  80. )
  81. flexmock(module).should_receive('make_environment').and_return({})
  82. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  83. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
  84. def test_execute_hook_with_empty_commands_does_not_raise():
  85. module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
  86. def test_execute_hook_on_error_logs_as_error():
  87. flexmock(module).should_receive('interpolate_context').replace_with(
  88. lambda hook_description, command, context: command
  89. )
  90. flexmock(module).should_receive('make_environment').and_return({})
  91. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  92. [':'],
  93. output_log_level=logging.ERROR,
  94. shell=True,
  95. extra_environment={},
  96. ).once()
  97. module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)
  98. def test_considered_soft_failure_treats_soft_fail_exit_code_as_soft_fail():
  99. error = subprocess.CalledProcessError(module.SOFT_FAIL_EXIT_CODE, 'try again')
  100. assert module.considered_soft_failure(error)
  101. def test_considered_soft_failure_does_not_treat_other_exit_code_as_soft_fail():
  102. error = subprocess.CalledProcessError(1, 'error')
  103. assert not module.considered_soft_failure(error)
  104. def test_considered_soft_failure_does_not_treat_other_exception_type_as_soft_fail():
  105. assert not module.considered_soft_failure(Exception())