test_create.py 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import create as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_expand_directory_with_basic_path_passes_it_through():
  7. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  8. flexmock(module.glob).should_receive('glob').and_return([])
  9. paths = module._expand_directory('foo')
  10. assert paths == ['foo']
  11. def test_expand_directory_with_glob_expands():
  12. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  13. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  14. paths = module._expand_directory('foo*')
  15. assert paths == ['foo', 'food']
  16. def test_expand_directories_flattens_expanded_directories():
  17. flexmock(module).should_receive('_expand_directory').with_args('~/foo').and_return(
  18. ['/root/foo']
  19. )
  20. flexmock(module).should_receive('_expand_directory').with_args('bar*').and_return(
  21. ['bar', 'barf']
  22. )
  23. paths = module._expand_directories(('~/foo', 'bar*'))
  24. assert paths == ('/root/foo', 'bar', 'barf')
  25. def test_expand_directories_considers_none_as_no_directories():
  26. paths = module._expand_directories(None)
  27. assert paths == ()
  28. def test_expand_home_directories_expands_tildes():
  29. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  30. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  31. paths = module._expand_home_directories(('~/bar', 'baz'))
  32. assert paths == ('/foo/bar', 'baz')
  33. def test_expand_home_directories_considers_none_as_no_directories():
  34. paths = module._expand_home_directories(None)
  35. assert paths == ()
  36. @pytest.mark.parametrize(
  37. 'directories,expected_directories',
  38. (
  39. (('/', '/root'), ('/',)),
  40. (('/', '/root/'), ('/',)),
  41. (('/root', '/'), ('/',)),
  42. (('/root', '/root/foo'), ('/root',)),
  43. (('/root/', '/root/foo'), ('/root/',)),
  44. (('/root', '/root/foo/'), ('/root',)),
  45. (('/root/foo', '/root'), ('/root',)),
  46. (('/root', '/etc', '/root/foo/bar'), ('/etc', '/root')),
  47. (('/root', '/root/foo', '/root/foo/bar'), ('/root',)),
  48. (('/dup', '/dup'), ('/dup',)),
  49. (('/foo', '/bar'), ('/bar', '/foo')),
  50. ),
  51. )
  52. def test_deduplicate_directories_removes_child_paths(directories, expected_directories):
  53. assert module.deduplicate_directories(directories) == expected_directories
  54. def test_write_pattern_file_does_not_raise():
  55. temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
  56. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  57. module._write_pattern_file(['exclude'])
  58. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  59. module._write_pattern_file([])
  60. def test_make_pattern_flags_includes_pattern_filename_when_given():
  61. pattern_flags = module._make_pattern_flags(
  62. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  63. )
  64. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  65. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  66. pattern_flags = module._make_pattern_flags(
  67. location_config={'patterns_from': ['patterns', 'other']}
  68. )
  69. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  70. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  71. pattern_flags = module._make_pattern_flags(
  72. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  73. )
  74. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  75. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  76. pattern_flags = module._make_pattern_flags(location_config={'patterns_from': None})
  77. assert pattern_flags == ()
  78. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  79. exclude_flags = module._make_exclude_flags(
  80. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  81. )
  82. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  83. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  84. exclude_flags = module._make_exclude_flags(
  85. location_config={'exclude_from': ['excludes', 'other']}
  86. )
  87. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  88. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  89. exclude_flags = module._make_exclude_flags(
  90. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  91. )
  92. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  93. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  94. exclude_flags = module._make_exclude_flags(location_config={'exclude_from': None})
  95. assert exclude_flags == ()
  96. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  97. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': True})
  98. assert exclude_flags == ('--exclude-caches',)
  99. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  100. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': False})
  101. assert exclude_flags == ()
  102. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  103. exclude_flags = module._make_exclude_flags(
  104. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  105. )
  106. assert exclude_flags == (
  107. '--exclude-if-present',
  108. 'exclude_me',
  109. '--exclude-if-present',
  110. 'also_me',
  111. )
  112. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  113. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': True})
  114. assert exclude_flags == ('--keep-exclude-tags',)
  115. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  116. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': False})
  117. assert exclude_flags == ()
  118. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  119. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': True})
  120. assert exclude_flags == ('--exclude-nodump',)
  121. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  122. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': False})
  123. assert exclude_flags == ()
  124. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  125. exclude_flags = module._make_exclude_flags(location_config={})
  126. assert exclude_flags == ()
  127. def test_borgmatic_source_directories_set_when_directory_exists():
  128. flexmock(module.os.path).should_receive('exists').and_return(True)
  129. flexmock(module.os.path).should_receive('expanduser')
  130. assert module.borgmatic_source_directories('/tmp') == ['/tmp']
  131. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  132. flexmock(module.os.path).should_receive('exists').and_return(False)
  133. flexmock(module.os.path).should_receive('expanduser')
  134. assert module.borgmatic_source_directories('/tmp') == []
  135. def test_borgmatic_source_directories_defaults_when_directory_not_given():
  136. flexmock(module.os.path).should_receive('exists').and_return(True)
  137. flexmock(module.os.path).should_receive('expanduser')
  138. assert module.borgmatic_source_directories(None) == [module.DEFAULT_BORGMATIC_SOURCE_DIRECTORY]
  139. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  140. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  141. def test_create_archive_calls_borg_with_parameters():
  142. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  143. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  144. flexmock(module).should_receive('_expand_directories').and_return(())
  145. flexmock(module).should_receive('_expand_home_directories').and_return(())
  146. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  147. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  148. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  149. flexmock(module).should_receive('execute_command').with_args(
  150. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  151. output_log_level=logging.INFO,
  152. output_file=None,
  153. borg_local_path='borg',
  154. )
  155. module.create_archive(
  156. dry_run=False,
  157. repository='repo',
  158. location_config={
  159. 'source_directories': ['foo', 'bar'],
  160. 'repositories': ['repo'],
  161. 'exclude_patterns': None,
  162. },
  163. storage_config={},
  164. )
  165. def test_create_archive_with_patterns_calls_borg_with_patterns():
  166. pattern_flags = ('--patterns-from', 'patterns')
  167. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  168. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  169. flexmock(module).should_receive('_expand_directories').and_return(())
  170. flexmock(module).should_receive('_expand_home_directories').and_return(())
  171. flexmock(module).should_receive('_write_pattern_file').and_return(
  172. flexmock(name='/tmp/patterns')
  173. ).and_return(None)
  174. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  175. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  176. flexmock(module).should_receive('execute_command').with_args(
  177. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  178. output_log_level=logging.INFO,
  179. output_file=None,
  180. borg_local_path='borg',
  181. )
  182. module.create_archive(
  183. dry_run=False,
  184. repository='repo',
  185. location_config={
  186. 'source_directories': ['foo', 'bar'],
  187. 'repositories': ['repo'],
  188. 'patterns': ['pattern'],
  189. },
  190. storage_config={},
  191. )
  192. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  193. exclude_flags = ('--exclude-from', 'excludes')
  194. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  195. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  196. flexmock(module).should_receive('_expand_directories').and_return(())
  197. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  198. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  199. flexmock(name='/tmp/excludes')
  200. )
  201. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  202. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  203. flexmock(module).should_receive('execute_command').with_args(
  204. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  205. output_log_level=logging.INFO,
  206. output_file=None,
  207. borg_local_path='borg',
  208. )
  209. module.create_archive(
  210. dry_run=False,
  211. repository='repo',
  212. location_config={
  213. 'source_directories': ['foo', 'bar'],
  214. 'repositories': ['repo'],
  215. 'exclude_patterns': ['exclude'],
  216. },
  217. storage_config={},
  218. )
  219. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  220. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  221. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  222. flexmock(module).should_receive('_expand_directories').and_return(())
  223. flexmock(module).should_receive('_expand_home_directories').and_return(())
  224. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  225. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  226. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  227. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  228. flexmock(module).should_receive('execute_command').with_args(
  229. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  230. output_log_level=logging.INFO,
  231. output_file=None,
  232. borg_local_path='borg',
  233. )
  234. insert_logging_mock(logging.INFO)
  235. module.create_archive(
  236. dry_run=False,
  237. repository='repo',
  238. location_config={
  239. 'source_directories': ['foo', 'bar'],
  240. 'repositories': ['repo'],
  241. 'exclude_patterns': None,
  242. },
  243. storage_config={},
  244. )
  245. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  246. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  247. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  248. flexmock(module).should_receive('_expand_directories').and_return(())
  249. flexmock(module).should_receive('_expand_home_directories').and_return(())
  250. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  251. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  252. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  253. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  254. flexmock(module).should_receive('execute_command').with_args(
  255. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  256. output_log_level=None,
  257. output_file=None,
  258. borg_local_path='borg',
  259. )
  260. insert_logging_mock(logging.INFO)
  261. module.create_archive(
  262. dry_run=False,
  263. repository='repo',
  264. location_config={
  265. 'source_directories': ['foo', 'bar'],
  266. 'repositories': ['repo'],
  267. 'exclude_patterns': None,
  268. },
  269. storage_config={},
  270. json=True,
  271. )
  272. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  273. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  274. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  275. flexmock(module).should_receive('_expand_directories').and_return(())
  276. flexmock(module).should_receive('_expand_home_directories').and_return(())
  277. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  278. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  279. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  280. flexmock(module).should_receive('execute_command').with_args(
  281. ('borg', 'create', '--debug', '--show-rc') + ARCHIVE_WITH_PATHS,
  282. output_log_level=logging.INFO,
  283. output_file=None,
  284. borg_local_path='borg',
  285. )
  286. insert_logging_mock(logging.DEBUG)
  287. module.create_archive(
  288. dry_run=False,
  289. repository='repo',
  290. location_config={
  291. 'source_directories': ['foo', 'bar'],
  292. 'repositories': ['repo'],
  293. 'exclude_patterns': None,
  294. },
  295. storage_config={},
  296. )
  297. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  298. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  299. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  300. flexmock(module).should_receive('_expand_directories').and_return(())
  301. flexmock(module).should_receive('_expand_home_directories').and_return(())
  302. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  303. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  304. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  305. flexmock(module).should_receive('execute_command').with_args(
  306. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  307. output_log_level=None,
  308. output_file=None,
  309. borg_local_path='borg',
  310. )
  311. insert_logging_mock(logging.DEBUG)
  312. module.create_archive(
  313. dry_run=False,
  314. repository='repo',
  315. location_config={
  316. 'source_directories': ['foo', 'bar'],
  317. 'repositories': ['repo'],
  318. 'exclude_patterns': None,
  319. },
  320. storage_config={},
  321. json=True,
  322. )
  323. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  324. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  325. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  326. flexmock(module).should_receive('_expand_directories').and_return(())
  327. flexmock(module).should_receive('_expand_home_directories').and_return(())
  328. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  329. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  330. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  331. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  332. flexmock(module).should_receive('execute_command').with_args(
  333. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  334. output_log_level=logging.INFO,
  335. output_file=None,
  336. borg_local_path='borg',
  337. )
  338. module.create_archive(
  339. dry_run=True,
  340. repository='repo',
  341. location_config={
  342. 'source_directories': ['foo', 'bar'],
  343. 'repositories': ['repo'],
  344. 'exclude_patterns': None,
  345. },
  346. storage_config={},
  347. )
  348. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  349. # --dry-run and --stats are mutually exclusive, see:
  350. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  351. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  352. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  353. flexmock(module).should_receive('_expand_directories').and_return(())
  354. flexmock(module).should_receive('_expand_home_directories').and_return(())
  355. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  356. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  357. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  358. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  359. flexmock(module).should_receive('execute_command').with_args(
  360. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  361. output_log_level=logging.INFO,
  362. output_file=None,
  363. borg_local_path='borg',
  364. )
  365. insert_logging_mock(logging.INFO)
  366. module.create_archive(
  367. dry_run=True,
  368. repository='repo',
  369. location_config={
  370. 'source_directories': ['foo', 'bar'],
  371. 'repositories': ['repo'],
  372. 'exclude_patterns': None,
  373. },
  374. storage_config={},
  375. stats=True,
  376. )
  377. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  378. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  379. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  380. flexmock(module).should_receive('_expand_directories').and_return(())
  381. flexmock(module).should_receive('_expand_home_directories').and_return(())
  382. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  383. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  384. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  385. flexmock(module).should_receive('execute_command').with_args(
  386. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  387. output_log_level=logging.INFO,
  388. output_file=None,
  389. borg_local_path='borg',
  390. )
  391. module.create_archive(
  392. dry_run=False,
  393. repository='repo',
  394. location_config={
  395. 'source_directories': ['foo', 'bar'],
  396. 'repositories': ['repo'],
  397. 'exclude_patterns': None,
  398. },
  399. storage_config={'checkpoint_interval': 600},
  400. )
  401. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  402. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  403. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  404. flexmock(module).should_receive('_expand_directories').and_return(())
  405. flexmock(module).should_receive('_expand_home_directories').and_return(())
  406. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  407. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  408. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  409. flexmock(module).should_receive('execute_command').with_args(
  410. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  411. output_log_level=logging.INFO,
  412. output_file=None,
  413. borg_local_path='borg',
  414. )
  415. module.create_archive(
  416. dry_run=False,
  417. repository='repo',
  418. location_config={
  419. 'source_directories': ['foo', 'bar'],
  420. 'repositories': ['repo'],
  421. 'exclude_patterns': None,
  422. },
  423. storage_config={'chunker_params': '1,2,3,4'},
  424. )
  425. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  426. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  427. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  428. flexmock(module).should_receive('_expand_directories').and_return(())
  429. flexmock(module).should_receive('_expand_home_directories').and_return(())
  430. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  431. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  432. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  433. flexmock(module).should_receive('execute_command').with_args(
  434. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  435. output_log_level=logging.INFO,
  436. output_file=None,
  437. borg_local_path='borg',
  438. )
  439. module.create_archive(
  440. dry_run=False,
  441. repository='repo',
  442. location_config={
  443. 'source_directories': ['foo', 'bar'],
  444. 'repositories': ['repo'],
  445. 'exclude_patterns': None,
  446. },
  447. storage_config={'compression': 'rle'},
  448. )
  449. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  450. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  451. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  452. flexmock(module).should_receive('_expand_directories').and_return(())
  453. flexmock(module).should_receive('_expand_home_directories').and_return(())
  454. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  455. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  456. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  457. flexmock(module).should_receive('execute_command').with_args(
  458. ('borg', 'create', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
  459. output_log_level=logging.INFO,
  460. output_file=None,
  461. borg_local_path='borg',
  462. )
  463. module.create_archive(
  464. dry_run=False,
  465. repository='repo',
  466. location_config={
  467. 'source_directories': ['foo', 'bar'],
  468. 'repositories': ['repo'],
  469. 'exclude_patterns': None,
  470. },
  471. storage_config={'remote_rate_limit': 100},
  472. )
  473. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  474. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  475. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  476. flexmock(module).should_receive('_expand_directories').and_return(())
  477. flexmock(module).should_receive('_expand_home_directories').and_return(())
  478. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  479. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  480. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  481. flexmock(module).should_receive('execute_command').with_args(
  482. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  483. output_log_level=logging.INFO,
  484. output_file=None,
  485. borg_local_path='borg',
  486. )
  487. module.create_archive(
  488. dry_run=False,
  489. repository='repo',
  490. location_config={
  491. 'source_directories': ['foo', 'bar'],
  492. 'repositories': ['repo'],
  493. 'one_file_system': True,
  494. 'exclude_patterns': None,
  495. },
  496. storage_config={},
  497. )
  498. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  499. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  500. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  501. flexmock(module).should_receive('_expand_directories').and_return(())
  502. flexmock(module).should_receive('_expand_home_directories').and_return(())
  503. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  504. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  505. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  506. flexmock(module).should_receive('execute_command').with_args(
  507. ('borg', 'create', '--numeric-owner') + ARCHIVE_WITH_PATHS,
  508. output_log_level=logging.INFO,
  509. output_file=None,
  510. borg_local_path='borg',
  511. )
  512. module.create_archive(
  513. dry_run=False,
  514. repository='repo',
  515. location_config={
  516. 'source_directories': ['foo', 'bar'],
  517. 'repositories': ['repo'],
  518. 'numeric_owner': True,
  519. 'exclude_patterns': None,
  520. },
  521. storage_config={},
  522. )
  523. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  524. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  525. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  526. flexmock(module).should_receive('_expand_directories').and_return(())
  527. flexmock(module).should_receive('_expand_home_directories').and_return(())
  528. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  529. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  530. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  531. flexmock(module).should_receive('execute_command').with_args(
  532. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  533. output_log_level=logging.INFO,
  534. output_file=None,
  535. borg_local_path='borg',
  536. )
  537. module.create_archive(
  538. dry_run=False,
  539. repository='repo',
  540. location_config={
  541. 'source_directories': ['foo', 'bar'],
  542. 'repositories': ['repo'],
  543. 'read_special': True,
  544. 'exclude_patterns': None,
  545. },
  546. storage_config={},
  547. )
  548. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  549. def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
  550. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  551. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  552. flexmock(module).should_receive('_expand_directories').and_return(())
  553. flexmock(module).should_receive('_expand_home_directories').and_return(())
  554. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  555. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  556. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  557. flexmock(module).should_receive('execute_command').with_args(
  558. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  559. output_log_level=logging.INFO,
  560. output_file=None,
  561. borg_local_path='borg',
  562. )
  563. module.create_archive(
  564. dry_run=False,
  565. repository='repo',
  566. location_config={
  567. 'source_directories': ['foo', 'bar'],
  568. 'repositories': ['repo'],
  569. option_name: True,
  570. 'exclude_patterns': None,
  571. },
  572. storage_config={},
  573. )
  574. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  575. def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
  576. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  577. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  578. flexmock(module).should_receive('_expand_directories').and_return(())
  579. flexmock(module).should_receive('_expand_home_directories').and_return(())
  580. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  581. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  582. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  583. flexmock(module).should_receive('execute_command').with_args(
  584. ('borg', 'create', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
  585. output_log_level=logging.INFO,
  586. output_file=None,
  587. borg_local_path='borg',
  588. )
  589. module.create_archive(
  590. dry_run=False,
  591. repository='repo',
  592. location_config={
  593. 'source_directories': ['foo', 'bar'],
  594. 'repositories': ['repo'],
  595. option_name: False,
  596. 'exclude_patterns': None,
  597. },
  598. storage_config={},
  599. )
  600. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  601. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  602. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  603. flexmock(module).should_receive('_expand_directories').and_return(())
  604. flexmock(module).should_receive('_expand_home_directories').and_return(())
  605. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  606. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  607. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  608. flexmock(module).should_receive('execute_command').with_args(
  609. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  610. output_log_level=logging.INFO,
  611. output_file=None,
  612. borg_local_path='borg',
  613. )
  614. module.create_archive(
  615. dry_run=False,
  616. repository='repo',
  617. location_config={
  618. 'source_directories': ['foo', 'bar'],
  619. 'repositories': ['repo'],
  620. 'files_cache': 'ctime,size',
  621. 'exclude_patterns': None,
  622. },
  623. storage_config={},
  624. )
  625. def test_create_archive_with_local_path_calls_borg_via_local_path():
  626. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  627. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  628. flexmock(module).should_receive('_expand_directories').and_return(())
  629. flexmock(module).should_receive('_expand_home_directories').and_return(())
  630. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  631. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  632. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  633. flexmock(module).should_receive('execute_command').with_args(
  634. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  635. output_log_level=logging.INFO,
  636. output_file=None,
  637. borg_local_path='borg1',
  638. )
  639. module.create_archive(
  640. dry_run=False,
  641. repository='repo',
  642. location_config={
  643. 'source_directories': ['foo', 'bar'],
  644. 'repositories': ['repo'],
  645. 'exclude_patterns': None,
  646. },
  647. storage_config={},
  648. local_path='borg1',
  649. )
  650. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  651. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  652. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  653. flexmock(module).should_receive('_expand_directories').and_return(())
  654. flexmock(module).should_receive('_expand_home_directories').and_return(())
  655. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  656. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  657. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  658. flexmock(module).should_receive('execute_command').with_args(
  659. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  660. output_log_level=logging.INFO,
  661. output_file=None,
  662. borg_local_path='borg',
  663. )
  664. module.create_archive(
  665. dry_run=False,
  666. repository='repo',
  667. location_config={
  668. 'source_directories': ['foo', 'bar'],
  669. 'repositories': ['repo'],
  670. 'exclude_patterns': None,
  671. },
  672. storage_config={},
  673. remote_path='borg1',
  674. )
  675. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  676. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  677. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  678. flexmock(module).should_receive('_expand_directories').and_return(())
  679. flexmock(module).should_receive('_expand_home_directories').and_return(())
  680. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  681. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  682. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  683. flexmock(module).should_receive('execute_command').with_args(
  684. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS,
  685. output_log_level=logging.INFO,
  686. output_file=None,
  687. borg_local_path='borg',
  688. )
  689. module.create_archive(
  690. dry_run=False,
  691. repository='repo',
  692. location_config={
  693. 'source_directories': ['foo', 'bar'],
  694. 'repositories': ['repo'],
  695. 'exclude_patterns': None,
  696. },
  697. storage_config={'umask': 740},
  698. )
  699. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  700. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  701. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  702. flexmock(module).should_receive('_expand_directories').and_return(())
  703. flexmock(module).should_receive('_expand_home_directories').and_return(())
  704. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  705. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  706. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  707. flexmock(module).should_receive('execute_command').with_args(
  708. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  709. output_log_level=logging.INFO,
  710. output_file=None,
  711. borg_local_path='borg',
  712. )
  713. module.create_archive(
  714. dry_run=False,
  715. repository='repo',
  716. location_config={
  717. 'source_directories': ['foo', 'bar'],
  718. 'repositories': ['repo'],
  719. 'exclude_patterns': None,
  720. },
  721. storage_config={'lock_wait': 5},
  722. )
  723. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  724. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  725. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  726. flexmock(module).should_receive('_expand_directories').and_return(())
  727. flexmock(module).should_receive('_expand_home_directories').and_return(())
  728. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  729. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  730. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  731. flexmock(module).should_receive('execute_command').with_args(
  732. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  733. output_log_level=logging.WARNING,
  734. output_file=None,
  735. borg_local_path='borg',
  736. )
  737. module.create_archive(
  738. dry_run=False,
  739. repository='repo',
  740. location_config={
  741. 'source_directories': ['foo', 'bar'],
  742. 'repositories': ['repo'],
  743. 'exclude_patterns': None,
  744. },
  745. storage_config={},
  746. stats=True,
  747. )
  748. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  749. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  750. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  751. flexmock(module).should_receive('_expand_directories').and_return(())
  752. flexmock(module).should_receive('_expand_home_directories').and_return(())
  753. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  754. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  755. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  756. flexmock(module).should_receive('execute_command').with_args(
  757. ('borg', 'create', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  758. output_log_level=logging.INFO,
  759. output_file=None,
  760. borg_local_path='borg',
  761. )
  762. insert_logging_mock(logging.INFO)
  763. module.create_archive(
  764. dry_run=False,
  765. repository='repo',
  766. location_config={
  767. 'source_directories': ['foo', 'bar'],
  768. 'repositories': ['repo'],
  769. 'exclude_patterns': None,
  770. },
  771. storage_config={},
  772. stats=True,
  773. )
  774. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  775. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  776. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  777. flexmock(module).should_receive('_expand_directories').and_return(())
  778. flexmock(module).should_receive('_expand_home_directories').and_return(())
  779. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  780. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  781. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  782. flexmock(module).should_receive('execute_command').with_args(
  783. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  784. output_log_level=logging.WARNING,
  785. output_file=None,
  786. borg_local_path='borg',
  787. )
  788. module.create_archive(
  789. dry_run=False,
  790. repository='repo',
  791. location_config={
  792. 'source_directories': ['foo', 'bar'],
  793. 'repositories': ['repo'],
  794. 'exclude_patterns': None,
  795. },
  796. storage_config={},
  797. files=True,
  798. )
  799. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  800. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  801. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  802. flexmock(module).should_receive('_expand_directories').and_return(())
  803. flexmock(module).should_receive('_expand_home_directories').and_return(())
  804. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  805. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  806. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  807. flexmock(module).should_receive('execute_command').with_args(
  808. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  809. output_log_level=logging.INFO,
  810. output_file=None,
  811. borg_local_path='borg',
  812. )
  813. insert_logging_mock(logging.INFO)
  814. module.create_archive(
  815. dry_run=False,
  816. repository='repo',
  817. location_config={
  818. 'source_directories': ['foo', 'bar'],
  819. 'repositories': ['repo'],
  820. 'exclude_patterns': None,
  821. },
  822. storage_config={},
  823. files=True,
  824. )
  825. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  826. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  827. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  828. flexmock(module).should_receive('_expand_directories').and_return(())
  829. flexmock(module).should_receive('_expand_home_directories').and_return(())
  830. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  831. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  832. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  833. flexmock(module).should_receive('execute_command').with_args(
  834. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS,
  835. output_log_level=logging.INFO,
  836. output_file=module.DO_NOT_CAPTURE,
  837. borg_local_path='borg',
  838. )
  839. insert_logging_mock(logging.INFO)
  840. module.create_archive(
  841. dry_run=False,
  842. repository='repo',
  843. location_config={
  844. 'source_directories': ['foo', 'bar'],
  845. 'repositories': ['repo'],
  846. 'exclude_patterns': None,
  847. },
  848. storage_config={},
  849. progress=True,
  850. )
  851. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  852. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  853. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  854. flexmock(module).should_receive('_expand_directories').and_return(())
  855. flexmock(module).should_receive('_expand_home_directories').and_return(())
  856. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  857. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  858. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  859. flexmock(module).should_receive('execute_command').with_args(
  860. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS,
  861. output_log_level=logging.INFO,
  862. output_file=module.DO_NOT_CAPTURE,
  863. borg_local_path='borg',
  864. )
  865. module.create_archive(
  866. dry_run=False,
  867. repository='repo',
  868. location_config={
  869. 'source_directories': ['foo', 'bar'],
  870. 'repositories': ['repo'],
  871. 'exclude_patterns': None,
  872. },
  873. storage_config={},
  874. progress=True,
  875. )
  876. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  877. processes = flexmock()
  878. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  879. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  880. flexmock(module).should_receive('_expand_directories').and_return(())
  881. flexmock(module).should_receive('_expand_home_directories').and_return(())
  882. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  883. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  884. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  885. flexmock(module).should_receive('execute_command_with_processes').with_args(
  886. ('borg', 'create', '--read-special', '--progress') + ARCHIVE_WITH_PATHS,
  887. processes=processes,
  888. output_log_level=logging.INFO,
  889. output_file=module.DO_NOT_CAPTURE,
  890. borg_local_path='borg',
  891. )
  892. module.create_archive(
  893. dry_run=False,
  894. repository='repo',
  895. location_config={
  896. 'source_directories': ['foo', 'bar'],
  897. 'repositories': ['repo'],
  898. 'exclude_patterns': None,
  899. },
  900. storage_config={},
  901. progress=True,
  902. stream_processes=processes,
  903. )
  904. def test_create_archive_with_json_calls_borg_with_json_parameter():
  905. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  906. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  907. flexmock(module).should_receive('_expand_directories').and_return(())
  908. flexmock(module).should_receive('_expand_home_directories').and_return(())
  909. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  910. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  911. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  912. flexmock(module).should_receive('execute_command').with_args(
  913. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  914. output_log_level=None,
  915. output_file=None,
  916. borg_local_path='borg',
  917. ).and_return('[]')
  918. json_output = module.create_archive(
  919. dry_run=False,
  920. repository='repo',
  921. location_config={
  922. 'source_directories': ['foo', 'bar'],
  923. 'repositories': ['repo'],
  924. 'exclude_patterns': None,
  925. },
  926. storage_config={},
  927. json=True,
  928. )
  929. assert json_output == '[]'
  930. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  931. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  932. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  933. flexmock(module).should_receive('_expand_directories').and_return(())
  934. flexmock(module).should_receive('_expand_home_directories').and_return(())
  935. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  936. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  937. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  938. flexmock(module).should_receive('execute_command').with_args(
  939. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  940. output_log_level=None,
  941. output_file=None,
  942. borg_local_path='borg',
  943. ).and_return('[]')
  944. json_output = module.create_archive(
  945. dry_run=False,
  946. repository='repo',
  947. location_config={
  948. 'source_directories': ['foo', 'bar'],
  949. 'repositories': ['repo'],
  950. 'exclude_patterns': None,
  951. },
  952. storage_config={},
  953. json=True,
  954. stats=True,
  955. )
  956. assert json_output == '[]'
  957. def test_create_archive_with_source_directories_glob_expands():
  958. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  959. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  960. flexmock(module).should_receive('_expand_directories').and_return(())
  961. flexmock(module).should_receive('_expand_home_directories').and_return(())
  962. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  963. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  964. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  965. flexmock(module).should_receive('execute_command').with_args(
  966. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  967. output_log_level=logging.INFO,
  968. output_file=None,
  969. borg_local_path='borg',
  970. )
  971. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  972. module.create_archive(
  973. dry_run=False,
  974. repository='repo',
  975. location_config={
  976. 'source_directories': ['foo*'],
  977. 'repositories': ['repo'],
  978. 'exclude_patterns': None,
  979. },
  980. storage_config={},
  981. )
  982. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  983. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  984. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  985. flexmock(module).should_receive('_expand_directories').and_return(())
  986. flexmock(module).should_receive('_expand_home_directories').and_return(())
  987. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  988. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  989. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  990. flexmock(module).should_receive('execute_command').with_args(
  991. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  992. output_log_level=logging.INFO,
  993. output_file=None,
  994. borg_local_path='borg',
  995. )
  996. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  997. module.create_archive(
  998. dry_run=False,
  999. repository='repo',
  1000. location_config={
  1001. 'source_directories': ['foo*'],
  1002. 'repositories': ['repo'],
  1003. 'exclude_patterns': None,
  1004. },
  1005. storage_config={},
  1006. )
  1007. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1008. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1009. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1010. flexmock(module).should_receive('_expand_directories').and_return(())
  1011. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1012. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1013. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1014. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1015. flexmock(module).should_receive('execute_command').with_args(
  1016. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1017. output_log_level=logging.INFO,
  1018. output_file=None,
  1019. borg_local_path='borg',
  1020. )
  1021. module.create_archive(
  1022. dry_run=False,
  1023. repository='repo',
  1024. location_config={
  1025. 'source_directories': ['foo*'],
  1026. 'repositories': ['repo'],
  1027. 'exclude_patterns': None,
  1028. },
  1029. storage_config={},
  1030. )
  1031. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1032. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1033. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1034. flexmock(module).should_receive('_expand_directories').and_return(())
  1035. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1036. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1037. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1038. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1039. flexmock(module).should_receive('execute_command').with_args(
  1040. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1041. output_log_level=logging.INFO,
  1042. output_file=None,
  1043. borg_local_path='borg',
  1044. )
  1045. module.create_archive(
  1046. dry_run=False,
  1047. repository='repo',
  1048. location_config={
  1049. 'source_directories': ['foo', 'bar'],
  1050. 'repositories': ['repo'],
  1051. 'exclude_patterns': None,
  1052. },
  1053. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1054. )
  1055. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1056. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1057. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1058. flexmock(module).should_receive('_expand_directories').and_return(())
  1059. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1060. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1061. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1062. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1063. flexmock(module).should_receive('execute_command').with_args(
  1064. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  1065. output_log_level=logging.INFO,
  1066. output_file=None,
  1067. borg_local_path='borg',
  1068. )
  1069. module.create_archive(
  1070. dry_run=False,
  1071. repository='repo',
  1072. location_config={
  1073. 'source_directories': ['foo', 'bar'],
  1074. 'repositories': ['repo'],
  1075. 'exclude_patterns': None,
  1076. },
  1077. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1078. )
  1079. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1080. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1081. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1082. flexmock(module).should_receive('_expand_directories').and_return(())
  1083. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1084. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1085. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1086. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1087. flexmock(module).should_receive('execute_command').with_args(
  1088. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  1089. output_log_level=logging.INFO,
  1090. output_file=None,
  1091. borg_local_path='borg',
  1092. )
  1093. module.create_archive(
  1094. dry_run=False,
  1095. repository='repo',
  1096. location_config={
  1097. 'source_directories': ['foo', 'bar'],
  1098. 'repositories': ['repo'],
  1099. 'exclude_patterns': None,
  1100. },
  1101. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1102. )
  1103. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1104. processes = flexmock()
  1105. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1106. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1107. flexmock(module).should_receive('_expand_directories').and_return(())
  1108. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1109. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1110. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1111. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1112. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1113. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  1114. processes=processes,
  1115. output_log_level=logging.INFO,
  1116. output_file=None,
  1117. borg_local_path='borg',
  1118. )
  1119. module.create_archive(
  1120. dry_run=False,
  1121. repository='repo',
  1122. location_config={
  1123. 'source_directories': ['foo', 'bar'],
  1124. 'repositories': ['repo'],
  1125. 'exclude_patterns': None,
  1126. },
  1127. storage_config={},
  1128. stream_processes=processes,
  1129. )