test_prune.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, **kwargs):
  7. flexmock(module).should_receive('execute_command').with_args(prune_command).once()
  8. BASE_PRUNE_FLAGS = (('--keep-daily', '1'), ('--keep-weekly', '2'), ('--keep-monthly', '3'))
  9. def test_make_prune_flags_returns_flags_from_config_plus_default_prefix():
  10. retention_config = OrderedDict((('keep_daily', 1), ('keep_weekly', 2), ('keep_monthly', 3)))
  11. result = module._make_prune_flags(retention_config)
  12. assert tuple(result) == BASE_PRUNE_FLAGS + (('--prefix', '{hostname}-'),)
  13. def test_make_prune_flags_accepts_prefix_with_placeholders():
  14. retention_config = OrderedDict((('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')))
  15. result = module._make_prune_flags(retention_config)
  16. expected = (('--keep-daily', '1'), ('--prefix', 'Documents_{hostname}-{now}'))
  17. assert tuple(result) == expected
  18. def test_make_prune_flags_treats_empty_prefix_as_no_prefix():
  19. retention_config = OrderedDict((('keep_daily', 1), ('prefix', '')))
  20. result = module._make_prune_flags(retention_config)
  21. expected = (('--keep-daily', '1'),)
  22. assert tuple(result) == expected
  23. def test_make_prune_flags_treats_none_prefix_as_no_prefix():
  24. retention_config = OrderedDict((('keep_daily', 1), ('prefix', None)))
  25. result = module._make_prune_flags(retention_config)
  26. expected = (('--keep-daily', '1'),)
  27. assert tuple(result) == expected
  28. PRUNE_COMMAND = ('borg', 'prune', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3')
  29. def test_prune_archives_calls_borg_with_parameters():
  30. retention_config = flexmock()
  31. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  32. BASE_PRUNE_FLAGS
  33. )
  34. insert_execute_command_mock(PRUNE_COMMAND + ('repo',))
  35. module.prune_archives(
  36. dry_run=False, repository='repo', storage_config={}, retention_config=retention_config
  37. )
  38. def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
  39. retention_config = flexmock()
  40. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  41. BASE_PRUNE_FLAGS
  42. )
  43. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', '--info', 'repo'))
  44. insert_logging_mock(logging.INFO)
  45. module.prune_archives(
  46. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  47. )
  48. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  49. retention_config = flexmock()
  50. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  51. BASE_PRUNE_FLAGS
  52. )
  53. insert_execute_command_mock(
  54. PRUNE_COMMAND + ('--stats', '--debug', '--list', '--show-rc', 'repo')
  55. )
  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'))
  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',))
  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'))
  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_umask_calls_borg_with_umask_parameters():
  96. storage_config = {'umask': '077'}
  97. retention_config = flexmock()
  98. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  99. BASE_PRUNE_FLAGS
  100. )
  101. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077', 'repo'))
  102. module.prune_archives(
  103. dry_run=False,
  104. repository='repo',
  105. storage_config=storage_config,
  106. retention_config=retention_config,
  107. )
  108. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  109. storage_config = {'lock_wait': 5}
  110. retention_config = flexmock()
  111. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  112. BASE_PRUNE_FLAGS
  113. )
  114. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5', 'repo'))
  115. module.prune_archives(
  116. dry_run=False,
  117. repository='repo',
  118. storage_config=storage_config,
  119. retention_config=retention_config,
  120. )