test_extract.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import extract as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(command, working_directory=None, error_on_warnings=True):
  6. flexmock(module).should_receive('execute_command').with_args(
  7. command, working_directory=working_directory, error_on_warnings=error_on_warnings
  8. ).once()
  9. def insert_execute_command_output_mock(command, result):
  10. flexmock(module).should_receive('execute_command').with_args(
  11. command, output_log_level=None
  12. ).and_return(result).once()
  13. def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
  14. insert_execute_command_output_mock(
  15. ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  16. )
  17. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
  18. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  19. def test_extract_last_archive_dry_run_without_any_archives_should_not_raise():
  20. insert_execute_command_output_mock(('borg', 'list', '--short', 'repo'), result='\n')
  21. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  22. def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
  23. insert_execute_command_output_mock(
  24. ('borg', 'list', '--short', '--info', 'repo'), result='archive1\narchive2\n'
  25. )
  26. insert_execute_command_mock(('borg', 'extract', '--dry-run', '--info', 'repo::archive2'))
  27. insert_logging_mock(logging.INFO)
  28. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  29. def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
  30. insert_execute_command_output_mock(
  31. ('borg', 'list', '--short', '--debug', '--show-rc', 'repo'), result='archive1\narchive2\n'
  32. )
  33. insert_execute_command_mock(
  34. ('borg', 'extract', '--dry-run', '--debug', '--show-rc', '--list', 'repo::archive2')
  35. )
  36. insert_logging_mock(logging.DEBUG)
  37. module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
  38. def test_extract_last_archive_dry_run_calls_borg_via_local_path():
  39. insert_execute_command_output_mock(
  40. ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'
  41. )
  42. insert_execute_command_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
  43. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
  44. def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
  45. insert_execute_command_output_mock(
  46. ('borg', 'list', '--short', '--remote-path', 'borg1', 'repo'), result='archive1\narchive2\n'
  47. )
  48. insert_execute_command_mock(
  49. ('borg', 'extract', '--dry-run', '--remote-path', 'borg1', 'repo::archive2')
  50. )
  51. module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
  52. def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_parameters():
  53. insert_execute_command_output_mock(
  54. ('borg', 'list', '--short', '--lock-wait', '5', 'repo'), result='archive1\narchive2\n'
  55. )
  56. insert_execute_command_mock(
  57. ('borg', 'extract', '--dry-run', '--lock-wait', '5', 'repo::archive2')
  58. )
  59. module.extract_last_archive_dry_run(repository='repo', lock_wait=5)
  60. def test_extract_archive_calls_borg_with_restore_path_parameters():
  61. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  62. insert_execute_command_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  63. module.extract_archive(
  64. dry_run=False,
  65. repository='repo',
  66. archive='archive',
  67. paths=['path1', 'path2'],
  68. location_config={},
  69. storage_config={},
  70. )
  71. def test_extract_archive_calls_borg_with_remote_path_parameters():
  72. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  73. insert_execute_command_mock(('borg', 'extract', '--remote-path', 'borg1', 'repo::archive'))
  74. module.extract_archive(
  75. dry_run=False,
  76. repository='repo',
  77. archive='archive',
  78. paths=None,
  79. location_config={},
  80. storage_config={},
  81. remote_path='borg1',
  82. )
  83. def test_extract_archive_calls_borg_with_numeric_owner_parameter():
  84. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  85. insert_execute_command_mock(('borg', 'extract', '--numeric-owner', 'repo::archive'))
  86. module.extract_archive(
  87. dry_run=False,
  88. repository='repo',
  89. archive='archive',
  90. paths=None,
  91. location_config={'numeric_owner': True},
  92. storage_config={},
  93. )
  94. def test_extract_archive_calls_borg_with_umask_parameters():
  95. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  96. insert_execute_command_mock(('borg', 'extract', '--umask', '0770', 'repo::archive'))
  97. module.extract_archive(
  98. dry_run=False,
  99. repository='repo',
  100. archive='archive',
  101. paths=None,
  102. location_config={},
  103. storage_config={'umask': '0770'},
  104. )
  105. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  106. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  107. insert_execute_command_mock(('borg', 'extract', '--lock-wait', '5', 'repo::archive'))
  108. module.extract_archive(
  109. dry_run=False,
  110. repository='repo',
  111. archive='archive',
  112. paths=None,
  113. location_config={},
  114. storage_config={'lock_wait': '5'},
  115. )
  116. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  117. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  118. insert_execute_command_mock(('borg', 'extract', '--info', 'repo::archive'))
  119. insert_logging_mock(logging.INFO)
  120. module.extract_archive(
  121. dry_run=False,
  122. repository='repo',
  123. archive='archive',
  124. paths=None,
  125. location_config={},
  126. storage_config={},
  127. )
  128. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  129. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  130. insert_execute_command_mock(
  131. ('borg', 'extract', '--debug', '--list', '--show-rc', 'repo::archive')
  132. )
  133. insert_logging_mock(logging.DEBUG)
  134. module.extract_archive(
  135. dry_run=False,
  136. repository='repo',
  137. archive='archive',
  138. paths=None,
  139. location_config={},
  140. storage_config={},
  141. )
  142. def test_extract_archive_calls_borg_with_dry_run_parameter():
  143. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  144. insert_execute_command_mock(('borg', 'extract', '--dry-run', 'repo::archive'))
  145. module.extract_archive(
  146. dry_run=True,
  147. repository='repo',
  148. archive='archive',
  149. paths=None,
  150. location_config={},
  151. storage_config={},
  152. )
  153. def test_extract_archive_calls_borg_with_destination_path():
  154. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  155. insert_execute_command_mock(('borg', 'extract', 'repo::archive'), working_directory='/dest')
  156. module.extract_archive(
  157. dry_run=False,
  158. repository='repo',
  159. archive='archive',
  160. paths=None,
  161. location_config={},
  162. storage_config={},
  163. destination_path='/dest',
  164. )
  165. def test_extract_archive_calls_borg_with_progress_parameter():
  166. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  167. flexmock(module).should_receive('execute_command_without_capture').with_args(
  168. ('borg', 'extract', '--progress', 'repo::archive'),
  169. working_directory=None,
  170. error_on_warnings=True,
  171. ).once()
  172. module.extract_archive(
  173. dry_run=False,
  174. repository='repo',
  175. archive='archive',
  176. paths=None,
  177. location_config={},
  178. storage_config={},
  179. progress=True,
  180. )