test_prune.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, error_on_warnings=False
  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(
  46. PRUNE_COMMAND + ('--stats', '--info', '--list', 'repo'), logging.INFO
  47. )
  48. insert_logging_mock(logging.INFO)
  49. module.prune_archives(
  50. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  51. )
  52. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  53. retention_config = flexmock()
  54. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  55. BASE_PRUNE_FLAGS
  56. )
  57. insert_execute_command_mock(
  58. PRUNE_COMMAND + ('--stats', '--debug', '--list', '--show-rc', 'repo'), logging.INFO
  59. )
  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():
  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_umask_calls_borg_with_umask_parameters():
  113. storage_config = {'umask': '077'}
  114. retention_config = flexmock()
  115. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  116. BASE_PRUNE_FLAGS
  117. )
  118. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  119. module.prune_archives(
  120. dry_run=False,
  121. repository='repo',
  122. storage_config=storage_config,
  123. retention_config=retention_config,
  124. )
  125. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  126. storage_config = {'lock_wait': 5}
  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 + ('--lock-wait', '5', 'repo'), logging.INFO)
  132. module.prune_archives(
  133. dry_run=False,
  134. repository='repo',
  135. storage_config=storage_config,
  136. retention_config=retention_config,
  137. )
  138. def test_prune_archives_with_extra_borg_options_calls_borg_with_extra_options():
  139. retention_config = flexmock()
  140. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  141. BASE_PRUNE_FLAGS
  142. )
  143. insert_execute_command_mock(PRUNE_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  144. module.prune_archives(
  145. dry_run=False,
  146. repository='repo',
  147. storage_config={'extra_borg_options': {'prune': '--extra --options'}},
  148. retention_config=retention_config,
  149. )