test_compact.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. extra_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_parameters():
  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(log_json=False),
  30. )
  31. def test_compact_segments_with_log_info_calls_borg_with_info_parameter():
  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(log_json=False),
  40. dry_run=False,
  41. )
  42. def test_compact_segments_with_log_debug_calls_borg_with_debug_parameter():
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  85. )
  86. def test_compact_segments_with_remote_path_calls_borg_with_remote_path_parameters():
  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(log_json=False),
  95. remote_path='borg1',
  96. )
  97. def test_compact_segments_with_progress_calls_borg_with_progress_parameter():
  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={},
  104. local_borg_version='1.2.3',
  105. global_arguments=flexmock(log_json=False),
  106. progress=True,
  107. )
  108. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_parameter():
  109. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  110. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.INFO)
  111. module.compact_segments(
  112. dry_run=False,
  113. repository_path='repo',
  114. config={},
  115. local_borg_version='1.2.3',
  116. global_arguments=flexmock(log_json=False),
  117. cleanup_commits=True,
  118. )
  119. def test_compact_segments_with_threshold_calls_borg_with_threshold_parameter():
  120. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  121. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.INFO)
  122. module.compact_segments(
  123. dry_run=False,
  124. repository_path='repo',
  125. config={},
  126. local_borg_version='1.2.3',
  127. global_arguments=flexmock(log_json=False),
  128. threshold=20,
  129. )
  130. def test_compact_segments_with_umask_calls_borg_with_umask_parameters():
  131. config = {'umask': '077'}
  132. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  133. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  134. module.compact_segments(
  135. dry_run=False,
  136. repository_path='repo',
  137. config=config,
  138. local_borg_version='1.2.3',
  139. global_arguments=flexmock(log_json=False),
  140. )
  141. def test_compact_segments_with_log_json_calls_borg_with_log_json_parameters():
  142. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  143. insert_execute_command_mock(COMPACT_COMMAND + ('--log-json', 'repo'), logging.INFO)
  144. module.compact_segments(
  145. dry_run=False,
  146. repository_path='repo',
  147. config={},
  148. local_borg_version='1.2.3',
  149. global_arguments=flexmock(log_json=True),
  150. )
  151. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_parameters():
  152. config = {'lock_wait': 5}
  153. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  154. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  155. module.compact_segments(
  156. dry_run=False,
  157. repository_path='repo',
  158. config=config,
  159. local_borg_version='1.2.3',
  160. global_arguments=flexmock(log_json=False),
  161. )
  162. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  163. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  164. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  165. module.compact_segments(
  166. dry_run=False,
  167. repository_path='repo',
  168. config={'extra_borg_options': {'compact': '--extra --options'}},
  169. local_borg_version='1.2.3',
  170. global_arguments=flexmock(log_json=False),
  171. )
  172. def test_compact_segments_calls_borg_with_working_directory():
  173. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  174. insert_execute_command_mock(
  175. COMPACT_COMMAND + ('repo',), logging.INFO, working_directory='/working/dir'
  176. )
  177. module.compact_segments(
  178. dry_run=False,
  179. repository_path='repo',
  180. config={'working_directory': '/working/dir'},
  181. local_borg_version='1.2.3',
  182. global_arguments=flexmock(log_json=False),
  183. )