test_prune.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. PRUNE_COMMAND = (
  19. 'borg',
  20. 'prune',
  21. 'repo',
  22. '--keep-daily',
  23. '1',
  24. '--keep-weekly',
  25. '2',
  26. '--keep-monthly',
  27. '3',
  28. )
  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)
  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'))
  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(PRUNE_COMMAND + ('--stats', '--debug', '--list', '--show-rc'))
  54. insert_logging_mock(logging.DEBUG)
  55. module.prune_archives(
  56. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  57. )
  58. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_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 + ('--dry-run',))
  64. module.prune_archives(
  65. repository='repo', storage_config={}, dry_run=True, retention_config=retention_config
  66. )
  67. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  68. retention_config = flexmock()
  69. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  70. BASE_PRUNE_FLAGS
  71. )
  72. insert_execute_command_mock(('borg1',) + PRUNE_COMMAND[1:])
  73. module.prune_archives(
  74. dry_run=False,
  75. repository='repo',
  76. storage_config={},
  77. retention_config=retention_config,
  78. local_path='borg1',
  79. )
  80. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  81. retention_config = flexmock()
  82. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  83. BASE_PRUNE_FLAGS
  84. )
  85. insert_execute_command_mock(PRUNE_COMMAND + ('--remote-path', 'borg1'))
  86. module.prune_archives(
  87. dry_run=False,
  88. repository='repo',
  89. storage_config={},
  90. retention_config=retention_config,
  91. remote_path='borg1',
  92. )
  93. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  94. storage_config = {'umask': '077'}
  95. retention_config = flexmock()
  96. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  97. BASE_PRUNE_FLAGS
  98. )
  99. insert_execute_command_mock(PRUNE_COMMAND + ('--umask', '077'))
  100. module.prune_archives(
  101. dry_run=False,
  102. repository='repo',
  103. storage_config=storage_config,
  104. retention_config=retention_config,
  105. )
  106. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  107. storage_config = {'lock_wait': 5}
  108. retention_config = flexmock()
  109. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  110. BASE_PRUNE_FLAGS
  111. )
  112. insert_execute_command_mock(PRUNE_COMMAND + ('--lock-wait', '5'))
  113. module.prune_archives(
  114. dry_run=False,
  115. repository='repo',
  116. storage_config=storage_config,
  117. retention_config=retention_config,
  118. )