test_export_tar.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  23. insert_execute_command_mock(
  24. ('borg', 'export-tar', 'repo::archive', 'test.tar', 'path1', 'path2')
  25. )
  26. module.export_tar_archive(
  27. dry_run=False,
  28. repository='repo',
  29. archive='archive',
  30. paths=['path1', 'path2'],
  31. destination_path='test.tar',
  32. storage_config={},
  33. local_borg_version='1.2.3',
  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. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  42. insert_execute_command_mock(
  43. ('borg1', 'export-tar', 'repo::archive', 'test.tar'), borg_local_path='borg1'
  44. )
  45. module.export_tar_archive(
  46. dry_run=False,
  47. repository='repo',
  48. archive='archive',
  49. paths=None,
  50. destination_path='test.tar',
  51. storage_config={},
  52. local_borg_version='1.2.3',
  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. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  62. insert_execute_command_mock(
  63. ('borg', 'export-tar', '--remote-path', 'borg1', 'repo::archive', 'test.tar')
  64. )
  65. module.export_tar_archive(
  66. dry_run=False,
  67. repository='repo',
  68. archive='archive',
  69. paths=None,
  70. destination_path='test.tar',
  71. storage_config={},
  72. local_borg_version='1.2.3',
  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. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  82. insert_execute_command_mock(
  83. ('borg', 'export-tar', '--umask', '0770', 'repo::archive', 'test.tar')
  84. )
  85. module.export_tar_archive(
  86. dry_run=False,
  87. repository='repo',
  88. archive='archive',
  89. paths=None,
  90. destination_path='test.tar',
  91. storage_config={'umask': '0770'},
  92. local_borg_version='1.2.3',
  93. )
  94. def test_export_tar_archive_calls_borg_with_lock_wait_parameters():
  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. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  101. insert_execute_command_mock(
  102. ('borg', 'export-tar', '--lock-wait', '5', 'repo::archive', 'test.tar')
  103. )
  104. module.export_tar_archive(
  105. dry_run=False,
  106. repository='repo',
  107. archive='archive',
  108. paths=None,
  109. destination_path='test.tar',
  110. storage_config={'lock_wait': '5'},
  111. local_borg_version='1.2.3',
  112. )
  113. def test_export_tar_archive_with_log_info_calls_borg_with_info_parameter():
  114. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  115. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  116. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  117. ('repo::archive',)
  118. )
  119. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  120. insert_execute_command_mock(('borg', 'export-tar', '--info', 'repo::archive', 'test.tar'))
  121. insert_logging_mock(logging.INFO)
  122. module.export_tar_archive(
  123. dry_run=False,
  124. repository='repo',
  125. archive='archive',
  126. paths=None,
  127. destination_path='test.tar',
  128. storage_config={},
  129. local_borg_version='1.2.3',
  130. )
  131. def test_export_tar_archive_with_log_debug_calls_borg_with_debug_parameters():
  132. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  133. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  134. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  135. ('repo::archive',)
  136. )
  137. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  138. insert_execute_command_mock(
  139. ('borg', 'export-tar', '--debug', '--show-rc', 'repo::archive', 'test.tar')
  140. )
  141. insert_logging_mock(logging.DEBUG)
  142. module.export_tar_archive(
  143. dry_run=False,
  144. repository='repo',
  145. archive='archive',
  146. paths=None,
  147. destination_path='test.tar',
  148. storage_config={},
  149. local_borg_version='1.2.3',
  150. )
  151. def test_export_tar_archive_calls_borg_with_dry_run_parameter():
  152. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  153. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  154. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  155. ('repo::archive',)
  156. )
  157. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  158. flexmock(module).should_receive('execute_command').never()
  159. module.export_tar_archive(
  160. dry_run=True,
  161. repository='repo',
  162. archive='archive',
  163. paths=None,
  164. destination_path='test.tar',
  165. storage_config={},
  166. local_borg_version='1.2.3',
  167. )
  168. def test_export_tar_archive_calls_borg_with_tar_filter_parameters():
  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.os.path).should_receive('abspath').and_return('repo')
  175. insert_execute_command_mock(
  176. ('borg', 'export-tar', '--tar-filter', 'bzip2', 'repo::archive', 'test.tar')
  177. )
  178. module.export_tar_archive(
  179. dry_run=False,
  180. repository='repo',
  181. archive='archive',
  182. paths=None,
  183. destination_path='test.tar',
  184. storage_config={},
  185. local_borg_version='1.2.3',
  186. tar_filter='bzip2',
  187. )
  188. def test_export_tar_archive_calls_borg_with_list_parameter():
  189. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  190. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  191. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  192. ('repo::archive',)
  193. )
  194. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  195. insert_execute_command_mock(
  196. ('borg', 'export-tar', '--list', 'repo::archive', 'test.tar'),
  197. output_log_level=logging.ANSWER,
  198. )
  199. module.export_tar_archive(
  200. dry_run=False,
  201. repository='repo',
  202. archive='archive',
  203. paths=None,
  204. destination_path='test.tar',
  205. storage_config={},
  206. local_borg_version='1.2.3',
  207. list_files=True,
  208. )
  209. def test_export_tar_archive_calls_borg_with_strip_components_parameter():
  210. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  211. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  212. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  213. ('repo::archive',)
  214. )
  215. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  216. insert_execute_command_mock(
  217. ('borg', 'export-tar', '--strip-components', '5', 'repo::archive', 'test.tar')
  218. )
  219. module.export_tar_archive(
  220. dry_run=False,
  221. repository='repo',
  222. archive='archive',
  223. paths=None,
  224. destination_path='test.tar',
  225. storage_config={},
  226. local_borg_version='1.2.3',
  227. strip_components=5,
  228. )
  229. def test_export_tar_archive_skips_abspath_for_remote_repository_parameter():
  230. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  231. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  232. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  233. ('server:repo::archive',)
  234. )
  235. flexmock(module.os.path).should_receive('abspath').never()
  236. insert_execute_command_mock(('borg', 'export-tar', 'server:repo::archive', 'test.tar'))
  237. module.export_tar_archive(
  238. dry_run=False,
  239. repository='server:repo',
  240. archive='archive',
  241. paths=None,
  242. destination_path='test.tar',
  243. storage_config={},
  244. local_borg_version='1.2.3',
  245. )
  246. def test_export_tar_archive_calls_borg_with_stdout_destination_path():
  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. ('repo::archive',)
  251. )
  252. flexmock(module.os.path).should_receive('abspath').and_return('repo')
  253. insert_execute_command_mock(('borg', 'export-tar', 'repo::archive', '-'), capture=False)
  254. module.export_tar_archive(
  255. dry_run=False,
  256. repository='repo',
  257. archive='archive',
  258. paths=None,
  259. destination_path='-',
  260. storage_config={},
  261. local_borg_version='1.2.3',
  262. )