2
0

test_command.py 3.5 KB

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