test_create.py 57 KB

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