test_export_tar.py 13 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. extra_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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  124. )
  125. def test_export_tar_archive_calls_borg_with_log_json_parameter():
  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={},
  139. local_borg_version='1.2.3',
  140. global_arguments=flexmock(log_json=True),
  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(log_json=False),
  160. )
  161. def test_export_tar_archive_with_log_info_calls_borg_with_info_parameter():
  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(log_json=False),
  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(log_json=False),
  198. )
  199. def test_export_tar_archive_calls_borg_with_dry_run_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. 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(log_json=False),
  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(log_json=False),
  234. tar_filter='bzip2',
  235. )
  236. def test_export_tar_archive_calls_borg_with_list_parameter():
  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={},
  253. local_borg_version='1.2.3',
  254. global_arguments=flexmock(log_json=False),
  255. list_files=True,
  256. )
  257. def test_export_tar_archive_calls_borg_with_strip_components_parameter():
  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(log_json=False),
  275. strip_components=5,
  276. )
  277. def test_export_tar_archive_skips_abspath_for_remote_repository_parameter():
  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(log_json=False),
  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(log_json=False),
  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(log_json=False),
  330. )