test_prune.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import logging
  2. from collections import OrderedDict
  3. from flexmock import flexmock
  4. from borgmatic.borg import prune as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(prune_command, output_log_level):
  7. flexmock(module.environment).should_receive('make_environment')
  8. flexmock(module).should_receive('execute_command').with_args(
  9. prune_command,
  10. output_log_level=output_log_level,
  11. borg_local_path=prune_command[0],
  12. extra_environment=None,
  13. ).once()
  14. BASE_PRUNE_FLAGS = (('--keep-daily', '1'), ('--keep-weekly', '2'), ('--keep-monthly', '3'))
  15. def test_make_prune_flags_returns_flags_from_config_plus_default_prefix():
  16. retention_config = OrderedDict((('keep_daily', 1), ('keep_weekly', 2), ('keep_monthly', 3)))
  17. result = module._make_prune_flags(retention_config)
  18. assert tuple(result) == BASE_PRUNE_FLAGS + (('--prefix', '{hostname}-'),)
  19. def test_make_prune_flags_accepts_prefix_with_placeholders():
  20. retention_config = OrderedDict((('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')))
  21. result = module._make_prune_flags(retention_config)
  22. expected = (('--keep-daily', '1'), ('--prefix', 'Documents_{hostname}-{now}'))
  23. assert tuple(result) == expected
  24. def test_make_prune_flags_treats_empty_prefix_as_no_prefix():
  25. retention_config = OrderedDict((('keep_daily', 1), ('prefix', '')))
  26. result = module._make_prune_flags(retention_config)
  27. expected = (('--keep-daily', '1'),)
  28. assert tuple(result) == expected
  29. def test_make_prune_flags_treats_none_prefix_as_no_prefix():
  30. retention_config = OrderedDict((('keep_daily', 1), ('prefix', None)))
  31. result = module._make_prune_flags(retention_config)
  32. expected = (('--keep-daily', '1'),)
  33. assert tuple(result) == expected
  34. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  35. def test_prune_archives_calls_borg_with_parameters():
  36. retention_config = flexmock()
  37. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  38. BASE_PRUNE_FLAGS
  39. )
  40. insert_execute_command_mock(PRUNE_COMMAND + ('repo',), logging.INFO)
  41. module.prune_archives(
  42. dry_run=False, repository='repo', storage_config={}, retention_config=retention_config
  43. )
  44. def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
  45. retention_config = flexmock()
  46. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  47. BASE_PRUNE_FLAGS
  48. )
  49. insert_execute_command_mock(PRUNE_COMMAND + ('--info', 'repo'), logging.INFO)
  50. insert_logging_mock(logging.INFO)
  51. module.prune_archives(
  52. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  53. )
  54. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  55. retention_config = flexmock()
  56. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  57. BASE_PRUNE_FLAGS
  58. )
  59. insert_execute_command_mock(PRUNE_COMMAND + ('--debug', '--show-rc', 'repo'), logging.INFO)
  60. insert_logging_mock(logging.DEBUG)
  61. module.prune_archives(
  62. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  63. )
  64. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  65. retention_config = flexmock()
  66. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  67. BASE_PRUNE_FLAGS
  68. )
  69. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  70. module.prune_archives(
  71. repository='repo', storage_config={}, dry_run=True, retention_config=retention_config
  72. )
  73. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  74. retention_config = flexmock()
  75. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  76. BASE_PRUNE_FLAGS
  77. )
  78. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  79. module.prune_archives(
  80. dry_run=False,
  81. repository='repo',
  82. storage_config={},
  83. retention_config=retention_config,
  84. local_path='borg1',
  85. )
  86. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  87. retention_config = flexmock()
  88. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  89. BASE_PRUNE_FLAGS
  90. )
  91. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  92. module.prune_archives(
  93. dry_run=False,
  94. repository='repo',
  95. storage_config={},
  96. retention_config=retention_config,
  97. remote_path='borg1',
  98. )
  99. def test_prune_archives_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  100. retention_config = flexmock()
  101. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  102. BASE_PRUNE_FLAGS
  103. )
  104. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), logging.WARNING)
  105. module.prune_archives(
  106. dry_run=False,
  107. repository='repo',
  108. storage_config={},
  109. retention_config=retention_config,
  110. stats=True,
  111. )
  112. def test_prune_archives_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  113. retention_config = flexmock()
  114. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  115. BASE_PRUNE_FLAGS
  116. )
  117. insert_logging_mock(logging.INFO)
  118. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', '--info', 'repo'), logging.INFO)
  119. module.prune_archives(
  120. dry_run=False,
  121. repository='repo',
  122. storage_config={},
  123. retention_config=retention_config,
  124. stats=True,
  125. )
  126. def test_prune_archives_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  127. retention_config = flexmock()
  128. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  129. BASE_PRUNE_FLAGS
  130. )
  131. insert_execute_command_mock(PRUNE_COMMAND + ('--list', 'repo'), logging.WARNING)
  132. module.prune_archives(
  133. dry_run=False,
  134. repository='repo',
  135. storage_config={},
  136. retention_config=retention_config,
  137. files=True,
  138. )
  139. def test_prune_archives_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  140. retention_config = flexmock()
  141. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  142. BASE_PRUNE_FLAGS
  143. )
  144. insert_logging_mock(logging.INFO)
  145. insert_execute_command_mock(PRUNE_COMMAND + ('--info', '--list', 'repo'), logging.INFO)
  146. module.prune_archives(
  147. dry_run=False,
  148. repository='repo',
  149. storage_config={},
  150. retention_config=retention_config,
  151. files=True,
  152. )
  153. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  154. storage_config = {'umask': '077'}
  155. retention_config = flexmock()
  156. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  157. BASE_PRUNE_FLAGS
  158. )
  159. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  160. module.prune_archives(
  161. dry_run=False,
  162. repository='repo',
  163. storage_config=storage_config,
  164. retention_config=retention_config,
  165. )
  166. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  167. storage_config = {'lock_wait': 5}
  168. retention_config = flexmock()
  169. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  170. BASE_PRUNE_FLAGS
  171. )
  172. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  173. module.prune_archives(
  174. dry_run=False,
  175. repository='repo',
  176. storage_config=storage_config,
  177. retention_config=retention_config,
  178. )
  179. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  180. retention_config = flexmock()
  181. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  182. BASE_PRUNE_FLAGS
  183. )
  184. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  185. module.prune_archives(
  186. dry_run=False,
  187. repository='repo',
  188. storage_config={'extra_borg_options': {'prune': '--extra --options'}},
  189. retention_config=retention_config,
  190. )