test_export_tar.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. borg_local_path='borg',
  9. borg_exit_codes=None,
  10. capture=True,
  11. ):
  12. flexmock(module.environment).should_receive('make_environment')
  13. flexmock(module).should_receive('execute_command').with_args(
  14. command,
  15. output_file=None if capture else module.DO_NOT_CAPTURE,
  16. output_log_level=output_log_level,
  17. borg_local_path=borg_local_path,
  18. borg_exit_codes=borg_exit_codes,
  19. extra_environment=None,
  20. ).once()
  21. def test_export_tar_archive_calls_borg_with_path_flags():
  22. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  23. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  24. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  25. ('repo::archive',)
  26. )
  27. insert_execute_command_mock(
  28. ('borg', 'export-tar', 'repo::archive', 'test.tar', 'path1', 'path2')
  29. )
  30. module.export_tar_archive(
  31. dry_run=False,
  32. repository_path='repo',
  33. archive='archive',
  34. paths=['path1', 'path2'],
  35. destination_path='test.tar',
  36. config={},
  37. local_borg_version='1.2.3',
  38. global_arguments=flexmock(log_json=False),
  39. )
  40. def test_export_tar_archive_calls_borg_with_local_path_flags():
  41. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  42. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  43. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  44. ('repo::archive',)
  45. )
  46. insert_execute_command_mock(
  47. ('borg1', 'export-tar', 'repo::archive', 'test.tar'), borg_local_path='borg1'
  48. )
  49. module.export_tar_archive(
  50. dry_run=False,
  51. repository_path='repo',
  52. archive='archive',
  53. paths=None,
  54. destination_path='test.tar',
  55. config={},
  56. local_borg_version='1.2.3',
  57. global_arguments=flexmock(log_json=False),
  58. local_path='borg1',
  59. )
  60. def test_export_tar_archive_calls_borg_using_exit_codes():
  61. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  62. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  63. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  64. ('repo::archive',)
  65. )
  66. borg_exit_codes = flexmock()
  67. insert_execute_command_mock(
  68. ('borg', 'export-tar', 'repo::archive', 'test.tar'),
  69. borg_exit_codes=borg_exit_codes,
  70. )
  71. module.export_tar_archive(
  72. dry_run=False,
  73. repository_path='repo',
  74. archive='archive',
  75. paths=None,
  76. destination_path='test.tar',
  77. config={'borg_exit_codes': borg_exit_codes},
  78. local_borg_version='1.2.3',
  79. global_arguments=flexmock(log_json=False),
  80. )
  81. def test_export_tar_archive_calls_borg_with_remote_path_flags():
  82. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  83. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  84. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  85. ('repo::archive',)
  86. )
  87. insert_execute_command_mock(
  88. ('borg', 'export-tar', '--remote-path', 'borg1', 'repo::archive', 'test.tar')
  89. )
  90. module.export_tar_archive(
  91. dry_run=False,
  92. repository_path='repo',
  93. archive='archive',
  94. paths=None,
  95. destination_path='test.tar',
  96. config={},
  97. local_borg_version='1.2.3',
  98. global_arguments=flexmock(log_json=False),
  99. remote_path='borg1',
  100. )
  101. def test_export_tar_archive_calls_borg_with_umask_flags():
  102. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  103. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  104. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  105. ('repo::archive',)
  106. )
  107. insert_execute_command_mock(
  108. ('borg', 'export-tar', '--umask', '0770', 'repo::archive', 'test.tar')
  109. )
  110. module.export_tar_archive(
  111. dry_run=False,
  112. repository_path='repo',
  113. archive='archive',
  114. paths=None,
  115. destination_path='test.tar',
  116. config={'umask': '0770'},
  117. local_borg_version='1.2.3',
  118. global_arguments=flexmock(log_json=False),
  119. )
  120. def test_export_tar_archive_calls_borg_with_log_json_parameter():
  121. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  122. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  123. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  124. ('repo::archive',)
  125. )
  126. insert_execute_command_mock(('borg', 'export-tar', '--log-json', 'repo::archive', 'test.tar'))
  127. module.export_tar_archive(
  128. dry_run=False,
  129. repository_path='repo',
  130. archive='archive',
  131. paths=None,
  132. destination_path='test.tar',
  133. config={},
  134. local_borg_version='1.2.3',
  135. global_arguments=flexmock(log_json=True),
  136. )
  137. def test_export_tar_archive_calls_borg_with_lock_wait_flags():
  138. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  139. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  140. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  141. ('repo::archive',)
  142. )
  143. insert_execute_command_mock(
  144. ('borg', 'export-tar', '--lock-wait', '5', 'repo::archive', 'test.tar')
  145. )
  146. module.export_tar_archive(
  147. dry_run=False,
  148. repository_path='repo',
  149. archive='archive',
  150. paths=None,
  151. destination_path='test.tar',
  152. config={'lock_wait': '5'},
  153. local_borg_version='1.2.3',
  154. global_arguments=flexmock(log_json=False),
  155. )
  156. def test_export_tar_archive_with_log_info_calls_borg_with_info_parameter():
  157. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  158. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  159. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  160. ('repo::archive',)
  161. )
  162. insert_execute_command_mock(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  163. insert_logging_mock(logging.INFO)
  164. module.export_tar_archive(
  165. dry_run=False,
  166. repository_path='repo',
  167. archive='archive',
  168. paths=None,
  169. destination_path='test.tar',
  170. config={},
  171. local_borg_version='1.2.3',
  172. global_arguments=flexmock(log_json=False),
  173. )
  174. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_flags():
  175. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  176. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  177. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  178. ('repo::archive',)
  179. )
  180. insert_execute_command_mock(
  181. ('borg', 'export-tar', '--debug', '--show-rc', 'repo::archive', 'test.tar')
  182. )
  183. insert_logging_mock(logging.DEBUG)
  184. module.export_tar_archive(
  185. dry_run=False,
  186. repository_path='repo',
  187. archive='archive',
  188. paths=None,
  189. destination_path='test.tar',
  190. config={},
  191. local_borg_version='1.2.3',
  192. global_arguments=flexmock(log_json=False),
  193. )
  194. def test_export_tar_archive_calls_borg_with_dry_run_parameter():
  195. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  196. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  197. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  198. ('repo::archive',)
  199. )
  200. flexmock(module).should_receive('execute_command').never()
  201. module.export_tar_archive(
  202. dry_run=True,
  203. repository_path='repo',
  204. archive='archive',
  205. paths=None,
  206. destination_path='test.tar',
  207. config={},
  208. local_borg_version='1.2.3',
  209. global_arguments=flexmock(log_json=False),
  210. )
  211. def test_export_tar_archive_calls_borg_with_tar_filter_flags():
  212. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  213. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  214. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  215. ('repo::archive',)
  216. )
  217. insert_execute_command_mock(
  218. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar')
  219. )
  220. module.export_tar_archive(
  221. dry_run=False,
  222. repository_path='repo',
  223. archive='archive',
  224. paths=None,
  225. destination_path='test.tar',
  226. config={},
  227. local_borg_version='1.2.3',
  228. global_arguments=flexmock(log_json=False),
  229. tar_filter='bzip2',
  230. )
  231. def test_export_tar_archive_calls_borg_with_list_parameter():
  232. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  233. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  234. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  235. ('repo::archive',)
  236. )
  237. insert_execute_command_mock(
  238. ('borg', 'export-tar', '--list', 'repo::archive', 'test.tar'),
  239. output_log_level=logging.ANSWER,
  240. )
  241. module.export_tar_archive(
  242. dry_run=False,
  243. repository_path='repo',
  244. archive='archive',
  245. paths=None,
  246. destination_path='test.tar',
  247. config={},
  248. local_borg_version='1.2.3',
  249. global_arguments=flexmock(log_json=False),
  250. list_files=True,
  251. )
  252. def test_export_tar_archive_calls_borg_with_strip_components_parameter():
  253. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  254. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  255. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  256. ('repo::archive',)
  257. )
  258. insert_execute_command_mock(
  259. ('borg', 'export-tar', '--strip-components', '5', 'repo::archive', 'test.tar')
  260. )
  261. module.export_tar_archive(
  262. dry_run=False,
  263. repository_path='repo',
  264. archive='archive',
  265. paths=None,
  266. destination_path='test.tar',
  267. config={},
  268. local_borg_version='1.2.3',
  269. global_arguments=flexmock(log_json=False),
  270. strip_components=5,
  271. )
  272. def test_export_tar_archive_skips_abspath_for_remote_repository_parameter():
  273. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  274. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  275. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  276. ('server:repo::archive',)
  277. )
  278. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  279. module.export_tar_archive(
  280. dry_run=False,
  281. repository_path='server:repo',
  282. archive='archive',
  283. paths=None,
  284. destination_path='test.tar',
  285. config={},
  286. local_borg_version='1.2.3',
  287. global_arguments=flexmock(log_json=False),
  288. )
  289. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  290. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  291. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  292. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  293. ('repo::archive',)
  294. )
  295. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  296. module.export_tar_archive(
  297. dry_run=False,
  298. repository_path='repo',
  299. archive='archive',
  300. paths=None,
  301. destination_path='-',
  302. config={},
  303. local_borg_version='1.2.3',
  304. global_arguments=flexmock(log_json=False),
  305. )