test_prune.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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
  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 + ('--stats', '--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(
  56. PRUNE_COMMAND + ('--stats', '--debug', '--list', '--show-rc', 'repo'), logging.INFO
  57. )
  58. insert_logging_mock(logging.DEBUG)
  59. module.prune_archives(
  60. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  61. )
  62. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  63. retention_config = flexmock()
  64. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  65. BASE_PRUNE_FLAGS
  66. )
  67. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run', 'repo'), logging.INFO)
  68. module.prune_archives(
  69. repository='repo', storage_config={}, dry_run=True, retention_config=retention_config
  70. )
  71. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  72. retention_config = flexmock()
  73. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  74. BASE_PRUNE_FLAGS
  75. )
  76. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:] + ('repo',), logging.INFO)
  77. module.prune_archives(
  78. dry_run=False,
  79. repository='repo',
  80. storage_config={},
  81. retention_config=retention_config,
  82. local_path='borg1',
  83. )
  84. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  85. retention_config = flexmock()
  86. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  87. BASE_PRUNE_FLAGS
  88. )
  89. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  90. module.prune_archives(
  91. dry_run=False,
  92. repository='repo',
  93. storage_config={},
  94. retention_config=retention_config,
  95. remote_path='borg1',
  96. )
  97. def test_prune_archives_with_stats_calls_borg_with_stats_parameter():
  98. retention_config = flexmock()
  99. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  100. BASE_PRUNE_FLAGS
  101. )
  102. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', 'repo'), logging.WARNING)
  103. module.prune_archives(
  104. dry_run=False,
  105. repository='repo',
  106. storage_config={},
  107. retention_config=retention_config,
  108. stats=True,
  109. )
  110. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  111. storage_config = {'umask': '077'}
  112. retention_config = flexmock()
  113. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  114. BASE_PRUNE_FLAGS
  115. )
  116. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  117. module.prune_archives(
  118. dry_run=False,
  119. repository='repo',
  120. storage_config=storage_config,
  121. retention_config=retention_config,
  122. )
  123. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  124. storage_config = {'lock_wait': 5}
  125. retention_config = flexmock()
  126. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  127. BASE_PRUNE_FLAGS
  128. )
  129. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  130. module.prune_archives(
  131. dry_run=False,
  132. repository='repo',
  133. storage_config=storage_config,
  134. retention_config=retention_config,
  135. )