2
0

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_subprocess_mock(check_call_command, **kwargs):
  7. subprocess = flexmock(module.subprocess)
  8. subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
  9. BASE_PRUNE_FLAGS = (('--keep-daily', '1'), ('--keep-weekly', '2'), ('--keep-monthly', '3'))
  10. def test_make_prune_flags_returns_flags_from_config_plus_default_prefix():
  11. retention_config = OrderedDict((('keep_daily', 1), ('keep_weekly', 2), ('keep_monthly', 3)))
  12. result = module._make_prune_flags(retention_config)
  13. assert tuple(result) == BASE_PRUNE_FLAGS + (('--prefix', '{hostname}-'),)
  14. def test_make_prune_flags_accepts_prefix_with_placeholders():
  15. retention_config = OrderedDict((('keep_daily', 1), ('prefix', 'Documents_{hostname}-{now}')))
  16. result = module._make_prune_flags(retention_config)
  17. expected = (('--keep-daily', '1'), ('--prefix', 'Documents_{hostname}-{now}'))
  18. assert tuple(result) == expected
  19. PRUNE_COMMAND = (
  20. 'borg',
  21. 'prune',
  22. 'repo',
  23. '--keep-daily',
  24. '1',
  25. '--keep-weekly',
  26. '2',
  27. '--keep-monthly',
  28. '3',
  29. )
  30. def test_prune_archives_calls_borg_with_parameters():
  31. retention_config = flexmock()
  32. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  33. BASE_PRUNE_FLAGS
  34. )
  35. insert_subprocess_mock(PRUNE_COMMAND)
  36. module.prune_archives(
  37. dry_run=False, repository='repo', storage_config={}, retention_config=retention_config
  38. )
  39. def test_prune_archives_with_log_info_calls_borg_with_info_parameter():
  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_subprocess_mock(PRUNE_COMMAND + ('--stats', '--info'))
  45. insert_logging_mock(logging.INFO)
  46. module.prune_archives(
  47. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  48. )
  49. def test_prune_archives_with_log_debug_calls_borg_with_debug_parameter():
  50. retention_config = flexmock()
  51. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  52. BASE_PRUNE_FLAGS
  53. )
  54. insert_subprocess_mock(PRUNE_COMMAND + ('--stats', '--debug', '--list', '--show-rc'))
  55. insert_logging_mock(logging.DEBUG)
  56. module.prune_archives(
  57. repository='repo', storage_config={}, dry_run=False, retention_config=retention_config
  58. )
  59. def test_prune_archives_with_dry_run_calls_borg_with_dry_run_parameter():
  60. retention_config = flexmock()
  61. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  62. BASE_PRUNE_FLAGS
  63. )
  64. insert_subprocess_mock(PRUNE_COMMAND + ('--dry-run',))
  65. module.prune_archives(
  66. repository='repo', storage_config={}, dry_run=True, retention_config=retention_config
  67. )
  68. def test_prune_archives_with_local_path_calls_borg_via_local_path():
  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_subprocess_mock(('borg1',) + PRUNE_COMMAND[1:])
  74. module.prune_archives(
  75. dry_run=False,
  76. repository='repo',
  77. storage_config={},
  78. retention_config=retention_config,
  79. local_path='borg1',
  80. )
  81. def test_prune_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  82. retention_config = flexmock()
  83. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  84. BASE_PRUNE_FLAGS
  85. )
  86. insert_subprocess_mock(PRUNE_COMMAND + ('--remote-path', 'borg1'))
  87. module.prune_archives(
  88. dry_run=False,
  89. repository='repo',
  90. storage_config={},
  91. retention_config=retention_config,
  92. remote_path='borg1',
  93. )
  94. def test_prune_archives_with_umask_calls_borg_with_umask_parameters():
  95. storage_config = {'umask': '077'}
  96. retention_config = flexmock()
  97. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  98. BASE_PRUNE_FLAGS
  99. )
  100. insert_subprocess_mock(PRUNE_COMMAND + ('--umask', '077'))
  101. module.prune_archives(
  102. dry_run=False,
  103. repository='repo',
  104. storage_config=storage_config,
  105. retention_config=retention_config,
  106. )
  107. def test_prune_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  108. storage_config = {'lock_wait': 5}
  109. retention_config = flexmock()
  110. flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
  111. BASE_PRUNE_FLAGS
  112. )
  113. insert_subprocess_mock(PRUNE_COMMAND + ('--lock-wait', '5'))
  114. module.prune_archives(
  115. dry_run=False,
  116. repository='repo',
  117. storage_config=storage_config,
  118. retention_config=retention_config,
  119. )