test_export_tar.py 11 KB

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