test_extract.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import logging
  2. import sys
  3. from flexmock import flexmock
  4. from borgmatic.borg import extract 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. def insert_subprocess_never():
  10. subprocess = flexmock(module.subprocess)
  11. subprocess.should_receive('check_call').never()
  12. def insert_subprocess_check_output_mock(check_output_command, result, **kwargs):
  13. subprocess = flexmock(module.subprocess)
  14. subprocess.should_receive('check_output').with_args(check_output_command, **kwargs).and_return(
  15. result
  16. ).once()
  17. def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
  18. flexmock(sys.stdout).encoding = 'utf-8'
  19. insert_subprocess_check_output_mock(
  20. ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
  21. )
  22. insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
  23. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  24. def test_extract_last_archive_dry_run_without_any_archives_should_bail():
  25. flexmock(sys.stdout).encoding = 'utf-8'
  26. insert_subprocess_check_output_mock(
  27. ('borg', 'list', '--short', 'repo'), result='\n'.encode('utf-8')
  28. )
  29. insert_subprocess_never()
  30. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  31. def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
  32. flexmock(sys.stdout).encoding = 'utf-8'
  33. insert_subprocess_check_output_mock(
  34. ('borg', 'list', '--short', 'repo', '--info'), result='archive1\narchive2\n'.encode('utf-8')
  35. )
  36. insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--info'))
  37. insert_logging_mock(logging.INFO)
  38. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  39. def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
  40. flexmock(sys.stdout).encoding = 'utf-8'
  41. insert_subprocess_check_output_mock(
  42. ('borg', 'list', '--short', 'repo', '--debug', '--show-rc'),
  43. result='archive1\narchive2\n'.encode('utf-8'),
  44. )
  45. insert_subprocess_mock(
  46. ('borg', 'extract', '--dry-run', 'repo::archive2', '--debug', '--show-rc', '--list')
  47. )
  48. insert_logging_mock(logging.DEBUG)
  49. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  50. def test_extract_last_archive_dry_run_calls_borg_via_local_path():
  51. flexmock(sys.stdout).encoding = 'utf-8'
  52. insert_subprocess_check_output_mock(
  53. ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
  54. )
  55. insert_subprocess_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
  56. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
  57. def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
  58. flexmock(sys.stdout).encoding = 'utf-8'
  59. insert_subprocess_check_output_mock(
  60. ('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'),
  61. result='archive1\narchive2\n'.encode('utf-8'),
  62. )
  63. insert_subprocess_mock(
  64. ('borg', 'extract', '--dry-run', 'repo::archive2', '--remote-path', 'borg1')
  65. )
  66. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
  67. def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_parameters():
  68. flexmock(sys.stdout).encoding = 'utf-8'
  69. insert_subprocess_check_output_mock(
  70. ('borg', 'list', '--short', 'repo', '--lock-wait', '5'),
  71. result='archive1\narchive2\n'.encode('utf-8'),
  72. )
  73. insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5'))
  74. module.extract_last_archive_dry_run(repository='repo', lock_wait=5)
  75. def test_extract_archive_calls_borg_with_remote_path_parameters():
  76. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--remote-path', 'borg1'))
  77. module.extract_archive(
  78. dry_run=False,
  79. repository='repo',
  80. archive='archive',
  81. restore_paths=None,
  82. storage_config={},
  83. remote_path='borg1',
  84. )
  85. def test_extract_archive_calls_borg_with_umask_parameters():
  86. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--umask', '0770'))
  87. module.extract_archive(
  88. dry_run=False,
  89. repository='repo',
  90. archive='archive',
  91. restore_paths=None,
  92. storage_config={'umask': '0770'},
  93. )
  94. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  95. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--lock-wait', '5'))
  96. module.extract_archive(
  97. dry_run=False,
  98. repository='repo',
  99. archive='archive',
  100. restore_paths=None,
  101. storage_config={'lock_wait': '5'},
  102. )
  103. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  104. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--info'))
  105. insert_logging_mock(logging.INFO)
  106. module.extract_archive(
  107. dry_run=False, repository='repo', archive='archive', restore_paths=None, storage_config={}
  108. )
  109. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  110. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--debug', '--list', '--show-rc'))
  111. insert_logging_mock(logging.DEBUG)
  112. module.extract_archive(
  113. dry_run=False, repository='repo', archive='archive', restore_paths=None, storage_config={}
  114. )
  115. def test_extract_archive_calls_borg_with_dry_run_parameter():
  116. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--dry-run'))
  117. module.extract_archive(
  118. dry_run=True, repository='repo', archive='archive', restore_paths=None, storage_config={}
  119. )
  120. def test_extract_archive_calls_borg_with_progress_parameter():
  121. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--progress'))
  122. module.extract_archive(
  123. dry_run=False,
  124. repository='repo',
  125. archive='archive',
  126. restore_paths=None,
  127. storage_config={},
  128. progress=True,
  129. )