test_export_tar.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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'), borg_local_path='borg1'
  53. )
  54. module.export_tar_archive(
  55. dry_run=False,
  56. repository_path='repo',
  57. archive='archive',
  58. paths=None,
  59. destination_path='test.tar',
  60. config={},
  61. local_borg_version='1.2.3',
  62. global_arguments=flexmock(),
  63. local_path='borg1',
  64. )
  65. def test_export_tar_archive_calls_borg_using_exit_codes():
  66. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  67. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  68. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  69. ('repo::archive',)
  70. )
  71. borg_exit_codes = flexmock()
  72. insert_execute_command_mock(
  73. ('borg', 'export-tar', 'repo::archive', 'test.tar'),
  74. borg_exit_codes=borg_exit_codes,
  75. )
  76. module.export_tar_archive(
  77. dry_run=False,
  78. repository_path='repo',
  79. archive='archive',
  80. paths=None,
  81. destination_path='test.tar',
  82. config={'borg_exit_codes': borg_exit_codes},
  83. local_borg_version='1.2.3',
  84. global_arguments=flexmock(),
  85. )
  86. def test_export_tar_archive_calls_borg_with_remote_path_flags():
  87. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  88. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  89. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  90. ('repo::archive',)
  91. )
  92. insert_execute_command_mock(
  93. ('borg', 'export-tar', '--remote-path', 'borg1', 'repo::archive', 'test.tar')
  94. )
  95. module.export_tar_archive(
  96. dry_run=False,
  97. repository_path='repo',
  98. archive='archive',
  99. paths=None,
  100. destination_path='test.tar',
  101. config={},
  102. local_borg_version='1.2.3',
  103. global_arguments=flexmock(),
  104. remote_path='borg1',
  105. )
  106. def test_export_tar_archive_calls_borg_with_umask_flags():
  107. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  108. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  109. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  110. ('repo::archive',)
  111. )
  112. insert_execute_command_mock(
  113. ('borg', 'export-tar', '--umask', '0770', 'repo::archive', 'test.tar')
  114. )
  115. module.export_tar_archive(
  116. dry_run=False,
  117. repository_path='repo',
  118. archive='archive',
  119. paths=None,
  120. destination_path='test.tar',
  121. config={'umask': '0770'},
  122. local_borg_version='1.2.3',
  123. global_arguments=flexmock(),
  124. )
  125. def test_export_tar_archive_calls_borg_with_log_json_flag():
  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(('borg', 'export-tar', '--log-json', 'repo::archive', 'test.tar'))
  132. module.export_tar_archive(
  133. dry_run=False,
  134. repository_path='repo',
  135. archive='archive',
  136. paths=None,
  137. destination_path='test.tar',
  138. config={'log_json': True},
  139. local_borg_version='1.2.3',
  140. global_arguments=flexmock(),
  141. )
  142. def test_export_tar_archive_calls_borg_with_lock_wait_flags():
  143. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  144. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  145. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  146. ('repo::archive',)
  147. )
  148. insert_execute_command_mock(
  149. ('borg', 'export-tar', '--lock-wait', '5', 'repo::archive', 'test.tar')
  150. )
  151. module.export_tar_archive(
  152. dry_run=False,
  153. repository_path='repo',
  154. archive='archive',
  155. paths=None,
  156. destination_path='test.tar',
  157. config={'lock_wait': '5'},
  158. local_borg_version='1.2.3',
  159. global_arguments=flexmock(),
  160. )
  161. def test_export_tar_archive_with_log_info_calls_borg_with_info_flag():
  162. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  163. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  164. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  165. ('repo::archive',)
  166. )
  167. insert_execute_command_mock(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  168. insert_logging_mock(logging.INFO)
  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. config={},
  176. local_borg_version='1.2.3',
  177. global_arguments=flexmock(),
  178. )
  179. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_flags():
  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', '--debug', '--show-rc', 'repo::archive', 'test.tar')
  187. )
  188. insert_logging_mock(logging.DEBUG)
  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_calls_borg_with_dry_run_flag():
  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. flexmock(module).should_receive('execute_command').never()
  206. module.export_tar_archive(
  207. dry_run=True,
  208. repository_path='repo',
  209. archive='archive',
  210. paths=None,
  211. destination_path='test.tar',
  212. config={},
  213. local_borg_version='1.2.3',
  214. global_arguments=flexmock(),
  215. )
  216. def test_export_tar_archive_calls_borg_with_tar_filter_flags():
  217. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  218. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  219. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  220. ('repo::archive',)
  221. )
  222. insert_execute_command_mock(
  223. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar')
  224. )
  225. module.export_tar_archive(
  226. dry_run=False,
  227. repository_path='repo',
  228. archive='archive',
  229. paths=None,
  230. destination_path='test.tar',
  231. config={},
  232. local_borg_version='1.2.3',
  233. global_arguments=flexmock(),
  234. tar_filter='bzip2',
  235. )
  236. def test_export_tar_archive_calls_borg_with_list_flag():
  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', '--list', 'repo::archive', 'test.tar'),
  244. output_log_level=logging.ANSWER,
  245. )
  246. module.export_tar_archive(
  247. dry_run=False,
  248. repository_path='repo',
  249. archive='archive',
  250. paths=None,
  251. destination_path='test.tar',
  252. config={'list_details': True},
  253. local_borg_version='1.2.3',
  254. global_arguments=flexmock(),
  255. )
  256. def test_export_tar_archive_calls_borg_with_strip_components_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', '--strip-components', '5', 'repo::archive', 'test.tar')
  264. )
  265. module.export_tar_archive(
  266. dry_run=False,
  267. repository_path='repo',
  268. archive='archive',
  269. paths=None,
  270. destination_path='test.tar',
  271. config={},
  272. local_borg_version='1.2.3',
  273. global_arguments=flexmock(),
  274. strip_components=5,
  275. )
  276. def test_export_tar_archive_skips_abspath_for_remote_repository_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. ('server:repo::archive',)
  281. )
  282. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  283. module.export_tar_archive(
  284. dry_run=False,
  285. repository_path='server:repo',
  286. archive='archive',
  287. paths=None,
  288. destination_path='test.tar',
  289. config={},
  290. local_borg_version='1.2.3',
  291. global_arguments=flexmock(),
  292. )
  293. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  294. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  295. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  296. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  297. ('repo::archive',)
  298. )
  299. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  300. module.export_tar_archive(
  301. dry_run=False,
  302. repository_path='repo',
  303. archive='archive',
  304. paths=None,
  305. destination_path='-',
  306. config={},
  307. local_borg_version='1.2.3',
  308. global_arguments=flexmock(),
  309. )
  310. def test_export_tar_archive_calls_borg_with_working_directory():
  311. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  312. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  313. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  314. ('repo::archive',)
  315. )
  316. insert_execute_command_mock(
  317. ('borg', 'export-tar', 'repo::archive', 'test.tar'),
  318. working_directory='/working/dir',
  319. )
  320. module.export_tar_archive(
  321. dry_run=False,
  322. repository_path='repo',
  323. archive='archive',
  324. paths=[],
  325. destination_path='test.tar',
  326. config={'working_directory': '/working/dir'},
  327. local_borg_version='1.2.3',
  328. global_arguments=flexmock(),
  329. )