test_create.py 123 KB

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