test_compact.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import compact as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(compact_command, output_log_level):
  6. flexmock(module.environment).should_receive('make_environment')
  7. flexmock(module).should_receive('execute_command').with_args(
  8. compact_command,
  9. output_log_level=output_log_level,
  10. borg_local_path=compact_command[0],
  11. extra_environment=None,
  12. ).once()
  13. COMPACT_COMMAND = ('borg', 'compact')
  14. def test_compact_segments_calls_borg_with_parameters():
  15. insert_execute_command_mock(COMPACT_COMMAND + ('repo',), logging.INFO)
  16. module.compact_segments(dry_run=False, repository='repo', storage_config={})
  17. def test_compact_segments_with_log_info_calls_borg_with_info_parameter():
  18. insert_execute_command_mock(COMPACT_COMMAND + ('--info', 'repo'), logging.INFO)
  19. insert_logging_mock(logging.INFO)
  20. module.compact_segments(repository='repo', storage_config={}, dry_run=False)
  21. def test_compact_segments_with_log_debug_calls_borg_with_debug_parameter():
  22. insert_execute_command_mock(COMPACT_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  23. insert_logging_mock(logging.DEBUG)
  24. module.compact_segments(repository='repo', storage_config={}, dry_run=False)
  25. def test_compact_segments_with_dry_run_skips_borg_call():
  26. flexmock(module).should_receive('execute_command').never()
  27. module.compact_segments(repository='repo', storage_config={}, dry_run=True)
  28. def test_compact_segments_with_local_path_calls_borg_via_local_path():
  29. insert_execute_command_mock(('borg1',) + COMPACT_COMMAND[1:] + ('repo',), logging.INFO)
  30. module.compact_segments(
  31. dry_run=False, repository='repo', storage_config={}, local_path='borg1',
  32. )
  33. def test_compact_segments_with_remote_path_calls_borg_with_remote_path_parameters():
  34. insert_execute_command_mock(COMPACT_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  35. module.compact_segments(
  36. dry_run=False, repository='repo', storage_config={}, remote_path='borg1',
  37. )
  38. def test_compact_segments_with_progress_calls_borg_with_progress_parameter():
  39. insert_execute_command_mock(COMPACT_COMMAND + ('--progress', 'repo'), logging.INFO)
  40. module.compact_segments(
  41. dry_run=False, repository='repo', storage_config={}, progress=True,
  42. )
  43. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_parameter():
  44. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.INFO)
  45. module.compact_segments(
  46. dry_run=False, repository='repo', storage_config={}, cleanup_commits=True,
  47. )
  48. def test_compact_segments_with_threshold_calls_borg_with_threshold_parameter():
  49. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.INFO)
  50. module.compact_segments(
  51. dry_run=False, repository='repo', storage_config={}, threshold=20,
  52. )
  53. def test_compact_segments_with_umask_calls_borg_with_umask_parameters():
  54. storage_config = {'umask': '077'}
  55. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  56. module.compact_segments(
  57. dry_run=False, repository='repo', storage_config=storage_config,
  58. )
  59. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_parameters():
  60. storage_config = {'lock_wait': 5}
  61. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  62. module.compact_segments(
  63. dry_run=False, repository='repo', storage_config=storage_config,
  64. )
  65. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  66. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  67. module.compact_segments(
  68. dry_run=False,
  69. repository='repo',
  70. storage_config={'extra_borg_options': {'compact': '--extra --options'}},
  71. )