test_compact.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.INFO)
  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.INFO)
  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.INFO)
  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_skips_borg_call():
  22. flexmock(module).should_receive('execute_command').never()
  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.INFO)
  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(COMPACT_COMMAND + ('--remote-path', 'borg1', 'repo'), logging.INFO)
  31. module.compact_segments(
  32. dry_run=False, repository='repo', storage_config={}, remote_path='borg1',
  33. )
  34. def test_compact_segments_with_progress_calls_borg_with_progress_parameter():
  35. insert_execute_command_mock(COMPACT_COMMAND + ('--progress', 'repo'), logging.INFO)
  36. module.compact_segments(
  37. dry_run=False, repository='repo', storage_config={}, progress=True,
  38. )
  39. def test_compact_segments_with_cleanup_commits_calls_borg_with_cleanup_commits_parameter():
  40. insert_execute_command_mock(COMPACT_COMMAND + ('--cleanup-commits', 'repo'), logging.INFO)
  41. module.compact_segments(
  42. dry_run=False, repository='repo', storage_config={}, cleanup_commits=True,
  43. )
  44. def test_compact_segments_with_threshold_calls_borg_with_threshold_parameter():
  45. insert_execute_command_mock(COMPACT_COMMAND + ('--threshold', '20', 'repo'), logging.INFO)
  46. module.compact_segments(
  47. dry_run=False, repository='repo', storage_config={}, threshold=20,
  48. )
  49. def test_compact_segments_with_umask_calls_borg_with_umask_parameters():
  50. storage_config = {'umask': '077'}
  51. insert_execute_command_mock(COMPACT_COMMAND + ('--umask', '077', 'repo'), logging.INFO)
  52. module.compact_segments(
  53. dry_run=False, repository='repo', storage_config=storage_config,
  54. )
  55. def test_compact_segments_with_lock_wait_calls_borg_with_lock_wait_parameters():
  56. storage_config = {'lock_wait': 5}
  57. insert_execute_command_mock(COMPACT_COMMAND + ('--lock-wait', '5', 'repo'), logging.INFO)
  58. module.compact_segments(
  59. dry_run=False, repository='repo', storage_config=storage_config,
  60. )
  61. def test_compact_segments_with_extra_borg_options_calls_borg_with_extra_options():
  62. insert_execute_command_mock(COMPACT_COMMAND + ('--extra', '--options', 'repo'), logging.INFO)
  63. module.compact_segments(
  64. dry_run=False,
  65. repository='repo',
  66. storage_config={'extra_borg_options': {'compact': '--extra --options'}},
  67. )