test_command.py 3.8 KB

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