test_compact.py 6.5 KB

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