test_export_tar.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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,
  7. output_log_level=logging.INFO,
  8. working_directory=None,
  9. borg_local_path='borg',
  10. borg_exit_codes=None,
  11. capture=True,
  12. ):
  13. flexmock(module.environment).should_receive('make_environment')
  14. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  15. working_directory,
  16. )
  17. flexmock(module).should_receive('execute_command').with_args(
  18. command,
  19. output_file=None if capture else module.DO_NOT_CAPTURE,
  20. output_log_level=output_log_level,
  21. environment=None,
  22. working_directory=working_directory,
  23. borg_local_path=borg_local_path,
  24. borg_exit_codes=borg_exit_codes,
  25. ).once()
  26. def test_export_tar_archive_calls_borg_with_path_flags():
  27. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  28. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  29. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  30. ('repo::archive',),
  31. )
  32. insert_execute_command_mock(
  33. ('borg', 'export-tar', 'repo::archive', 'test.tar', 'path1', 'path2'),
  34. )
  35. module.export_tar_archive(
  36. dry_run=False,
  37. repository_path='repo',
  38. archive='archive',
  39. paths=['path1', 'path2'],
  40. destination_path='test.tar',
  41. config={},
  42. local_borg_version='1.2.3',
  43. global_arguments=flexmock(),
  44. )
  45. def test_export_tar_archive_calls_borg_with_local_path_flags():
  46. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  47. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  48. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  49. ('repo::archive',),
  50. )
  51. insert_execute_command_mock(
  52. ('borg1', 'export-tar', 'repo::archive', 'test.tar'),
  53. borg_local_path='borg1',
  54. )
  55. module.export_tar_archive(
  56. dry_run=False,
  57. repository_path='repo',
  58. archive='archive',
  59. paths=None,
  60. destination_path='test.tar',
  61. config={},
  62. local_borg_version='1.2.3',
  63. global_arguments=flexmock(),
  64. local_path='borg1',
  65. )
  66. def test_export_tar_archive_calls_borg_using_exit_codes():
  67. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  68. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  69. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  70. ('repo::archive',),
  71. )
  72. borg_exit_codes = flexmock()
  73. insert_execute_command_mock(
  74. ('borg', 'export-tar', 'repo::archive', 'test.tar'),
  75. borg_exit_codes=borg_exit_codes,
  76. )
  77. module.export_tar_archive(
  78. dry_run=False,
  79. repository_path='repo',
  80. archive='archive',
  81. paths=None,
  82. destination_path='test.tar',
  83. config={'borg_exit_codes': borg_exit_codes},
  84. local_borg_version='1.2.3',
  85. global_arguments=flexmock(),
  86. )
  87. def test_export_tar_archive_calls_borg_with_remote_path_flags():
  88. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  89. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  90. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  91. ('repo::archive',),
  92. )
  93. insert_execute_command_mock(
  94. ('borg', 'export-tar', '--remote-path', 'borg1', 'repo::archive', 'test.tar'),
  95. )
  96. module.export_tar_archive(
  97. dry_run=False,
  98. repository_path='repo',
  99. archive='archive',
  100. paths=None,
  101. destination_path='test.tar',
  102. config={},
  103. local_borg_version='1.2.3',
  104. global_arguments=flexmock(),
  105. remote_path='borg1',
  106. )
  107. def test_export_tar_archive_calls_borg_with_umask_flags():
  108. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  109. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  110. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  111. ('repo::archive',),
  112. )
  113. insert_execute_command_mock(
  114. ('borg', 'export-tar', '--umask', '0770', 'repo::archive', 'test.tar'),
  115. )
  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. config={'umask': '0770'},
  123. local_borg_version='1.2.3',
  124. global_arguments=flexmock(),
  125. )
  126. def test_export_tar_archive_calls_borg_with_log_json_flag():
  127. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  128. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  129. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  130. ('repo::archive',),
  131. )
  132. insert_execute_command_mock(('borg', 'export-tar', '--log-json', 'repo::archive', 'test.tar'))
  133. module.export_tar_archive(
  134. dry_run=False,
  135. repository_path='repo',
  136. archive='archive',
  137. paths=None,
  138. destination_path='test.tar',
  139. config={'log_json': True},
  140. local_borg_version='1.2.3',
  141. global_arguments=flexmock(),
  142. )
  143. def test_export_tar_archive_calls_borg_with_lock_wait_flags():
  144. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  145. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  146. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  147. ('repo::archive',),
  148. )
  149. insert_execute_command_mock(
  150. ('borg', 'export-tar', '--lock-wait', '5', 'repo::archive', 'test.tar'),
  151. )
  152. module.export_tar_archive(
  153. dry_run=False,
  154. repository_path='repo',
  155. archive='archive',
  156. paths=None,
  157. destination_path='test.tar',
  158. config={'lock_wait': '5'},
  159. local_borg_version='1.2.3',
  160. global_arguments=flexmock(),
  161. )
  162. def test_export_tar_archive_calls_borg_with_extra_borg_options():
  163. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  164. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  165. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  166. ('repo::archive',),
  167. )
  168. insert_execute_command_mock(
  169. ('borg', 'export-tar', '--extra', 'value with space', 'repo::archive', 'test.tar'),
  170. )
  171. module.export_tar_archive(
  172. dry_run=False,
  173. repository_path='repo',
  174. archive='archive',
  175. paths=None,
  176. destination_path='test.tar',
  177. config={'extra_borg_options': {'export_tar': '--extra "value with space"'}},
  178. local_borg_version='1.2.3',
  179. global_arguments=flexmock(),
  180. )
  181. def test_export_tar_archive_with_log_info_calls_borg_with_info_flag():
  182. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  183. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  184. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  185. ('repo::archive',),
  186. )
  187. insert_execute_command_mock(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  188. insert_logging_mock(logging.INFO)
  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. config={},
  196. local_borg_version='1.2.3',
  197. global_arguments=flexmock(),
  198. )
  199. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_flags():
  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', '--debug', '--show-rc', 'repo::archive', 'test.tar'),
  207. )
  208. insert_logging_mock(logging.DEBUG)
  209. module.export_tar_archive(
  210. dry_run=False,
  211. repository_path='repo',
  212. archive='archive',
  213. paths=None,
  214. destination_path='test.tar',
  215. config={},
  216. local_borg_version='1.2.3',
  217. global_arguments=flexmock(),
  218. )
  219. def test_export_tar_archive_calls_borg_with_dry_run_flag():
  220. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  221. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  222. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  223. ('repo::archive',),
  224. )
  225. flexmock(module).should_receive('execute_command').never()
  226. module.export_tar_archive(
  227. dry_run=True,
  228. repository_path='repo',
  229. archive='archive',
  230. paths=None,
  231. destination_path='test.tar',
  232. config={},
  233. local_borg_version='1.2.3',
  234. global_arguments=flexmock(),
  235. )
  236. def test_export_tar_archive_calls_borg_with_tar_filter_flags():
  237. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  238. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  239. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  240. ('repo::archive',),
  241. )
  242. insert_execute_command_mock(
  243. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar'),
  244. )
  245. module.export_tar_archive(
  246. dry_run=False,
  247. repository_path='repo',
  248. archive='archive',
  249. paths=None,
  250. destination_path='test.tar',
  251. config={},
  252. local_borg_version='1.2.3',
  253. global_arguments=flexmock(),
  254. tar_filter='bzip2',
  255. )
  256. def test_export_tar_archive_calls_borg_with_list_flag():
  257. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  258. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  259. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  260. ('repo::archive',),
  261. )
  262. insert_execute_command_mock(
  263. ('borg', 'export-tar', '--list', 'repo::archive', 'test.tar'),
  264. output_log_level=logging.ANSWER,
  265. )
  266. module.export_tar_archive(
  267. dry_run=False,
  268. repository_path='repo',
  269. archive='archive',
  270. paths=None,
  271. destination_path='test.tar',
  272. config={'list_details': True},
  273. local_borg_version='1.2.3',
  274. global_arguments=flexmock(),
  275. )
  276. def test_export_tar_archive_calls_borg_with_strip_components_flag():
  277. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  278. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  279. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  280. ('repo::archive',),
  281. )
  282. insert_execute_command_mock(
  283. ('borg', 'export-tar', '--strip-components', '5', 'repo::archive', 'test.tar'),
  284. )
  285. module.export_tar_archive(
  286. dry_run=False,
  287. repository_path='repo',
  288. archive='archive',
  289. paths=None,
  290. destination_path='test.tar',
  291. config={},
  292. local_borg_version='1.2.3',
  293. global_arguments=flexmock(),
  294. strip_components=5,
  295. )
  296. def test_export_tar_archive_skips_abspath_for_remote_repository_flag():
  297. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  298. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  299. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  300. ('server:repo::archive',),
  301. )
  302. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  303. module.export_tar_archive(
  304. dry_run=False,
  305. repository_path='server:repo',
  306. archive='archive',
  307. paths=None,
  308. destination_path='test.tar',
  309. config={},
  310. local_borg_version='1.2.3',
  311. global_arguments=flexmock(),
  312. )
  313. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  314. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  315. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  316. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  317. ('repo::archive',),
  318. )
  319. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  320. module.export_tar_archive(
  321. dry_run=False,
  322. repository_path='repo',
  323. archive='archive',
  324. paths=None,
  325. destination_path='-',
  326. config={},
  327. local_borg_version='1.2.3',
  328. global_arguments=flexmock(),
  329. )
  330. def test_export_tar_archive_calls_borg_with_working_directory():
  331. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  332. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  333. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  334. ('repo::archive',),
  335. )
  336. insert_execute_command_mock(
  337. ('borg', 'export-tar', 'repo::archive', 'test.tar'),
  338. working_directory='/working/dir',
  339. )
  340. module.export_tar_archive(
  341. dry_run=False,
  342. repository_path='repo',
  343. archive='archive',
  344. paths=[],
  345. destination_path='test.tar',
  346. config={'working_directory': '/working/dir'},
  347. local_borg_version='1.2.3',
  348. global_arguments=flexmock(),
  349. )