test_prune.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 = (
  29. 'borg',
  30. 'prune',
  31. 'repo',
  32. '--keep-daily',
  33. '1',
  34. '--keep-weekly',
  35. '2',
  36. '--keep-monthly',
  37. '3',
  38. )
  39. def test_prune_archives_calls_borg_with_parameters():
  40. retention_config = flexmock()
  41. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  42. BASE_PRUNE_FLAGS
  43. )
  44. insert_execute_command_mock(PRUNE_COMMAND)
  45. module.prune_archives(
  46. dry_run=False, repository='repo', storage_config={}, retention_config=retention_config
  47. )
  48. def test_prune_archives_with_log_info_calls_borg_with_info_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(PRUNE_COMMAND + ('--stats', '--info'))
  54. insert_logging_mock(logging.INFO)
  55. module.prune_archives(
  56. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  57. )
  58. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  59. retention_config = flexmock()
  60. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  61. BASE_PRUNE_FLAGS
  62. )
  63. insert_execute_command_mock(PRUNE_COMMAND + ('--stats', '--debug', '--list', '--show-rc'))
  64. insert_logging_mock(logging.DEBUG)
  65. module.prune_archives(
  66. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  67. )
  68. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  69. retention_config = flexmock()
  70. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  71. BASE_PRUNE_FLAGS
  72. )
  73. insert_execute_command_mock(PRUNE_COMMAND + ('--dry-run',))
  74. module.prune_archives(
  75. repository='repo', storage_config={}, dry_run=True, retention_config=retention_config
  76. )
  77. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  78. retention_config = flexmock()
  79. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  80. BASE_PRUNE_FLAGS
  81. )
  82. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:])
  83. module.prune_archives(
  84. dry_run=False,
  85. repository='repo',
  86. storage_config={},
  87. retention_config=retention_config,
  88. local_path='borg1',
  89. )
  90. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  91. retention_config = flexmock()
  92. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  93. BASE_PRUNE_FLAGS
  94. )
  95. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1'))
  96. module.prune_archives(
  97. dry_run=False,
  98. repository='repo',
  99. storage_config={},
  100. retention_config=retention_config,
  101. remote_path='borg1',
  102. )
  103. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  104. storage_config = {'umask': '077'}
  105. retention_config = flexmock()
  106. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  107. BASE_PRUNE_FLAGS
  108. )
  109. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077'))
  110. module.prune_archives(
  111. dry_run=False,
  112. repository='repo',
  113. storage_config=storage_config,
  114. retention_config=retention_config,
  115. )
  116. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  117. storage_config = {'lock_wait': 5}
  118. retention_config = flexmock()
  119. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  120. BASE_PRUNE_FLAGS
  121. )
  122. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5'))
  123. module.prune_archives(
  124. dry_run=False,
  125. repository='repo',
  126. storage_config=storage_config,
  127. retention_config=retention_config,
  128. )