test_export_tar.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import export_tar as module
  4. from ..test_verbosity import insert_logging_mock
  5. def insert_execute_command_mock(
  6. command, output_log_level=logging.INFO, borg_local_path='borg', capture=True
  7. ):
  8. flexmock(module.environment).should_receive('make_environment')
  9. flexmock(module).should_receive('execute_command').with_args(
  10. command,
  11. output_file=None if capture else module.DO_NOT_CAPTURE,
  12. output_log_level=output_log_level,
  13. borg_local_path=borg_local_path,
  14. extra_environment=None,
  15. ).once()
  16. def test_export_tar_archive_calls_borg_with_path_parameters():
  17. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  18. ('repo::archive',)
  19. )
  20. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  21. insert_execute_command_mock(
  22. ('borg', 'export-tar', 'repo::archive', 'test.tar', 'path1', 'path2')
  23. )
  24. module.export_tar_archive(
  25. dry_run=False,
  26. repository='repo',
  27. archive='archive',
  28. paths=['path1', 'path2'],
  29. destination_path='test.tar',
  30. storage_config={},
  31. local_borg_version='1.2.3',
  32. )
  33. def test_export_tar_archive_calls_borg_with_local_path_parameters():
  34. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  35. ('repo::archive',)
  36. )
  37. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  38. insert_execute_command_mock(
  39. ('borg1', 'export-tar', 'repo::archive', 'test.tar'), borg_local_path='borg1'
  40. )
  41. module.export_tar_archive(
  42. dry_run=False,
  43. repository='repo',
  44. archive='archive',
  45. paths=None,
  46. destination_path='test.tar',
  47. storage_config={},
  48. local_borg_version='1.2.3',
  49. local_path='borg1',
  50. )
  51. def test_export_tar_archive_calls_borg_with_remote_path_parameters():
  52. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  53. ('repo::archive',)
  54. )
  55. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  56. insert_execute_command_mock(
  57. ('borg', 'export-tar', '--remote-path', 'borg1', 'repo::archive', 'test.tar')
  58. )
  59. module.export_tar_archive(
  60. dry_run=False,
  61. repository='repo',
  62. archive='archive',
  63. paths=None,
  64. destination_path='test.tar',
  65. storage_config={},
  66. local_borg_version='1.2.3',
  67. remote_path='borg1',
  68. )
  69. def test_export_tar_archive_calls_borg_with_umask_parameters():
  70. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  71. ('repo::archive',)
  72. )
  73. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  74. insert_execute_command_mock(
  75. ('borg', 'export-tar', '--umask', '0770', 'repo::archive', 'test.tar')
  76. )
  77. module.export_tar_archive(
  78. dry_run=False,
  79. repository='repo',
  80. archive='archive',
  81. paths=None,
  82. destination_path='test.tar',
  83. storage_config={'umask': '0770'},
  84. local_borg_version='1.2.3',
  85. )
  86. def test_export_tar_archive_calls_borg_with_lock_wait_parameters():
  87. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  88. ('repo::archive',)
  89. )
  90. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  91. insert_execute_command_mock(
  92. ('borg', 'export-tar', '--lock-wait', '5', 'repo::archive', 'test.tar')
  93. )
  94. module.export_tar_archive(
  95. dry_run=False,
  96. repository='repo',
  97. archive='archive',
  98. paths=None,
  99. destination_path='test.tar',
  100. storage_config={'lock_wait': '5'},
  101. local_borg_version='1.2.3',
  102. )
  103. def test_export_tar_archive_with_log_info_calls_borg_with_info_parameter():
  104. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  105. ('repo::archive',)
  106. )
  107. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  108. insert_execute_command_mock(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  109. insert_logging_mock(logging.INFO)
  110. module.export_tar_archive(
  111. dry_run=False,
  112. repository='repo',
  113. archive='archive',
  114. paths=None,
  115. destination_path='test.tar',
  116. storage_config={},
  117. local_borg_version='1.2.3',
  118. )
  119. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_parameters():
  120. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  121. ('repo::archive',)
  122. )
  123. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  124. insert_execute_command_mock(
  125. ('borg', 'export-tar', '--debug', '--show-rc', 'repo::archive', 'test.tar')
  126. )
  127. insert_logging_mock(logging.DEBUG)
  128. module.export_tar_archive(
  129. dry_run=False,
  130. repository='repo',
  131. archive='archive',
  132. paths=None,
  133. destination_path='test.tar',
  134. storage_config={},
  135. local_borg_version='1.2.3',
  136. )
  137. def test_export_tar_archive_calls_borg_with_dry_run_parameter():
  138. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  139. ('repo::archive',)
  140. )
  141. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  142. flexmock(module).should_receive('execute_command').never()
  143. module.export_tar_archive(
  144. dry_run=True,
  145. repository='repo',
  146. archive='archive',
  147. paths=None,
  148. destination_path='test.tar',
  149. storage_config={},
  150. local_borg_version='1.2.3',
  151. )
  152. def test_export_tar_archive_calls_borg_with_tar_filter_parameters():
  153. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  154. ('repo::archive',)
  155. )
  156. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  157. insert_execute_command_mock(
  158. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar')
  159. )
  160. module.export_tar_archive(
  161. dry_run=False,
  162. repository='repo',
  163. archive='archive',
  164. paths=None,
  165. destination_path='test.tar',
  166. storage_config={},
  167. local_borg_version='1.2.3',
  168. tar_filter='bzip2',
  169. )
  170. def test_export_tar_archive_calls_borg_with_list_parameter():
  171. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  172. ('repo::archive',)
  173. )
  174. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  175. insert_execute_command_mock(
  176. ('borg', 'export-tar', '--list', 'repo::archive', 'test.tar'),
  177. output_log_level=logging.WARNING,
  178. )
  179. module.export_tar_archive(
  180. dry_run=False,
  181. repository='repo',
  182. archive='archive',
  183. paths=None,
  184. destination_path='test.tar',
  185. storage_config={},
  186. local_borg_version='1.2.3',
  187. list_files=True,
  188. )
  189. def test_export_tar_archive_calls_borg_with_strip_components_parameter():
  190. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  191. ('repo::archive',)
  192. )
  193. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  194. insert_execute_command_mock(
  195. ('borg', 'export-tar', '--strip-components', '5', 'repo::archive', 'test.tar')
  196. )
  197. module.export_tar_archive(
  198. dry_run=False,
  199. repository='repo',
  200. archive='archive',
  201. paths=None,
  202. destination_path='test.tar',
  203. storage_config={},
  204. local_borg_version='1.2.3',
  205. strip_components=5,
  206. )
  207. def test_export_tar_archive_skips_abspath_for_remote_repository_parameter():
  208. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  209. ('server:repo::archive',)
  210. )
  211. flexmock(module.os.path).should_receive('abspath').never()
  212. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  213. module.export_tar_archive(
  214. dry_run=False,
  215. repository='server:repo',
  216. archive='archive',
  217. paths=None,
  218. destination_path='test.tar',
  219. storage_config={},
  220. local_borg_version='1.2.3',
  221. )
  222. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  223. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  224. ('repo::archive',)
  225. )
  226. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  227. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  228. module.export_tar_archive(
  229. dry_run=False,
  230. repository='repo',
  231. archive='archive',
  232. paths=None,
  233. destination_path='-',
  234. storage_config={},
  235. local_borg_version='1.2.3',
  236. )