test_create.py 125 KB

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