test_create.py 124 KB

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