test_extract.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_restore_path_parameters():
  76. insert_subprocess_mock(('borg', 'extract', 'repo::archive', 'path1', 'path2'))
  77. module.extract_archive(
  78. dry_run=False,
  79. repository='repo',
  80. archive='archive',
  81. restore_paths=['path1', 'path2'],
  82. location_config={},
  83. storage_config={},
  84. )
  85. def test_extract_archive_calls_borg_with_remote_path_parameters():
  86. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--remote-path', 'borg1'))
  87. module.extract_archive(
  88. dry_run=False,
  89. repository='repo',
  90. archive='archive',
  91. restore_paths=None,
  92. location_config={},
  93. storage_config={},
  94. remote_path='borg1',
  95. )
  96. def test_extract_archive_calls_borg_with_numeric_owner_parameter():
  97. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--numeric-owner'))
  98. module.extract_archive(
  99. dry_run=False,
  100. repository='repo',
  101. archive='archive',
  102. restore_paths=None,
  103. location_config={'numeric_owner': True},
  104. storage_config={},
  105. )
  106. def test_extract_archive_calls_borg_with_umask_parameters():
  107. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--umask', '0770'))
  108. module.extract_archive(
  109. dry_run=False,
  110. repository='repo',
  111. archive='archive',
  112. restore_paths=None,
  113. location_config={},
  114. storage_config={'umask': '0770'},
  115. )
  116. def test_extract_archive_calls_borg_with_lock_wait_parameters():
  117. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--lock-wait', '5'))
  118. module.extract_archive(
  119. dry_run=False,
  120. repository='repo',
  121. archive='archive',
  122. restore_paths=None,
  123. location_config={},
  124. storage_config={'lock_wait': '5'},
  125. )
  126. def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
  127. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--info'))
  128. insert_logging_mock(logging.INFO)
  129. module.extract_archive(
  130. dry_run=False,
  131. repository='repo',
  132. archive='archive',
  133. restore_paths=None,
  134. location_config={},
  135. storage_config={},
  136. )
  137. def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
  138. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--debug', '--list', '--show-rc'))
  139. insert_logging_mock(logging.DEBUG)
  140. module.extract_archive(
  141. dry_run=False,
  142. repository='repo',
  143. archive='archive',
  144. restore_paths=None,
  145. location_config={},
  146. storage_config={},
  147. )
  148. def test_extract_archive_calls_borg_with_dry_run_parameter():
  149. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--dry-run'))
  150. module.extract_archive(
  151. dry_run=True,
  152. repository='repo',
  153. archive='archive',
  154. restore_paths=None,
  155. location_config={},
  156. storage_config={},
  157. )
  158. def test_extract_archive_calls_borg_with_progress_parameter():
  159. insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--progress'))
  160. module.extract_archive(
  161. dry_run=False,
  162. repository='repo',
  163. archive='archive',
  164. restore_paths=None,
  165. location_config={},
  166. storage_config={},
  167. progress=True,
  168. )