test_compact.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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(
  6. compact_command, output_log_level, working_directory=None, borg_exit_codes=None
  7. ):
  8. flexmock(module.environment).should_receive('make_environment')
  9. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  10. working_directory
  11. )
  12. flexmock(module).should_receive('execute_command').with_args(
  13. compact_command,
  14. output_log_level=output_log_level,
  15. environment=None,
  16. working_directory=working_directory,
  17. borg_local_path=compact_command[0],
  18. borg_exit_codes=borg_exit_codes,
  19. ).once()
  20. COMPACT_COMMAND = ('borg', 'compact')
  21. def test_compact_segments_calls_borg_with_flags():
  22. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  23. insert_execute_command_mock(COMPACT_COMMAND + ('repo',), logging.INFO)
  24. module.compact_segments(
  25. dry_run=False,
  26. repository_path='repo',
  27. config={},
  28. local_borg_version='1.2.3',
  29. global_arguments=flexmock(),
  30. )
  31. def test_compact_segments_with_log_info_calls_borg_with_info_flag():
  32. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  33. insert_execute_command_mock(COMPACT_COMMAND + ('--info', 'repo'), logging.INFO)
  34. insert_logging_mock(logging.INFO)
  35. module.compact_segments(
  36. repository_path='repo',
  37. config={},
  38. local_borg_version='1.2.3',
  39. global_arguments=flexmock(),
  40. dry_run=False,
  41. )
  42. def test_compact_segments_with_log_debug_calls_borg_with_debug_flag():
  43. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  44. insert_execute_command_mock(COMPACT_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  45. insert_logging_mock(logging.DEBUG)
  46. module.compact_segments(
  47. repository_path='repo',
  48. config={},
  49. local_borg_version='1.2.3',
  50. global_arguments=flexmock(),
  51. dry_run=False,
  52. )
  53. def test_compact_segments_with_dry_run_skips_borg_call():
  54. flexmock(module).should_receive('execute_command').never()
  55. module.compact_segments(
  56. repository_path='repo',
  57. config={},
  58. local_borg_version='1.2.3',
  59. global_arguments=flexmock(),
  60. dry_run=True,
  61. )
  62. def test_compact_segments_with_local_path_calls_borg_via_local_path():
  63. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  64. insert_execute_command_mock(('borg1',) + COMPACT_COMMAND[1:] + ('repo',), logging.INFO)
  65. module.compact_segments(
  66. dry_run=False,
  67. repository_path='repo',
  68. config={},
  69. local_borg_version='1.2.3',
  70. global_arguments=flexmock(),
  71. local_path='borg1',
  72. )
  73. def test_compact_segments_with_exit_codes_calls_borg_using_them():
  74. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  75. borg_exit_codes = flexmock()
  76. insert_execute_command_mock(
  77. COMPACT_COMMAND + ('repo',), logging.INFO, borg_exit_codes=borg_exit_codes
  78. )
  79. module.compact_segments(
  80. dry_run=False,
  81. repository_path='repo',
  82. config={'borg_exit_codes': borg_exit_codes},
  83. local_borg_version='1.2.3',
  84. global_arguments=flexmock(),
  85. )
  86. def test_compact_segments_with_remote_path_calls_borg_with_remote_path_flags():
  87. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  88. insert_execute_command_mock(COMPACT_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  89. module.compact_segments(
  90. dry_run=False,
  91. repository_path='repo',
  92. config={},
  93. local_borg_version='1.2.3',
  94. global_arguments=flexmock(),
  95. remote_path='borg1',
  96. )
  97. def test_compact_segments_with_progress_calls_borg_with_progress_flag():
  98. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  99. insert_execute_command_mock(COMPACT_COMMAND + ('--progress', 'repo'), logging.INFO)
  100. module.compact_segments(
  101. dry_run=False,
  102. repository_path='repo',
  103. config={'progress': True},
  104. local_borg_version='1.2.3',
  105. global_arguments=flexmock(),
  106. )
  107. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_flag():
  108. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  109. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.INFO)
  110. module.compact_segments(
  111. dry_run=False,
  112. repository_path='repo',
  113. config={},
  114. local_borg_version='1.2.3',
  115. global_arguments=flexmock(),
  116. cleanup_commits=True,
  117. )
  118. def test_compact_segments_with_threshold_calls_borg_with_threshold_flag():
  119. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  120. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.INFO)
  121. module.compact_segments(
  122. dry_run=False,
  123. repository_path='repo',
  124. config={'compact_threshold': 20},
  125. local_borg_version='1.2.3',
  126. global_arguments=flexmock(),
  127. )
  128. def test_compact_segments_with_umask_calls_borg_with_umask_flags():
  129. config = {'umask': '077'}
  130. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  131. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  132. module.compact_segments(
  133. dry_run=False,
  134. repository_path='repo',
  135. config=config,
  136. local_borg_version='1.2.3',
  137. global_arguments=flexmock(),
  138. )
  139. def test_compact_segments_with_log_json_calls_borg_with_log_json_flags():
  140. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  141. insert_execute_command_mock(COMPACT_COMMAND + ('--log-json', 'repo'), logging.INFO)
  142. module.compact_segments(
  143. dry_run=False,
  144. repository_path='repo',
  145. config={'log_json': True},
  146. local_borg_version='1.2.3',
  147. global_arguments=flexmock(),
  148. )
  149. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_flags():
  150. config = {'lock_wait': 5}
  151. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  152. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  153. module.compact_segments(
  154. dry_run=False,
  155. repository_path='repo',
  156. config=config,
  157. local_borg_version='1.2.3',
  158. global_arguments=flexmock(),
  159. )
  160. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  161. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  162. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  163. module.compact_segments(
  164. dry_run=False,
  165. repository_path='repo',
  166. config={'extra_borg_options': {'compact': '--extra --options'}},
  167. local_borg_version='1.2.3',
  168. global_arguments=flexmock(),
  169. )
  170. def test_compact_segments_calls_borg_with_working_directory():
  171. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  172. insert_execute_command_mock(
  173. COMPACT_COMMAND + ('repo',), logging.INFO, working_directory='/working/dir'
  174. )
  175. module.compact_segments(
  176. dry_run=False,
  177. repository_path='repo',
  178. config={'working_directory': '/working/dir'},
  179. local_borg_version='1.2.3',
  180. global_arguments=flexmock(),
  181. )