test_command.py 4.0 KB

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