test_create.py 115 KB

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