test_export_tar.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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_with_log_info_calls_borg_with_info_flag():
  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(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  169. insert_logging_mock(logging.INFO)
  170. module.export_tar_archive(
  171. dry_run=False,
  172. repository_path='repo',
  173. archive='archive',
  174. paths=None,
  175. destination_path='test.tar',
  176. config={},
  177. local_borg_version='1.2.3',
  178. global_arguments=flexmock(),
  179. )
  180. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_flags():
  181. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  182. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  183. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  184. ('repo::archive',),
  185. )
  186. insert_execute_command_mock(
  187. ('borg', 'export-tar', '--debug', '--show-rc', 'repo::archive', 'test.tar'),
  188. )
  189. insert_logging_mock(logging.DEBUG)
  190. module.export_tar_archive(
  191. dry_run=False,
  192. repository_path='repo',
  193. archive='archive',
  194. paths=None,
  195. destination_path='test.tar',
  196. config={},
  197. local_borg_version='1.2.3',
  198. global_arguments=flexmock(),
  199. )
  200. def test_export_tar_archive_calls_borg_with_dry_run_flag():
  201. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  202. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  203. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  204. ('repo::archive',),
  205. )
  206. flexmock(module).should_receive('execute_command').never()
  207. module.export_tar_archive(
  208. dry_run=True,
  209. repository_path='repo',
  210. archive='archive',
  211. paths=None,
  212. destination_path='test.tar',
  213. config={},
  214. local_borg_version='1.2.3',
  215. global_arguments=flexmock(),
  216. )
  217. def test_export_tar_archive_calls_borg_with_tar_filter_flags():
  218. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  219. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  220. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  221. ('repo::archive',),
  222. )
  223. insert_execute_command_mock(
  224. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar'),
  225. )
  226. module.export_tar_archive(
  227. dry_run=False,
  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. tar_filter='bzip2',
  236. )
  237. def test_export_tar_archive_calls_borg_with_list_flag():
  238. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  239. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  240. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  241. ('repo::archive',),
  242. )
  243. insert_execute_command_mock(
  244. ('borg', 'export-tar', '--list', 'repo::archive', 'test.tar'),
  245. output_log_level=logging.ANSWER,
  246. )
  247. module.export_tar_archive(
  248. dry_run=False,
  249. repository_path='repo',
  250. archive='archive',
  251. paths=None,
  252. destination_path='test.tar',
  253. config={'list_details': True},
  254. local_borg_version='1.2.3',
  255. global_arguments=flexmock(),
  256. )
  257. def test_export_tar_archive_calls_borg_with_strip_components_flag():
  258. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  259. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  260. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  261. ('repo::archive',),
  262. )
  263. insert_execute_command_mock(
  264. ('borg', 'export-tar', '--strip-components', '5', 'repo::archive', 'test.tar'),
  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={},
  273. local_borg_version='1.2.3',
  274. global_arguments=flexmock(),
  275. strip_components=5,
  276. )
  277. def test_export_tar_archive_skips_abspath_for_remote_repository_flag():
  278. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  279. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  280. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  281. ('server:repo::archive',),
  282. )
  283. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  284. module.export_tar_archive(
  285. dry_run=False,
  286. repository_path='server:repo',
  287. archive='archive',
  288. paths=None,
  289. destination_path='test.tar',
  290. config={},
  291. local_borg_version='1.2.3',
  292. global_arguments=flexmock(),
  293. )
  294. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  295. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  296. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  297. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  298. ('repo::archive',),
  299. )
  300. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  301. module.export_tar_archive(
  302. dry_run=False,
  303. repository_path='repo',
  304. archive='archive',
  305. paths=None,
  306. destination_path='-',
  307. config={},
  308. local_borg_version='1.2.3',
  309. global_arguments=flexmock(),
  310. )
  311. def test_export_tar_archive_calls_borg_with_working_directory():
  312. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  313. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  314. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  315. ('repo::archive',),
  316. )
  317. insert_execute_command_mock(
  318. ('borg', 'export-tar', 'repo::archive', 'test.tar'),
  319. working_directory='/working/dir',
  320. )
  321. module.export_tar_archive(
  322. dry_run=False,
  323. repository_path='repo',
  324. archive='archive',
  325. paths=[],
  326. destination_path='test.tar',
  327. config={'working_directory': '/working/dir'},
  328. local_borg_version='1.2.3',
  329. global_arguments=flexmock(),
  330. )