test_hook.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic import hook as module
  4. def test_execute_hook_invokes_each_command():
  5. flexmock(module.execute).should_receive('execute_command').with_args(
  6. [':'], output_log_level=logging.WARNING, shell=True
  7. ).once()
  8. module.execute_hook([':'], None, 'config.yaml', 'pre-backup', dry_run=False)
  9. def test_execute_hook_with_multiple_commands_invokes_each_command():
  10. flexmock(module.execute).should_receive('execute_command').with_args(
  11. [':'], output_log_level=logging.WARNING, shell=True
  12. ).once()
  13. flexmock(module.execute).should_receive('execute_command').with_args(
  14. ['true'], output_log_level=logging.WARNING, shell=True
  15. ).once()
  16. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=False)
  17. def test_execute_hook_with_umask_sets_that_umask():
  18. flexmock(module.os).should_receive('umask').with_args(0o77).and_return(0o22).once()
  19. flexmock(module.os).should_receive('umask').with_args(0o22).once()
  20. flexmock(module.execute).should_receive('execute_command').with_args(
  21. [':'], output_log_level=logging.WARNING, shell=True
  22. )
  23. module.execute_hook([':'], 77, 'config.yaml', 'pre-backup', dry_run=False)
  24. def test_execute_hook_with_dry_run_skips_commands():
  25. flexmock(module.execute).should_receive('execute_command').never()
  26. module.execute_hook([':', 'true'], None, 'config.yaml', 'pre-backup', dry_run=True)
  27. def test_execute_hook_with_empty_commands_does_not_raise():
  28. module.execute_hook([], None, 'config.yaml', 'post-backup', dry_run=False)
  29. def test_execute_hook_on_error_logs_as_error():
  30. flexmock(module.execute).should_receive('execute_command').with_args(
  31. [':'], output_log_level=logging.ERROR, shell=True
  32. ).once()
  33. module.execute_hook([':'], None, 'config.yaml', 'on-error', dry_run=False)