test_prune.py 8.3 KB

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