test_command.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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('test.yaml', '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('test.yaml', '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('test.yaml', '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('test.yaml', 'pre-backup', command, context)
  21. == "ls bar 'hi; naughty-command'"
  22. )
  23. def test_execute_hook_invokes_each_command():
  24. flexmock(module).should_receive('interpolate_context').replace_with(
  25. lambda config_file, hook_description, command, context: command
  26. )
  27. flexmock(module.execute).should_receive('execute_command').with_args(
  28. [':'], output_log_level=logging.WARNING, shell=True
  29. ).once()
  30. module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
  31. def test_execute_hook_with_multiple_commands_invokes_each_command():
  32. flexmock(module).should_receive('interpolate_context').replace_with(
  33. lambda config_file, hook_description, command, context: command
  34. )
  35. flexmock(module.execute).should_receive('execute_command').with_args(
  36. [':'], output_log_level=logging.WARNING, shell=True
  37. ).once()
  38. flexmock(module.execute).should_receive('execute_command').with_args(
  39. ['true'], output_log_level=logging.WARNING, shell=True
  40. ).once()
  41. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
  42. def test_execute_hook_with_umask_sets_that_umask():
  43. flexmock(module).should_receive('interpolate_context').replace_with(
  44. lambda config_file, hook_description, command, context: command
  45. )
  46. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  47. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  48. flexmock(module.execute).should_receive('execute_command').with_args(
  49. [':'], output_log_level=logging.WARNING, shell=True
  50. )
  51. module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
  52. def test_execute_hook_with_dry_run_skips_commands():
  53. flexmock(module).should_receive('interpolate_context').replace_with(
  54. lambda config_file, hook_description, command, context: command
  55. )
  56. flexmock(module.execute).should_receive('execute_command').never()
  57. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
  58. def test_execute_hook_with_empty_commands_does_not_raise():
  59. module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
  60. def test_execute_hook_on_error_logs_as_error():
  61. flexmock(module).should_receive('interpolate_context').replace_with(
  62. lambda config_file, hook_description, command, context: command
  63. )
  64. flexmock(module.execute).should_receive('execute_command').with_args(
  65. [':'], output_log_level=logging.ERROR, shell=True
  66. ).once()
  67. module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)
  68. def test_considered_soft_failure_treats_soft_fail_exit_code_as_soft_fail():
  69. error = subprocess.CalledProcessError(module.SOFT_FAIL_EXIT_CODE, 'try again')
  70. assert module.considered_soft_failure('config.yaml', error)
  71. def test_considered_soft_failure_does_not_treat_other_exit_code_as_soft_fail():
  72. error = subprocess.CalledProcessError(1, 'error')
  73. assert not module.considered_soft_failure('config.yaml', error)
  74. def test_considered_soft_failure_does_not_treat_other_exception_type_as_soft_fail():
  75. assert not module.considered_soft_failure('config.yaml', Exception())