test_compact.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import compact as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(compact_command, output_log_level):
  6. flexmock(module).should_receive('execute_command').with_args(
  7. compact_command, output_log_level=output_log_level, borg_local_path=compact_command[0]
  8. ).once()
  9. COMPACT_COMMAND = ('borg', 'compact')
  10. def test_compact_segments_calls_borg_with_parameters():
  11. insert_execute_command_mock(COMPACT_COMMAND + ('repo',), logging.WARNING)
  12. module.compact_segments(dry_run=False, repository='repo', storage_config={})
  13. def test_compact_segments_with_log_info_calls_borg_with_info_parameter():
  14. insert_execute_command_mock(COMPACT_COMMAND + ('--info', 'repo'), logging.WARNING)
  15. insert_logging_mock(logging.INFO)
  16. module.compact_segments(repository='repo', storage_config={}, dry_run=False)
  17. def test_compact_segments_with_log_debug_calls_borg_with_debug_parameter():
  18. insert_execute_command_mock(COMPACT_COMMAND + ('--debug', '--show-rc', 'repo'), logging.WARNING)
  19. insert_logging_mock(logging.DEBUG)
  20. module.compact_segments(repository='repo', storage_config={}, dry_run=False)
  21. def test_compact_segments_with_dry_run_calls_borg_with_dry_run_parameter():
  22. insert_execute_command_mock(COMPACT_COMMAND + ('--dry-run', 'repo'), logging.WARNING)
  23. module.compact_segments(repository='repo', storage_config={}, dry_run=True)
  24. def test_compact_segments_with_local_path_calls_borg_via_local_path():
  25. insert_execute_command_mock(('borg1',) + COMPACT_COMMAND[1:] + ('repo',), logging.WARNING)
  26. module.compact_segments(
  27. dry_run=False, repository='repo', storage_config={}, local_path='borg1',
  28. )
  29. def test_compact_segments_with_remote_path_calls_borg_with_remote_path_parameters():
  30. insert_execute_command_mock(
  31. COMPACT_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.WARNING
  32. )
  33. module.compact_segments(
  34. dry_run=False, repository='repo', storage_config={}, remote_path='borg1',
  35. )
  36. def test_compact_segments_with_progress_calls_borg_with_progress_parameter():
  37. insert_execute_command_mock(COMPACT_COMMAND + ('--progress', 'repo'), logging.WARNING)
  38. module.compact_segments(
  39. dry_run=False, repository='repo', storage_config={}, progress=True,
  40. )
  41. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_parameter():
  42. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.WARNING)
  43. module.compact_segments(
  44. dry_run=False, repository='repo', storage_config={}, cleanup_commits=True,
  45. )
  46. def test_compact_segments_with_threshold_calls_borg_with_threshold_parameter():
  47. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.WARNING)
  48. module.compact_segments(
  49. dry_run=False, repository='repo', storage_config={}, threshold=20,
  50. )
  51. def test_compact_segments_with_umask_calls_borg_with_umask_parameters():
  52. storage_config = {'umask': '077'}
  53. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.WARNING)
  54. module.compact_segments(
  55. dry_run=False, repository='repo', storage_config=storage_config,
  56. )
  57. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_parameters():
  58. storage_config = {'lock_wait': 5}
  59. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.WARNING)
  60. module.compact_segments(
  61. dry_run=False, repository='repo', storage_config=storage_config,
  62. )
  63. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  64. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.WARNING)
  65. module.compact_segments(
  66. dry_run=False,
  67. repository='repo',
  68. storage_config={'extra_borg_options': {'compact': '--extra --options'}},
  69. )