test_create.py 130 KB

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