test_compact.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, borg_exit_codes=None):
  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. borg_exit_codes=borg_exit_codes,
  12. extra_environment=None,
  13. ).once()
  14. COMPACT_COMMAND = ('borg', 'compact')
  15. def test_compact_segments_calls_borg_with_parameters():
  16. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  17. insert_execute_command_mock(COMPACT_COMMAND + ('repo',), logging.INFO)
  18. module.compact_segments(
  19. dry_run=False,
  20. repository_path='repo',
  21. config={},
  22. local_borg_version='1.2.3',
  23. global_arguments=flexmock(log_json=False),
  24. )
  25. def test_compact_segments_with_log_info_calls_borg_with_info_parameter():
  26. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  27. insert_execute_command_mock(COMPACT_COMMAND + ('--info', 'repo'), logging.INFO)
  28. insert_logging_mock(logging.INFO)
  29. module.compact_segments(
  30. repository_path='repo',
  31. config={},
  32. local_borg_version='1.2.3',
  33. global_arguments=flexmock(log_json=False),
  34. dry_run=False,
  35. )
  36. def test_compact_segments_with_log_debug_calls_borg_with_debug_parameter():
  37. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  38. insert_execute_command_mock(COMPACT_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  39. insert_logging_mock(logging.DEBUG)
  40. module.compact_segments(
  41. repository_path='repo',
  42. config={},
  43. local_borg_version='1.2.3',
  44. global_arguments=flexmock(log_json=False),
  45. dry_run=False,
  46. )
  47. def test_compact_segments_with_dry_run_skips_borg_call():
  48. flexmock(module).should_receive('execute_command').never()
  49. module.compact_segments(
  50. repository_path='repo',
  51. config={},
  52. local_borg_version='1.2.3',
  53. global_arguments=flexmock(log_json=False),
  54. dry_run=True,
  55. )
  56. def test_compact_segments_with_local_path_calls_borg_via_local_path():
  57. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  58. insert_execute_command_mock(('borg1',) + COMPACT_COMMAND[1:] + ('repo',), logging.INFO)
  59. module.compact_segments(
  60. dry_run=False,
  61. repository_path='repo',
  62. config={},
  63. local_borg_version='1.2.3',
  64. global_arguments=flexmock(log_json=False),
  65. local_path='borg1',
  66. )
  67. def test_compact_segments_with_exit_codes_calls_borg_using_them():
  68. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  69. borg_exit_codes = flexmock()
  70. insert_execute_command_mock(
  71. COMPACT_COMMAND + ('repo',), logging.INFO, borg_exit_codes=borg_exit_codes
  72. )
  73. module.compact_segments(
  74. dry_run=False,
  75. repository_path='repo',
  76. config={'borg_exit_codes': borg_exit_codes},
  77. local_borg_version='1.2.3',
  78. global_arguments=flexmock(log_json=False),
  79. )
  80. def test_compact_segments_with_remote_path_calls_borg_with_remote_path_parameters():
  81. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  82. insert_execute_command_mock(COMPACT_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  83. module.compact_segments(
  84. dry_run=False,
  85. repository_path='repo',
  86. config={},
  87. local_borg_version='1.2.3',
  88. global_arguments=flexmock(log_json=False),
  89. remote_path='borg1',
  90. )
  91. def test_compact_segments_with_progress_calls_borg_with_progress_parameter():
  92. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  93. insert_execute_command_mock(COMPACT_COMMAND + ('--progress', 'repo'), logging.INFO)
  94. module.compact_segments(
  95. dry_run=False,
  96. repository_path='repo',
  97. config={},
  98. local_borg_version='1.2.3',
  99. global_arguments=flexmock(log_json=False),
  100. progress=True,
  101. )
  102. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_parameter():
  103. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  104. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.INFO)
  105. module.compact_segments(
  106. dry_run=False,
  107. repository_path='repo',
  108. config={},
  109. local_borg_version='1.2.3',
  110. global_arguments=flexmock(log_json=False),
  111. cleanup_commits=True,
  112. )
  113. def test_compact_segments_with_threshold_calls_borg_with_threshold_parameter():
  114. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  115. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.INFO)
  116. module.compact_segments(
  117. dry_run=False,
  118. repository_path='repo',
  119. config={},
  120. local_borg_version='1.2.3',
  121. global_arguments=flexmock(log_json=False),
  122. threshold=20,
  123. )
  124. def test_compact_segments_with_umask_calls_borg_with_umask_parameters():
  125. config = {'umask': '077'}
  126. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  127. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  128. module.compact_segments(
  129. dry_run=False,
  130. repository_path='repo',
  131. config=config,
  132. local_borg_version='1.2.3',
  133. global_arguments=flexmock(log_json=False),
  134. )
  135. def test_compact_segments_with_log_json_calls_borg_with_log_json_parameters():
  136. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  137. insert_execute_command_mock(COMPACT_COMMAND + ('--log-json', 'repo'), logging.INFO)
  138. module.compact_segments(
  139. dry_run=False,
  140. repository_path='repo',
  141. config={},
  142. local_borg_version='1.2.3',
  143. global_arguments=flexmock(log_json=True),
  144. )
  145. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_parameters():
  146. config = {'lock_wait': 5}
  147. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  148. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  149. module.compact_segments(
  150. dry_run=False,
  151. repository_path='repo',
  152. config=config,
  153. local_borg_version='1.2.3',
  154. global_arguments=flexmock(log_json=False),
  155. )
  156. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  157. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  158. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  159. module.compact_segments(
  160. dry_run=False,
  161. repository_path='repo',
  162. config={'extra_borg_options': {'compact': '--extra --options'}},
  163. local_borg_version='1.2.3',
  164. global_arguments=flexmock(log_json=False),
  165. )