test_create.py 116 KB

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