test_export_tar.py 9.6 KB

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