test_create.py 47 KB

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