test_command.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. def test_execute_hook_invokes_each_command():
  31. flexmock(module).should_receive('interpolate_context').replace_with(
  32. lambda hook_description, command, context: command
  33. )
  34. flexmock(module).should_receive('make_environment').and_return({})
  35. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  36. [':'],
  37. output_log_level=logging.WARNING,
  38. shell=True,
  39. environment={},
  40. ).once()
  41. module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
  42. def test_execute_hook_with_multiple_commands_invokes_each_command():
  43. flexmock(module).should_receive('interpolate_context').replace_with(
  44. lambda hook_description, command, context: command
  45. )
  46. flexmock(module).should_receive('make_environment').and_return({})
  47. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  48. [':'],
  49. output_log_level=logging.WARNING,
  50. shell=True,
  51. environment={},
  52. ).once()
  53. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  54. ['true'],
  55. output_log_level=logging.WARNING,
  56. shell=True,
  57. environment={},
  58. ).once()
  59. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
  60. def test_execute_hook_with_umask_sets_that_umask():
  61. flexmock(module).should_receive('interpolate_context').replace_with(
  62. lambda hook_description, command, context: command
  63. )
  64. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  65. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  66. flexmock(module).should_receive('make_environment').and_return({})
  67. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  68. [':'],
  69. output_log_level=logging.WARNING,
  70. shell=True,
  71. environment={},
  72. )
  73. module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
  74. def test_execute_hook_with_dry_run_skips_commands():
  75. flexmock(module).should_receive('interpolate_context').replace_with(
  76. lambda hook_description, command, context: command
  77. )
  78. flexmock(module).should_receive('make_environment').and_return({})
  79. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  80. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
  81. def test_execute_hook_with_empty_commands_does_not_raise():
  82. module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
  83. def test_execute_hook_on_error_logs_as_error():
  84. flexmock(module).should_receive('interpolate_context').replace_with(
  85. lambda hook_description, command, context: command
  86. )
  87. flexmock(module).should_receive('make_environment').and_return({})
  88. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  89. [':'],
  90. output_log_level=logging.ERROR,
  91. shell=True,
  92. environment={},
  93. ).once()
  94. module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)
  95. def test_considered_soft_failure_treats_soft_fail_exit_code_as_soft_fail():
  96. error = subprocess.CalledProcessError(module.SOFT_FAIL_EXIT_CODE, 'try again')
  97. assert module.considered_soft_failure(error)
  98. def test_considered_soft_failure_does_not_treat_other_exit_code_as_soft_fail():
  99. error = subprocess.CalledProcessError(1, 'error')
  100. assert not module.considered_soft_failure(error)
  101. def test_considered_soft_failure_does_not_treat_other_exception_type_as_soft_fail():
  102. assert not module.considered_soft_failure(Exception())