test_create.py 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. import logging
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import create as module
  6. from ..test_verbosity import insert_logging_mock
  7. def test_expand_directory_with_basic_path_passes_it_through():
  8. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  9. flexmock(module.glob).should_receive('glob').and_return([])
  10. paths = module.expand_directory('foo')
  11. assert paths == ['foo']
  12. def test_expand_directory_with_glob_expands():
  13. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  14. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  15. paths = module.expand_directory('foo*')
  16. assert paths == ['foo', 'food']
  17. def test_expand_directories_flattens_expanded_directories():
  18. flexmock(module).should_receive('expand_directory').with_args('~/foo').and_return(['/root/foo'])
  19. flexmock(module).should_receive('expand_directory').with_args('bar*').and_return(
  20. ['bar', 'barf']
  21. )
  22. paths = module.expand_directories(('~/foo', 'bar*'))
  23. assert paths == ('/root/foo', 'bar', 'barf')
  24. def test_expand_directories_considers_none_as_no_directories():
  25. paths = module.expand_directories(None)
  26. assert paths == ()
  27. def test_expand_home_directories_expands_tildes():
  28. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  29. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  30. paths = module.expand_home_directories(('~/bar', 'baz'))
  31. assert paths == ('/foo/bar', 'baz')
  32. def test_expand_home_directories_considers_none_as_no_directories():
  33. paths = module.expand_home_directories(None)
  34. assert paths == ()
  35. def test_map_directories_to_devices_gives_device_id_per_path():
  36. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  37. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=66))
  38. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  39. assert device_map == {
  40. '/foo': 55,
  41. '/bar': 66,
  42. }
  43. def test_map_directories_to_devices_with_missing_path_does_not_error():
  44. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  45. flexmock(module.os).should_receive('stat').with_args('/bar').and_raise(FileNotFoundError)
  46. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  47. assert device_map == {
  48. '/foo': 55,
  49. '/bar': None,
  50. }
  51. @pytest.mark.parametrize(
  52. 'directories,additional_directories,expected_directories',
  53. (
  54. ({'/': 1, '/root': 1}, {}, ('/',)),
  55. ({'/': 1, '/root/': 1}, {}, ('/',)),
  56. ({'/': 1, '/root': 2}, {}, ('/', '/root')),
  57. ({'/root': 1, '/': 1}, {}, ('/',)),
  58. ({'/root': 1, '/root/foo': 1}, {}, ('/root',)),
  59. ({'/root/': 1, '/root/foo': 1}, {}, ('/root/',)),
  60. ({'/root': 1, '/root/foo/': 1}, {}, ('/root',)),
  61. ({'/root': 1, '/root/foo': 2}, {}, ('/root', '/root/foo')),
  62. ({'/root/foo': 1, '/root': 1}, {}, ('/root',)),
  63. ({'/root': None, '/root/foo': None}, {}, ('/root', '/root/foo')),
  64. ({'/root': 1, '/etc': 1, '/root/foo/bar': 1}, {}, ('/etc', '/root')),
  65. ({'/root': 1, '/root/foo': 1, '/root/foo/bar': 1}, {}, ('/root',)),
  66. ({'/dup': 1, '/dup': 1}, {}, ('/dup',)),
  67. ({'/foo': 1, '/bar': 1}, {}, ('/bar', '/foo')),
  68. ({'/foo': 1, '/bar': 2}, {}, ('/bar', '/foo')),
  69. ({'/root/foo': 1}, {'/root': 1}, ()),
  70. ({'/root/foo': 1}, {'/root': 2}, ('/root/foo',)),
  71. ({'/root/foo': 1}, {}, ('/root/foo',)),
  72. ),
  73. )
  74. def test_deduplicate_directories_removes_child_paths_on_the_same_filesystem(
  75. directories, additional_directories, expected_directories
  76. ):
  77. assert (
  78. module.deduplicate_directories(directories, additional_directories) == expected_directories
  79. )
  80. def test_write_pattern_file_writes_pattern_lines():
  81. temporary_file = flexmock(name='filename', flush=lambda: None)
  82. temporary_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  83. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  84. module.write_pattern_file(['R /foo', '+ /foo/bar'])
  85. def test_write_pattern_file_with_sources_writes_sources_as_roots():
  86. temporary_file = flexmock(name='filename', flush=lambda: None)
  87. temporary_file.should_receive('write').with_args('R /foo\n+ /foo/bar\nR /baz\nR /quux')
  88. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  89. module.write_pattern_file(['R /foo', '+ /foo/bar'], sources=['/baz', '/quux'])
  90. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  91. module.write_pattern_file([])
  92. def test_write_pattern_file_overwrites_existing_file():
  93. pattern_file = flexmock(name='filename', flush=lambda: None)
  94. pattern_file.should_receive('seek').with_args(0).once()
  95. pattern_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  96. flexmock(module.tempfile).should_receive('NamedTemporaryFile').never()
  97. module.write_pattern_file(['R /foo', '+ /foo/bar'], pattern_file=pattern_file)
  98. @pytest.mark.parametrize(
  99. 'filename_lists,opened_filenames',
  100. (
  101. ([('foo', 'bar'), ('baz', 'quux')], ('foo', 'bar', 'baz', 'quux')),
  102. ([None, ('foo', 'bar')], ('foo', 'bar')),
  103. ([None, None], ()),
  104. ),
  105. )
  106. def test_ensure_files_readable_opens_filenames(filename_lists, opened_filenames):
  107. for expected_filename in opened_filenames:
  108. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  109. expected_filename
  110. ).and_return(flexmock(close=lambda: None))
  111. module.ensure_files_readable(*filename_lists)
  112. def test_make_pattern_flags_includes_pattern_filename_when_given():
  113. pattern_flags = module.make_pattern_flags(
  114. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  115. )
  116. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  117. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  118. pattern_flags = module.make_pattern_flags(
  119. location_config={'patterns_from': ['patterns', 'other']}
  120. )
  121. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  122. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  123. pattern_flags = module.make_pattern_flags(
  124. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  125. )
  126. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  127. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  128. pattern_flags = module.make_pattern_flags(location_config={'patterns_from': None})
  129. assert pattern_flags == ()
  130. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  131. exclude_flags = module.make_exclude_flags(
  132. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  133. )
  134. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  135. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  136. exclude_flags = module.make_exclude_flags(
  137. location_config={'exclude_from': ['excludes', 'other']}
  138. )
  139. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  140. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  141. exclude_flags = module.make_exclude_flags(
  142. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  143. )
  144. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  145. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  146. exclude_flags = module.make_exclude_flags(location_config={'exclude_from': None})
  147. assert exclude_flags == ()
  148. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  149. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': True})
  150. assert exclude_flags == ('--exclude-caches',)
  151. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  152. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': False})
  153. assert exclude_flags == ()
  154. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  155. exclude_flags = module.make_exclude_flags(
  156. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  157. )
  158. assert exclude_flags == (
  159. '--exclude-if-present',
  160. 'exclude_me',
  161. '--exclude-if-present',
  162. 'also_me',
  163. )
  164. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  165. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': True})
  166. assert exclude_flags == ('--keep-exclude-tags',)
  167. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  168. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': False})
  169. assert exclude_flags == ()
  170. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  171. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': True})
  172. assert exclude_flags == ('--exclude-nodump',)
  173. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  174. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': False})
  175. assert exclude_flags == ()
  176. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  177. exclude_flags = module.make_exclude_flags(location_config={})
  178. assert exclude_flags == ()
  179. def test_collect_borgmatic_source_directories_set_when_directory_exists():
  180. flexmock(module.os.path).should_receive('exists').and_return(True)
  181. flexmock(module.os.path).should_receive('expanduser')
  182. assert module.collect_borgmatic_source_directories('/tmp') == ['/tmp']
  183. def test_collect_borgmatic_source_directories_empty_when_directory_does_not_exist():
  184. flexmock(module.os.path).should_receive('exists').and_return(False)
  185. flexmock(module.os.path).should_receive('expanduser')
  186. assert module.collect_borgmatic_source_directories('/tmp') == []
  187. def test_collect_borgmatic_source_directories_defaults_when_directory_not_given():
  188. flexmock(module.os.path).should_receive('exists').and_return(True)
  189. flexmock(module.os.path).should_receive('expanduser')
  190. assert module.collect_borgmatic_source_directories(None) == [
  191. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  192. ]
  193. def test_pattern_root_directories_deals_with_none_patterns():
  194. assert module.pattern_root_directories(patterns=None) == []
  195. def test_pattern_root_directories_parses_roots_and_ignores_others():
  196. assert module.pattern_root_directories(
  197. ['R /root', '+ /root/foo', '- /root/foo/bar', 'R /baz']
  198. ) == ['/root', '/baz']
  199. @pytest.mark.parametrize(
  200. 'character_device,block_device,fifo,expected_result',
  201. (
  202. (False, False, False, False),
  203. (True, False, False, True),
  204. (False, True, False, True),
  205. (True, True, False, True),
  206. (False, False, True, True),
  207. (False, True, True, True),
  208. (True, False, True, True),
  209. ),
  210. )
  211. def test_special_file_looks_at_file_type(character_device, block_device, fifo, expected_result):
  212. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mode=flexmock()))
  213. flexmock(module.stat).should_receive('S_ISCHR').and_return(character_device)
  214. flexmock(module.stat).should_receive('S_ISBLK').and_return(block_device)
  215. flexmock(module.stat).should_receive('S_ISFIFO').and_return(fifo)
  216. assert module.special_file('/dev/special') == expected_result
  217. def test_any_parent_directories_treats_parents_as_match():
  218. module.any_parent_directories('/foo/bar.txt', ('/foo', '/etc'))
  219. def test_any_parent_directories_treats_grandparents_as_match():
  220. module.any_parent_directories('/foo/bar/baz.txt', ('/foo', '/etc'))
  221. def test_any_parent_directories_treats_unrelated_paths_as_non_match():
  222. module.any_parent_directories('/foo/bar.txt', ('/usr', '/etc'))
  223. def test_collect_special_file_paths_parses_special_files_from_borg_dry_run_file_list():
  224. flexmock(module).should_receive('execute_command').and_return(
  225. 'Processing files ...\n- /foo\n- /bar\n- /baz'
  226. )
  227. flexmock(module).should_receive('special_file').and_return(True)
  228. flexmock(module).should_receive('any_parent_directories').and_return(False)
  229. assert module.collect_special_file_paths(
  230. ('borg', 'create'),
  231. local_path=None,
  232. working_directory=None,
  233. borg_environment=None,
  234. skip_directories=flexmock(),
  235. ) == ('/foo', '/bar', '/baz')
  236. def test_collect_special_file_paths_excludes_requested_directories():
  237. flexmock(module).should_receive('execute_command').and_return('- /foo\n- /bar\n- /baz')
  238. flexmock(module).should_receive('special_file').and_return(True)
  239. flexmock(module).should_receive('any_parent_directories').and_return(False).and_return(
  240. True
  241. ).and_return(False)
  242. assert module.collect_special_file_paths(
  243. ('borg', 'create'),
  244. local_path=None,
  245. working_directory=None,
  246. borg_environment=None,
  247. skip_directories=flexmock(),
  248. ) == ('/foo', '/baz')
  249. def test_collect_special_file_paths_excludes_non_special_files():
  250. flexmock(module).should_receive('execute_command').and_return('- /foo\n- /bar\n- /baz')
  251. flexmock(module).should_receive('special_file').and_return(True).and_return(False).and_return(
  252. True
  253. )
  254. flexmock(module).should_receive('any_parent_directories').and_return(False)
  255. assert module.collect_special_file_paths(
  256. ('borg', 'create'),
  257. local_path=None,
  258. working_directory=None,
  259. borg_environment=None,
  260. skip_directories=flexmock(),
  261. ) == ('/foo', '/baz')
  262. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  263. REPO_ARCHIVE_WITH_PATHS = (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'bar')
  264. def test_create_archive_calls_borg_with_parameters():
  265. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  266. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  267. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  268. flexmock(module).should_receive('expand_directories').and_return(())
  269. flexmock(module).should_receive('pattern_root_directories').and_return([])
  270. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  271. flexmock(module).should_receive('expand_home_directories').and_return(())
  272. flexmock(module).should_receive('write_pattern_file').and_return(None)
  273. flexmock(module.feature).should_receive('available').and_return(True)
  274. flexmock(module).should_receive('ensure_files_readable')
  275. flexmock(module).should_receive('make_pattern_flags').and_return(())
  276. flexmock(module).should_receive('make_exclude_flags').and_return(())
  277. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  278. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  279. )
  280. flexmock(module.environment).should_receive('make_environment')
  281. flexmock(module).should_receive('execute_command').with_args(
  282. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  283. output_log_level=logging.INFO,
  284. output_file=None,
  285. borg_local_path='borg',
  286. working_directory=None,
  287. extra_environment=None,
  288. )
  289. module.create_archive(
  290. dry_run=False,
  291. repository='repo',
  292. location_config={
  293. 'source_directories': ['foo', 'bar'],
  294. 'repositories': ['repo'],
  295. 'exclude_patterns': None,
  296. },
  297. storage_config={},
  298. local_borg_version='1.2.3',
  299. )
  300. def test_create_archive_calls_borg_with_environment():
  301. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  302. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  303. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  304. flexmock(module).should_receive('expand_directories').and_return(())
  305. flexmock(module).should_receive('pattern_root_directories').and_return([])
  306. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  307. flexmock(module).should_receive('expand_home_directories').and_return(())
  308. flexmock(module).should_receive('write_pattern_file').and_return(None)
  309. flexmock(module.feature).should_receive('available').and_return(True)
  310. flexmock(module).should_receive('ensure_files_readable')
  311. flexmock(module).should_receive('make_pattern_flags').and_return(())
  312. flexmock(module).should_receive('make_exclude_flags').and_return(())
  313. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  314. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  315. )
  316. environment = {'BORG_THINGY': 'YUP'}
  317. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  318. flexmock(module).should_receive('execute_command').with_args(
  319. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  320. output_log_level=logging.INFO,
  321. output_file=None,
  322. borg_local_path='borg',
  323. working_directory=None,
  324. extra_environment=environment,
  325. )
  326. module.create_archive(
  327. dry_run=False,
  328. repository='repo',
  329. location_config={
  330. 'source_directories': ['foo', 'bar'],
  331. 'repositories': ['repo'],
  332. 'exclude_patterns': None,
  333. },
  334. storage_config={},
  335. local_borg_version='1.2.3',
  336. )
  337. def test_create_archive_with_patterns_calls_borg_with_patterns_including_converted_source_directories():
  338. pattern_flags = ('--patterns-from', 'patterns')
  339. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  340. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  341. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  342. flexmock(module).should_receive('expand_directories').and_return(())
  343. flexmock(module).should_receive('pattern_root_directories').and_return([])
  344. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  345. flexmock(module).should_receive('expand_home_directories').and_return(())
  346. flexmock(module).should_receive('write_pattern_file').and_return(
  347. flexmock(name='/tmp/patterns')
  348. ).and_return(None)
  349. flexmock(module.feature).should_receive('available').and_return(True)
  350. flexmock(module).should_receive('ensure_files_readable')
  351. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  352. flexmock(module).should_receive('make_exclude_flags').and_return(())
  353. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  354. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  355. )
  356. flexmock(module.environment).should_receive('make_environment')
  357. flexmock(module).should_receive('execute_command').with_args(
  358. ('borg', 'create') + pattern_flags + (f'repo::{DEFAULT_ARCHIVE_NAME}',),
  359. output_log_level=logging.INFO,
  360. output_file=None,
  361. borg_local_path='borg',
  362. working_directory=None,
  363. extra_environment=None,
  364. )
  365. module.create_archive(
  366. dry_run=False,
  367. repository='repo',
  368. location_config={
  369. 'source_directories': ['foo', 'bar'],
  370. 'repositories': ['repo'],
  371. 'patterns': ['pattern'],
  372. },
  373. storage_config={},
  374. local_borg_version='1.2.3',
  375. )
  376. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  377. exclude_flags = ('--exclude-from', 'excludes')
  378. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  379. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  380. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  381. flexmock(module).should_receive('expand_directories').and_return(())
  382. flexmock(module).should_receive('pattern_root_directories').and_return([])
  383. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  384. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  385. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  386. flexmock(name='/tmp/excludes')
  387. )
  388. flexmock(module.feature).should_receive('available').and_return(True)
  389. flexmock(module).should_receive('ensure_files_readable')
  390. flexmock(module).should_receive('make_pattern_flags').and_return(())
  391. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  392. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  393. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  394. )
  395. flexmock(module.environment).should_receive('make_environment')
  396. flexmock(module).should_receive('execute_command').with_args(
  397. ('borg', 'create') + exclude_flags + REPO_ARCHIVE_WITH_PATHS,
  398. output_log_level=logging.INFO,
  399. output_file=None,
  400. borg_local_path='borg',
  401. working_directory=None,
  402. extra_environment=None,
  403. )
  404. module.create_archive(
  405. dry_run=False,
  406. repository='repo',
  407. location_config={
  408. 'source_directories': ['foo', 'bar'],
  409. 'repositories': ['repo'],
  410. 'exclude_patterns': ['exclude'],
  411. },
  412. storage_config={},
  413. local_borg_version='1.2.3',
  414. )
  415. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  416. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  417. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  418. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  419. flexmock(module).should_receive('expand_directories').and_return(())
  420. flexmock(module).should_receive('pattern_root_directories').and_return([])
  421. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  422. flexmock(module).should_receive('expand_home_directories').and_return(())
  423. flexmock(module).should_receive('write_pattern_file').and_return(None)
  424. flexmock(module.feature).should_receive('available').and_return(True)
  425. flexmock(module).should_receive('ensure_files_readable')
  426. flexmock(module).should_receive('make_pattern_flags').and_return(())
  427. flexmock(module).should_receive('make_exclude_flags').and_return(())
  428. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  429. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  430. )
  431. flexmock(module.environment).should_receive('make_environment')
  432. flexmock(module).should_receive('execute_command').with_args(
  433. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  434. output_log_level=logging.INFO,
  435. output_file=None,
  436. borg_local_path='borg',
  437. working_directory=None,
  438. extra_environment=None,
  439. )
  440. insert_logging_mock(logging.INFO)
  441. module.create_archive(
  442. dry_run=False,
  443. repository='repo',
  444. location_config={
  445. 'source_directories': ['foo', 'bar'],
  446. 'repositories': ['repo'],
  447. 'exclude_patterns': None,
  448. },
  449. storage_config={},
  450. local_borg_version='1.2.3',
  451. )
  452. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  453. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  454. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  455. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  456. flexmock(module).should_receive('expand_directories').and_return(())
  457. flexmock(module).should_receive('pattern_root_directories').and_return([])
  458. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  459. flexmock(module).should_receive('expand_home_directories').and_return(())
  460. flexmock(module).should_receive('write_pattern_file').and_return(None)
  461. flexmock(module.feature).should_receive('available').and_return(True)
  462. flexmock(module).should_receive('ensure_files_readable')
  463. flexmock(module).should_receive('make_pattern_flags').and_return(())
  464. flexmock(module).should_receive('make_exclude_flags').and_return(())
  465. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  466. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  467. )
  468. flexmock(module.environment).should_receive('make_environment')
  469. flexmock(module).should_receive('execute_command').with_args(
  470. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  471. output_log_level=None,
  472. output_file=None,
  473. borg_local_path='borg',
  474. working_directory=None,
  475. extra_environment=None,
  476. )
  477. insert_logging_mock(logging.INFO)
  478. module.create_archive(
  479. dry_run=False,
  480. repository='repo',
  481. location_config={
  482. 'source_directories': ['foo', 'bar'],
  483. 'repositories': ['repo'],
  484. 'exclude_patterns': None,
  485. },
  486. storage_config={},
  487. local_borg_version='1.2.3',
  488. json=True,
  489. )
  490. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  491. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  492. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  493. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  494. flexmock(module).should_receive('expand_directories').and_return(())
  495. flexmock(module).should_receive('pattern_root_directories').and_return([])
  496. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  497. flexmock(module).should_receive('expand_home_directories').and_return(())
  498. flexmock(module).should_receive('write_pattern_file').and_return(None)
  499. flexmock(module.feature).should_receive('available').and_return(True)
  500. flexmock(module).should_receive('ensure_files_readable')
  501. flexmock(module).should_receive('make_pattern_flags').and_return(())
  502. flexmock(module).should_receive('make_exclude_flags').and_return(())
  503. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  504. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  505. )
  506. flexmock(module.environment).should_receive('make_environment')
  507. flexmock(module).should_receive('execute_command').with_args(
  508. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--debug', '--show-rc'),
  509. output_log_level=logging.INFO,
  510. output_file=None,
  511. borg_local_path='borg',
  512. working_directory=None,
  513. extra_environment=None,
  514. )
  515. insert_logging_mock(logging.DEBUG)
  516. module.create_archive(
  517. dry_run=False,
  518. repository='repo',
  519. location_config={
  520. 'source_directories': ['foo', 'bar'],
  521. 'repositories': ['repo'],
  522. 'exclude_patterns': None,
  523. },
  524. storage_config={},
  525. local_borg_version='1.2.3',
  526. )
  527. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  528. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  529. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  530. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  531. flexmock(module).should_receive('expand_directories').and_return(())
  532. flexmock(module).should_receive('pattern_root_directories').and_return([])
  533. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  534. flexmock(module).should_receive('expand_home_directories').and_return(())
  535. flexmock(module).should_receive('write_pattern_file').and_return(None)
  536. flexmock(module.feature).should_receive('available').and_return(True)
  537. flexmock(module).should_receive('ensure_files_readable')
  538. flexmock(module).should_receive('make_pattern_flags').and_return(())
  539. flexmock(module).should_receive('make_exclude_flags').and_return(())
  540. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  541. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  542. )
  543. flexmock(module.environment).should_receive('make_environment')
  544. flexmock(module).should_receive('execute_command').with_args(
  545. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  546. output_log_level=None,
  547. output_file=None,
  548. borg_local_path='borg',
  549. working_directory=None,
  550. extra_environment=None,
  551. )
  552. insert_logging_mock(logging.DEBUG)
  553. module.create_archive(
  554. dry_run=False,
  555. repository='repo',
  556. location_config={
  557. 'source_directories': ['foo', 'bar'],
  558. 'repositories': ['repo'],
  559. 'exclude_patterns': None,
  560. },
  561. storage_config={},
  562. local_borg_version='1.2.3',
  563. json=True,
  564. )
  565. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  566. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  567. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  568. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  569. flexmock(module).should_receive('expand_directories').and_return(())
  570. flexmock(module).should_receive('pattern_root_directories').and_return([])
  571. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  572. flexmock(module).should_receive('expand_home_directories').and_return(())
  573. flexmock(module).should_receive('write_pattern_file').and_return(None)
  574. flexmock(module.feature).should_receive('available').and_return(True)
  575. flexmock(module).should_receive('ensure_files_readable')
  576. flexmock(module).should_receive('make_pattern_flags').and_return(())
  577. flexmock(module).should_receive('make_exclude_flags').and_return(())
  578. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  579. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  580. )
  581. flexmock(module.environment).should_receive('make_environment')
  582. flexmock(module).should_receive('execute_command').with_args(
  583. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  584. output_log_level=logging.INFO,
  585. output_file=None,
  586. borg_local_path='borg',
  587. working_directory=None,
  588. extra_environment=None,
  589. )
  590. module.create_archive(
  591. dry_run=True,
  592. repository='repo',
  593. location_config={
  594. 'source_directories': ['foo', 'bar'],
  595. 'repositories': ['repo'],
  596. 'exclude_patterns': None,
  597. },
  598. storage_config={},
  599. local_borg_version='1.2.3',
  600. )
  601. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  602. # --dry-run and --stats are mutually exclusive, see:
  603. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  604. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  605. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  606. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  607. flexmock(module).should_receive('expand_directories').and_return(())
  608. flexmock(module).should_receive('pattern_root_directories').and_return([])
  609. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  610. flexmock(module).should_receive('expand_home_directories').and_return(())
  611. flexmock(module).should_receive('write_pattern_file').and_return(None)
  612. flexmock(module.feature).should_receive('available').and_return(True)
  613. flexmock(module).should_receive('ensure_files_readable')
  614. flexmock(module).should_receive('make_pattern_flags').and_return(())
  615. flexmock(module).should_receive('make_exclude_flags').and_return(())
  616. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  617. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  618. )
  619. flexmock(module.environment).should_receive('make_environment')
  620. flexmock(module).should_receive('execute_command').with_args(
  621. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  622. output_log_level=logging.INFO,
  623. output_file=None,
  624. borg_local_path='borg',
  625. working_directory=None,
  626. extra_environment=None,
  627. )
  628. insert_logging_mock(logging.INFO)
  629. module.create_archive(
  630. dry_run=True,
  631. repository='repo',
  632. location_config={
  633. 'source_directories': ['foo', 'bar'],
  634. 'repositories': ['repo'],
  635. 'exclude_patterns': None,
  636. },
  637. storage_config={},
  638. local_borg_version='1.2.3',
  639. stats=True,
  640. )
  641. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  642. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  643. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  644. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  645. flexmock(module).should_receive('expand_directories').and_return(())
  646. flexmock(module).should_receive('pattern_root_directories').and_return([])
  647. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  648. flexmock(module).should_receive('expand_home_directories').and_return(())
  649. flexmock(module).should_receive('write_pattern_file').and_return(None)
  650. flexmock(module.feature).should_receive('available').and_return(True)
  651. flexmock(module).should_receive('ensure_files_readable')
  652. flexmock(module).should_receive('make_pattern_flags').and_return(())
  653. flexmock(module).should_receive('make_exclude_flags').and_return(())
  654. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  655. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  656. )
  657. flexmock(module.environment).should_receive('make_environment')
  658. flexmock(module).should_receive('execute_command').with_args(
  659. ('borg', 'create', '--checkpoint-interval', '600') + REPO_ARCHIVE_WITH_PATHS,
  660. output_log_level=logging.INFO,
  661. output_file=None,
  662. borg_local_path='borg',
  663. working_directory=None,
  664. extra_environment=None,
  665. )
  666. module.create_archive(
  667. dry_run=False,
  668. repository='repo',
  669. location_config={
  670. 'source_directories': ['foo', 'bar'],
  671. 'repositories': ['repo'],
  672. 'exclude_patterns': None,
  673. },
  674. storage_config={'checkpoint_interval': 600},
  675. local_borg_version='1.2.3',
  676. )
  677. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  678. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  679. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  680. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  681. flexmock(module).should_receive('expand_directories').and_return(())
  682. flexmock(module).should_receive('pattern_root_directories').and_return([])
  683. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  684. flexmock(module).should_receive('expand_home_directories').and_return(())
  685. flexmock(module).should_receive('write_pattern_file').and_return(None)
  686. flexmock(module.feature).should_receive('available').and_return(True)
  687. flexmock(module).should_receive('ensure_files_readable')
  688. flexmock(module).should_receive('make_pattern_flags').and_return(())
  689. flexmock(module).should_receive('make_exclude_flags').and_return(())
  690. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  691. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  692. )
  693. flexmock(module.environment).should_receive('make_environment')
  694. flexmock(module).should_receive('execute_command').with_args(
  695. ('borg', 'create', '--chunker-params', '1,2,3,4') + REPO_ARCHIVE_WITH_PATHS,
  696. output_log_level=logging.INFO,
  697. output_file=None,
  698. borg_local_path='borg',
  699. working_directory=None,
  700. extra_environment=None,
  701. )
  702. module.create_archive(
  703. dry_run=False,
  704. repository='repo',
  705. location_config={
  706. 'source_directories': ['foo', 'bar'],
  707. 'repositories': ['repo'],
  708. 'exclude_patterns': None,
  709. },
  710. storage_config={'chunker_params': '1,2,3,4'},
  711. local_borg_version='1.2.3',
  712. )
  713. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  714. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  715. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  716. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  717. flexmock(module).should_receive('expand_directories').and_return(())
  718. flexmock(module).should_receive('pattern_root_directories').and_return([])
  719. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  720. flexmock(module).should_receive('expand_home_directories').and_return(())
  721. flexmock(module).should_receive('write_pattern_file').and_return(None)
  722. flexmock(module.feature).should_receive('available').and_return(True)
  723. flexmock(module).should_receive('ensure_files_readable')
  724. flexmock(module).should_receive('make_pattern_flags').and_return(())
  725. flexmock(module).should_receive('make_exclude_flags').and_return(())
  726. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  727. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  728. )
  729. flexmock(module.environment).should_receive('make_environment')
  730. flexmock(module).should_receive('execute_command').with_args(
  731. ('borg', 'create', '--compression', 'rle') + REPO_ARCHIVE_WITH_PATHS,
  732. output_log_level=logging.INFO,
  733. output_file=None,
  734. borg_local_path='borg',
  735. working_directory=None,
  736. extra_environment=None,
  737. )
  738. module.create_archive(
  739. dry_run=False,
  740. repository='repo',
  741. location_config={
  742. 'source_directories': ['foo', 'bar'],
  743. 'repositories': ['repo'],
  744. 'exclude_patterns': None,
  745. },
  746. storage_config={'compression': 'rle'},
  747. local_borg_version='1.2.3',
  748. )
  749. @pytest.mark.parametrize(
  750. 'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  751. )
  752. def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  753. feature_available, option_flag
  754. ):
  755. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  756. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  757. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  758. flexmock(module).should_receive('expand_directories').and_return(())
  759. flexmock(module).should_receive('pattern_root_directories').and_return([])
  760. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  761. flexmock(module).should_receive('expand_home_directories').and_return(())
  762. flexmock(module).should_receive('write_pattern_file').and_return(None)
  763. flexmock(module.feature).should_receive('available').and_return(feature_available)
  764. flexmock(module).should_receive('ensure_files_readable')
  765. flexmock(module).should_receive('make_pattern_flags').and_return(())
  766. flexmock(module).should_receive('make_exclude_flags').and_return(())
  767. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  768. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  769. )
  770. flexmock(module.environment).should_receive('make_environment')
  771. flexmock(module).should_receive('execute_command').with_args(
  772. ('borg', 'create', option_flag, '100') + REPO_ARCHIVE_WITH_PATHS,
  773. output_log_level=logging.INFO,
  774. output_file=None,
  775. borg_local_path='borg',
  776. working_directory=None,
  777. extra_environment=None,
  778. )
  779. module.create_archive(
  780. dry_run=False,
  781. repository='repo',
  782. location_config={
  783. 'source_directories': ['foo', 'bar'],
  784. 'repositories': ['repo'],
  785. 'exclude_patterns': None,
  786. },
  787. storage_config={'upload_rate_limit': 100},
  788. local_borg_version='1.2.3',
  789. )
  790. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  791. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  792. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  793. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  794. flexmock(module).should_receive('expand_directories').and_return(())
  795. flexmock(module).should_receive('pattern_root_directories').and_return([])
  796. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  797. '/working/dir'
  798. )
  799. flexmock(module).should_receive('expand_home_directories').and_return(())
  800. flexmock(module).should_receive('write_pattern_file').and_return(None)
  801. flexmock(module.feature).should_receive('available').and_return(True)
  802. flexmock(module).should_receive('ensure_files_readable')
  803. flexmock(module).should_receive('make_pattern_flags').and_return(())
  804. flexmock(module).should_receive('make_exclude_flags').and_return(())
  805. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  806. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  807. )
  808. flexmock(module.environment).should_receive('make_environment')
  809. flexmock(module).should_receive('execute_command').with_args(
  810. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  811. output_log_level=logging.INFO,
  812. output_file=None,
  813. borg_local_path='borg',
  814. working_directory='/working/dir',
  815. extra_environment=None,
  816. )
  817. module.create_archive(
  818. dry_run=False,
  819. repository='repo',
  820. location_config={
  821. 'source_directories': ['foo', 'bar'],
  822. 'repositories': ['repo'],
  823. 'working_directory': '/working/dir',
  824. 'exclude_patterns': None,
  825. },
  826. storage_config={},
  827. local_borg_version='1.2.3',
  828. )
  829. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  830. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  831. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  832. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  833. flexmock(module).should_receive('expand_directories').and_return(())
  834. flexmock(module).should_receive('pattern_root_directories').and_return([])
  835. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  836. flexmock(module).should_receive('expand_home_directories').and_return(())
  837. flexmock(module).should_receive('write_pattern_file').and_return(None)
  838. flexmock(module.feature).should_receive('available').and_return(True)
  839. flexmock(module).should_receive('ensure_files_readable')
  840. flexmock(module).should_receive('make_pattern_flags').and_return(())
  841. flexmock(module).should_receive('make_exclude_flags').and_return(())
  842. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  843. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  844. )
  845. flexmock(module.environment).should_receive('make_environment')
  846. flexmock(module).should_receive('execute_command').with_args(
  847. ('borg', 'create', '--one-file-system') + REPO_ARCHIVE_WITH_PATHS,
  848. output_log_level=logging.INFO,
  849. output_file=None,
  850. borg_local_path='borg',
  851. working_directory=None,
  852. extra_environment=None,
  853. )
  854. module.create_archive(
  855. dry_run=False,
  856. repository='repo',
  857. location_config={
  858. 'source_directories': ['foo', 'bar'],
  859. 'repositories': ['repo'],
  860. 'one_file_system': True,
  861. 'exclude_patterns': None,
  862. },
  863. storage_config={},
  864. local_borg_version='1.2.3',
  865. )
  866. @pytest.mark.parametrize(
  867. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')),
  868. )
  869. def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
  870. feature_available, option_flag
  871. ):
  872. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  873. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  874. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  875. flexmock(module).should_receive('expand_directories').and_return(())
  876. flexmock(module).should_receive('pattern_root_directories').and_return([])
  877. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  878. flexmock(module).should_receive('expand_home_directories').and_return(())
  879. flexmock(module).should_receive('write_pattern_file').and_return(None)
  880. flexmock(module.feature).should_receive('available').and_return(feature_available)
  881. flexmock(module).should_receive('ensure_files_readable')
  882. flexmock(module).should_receive('make_pattern_flags').and_return(())
  883. flexmock(module).should_receive('make_exclude_flags').and_return(())
  884. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  885. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  886. )
  887. flexmock(module.environment).should_receive('make_environment')
  888. flexmock(module).should_receive('execute_command').with_args(
  889. ('borg', 'create', option_flag) + REPO_ARCHIVE_WITH_PATHS,
  890. output_log_level=logging.INFO,
  891. output_file=None,
  892. borg_local_path='borg',
  893. working_directory=None,
  894. extra_environment=None,
  895. )
  896. module.create_archive(
  897. dry_run=False,
  898. repository='repo',
  899. location_config={
  900. 'source_directories': ['foo', 'bar'],
  901. 'repositories': ['repo'],
  902. 'numeric_ids': True,
  903. 'exclude_patterns': None,
  904. },
  905. storage_config={},
  906. local_borg_version='1.2.3',
  907. )
  908. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  909. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  910. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  911. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  912. flexmock(module).should_receive('expand_directories').and_return(())
  913. flexmock(module).should_receive('pattern_root_directories').and_return([])
  914. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  915. flexmock(module).should_receive('expand_home_directories').and_return(())
  916. flexmock(module).should_receive('write_pattern_file').and_return(None)
  917. flexmock(module.feature).should_receive('available').and_return(True)
  918. flexmock(module).should_receive('ensure_files_readable')
  919. flexmock(module).should_receive('make_pattern_flags').and_return(())
  920. flexmock(module).should_receive('make_exclude_flags').and_return(())
  921. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  922. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  923. )
  924. flexmock(module.environment).should_receive('make_environment')
  925. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  926. create_command = ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS
  927. flexmock(module).should_receive('execute_command').with_args(
  928. create_command + ('--dry-run', '--list'),
  929. output_log_level=logging.INFO,
  930. output_file=None,
  931. borg_local_path='borg',
  932. working_directory=None,
  933. extra_environment=None,
  934. )
  935. flexmock(module).should_receive('execute_command').with_args(
  936. create_command,
  937. output_log_level=logging.INFO,
  938. output_file=None,
  939. borg_local_path='borg',
  940. working_directory=None,
  941. extra_environment=None,
  942. )
  943. module.create_archive(
  944. dry_run=False,
  945. repository='repo',
  946. location_config={
  947. 'source_directories': ['foo', 'bar'],
  948. 'repositories': ['repo'],
  949. 'read_special': True,
  950. 'exclude_patterns': None,
  951. },
  952. storage_config={},
  953. local_borg_version='1.2.3',
  954. )
  955. def test_create_archive_with_read_special_adds_special_files_to_excludes():
  956. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  957. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  958. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  959. flexmock(module).should_receive('expand_directories').and_return(())
  960. flexmock(module).should_receive('pattern_root_directories').and_return([])
  961. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  962. flexmock(module).should_receive('expand_home_directories').and_return(())
  963. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  964. None
  965. ).and_return(flexmock(name='/excludes'))
  966. flexmock(module.feature).should_receive('available').and_return(True)
  967. flexmock(module).should_receive('ensure_files_readable')
  968. flexmock(module).should_receive('make_pattern_flags').and_return(())
  969. flexmock(module).should_receive('make_exclude_flags').and_return(()).and_return(
  970. '--exclude-from', '/excludes'
  971. )
  972. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  973. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  974. )
  975. flexmock(module.environment).should_receive('make_environment')
  976. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  977. create_command = ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS
  978. flexmock(module).should_receive('execute_command').with_args(
  979. create_command + ('--dry-run', '--list'),
  980. output_log_level=logging.INFO,
  981. output_file=None,
  982. borg_local_path='borg',
  983. working_directory=None,
  984. extra_environment=None,
  985. )
  986. flexmock(module).should_receive('execute_command').with_args(
  987. create_command + ('--exclude-from', '/excludes'),
  988. output_log_level=logging.INFO,
  989. output_file=None,
  990. borg_local_path='borg',
  991. working_directory=None,
  992. extra_environment=None,
  993. )
  994. module.create_archive(
  995. dry_run=False,
  996. repository='repo',
  997. location_config={
  998. 'source_directories': ['foo', 'bar'],
  999. 'repositories': ['repo'],
  1000. 'read_special': True,
  1001. 'exclude_patterns': None,
  1002. },
  1003. storage_config={},
  1004. local_borg_version='1.2.3',
  1005. )
  1006. @pytest.mark.parametrize(
  1007. 'option_name,option_value',
  1008. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  1009. )
  1010. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  1011. option_name, option_value
  1012. ):
  1013. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  1014. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1015. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1016. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1017. flexmock(module).should_receive('expand_directories').and_return(())
  1018. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1019. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1020. flexmock(module).should_receive('expand_home_directories').and_return(())
  1021. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1022. flexmock(module.feature).should_receive('available').and_return(True)
  1023. flexmock(module).should_receive('ensure_files_readable')
  1024. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1025. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1026. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1027. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1028. )
  1029. flexmock(module.environment).should_receive('make_environment')
  1030. flexmock(module).should_receive('execute_command').with_args(
  1031. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1032. output_log_level=logging.INFO,
  1033. output_file=None,
  1034. borg_local_path='borg',
  1035. working_directory=None,
  1036. extra_environment=None,
  1037. )
  1038. module.create_archive(
  1039. dry_run=False,
  1040. repository='repo',
  1041. location_config={
  1042. 'source_directories': ['foo', 'bar'],
  1043. 'repositories': ['repo'],
  1044. option_name: option_value,
  1045. 'exclude_patterns': None,
  1046. },
  1047. storage_config={},
  1048. local_borg_version='1.2.3',
  1049. )
  1050. @pytest.mark.parametrize(
  1051. 'option_value,feature_available,option_flag',
  1052. (
  1053. (True, True, '--atime'),
  1054. (True, False, None),
  1055. (False, True, None),
  1056. (False, False, '--noatime'),
  1057. ),
  1058. )
  1059. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  1060. option_value, feature_available, option_flag
  1061. ):
  1062. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1063. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1064. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1065. flexmock(module).should_receive('expand_directories').and_return(())
  1066. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1067. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1068. flexmock(module).should_receive('expand_home_directories').and_return(())
  1069. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1070. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1071. flexmock(module).should_receive('ensure_files_readable')
  1072. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1073. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1074. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1075. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1076. )
  1077. flexmock(module.environment).should_receive('make_environment')
  1078. flexmock(module).should_receive('execute_command').with_args(
  1079. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1080. output_log_level=logging.INFO,
  1081. output_file=None,
  1082. borg_local_path='borg',
  1083. working_directory=None,
  1084. extra_environment=None,
  1085. )
  1086. module.create_archive(
  1087. dry_run=False,
  1088. repository='repo',
  1089. location_config={
  1090. 'source_directories': ['foo', 'bar'],
  1091. 'repositories': ['repo'],
  1092. 'atime': option_value,
  1093. 'exclude_patterns': None,
  1094. },
  1095. storage_config={},
  1096. local_borg_version='1.2.3',
  1097. )
  1098. @pytest.mark.parametrize(
  1099. 'option_value,feature_available,option_flag',
  1100. (
  1101. (True, True, None),
  1102. (True, False, None),
  1103. (False, True, '--noflags'),
  1104. (False, False, '--nobsdflags'),
  1105. ),
  1106. )
  1107. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  1108. option_value, feature_available, option_flag
  1109. ):
  1110. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1111. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1112. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1113. flexmock(module).should_receive('expand_directories').and_return(())
  1114. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1115. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1116. flexmock(module).should_receive('expand_home_directories').and_return(())
  1117. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1118. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1119. flexmock(module).should_receive('ensure_files_readable')
  1120. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1121. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1122. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1123. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1124. )
  1125. flexmock(module.environment).should_receive('make_environment')
  1126. flexmock(module).should_receive('execute_command').with_args(
  1127. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1128. output_log_level=logging.INFO,
  1129. output_file=None,
  1130. borg_local_path='borg',
  1131. working_directory=None,
  1132. extra_environment=None,
  1133. )
  1134. module.create_archive(
  1135. dry_run=False,
  1136. repository='repo',
  1137. location_config={
  1138. 'source_directories': ['foo', 'bar'],
  1139. 'repositories': ['repo'],
  1140. 'flags': option_value,
  1141. 'exclude_patterns': None,
  1142. },
  1143. storage_config={},
  1144. local_borg_version='1.2.3',
  1145. )
  1146. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  1147. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1148. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1149. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1150. flexmock(module).should_receive('expand_directories').and_return(())
  1151. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1152. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1153. flexmock(module).should_receive('expand_home_directories').and_return(())
  1154. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1155. flexmock(module.feature).should_receive('available').and_return(True)
  1156. flexmock(module).should_receive('ensure_files_readable')
  1157. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1158. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1159. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1160. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1161. )
  1162. flexmock(module.environment).should_receive('make_environment')
  1163. flexmock(module).should_receive('execute_command').with_args(
  1164. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  1165. output_log_level=logging.INFO,
  1166. output_file=None,
  1167. borg_local_path='borg',
  1168. working_directory=None,
  1169. extra_environment=None,
  1170. )
  1171. module.create_archive(
  1172. dry_run=False,
  1173. repository='repo',
  1174. location_config={
  1175. 'source_directories': ['foo', 'bar'],
  1176. 'repositories': ['repo'],
  1177. 'files_cache': 'ctime,size',
  1178. 'exclude_patterns': None,
  1179. },
  1180. storage_config={},
  1181. local_borg_version='1.2.3',
  1182. )
  1183. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1184. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1185. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1186. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1187. flexmock(module).should_receive('expand_directories').and_return(())
  1188. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1189. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1190. flexmock(module).should_receive('expand_home_directories').and_return(())
  1191. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1192. flexmock(module.feature).should_receive('available').and_return(True)
  1193. flexmock(module).should_receive('ensure_files_readable')
  1194. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1195. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1196. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1197. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1198. )
  1199. flexmock(module.environment).should_receive('make_environment')
  1200. flexmock(module).should_receive('execute_command').with_args(
  1201. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1202. output_log_level=logging.INFO,
  1203. output_file=None,
  1204. borg_local_path='borg1',
  1205. working_directory=None,
  1206. extra_environment=None,
  1207. )
  1208. module.create_archive(
  1209. dry_run=False,
  1210. repository='repo',
  1211. location_config={
  1212. 'source_directories': ['foo', 'bar'],
  1213. 'repositories': ['repo'],
  1214. 'exclude_patterns': None,
  1215. },
  1216. storage_config={},
  1217. local_borg_version='1.2.3',
  1218. local_path='borg1',
  1219. )
  1220. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1221. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1222. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1223. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1224. flexmock(module).should_receive('expand_directories').and_return(())
  1225. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1226. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1227. flexmock(module).should_receive('expand_home_directories').and_return(())
  1228. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1229. flexmock(module.feature).should_receive('available').and_return(True)
  1230. flexmock(module).should_receive('ensure_files_readable')
  1231. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1232. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1233. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1234. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1235. )
  1236. flexmock(module.environment).should_receive('make_environment')
  1237. flexmock(module).should_receive('execute_command').with_args(
  1238. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1239. output_log_level=logging.INFO,
  1240. output_file=None,
  1241. borg_local_path='borg',
  1242. working_directory=None,
  1243. extra_environment=None,
  1244. )
  1245. module.create_archive(
  1246. dry_run=False,
  1247. repository='repo',
  1248. location_config={
  1249. 'source_directories': ['foo', 'bar'],
  1250. 'repositories': ['repo'],
  1251. 'exclude_patterns': None,
  1252. },
  1253. storage_config={},
  1254. local_borg_version='1.2.3',
  1255. remote_path='borg1',
  1256. )
  1257. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1258. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1259. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1260. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1261. flexmock(module).should_receive('expand_directories').and_return(())
  1262. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1263. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1264. flexmock(module).should_receive('expand_home_directories').and_return(())
  1265. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1266. flexmock(module.feature).should_receive('available').and_return(True)
  1267. flexmock(module).should_receive('ensure_files_readable')
  1268. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1269. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1270. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1271. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1272. )
  1273. flexmock(module.environment).should_receive('make_environment')
  1274. flexmock(module).should_receive('execute_command').with_args(
  1275. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1276. output_log_level=logging.INFO,
  1277. output_file=None,
  1278. borg_local_path='borg',
  1279. working_directory=None,
  1280. extra_environment=None,
  1281. )
  1282. module.create_archive(
  1283. dry_run=False,
  1284. repository='repo',
  1285. location_config={
  1286. 'source_directories': ['foo', 'bar'],
  1287. 'repositories': ['repo'],
  1288. 'exclude_patterns': None,
  1289. },
  1290. storage_config={'umask': 740},
  1291. local_borg_version='1.2.3',
  1292. )
  1293. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1294. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1295. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1296. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1297. flexmock(module).should_receive('expand_directories').and_return(())
  1298. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1299. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1300. flexmock(module).should_receive('expand_home_directories').and_return(())
  1301. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1302. flexmock(module.feature).should_receive('available').and_return(True)
  1303. flexmock(module).should_receive('ensure_files_readable')
  1304. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1305. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1306. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1307. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1308. )
  1309. flexmock(module.environment).should_receive('make_environment')
  1310. flexmock(module).should_receive('execute_command').with_args(
  1311. ('borg', 'create', '--lock-wait', '5') + REPO_ARCHIVE_WITH_PATHS,
  1312. output_log_level=logging.INFO,
  1313. output_file=None,
  1314. borg_local_path='borg',
  1315. working_directory=None,
  1316. extra_environment=None,
  1317. )
  1318. module.create_archive(
  1319. dry_run=False,
  1320. repository='repo',
  1321. location_config={
  1322. 'source_directories': ['foo', 'bar'],
  1323. 'repositories': ['repo'],
  1324. 'exclude_patterns': None,
  1325. },
  1326. storage_config={'lock_wait': 5},
  1327. local_borg_version='1.2.3',
  1328. )
  1329. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  1330. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1331. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1332. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1333. flexmock(module).should_receive('expand_directories').and_return(())
  1334. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1335. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1336. flexmock(module).should_receive('expand_home_directories').and_return(())
  1337. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1338. flexmock(module.feature).should_receive('available').and_return(True)
  1339. flexmock(module).should_receive('ensure_files_readable')
  1340. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1341. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1342. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1343. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1344. )
  1345. flexmock(module.environment).should_receive('make_environment')
  1346. flexmock(module).should_receive('execute_command').with_args(
  1347. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--stats',),
  1348. output_log_level=logging.WARNING,
  1349. output_file=None,
  1350. borg_local_path='borg',
  1351. working_directory=None,
  1352. extra_environment=None,
  1353. )
  1354. module.create_archive(
  1355. dry_run=False,
  1356. repository='repo',
  1357. location_config={
  1358. 'source_directories': ['foo', 'bar'],
  1359. 'repositories': ['repo'],
  1360. 'exclude_patterns': None,
  1361. },
  1362. storage_config={},
  1363. local_borg_version='1.2.3',
  1364. stats=True,
  1365. )
  1366. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  1367. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1368. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1369. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1370. flexmock(module).should_receive('expand_directories').and_return(())
  1371. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1372. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1373. flexmock(module).should_receive('expand_home_directories').and_return(())
  1374. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1375. flexmock(module.feature).should_receive('available').and_return(True)
  1376. flexmock(module).should_receive('ensure_files_readable')
  1377. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1378. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1379. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1380. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1381. )
  1382. flexmock(module.environment).should_receive('make_environment')
  1383. flexmock(module).should_receive('execute_command').with_args(
  1384. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info', '--stats'),
  1385. output_log_level=logging.INFO,
  1386. output_file=None,
  1387. borg_local_path='borg',
  1388. working_directory=None,
  1389. extra_environment=None,
  1390. )
  1391. insert_logging_mock(logging.INFO)
  1392. module.create_archive(
  1393. dry_run=False,
  1394. repository='repo',
  1395. location_config={
  1396. 'source_directories': ['foo', 'bar'],
  1397. 'repositories': ['repo'],
  1398. 'exclude_patterns': None,
  1399. },
  1400. storage_config={},
  1401. local_borg_version='1.2.3',
  1402. stats=True,
  1403. )
  1404. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  1405. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1406. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1407. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1408. flexmock(module).should_receive('expand_directories').and_return(())
  1409. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1410. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1411. flexmock(module).should_receive('expand_home_directories').and_return(())
  1412. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1413. flexmock(module.feature).should_receive('available').and_return(True)
  1414. flexmock(module).should_receive('ensure_files_readable')
  1415. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1416. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1417. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1418. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1419. )
  1420. flexmock(module.environment).should_receive('make_environment')
  1421. flexmock(module).should_receive('execute_command').with_args(
  1422. ('borg', 'create', '--list', '--filter', 'AME-') + REPO_ARCHIVE_WITH_PATHS,
  1423. output_log_level=logging.WARNING,
  1424. output_file=None,
  1425. borg_local_path='borg',
  1426. working_directory=None,
  1427. extra_environment=None,
  1428. )
  1429. module.create_archive(
  1430. dry_run=False,
  1431. repository='repo',
  1432. location_config={
  1433. 'source_directories': ['foo', 'bar'],
  1434. 'repositories': ['repo'],
  1435. 'exclude_patterns': None,
  1436. },
  1437. storage_config={},
  1438. local_borg_version='1.2.3',
  1439. list_files=True,
  1440. )
  1441. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1442. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1443. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1444. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1445. flexmock(module).should_receive('expand_directories').and_return(())
  1446. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1447. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1448. flexmock(module).should_receive('expand_home_directories').and_return(())
  1449. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1450. flexmock(module.feature).should_receive('available').and_return(True)
  1451. flexmock(module).should_receive('ensure_files_readable')
  1452. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1453. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1454. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1455. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1456. )
  1457. flexmock(module.environment).should_receive('make_environment')
  1458. flexmock(module).should_receive('execute_command').with_args(
  1459. ('borg', 'create', '--list', '--filter', 'AME-') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  1460. output_log_level=logging.INFO,
  1461. output_file=None,
  1462. borg_local_path='borg',
  1463. working_directory=None,
  1464. extra_environment=None,
  1465. )
  1466. insert_logging_mock(logging.INFO)
  1467. module.create_archive(
  1468. dry_run=False,
  1469. repository='repo',
  1470. location_config={
  1471. 'source_directories': ['foo', 'bar'],
  1472. 'repositories': ['repo'],
  1473. 'exclude_patterns': None,
  1474. },
  1475. storage_config={},
  1476. local_borg_version='1.2.3',
  1477. list_files=True,
  1478. )
  1479. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1480. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1481. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1482. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1483. flexmock(module).should_receive('expand_directories').and_return(())
  1484. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1485. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1486. flexmock(module).should_receive('expand_home_directories').and_return(())
  1487. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1488. flexmock(module.feature).should_receive('available').and_return(True)
  1489. flexmock(module).should_receive('ensure_files_readable')
  1490. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1491. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1492. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1493. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1494. )
  1495. flexmock(module.environment).should_receive('make_environment')
  1496. flexmock(module).should_receive('execute_command').with_args(
  1497. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info', '--progress',),
  1498. output_log_level=logging.INFO,
  1499. output_file=module.DO_NOT_CAPTURE,
  1500. borg_local_path='borg',
  1501. working_directory=None,
  1502. extra_environment=None,
  1503. )
  1504. insert_logging_mock(logging.INFO)
  1505. module.create_archive(
  1506. dry_run=False,
  1507. repository='repo',
  1508. location_config={
  1509. 'source_directories': ['foo', 'bar'],
  1510. 'repositories': ['repo'],
  1511. 'exclude_patterns': None,
  1512. },
  1513. storage_config={},
  1514. local_borg_version='1.2.3',
  1515. progress=True,
  1516. )
  1517. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1518. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1519. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1520. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1521. flexmock(module).should_receive('expand_directories').and_return(())
  1522. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1523. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1524. flexmock(module).should_receive('expand_home_directories').and_return(())
  1525. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1526. flexmock(module.feature).should_receive('available').and_return(True)
  1527. flexmock(module).should_receive('ensure_files_readable')
  1528. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1529. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1530. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1531. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1532. )
  1533. flexmock(module.environment).should_receive('make_environment')
  1534. flexmock(module).should_receive('execute_command').with_args(
  1535. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--progress',),
  1536. output_log_level=logging.INFO,
  1537. output_file=module.DO_NOT_CAPTURE,
  1538. borg_local_path='borg',
  1539. working_directory=None,
  1540. extra_environment=None,
  1541. )
  1542. module.create_archive(
  1543. dry_run=False,
  1544. repository='repo',
  1545. location_config={
  1546. 'source_directories': ['foo', 'bar'],
  1547. 'repositories': ['repo'],
  1548. 'exclude_patterns': None,
  1549. },
  1550. storage_config={},
  1551. local_borg_version='1.2.3',
  1552. progress=True,
  1553. )
  1554. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1555. processes = flexmock()
  1556. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1557. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1558. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1559. flexmock(module).should_receive('expand_directories').and_return(())
  1560. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1561. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1562. flexmock(module).should_receive('expand_home_directories').and_return(())
  1563. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1564. flexmock(module.feature).should_receive('available').and_return(True)
  1565. flexmock(module).should_receive('ensure_files_readable')
  1566. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1567. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1568. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1569. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1570. )
  1571. flexmock(module.environment).should_receive('make_environment')
  1572. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1573. create_command = (
  1574. ('borg', 'create', '--one-file-system', '--read-special')
  1575. + REPO_ARCHIVE_WITH_PATHS
  1576. + ('--progress',)
  1577. )
  1578. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1579. create_command + ('--dry-run', '--list'),
  1580. processes=processes,
  1581. output_log_level=logging.INFO,
  1582. output_file=module.DO_NOT_CAPTURE,
  1583. borg_local_path='borg',
  1584. working_directory=None,
  1585. extra_environment=None,
  1586. )
  1587. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1588. create_command,
  1589. processes=processes,
  1590. output_log_level=logging.INFO,
  1591. output_file=module.DO_NOT_CAPTURE,
  1592. borg_local_path='borg',
  1593. working_directory=None,
  1594. extra_environment=None,
  1595. )
  1596. module.create_archive(
  1597. dry_run=False,
  1598. repository='repo',
  1599. location_config={
  1600. 'source_directories': ['foo', 'bar'],
  1601. 'repositories': ['repo'],
  1602. 'exclude_patterns': None,
  1603. },
  1604. storage_config={},
  1605. local_borg_version='1.2.3',
  1606. progress=True,
  1607. stream_processes=processes,
  1608. )
  1609. def test_create_archive_with_stream_processes_ignores_read_special_false_and_logs_warnings():
  1610. processes = flexmock()
  1611. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1612. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1613. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1614. flexmock(module).should_receive('expand_directories').and_return(())
  1615. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1616. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1617. flexmock(module).should_receive('expand_home_directories').and_return(())
  1618. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1619. flexmock(module.feature).should_receive('available').and_return(True)
  1620. flexmock(module).should_receive('ensure_files_readable')
  1621. flexmock(module.logger).should_receive('warning').twice()
  1622. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1623. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1624. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1625. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1626. )
  1627. flexmock(module.environment).should_receive('make_environment')
  1628. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1629. create_command = (
  1630. 'borg',
  1631. 'create',
  1632. '--one-file-system',
  1633. '--read-special',
  1634. ) + REPO_ARCHIVE_WITH_PATHS
  1635. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1636. create_command + ('--dry-run', '--list'),
  1637. processes=processes,
  1638. output_log_level=logging.INFO,
  1639. output_file=None,
  1640. borg_local_path='borg',
  1641. working_directory=None,
  1642. extra_environment=None,
  1643. )
  1644. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1645. create_command,
  1646. processes=processes,
  1647. output_log_level=logging.INFO,
  1648. output_file=None,
  1649. borg_local_path='borg',
  1650. working_directory=None,
  1651. extra_environment=None,
  1652. )
  1653. module.create_archive(
  1654. dry_run=False,
  1655. repository='repo',
  1656. location_config={
  1657. 'source_directories': ['foo', 'bar'],
  1658. 'repositories': ['repo'],
  1659. 'exclude_patterns': None,
  1660. 'read_special': False,
  1661. },
  1662. storage_config={},
  1663. local_borg_version='1.2.3',
  1664. stream_processes=processes,
  1665. )
  1666. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1667. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1668. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1669. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1670. flexmock(module).should_receive('expand_directories').and_return(())
  1671. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1672. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1673. flexmock(module).should_receive('expand_home_directories').and_return(())
  1674. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1675. flexmock(module.feature).should_receive('available').and_return(True)
  1676. flexmock(module).should_receive('ensure_files_readable')
  1677. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1678. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1679. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1680. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1681. )
  1682. flexmock(module.environment).should_receive('make_environment')
  1683. flexmock(module).should_receive('execute_command').with_args(
  1684. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1685. output_log_level=None,
  1686. output_file=None,
  1687. borg_local_path='borg',
  1688. working_directory=None,
  1689. extra_environment=None,
  1690. ).and_return('[]')
  1691. json_output = module.create_archive(
  1692. dry_run=False,
  1693. repository='repo',
  1694. location_config={
  1695. 'source_directories': ['foo', 'bar'],
  1696. 'repositories': ['repo'],
  1697. 'exclude_patterns': None,
  1698. },
  1699. storage_config={},
  1700. local_borg_version='1.2.3',
  1701. json=True,
  1702. )
  1703. assert json_output == '[]'
  1704. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1705. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1706. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1707. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1708. flexmock(module).should_receive('expand_directories').and_return(())
  1709. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1710. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1711. flexmock(module).should_receive('expand_home_directories').and_return(())
  1712. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1713. flexmock(module.feature).should_receive('available').and_return(True)
  1714. flexmock(module).should_receive('ensure_files_readable')
  1715. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1716. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1717. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1718. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1719. )
  1720. flexmock(module.environment).should_receive('make_environment')
  1721. flexmock(module).should_receive('execute_command').with_args(
  1722. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1723. output_log_level=None,
  1724. output_file=None,
  1725. borg_local_path='borg',
  1726. working_directory=None,
  1727. extra_environment=None,
  1728. ).and_return('[]')
  1729. json_output = module.create_archive(
  1730. dry_run=False,
  1731. repository='repo',
  1732. location_config={
  1733. 'source_directories': ['foo', 'bar'],
  1734. 'repositories': ['repo'],
  1735. 'exclude_patterns': None,
  1736. },
  1737. storage_config={},
  1738. local_borg_version='1.2.3',
  1739. json=True,
  1740. stats=True,
  1741. )
  1742. assert json_output == '[]'
  1743. def test_create_archive_with_source_directories_glob_expands():
  1744. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1745. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1746. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1747. flexmock(module).should_receive('expand_directories').and_return(())
  1748. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1749. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1750. flexmock(module).should_receive('expand_home_directories').and_return(())
  1751. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1752. flexmock(module.feature).should_receive('available').and_return(True)
  1753. flexmock(module).should_receive('ensure_files_readable')
  1754. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1755. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1756. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1757. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1758. )
  1759. flexmock(module.environment).should_receive('make_environment')
  1760. flexmock(module).should_receive('execute_command').with_args(
  1761. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1762. output_log_level=logging.INFO,
  1763. output_file=None,
  1764. borg_local_path='borg',
  1765. working_directory=None,
  1766. extra_environment=None,
  1767. )
  1768. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1769. module.create_archive(
  1770. dry_run=False,
  1771. repository='repo',
  1772. location_config={
  1773. 'source_directories': ['foo*'],
  1774. 'repositories': ['repo'],
  1775. 'exclude_patterns': None,
  1776. },
  1777. storage_config={},
  1778. local_borg_version='1.2.3',
  1779. )
  1780. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1781. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1782. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1783. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1784. flexmock(module).should_receive('expand_directories').and_return(())
  1785. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1786. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1787. flexmock(module).should_receive('expand_home_directories').and_return(())
  1788. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1789. flexmock(module.feature).should_receive('available').and_return(True)
  1790. flexmock(module).should_receive('ensure_files_readable')
  1791. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1792. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1793. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1794. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1795. )
  1796. flexmock(module.environment).should_receive('make_environment')
  1797. flexmock(module).should_receive('execute_command').with_args(
  1798. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1799. output_log_level=logging.INFO,
  1800. output_file=None,
  1801. borg_local_path='borg',
  1802. working_directory=None,
  1803. extra_environment=None,
  1804. )
  1805. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1806. module.create_archive(
  1807. dry_run=False,
  1808. repository='repo',
  1809. location_config={
  1810. 'source_directories': ['foo*'],
  1811. 'repositories': ['repo'],
  1812. 'exclude_patterns': None,
  1813. },
  1814. storage_config={},
  1815. local_borg_version='1.2.3',
  1816. )
  1817. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1818. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1819. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1820. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1821. flexmock(module).should_receive('expand_directories').and_return(())
  1822. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1823. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1824. flexmock(module).should_receive('expand_home_directories').and_return(())
  1825. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1826. flexmock(module.feature).should_receive('available').and_return(True)
  1827. flexmock(module).should_receive('ensure_files_readable')
  1828. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1829. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1830. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1831. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1832. )
  1833. flexmock(module.environment).should_receive('make_environment')
  1834. flexmock(module).should_receive('execute_command').with_args(
  1835. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1836. output_log_level=logging.INFO,
  1837. output_file=None,
  1838. borg_local_path='borg',
  1839. working_directory=None,
  1840. extra_environment=None,
  1841. )
  1842. module.create_archive(
  1843. dry_run=False,
  1844. repository='repo',
  1845. location_config={
  1846. 'source_directories': ['foo*'],
  1847. 'repositories': ['repo'],
  1848. 'exclude_patterns': None,
  1849. },
  1850. storage_config={},
  1851. local_borg_version='1.2.3',
  1852. )
  1853. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1854. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1855. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1856. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1857. flexmock(module).should_receive('expand_directories').and_return(())
  1858. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1859. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1860. flexmock(module).should_receive('expand_home_directories').and_return(())
  1861. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1862. flexmock(module.feature).should_receive('available').and_return(True)
  1863. flexmock(module).should_receive('ensure_files_readable')
  1864. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1865. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1866. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1867. ('repo::ARCHIVE_NAME',)
  1868. )
  1869. flexmock(module.environment).should_receive('make_environment')
  1870. flexmock(module).should_receive('execute_command').with_args(
  1871. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1872. output_log_level=logging.INFO,
  1873. output_file=None,
  1874. borg_local_path='borg',
  1875. working_directory=None,
  1876. extra_environment=None,
  1877. )
  1878. module.create_archive(
  1879. dry_run=False,
  1880. repository='repo',
  1881. location_config={
  1882. 'source_directories': ['foo', 'bar'],
  1883. 'repositories': ['repo'],
  1884. 'exclude_patterns': None,
  1885. },
  1886. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1887. local_borg_version='1.2.3',
  1888. )
  1889. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1890. repository_archive_pattern = 'repo::Documents_{hostname}-{now}'
  1891. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1892. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1893. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1894. flexmock(module).should_receive('expand_directories').and_return(())
  1895. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1896. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1897. flexmock(module).should_receive('expand_home_directories').and_return(())
  1898. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1899. flexmock(module.feature).should_receive('available').and_return(True)
  1900. flexmock(module).should_receive('ensure_files_readable')
  1901. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1902. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1903. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1904. (repository_archive_pattern,)
  1905. )
  1906. flexmock(module.environment).should_receive('make_environment')
  1907. flexmock(module).should_receive('execute_command').with_args(
  1908. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1909. output_log_level=logging.INFO,
  1910. output_file=None,
  1911. borg_local_path='borg',
  1912. working_directory=None,
  1913. extra_environment=None,
  1914. )
  1915. module.create_archive(
  1916. dry_run=False,
  1917. repository='repo',
  1918. location_config={
  1919. 'source_directories': ['foo', 'bar'],
  1920. 'repositories': ['repo'],
  1921. 'exclude_patterns': None,
  1922. },
  1923. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1924. local_borg_version='1.2.3',
  1925. )
  1926. def test_create_archive_with_repository_accepts_borg_placeholders():
  1927. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}'
  1928. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1929. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1930. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1931. flexmock(module).should_receive('expand_directories').and_return(())
  1932. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1933. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1934. flexmock(module).should_receive('expand_home_directories').and_return(())
  1935. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1936. flexmock(module.feature).should_receive('available').and_return(True)
  1937. flexmock(module).should_receive('ensure_files_readable')
  1938. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1939. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1940. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1941. (repository_archive_pattern,)
  1942. )
  1943. flexmock(module.environment).should_receive('make_environment')
  1944. flexmock(module).should_receive('execute_command').with_args(
  1945. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1946. output_log_level=logging.INFO,
  1947. output_file=None,
  1948. borg_local_path='borg',
  1949. working_directory=None,
  1950. extra_environment=None,
  1951. )
  1952. module.create_archive(
  1953. dry_run=False,
  1954. repository='{fqdn}',
  1955. location_config={
  1956. 'source_directories': ['foo', 'bar'],
  1957. 'repositories': ['{fqdn}'],
  1958. 'exclude_patterns': None,
  1959. },
  1960. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1961. local_borg_version='1.2.3',
  1962. )
  1963. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1964. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1965. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1966. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1967. flexmock(module).should_receive('expand_directories').and_return(())
  1968. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1969. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1970. flexmock(module).should_receive('expand_home_directories').and_return(())
  1971. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1972. flexmock(module.feature).should_receive('available').and_return(True)
  1973. flexmock(module).should_receive('ensure_files_readable')
  1974. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1975. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1976. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1977. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1978. )
  1979. flexmock(module.environment).should_receive('make_environment')
  1980. flexmock(module).should_receive('execute_command').with_args(
  1981. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  1982. output_log_level=logging.INFO,
  1983. output_file=None,
  1984. borg_local_path='borg',
  1985. working_directory=None,
  1986. extra_environment=None,
  1987. )
  1988. module.create_archive(
  1989. dry_run=False,
  1990. repository='repo',
  1991. location_config={
  1992. 'source_directories': ['foo', 'bar'],
  1993. 'repositories': ['repo'],
  1994. 'exclude_patterns': None,
  1995. },
  1996. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1997. local_borg_version='1.2.3',
  1998. )
  1999. def test_create_archive_with_stream_processes_calls_borg_with_processes_and_read_special():
  2000. processes = flexmock()
  2001. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2002. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2003. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2004. flexmock(module).should_receive('expand_directories').and_return(())
  2005. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2006. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2007. flexmock(module).should_receive('expand_home_directories').and_return(())
  2008. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2009. flexmock(module.feature).should_receive('available').and_return(True)
  2010. flexmock(module).should_receive('ensure_files_readable')
  2011. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2012. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2013. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2014. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2015. )
  2016. flexmock(module.environment).should_receive('make_environment')
  2017. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  2018. create_command = (
  2019. 'borg',
  2020. 'create',
  2021. '--one-file-system',
  2022. '--read-special',
  2023. ) + REPO_ARCHIVE_WITH_PATHS
  2024. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2025. create_command + ('--dry-run', 'list'),
  2026. processes=processes,
  2027. output_log_level=logging.INFO,
  2028. output_file=None,
  2029. borg_local_path='borg',
  2030. working_directory=None,
  2031. extra_environment=None,
  2032. )
  2033. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2034. create_command,
  2035. processes=processes,
  2036. output_log_level=logging.INFO,
  2037. output_file=None,
  2038. borg_local_path='borg',
  2039. working_directory=None,
  2040. extra_environment=None,
  2041. )
  2042. module.create_archive(
  2043. dry_run=False,
  2044. repository='repo',
  2045. location_config={
  2046. 'source_directories': ['foo', 'bar'],
  2047. 'repositories': ['repo'],
  2048. 'exclude_patterns': None,
  2049. },
  2050. storage_config={},
  2051. local_borg_version='1.2.3',
  2052. stream_processes=processes,
  2053. )