test_compact.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  16. insert_execute_command_mock(COMPACT_COMMAND + ('repo',), logging.INFO)
  17. module.compact_segments(
  18. dry_run=False, repository='repo', storage_config={}, local_borg_version='1.2.3'
  19. )
  20. def test_compact_segments_with_log_info_calls_borg_with_info_parameter():
  21. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  22. insert_execute_command_mock(COMPACT_COMMAND + ('--info', 'repo'), logging.INFO)
  23. insert_logging_mock(logging.INFO)
  24. module.compact_segments(
  25. repository='repo', storage_config={}, local_borg_version='1.2.3', dry_run=False
  26. )
  27. def test_compact_segments_with_log_debug_calls_borg_with_debug_parameter():
  28. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  29. insert_execute_command_mock(COMPACT_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  30. insert_logging_mock(logging.DEBUG)
  31. module.compact_segments(
  32. repository='repo', storage_config={}, local_borg_version='1.2.3', dry_run=False
  33. )
  34. def test_compact_segments_with_dry_run_skips_borg_call():
  35. flexmock(module).should_receive('execute_command').never()
  36. module.compact_segments(
  37. repository='repo', storage_config={}, local_borg_version='1.2.3', dry_run=True
  38. )
  39. def test_compact_segments_with_local_path_calls_borg_via_local_path():
  40. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  41. insert_execute_command_mock(('borg1',) + COMPACT_COMMAND[1:] + ('repo',), logging.INFO)
  42. module.compact_segments(
  43. dry_run=False,
  44. repository='repo',
  45. storage_config={},
  46. local_borg_version='1.2.3',
  47. local_path='borg1',
  48. )
  49. def test_compact_segments_with_remote_path_calls_borg_with_remote_path_parameters():
  50. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  51. insert_execute_command_mock(COMPACT_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  52. module.compact_segments(
  53. dry_run=False,
  54. repository='repo',
  55. storage_config={},
  56. local_borg_version='1.2.3',
  57. remote_path='borg1',
  58. )
  59. def test_compact_segments_with_progress_calls_borg_with_progress_parameter():
  60. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  61. insert_execute_command_mock(COMPACT_COMMAND + ('--progress', 'repo'), logging.INFO)
  62. module.compact_segments(
  63. dry_run=False,
  64. repository='repo',
  65. storage_config={},
  66. local_borg_version='1.2.3',
  67. progress=True,
  68. )
  69. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_parameter():
  70. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  71. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.INFO)
  72. module.compact_segments(
  73. dry_run=False,
  74. repository='repo',
  75. storage_config={},
  76. local_borg_version='1.2.3',
  77. cleanup_commits=True,
  78. )
  79. def test_compact_segments_with_threshold_calls_borg_with_threshold_parameter():
  80. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  81. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.INFO)
  82. module.compact_segments(
  83. dry_run=False,
  84. repository='repo',
  85. storage_config={},
  86. local_borg_version='1.2.3',
  87. threshold=20,
  88. )
  89. def test_compact_segments_with_umask_calls_borg_with_umask_parameters():
  90. storage_config = {'umask': '077'}
  91. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  92. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  93. module.compact_segments(
  94. dry_run=False, repository='repo', storage_config=storage_config, local_borg_version='1.2.3'
  95. )
  96. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_parameters():
  97. storage_config = {'lock_wait': 5}
  98. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  99. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  100. module.compact_segments(
  101. dry_run=False, repository='repo', storage_config=storage_config, local_borg_version='1.2.3'
  102. )
  103. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  104. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  105. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  106. module.compact_segments(
  107. dry_run=False,
  108. repository='repo',
  109. storage_config={'extra_borg_options': {'compact': '--extra --options'}},
  110. local_borg_version='1.2.3',
  111. )