test_command.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.hooks import command as module
  4. def test_interpolate_context_passes_through_command_without_variable():
  5. assert module.interpolate_context('ls', {'foo': 'bar'}) == 'ls'
  6. def test_interpolate_context_passes_through_command_with_unknown_variable():
  7. assert module.interpolate_context('ls {baz}', {'foo': 'bar'}) == 'ls {baz}'
  8. def test_interpolate_context_interpolates_variables():
  9. context = {'foo': 'bar', 'baz': 'quux'}
  10. assert module.interpolate_context('ls {foo}{baz} {baz}', context) == 'ls barquux quux'
  11. def test_execute_hook_invokes_each_command():
  12. flexmock(module).should_receive('interpolate_context').replace_with(
  13. lambda command, context: command
  14. )
  15. flexmock(module.execute).should_receive('execute_command').with_args(
  16. [':'], output_log_level=logging.WARNING, shell=True
  17. ).once()
  18. module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
  19. def test_execute_hook_with_multiple_commands_invokes_each_command():
  20. flexmock(module).should_receive('interpolate_context').replace_with(
  21. lambda command, context: command
  22. )
  23. flexmock(module.execute).should_receive('execute_command').with_args(
  24. [':'], output_log_level=logging.WARNING, shell=True
  25. ).once()
  26. flexmock(module.execute).should_receive('execute_command').with_args(
  27. ['true'], output_log_level=logging.WARNING, shell=True
  28. ).once()
  29. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
  30. def test_execute_hook_with_umask_sets_that_umask():
  31. flexmock(module).should_receive('interpolate_context').replace_with(
  32. lambda command, context: command
  33. )
  34. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  35. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  36. flexmock(module.execute).should_receive('execute_command').with_args(
  37. [':'], output_log_level=logging.WARNING, shell=True
  38. )
  39. module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
  40. def test_execute_hook_with_dry_run_skips_commands():
  41. flexmock(module).should_receive('interpolate_context').replace_with(
  42. lambda command, context: command
  43. )
  44. flexmock(module.execute).should_receive('execute_command').never()
  45. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
  46. def test_execute_hook_with_empty_commands_does_not_raise():
  47. module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
  48. def test_execute_hook_on_error_logs_as_error():
  49. flexmock(module).should_receive('interpolate_context').replace_with(
  50. lambda command, context: command
  51. )
  52. flexmock(module.execute).should_receive('execute_command').with_args(
  53. [':'], output_log_level=logging.ERROR, shell=True
  54. ).once()
  55. module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)