test_create.py 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  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_exclude_patterns_calls_borg_with_excludes():
  425. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  426. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  427. exclude_flags = ('--exclude-from', 'excludes')
  428. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  429. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  430. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  431. flexmock(module).should_receive('expand_directories').and_return(())
  432. flexmock(module).should_receive('pattern_root_directories').and_return([])
  433. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  434. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  435. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  436. flexmock(name='/tmp/excludes')
  437. )
  438. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  439. flexmock(module.feature).should_receive('available').and_return(True)
  440. flexmock(module).should_receive('ensure_files_readable')
  441. flexmock(module).should_receive('make_pattern_flags').and_return(())
  442. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  443. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  444. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  445. )
  446. flexmock(module.environment).should_receive('make_environment')
  447. flexmock(module).should_receive('execute_command').with_args(
  448. ('borg', 'create') + exclude_flags + REPO_ARCHIVE_WITH_PATHS,
  449. output_log_level=logging.INFO,
  450. output_file=None,
  451. borg_local_path='borg',
  452. working_directory=None,
  453. extra_environment=None,
  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. 'exclude_patterns': ['exclude'],
  462. },
  463. storage_config={},
  464. local_borg_version='1.2.3',
  465. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  466. )
  467. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  468. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  469. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  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(())
  477. flexmock(module).should_receive('write_pattern_file').and_return(None)
  478. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  479. flexmock(module.feature).should_receive('available').and_return(True)
  480. flexmock(module).should_receive('ensure_files_readable')
  481. flexmock(module).should_receive('make_pattern_flags').and_return(())
  482. flexmock(module).should_receive('make_exclude_flags').and_return(())
  483. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  484. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  485. )
  486. flexmock(module.environment).should_receive('make_environment')
  487. flexmock(module).should_receive('execute_command').with_args(
  488. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  489. output_log_level=logging.INFO,
  490. output_file=None,
  491. borg_local_path='borg',
  492. working_directory=None,
  493. extra_environment=None,
  494. )
  495. insert_logging_mock(logging.INFO)
  496. module.create_archive(
  497. dry_run=False,
  498. repository_path='repo',
  499. location_config={
  500. 'source_directories': ['foo', 'bar'],
  501. 'repositories': ['repo'],
  502. 'exclude_patterns': None,
  503. },
  504. storage_config={},
  505. local_borg_version='1.2.3',
  506. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  507. )
  508. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  509. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  510. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  511. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  512. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  513. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  514. flexmock(module).should_receive('expand_directories').and_return(())
  515. flexmock(module).should_receive('pattern_root_directories').and_return([])
  516. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  517. flexmock(module).should_receive('expand_home_directories').and_return(())
  518. flexmock(module).should_receive('write_pattern_file').and_return(None)
  519. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  520. flexmock(module.feature).should_receive('available').and_return(True)
  521. flexmock(module).should_receive('ensure_files_readable')
  522. flexmock(module).should_receive('make_pattern_flags').and_return(())
  523. flexmock(module).should_receive('make_exclude_flags').and_return(())
  524. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  525. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  526. )
  527. flexmock(module.environment).should_receive('make_environment')
  528. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  529. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  530. working_directory=None,
  531. extra_environment=None,
  532. )
  533. insert_logging_mock(logging.INFO)
  534. module.create_archive(
  535. dry_run=False,
  536. repository_path='repo',
  537. location_config={
  538. 'source_directories': ['foo', 'bar'],
  539. 'repositories': ['repo'],
  540. 'exclude_patterns': None,
  541. },
  542. storage_config={},
  543. local_borg_version='1.2.3',
  544. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  545. json=True,
  546. )
  547. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  548. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  549. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  550. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  551. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  552. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  553. flexmock(module).should_receive('expand_directories').and_return(())
  554. flexmock(module).should_receive('pattern_root_directories').and_return([])
  555. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  556. flexmock(module).should_receive('expand_home_directories').and_return(())
  557. flexmock(module).should_receive('write_pattern_file').and_return(None)
  558. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  559. flexmock(module.feature).should_receive('available').and_return(True)
  560. flexmock(module).should_receive('ensure_files_readable')
  561. flexmock(module).should_receive('make_pattern_flags').and_return(())
  562. flexmock(module).should_receive('make_exclude_flags').and_return(())
  563. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  564. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  565. )
  566. flexmock(module.environment).should_receive('make_environment')
  567. flexmock(module).should_receive('execute_command').with_args(
  568. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--debug', '--show-rc'),
  569. output_log_level=logging.INFO,
  570. output_file=None,
  571. borg_local_path='borg',
  572. working_directory=None,
  573. extra_environment=None,
  574. )
  575. insert_logging_mock(logging.DEBUG)
  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. )
  588. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  589. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  590. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  591. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  592. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  593. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  594. flexmock(module).should_receive('expand_directories').and_return(())
  595. flexmock(module).should_receive('pattern_root_directories').and_return([])
  596. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  597. flexmock(module).should_receive('expand_home_directories').and_return(())
  598. flexmock(module).should_receive('write_pattern_file').and_return(None)
  599. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  600. flexmock(module.feature).should_receive('available').and_return(True)
  601. flexmock(module).should_receive('ensure_files_readable')
  602. flexmock(module).should_receive('make_pattern_flags').and_return(())
  603. flexmock(module).should_receive('make_exclude_flags').and_return(())
  604. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  605. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  606. )
  607. flexmock(module.environment).should_receive('make_environment')
  608. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  609. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  610. working_directory=None,
  611. extra_environment=None,
  612. )
  613. insert_logging_mock(logging.DEBUG)
  614. module.create_archive(
  615. dry_run=False,
  616. repository_path='repo',
  617. location_config={
  618. 'source_directories': ['foo', 'bar'],
  619. 'repositories': ['repo'],
  620. 'exclude_patterns': None,
  621. },
  622. storage_config={},
  623. local_borg_version='1.2.3',
  624. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  625. json=True,
  626. )
  627. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  628. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  629. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  630. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  631. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  632. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  633. flexmock(module).should_receive('expand_directories').and_return(())
  634. flexmock(module).should_receive('pattern_root_directories').and_return([])
  635. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  636. flexmock(module).should_receive('expand_home_directories').and_return(())
  637. flexmock(module).should_receive('write_pattern_file').and_return(None)
  638. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  639. flexmock(module.feature).should_receive('available').and_return(True)
  640. flexmock(module).should_receive('ensure_files_readable')
  641. flexmock(module).should_receive('make_pattern_flags').and_return(())
  642. flexmock(module).should_receive('make_exclude_flags').and_return(())
  643. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  644. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  645. )
  646. flexmock(module.environment).should_receive('make_environment')
  647. flexmock(module).should_receive('execute_command').with_args(
  648. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  649. output_log_level=logging.INFO,
  650. output_file=None,
  651. borg_local_path='borg',
  652. working_directory=None,
  653. extra_environment=None,
  654. )
  655. module.create_archive(
  656. dry_run=True,
  657. repository_path='repo',
  658. location_config={
  659. 'source_directories': ['foo', 'bar'],
  660. 'repositories': ['repo'],
  661. 'exclude_patterns': None,
  662. },
  663. storage_config={},
  664. local_borg_version='1.2.3',
  665. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  666. )
  667. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  668. # --dry-run and --stats are mutually exclusive, see:
  669. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  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 + ('--info',),
  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. insert_logging_mock(logging.INFO)
  698. module.create_archive(
  699. dry_run=True,
  700. repository_path='repo',
  701. location_config={
  702. 'source_directories': ['foo', 'bar'],
  703. 'repositories': ['repo'],
  704. 'exclude_patterns': None,
  705. },
  706. storage_config={},
  707. local_borg_version='1.2.3',
  708. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  709. stats=True,
  710. )
  711. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  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', '--checkpoint-interval', '600') + REPO_ARCHIVE_WITH_PATHS,
  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. module.create_archive(
  740. dry_run=False,
  741. repository_path='repo',
  742. location_config={
  743. 'source_directories': ['foo', 'bar'],
  744. 'repositories': ['repo'],
  745. 'exclude_patterns': None,
  746. },
  747. storage_config={'checkpoint_interval': 600},
  748. local_borg_version='1.2.3',
  749. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  750. )
  751. def test_create_archive_with_checkpoint_volume_calls_borg_with_checkpoint_volume_parameters():
  752. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  753. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  754. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  755. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  756. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  757. flexmock(module).should_receive('expand_directories').and_return(())
  758. flexmock(module).should_receive('pattern_root_directories').and_return([])
  759. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  760. flexmock(module).should_receive('expand_home_directories').and_return(())
  761. flexmock(module).should_receive('write_pattern_file').and_return(None)
  762. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  763. flexmock(module.feature).should_receive('available').and_return(True)
  764. flexmock(module).should_receive('ensure_files_readable')
  765. flexmock(module).should_receive('make_pattern_flags').and_return(())
  766. flexmock(module).should_receive('make_exclude_flags').and_return(())
  767. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  768. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  769. )
  770. flexmock(module.environment).should_receive('make_environment')
  771. flexmock(module).should_receive('execute_command').with_args(
  772. ('borg', 'create', '--checkpoint-volume', '1024') + REPO_ARCHIVE_WITH_PATHS,
  773. output_log_level=logging.INFO,
  774. output_file=None,
  775. borg_local_path='borg',
  776. working_directory=None,
  777. extra_environment=None,
  778. )
  779. module.create_archive(
  780. dry_run=False,
  781. repository_path='repo',
  782. location_config={
  783. 'source_directories': ['foo', 'bar'],
  784. 'repositories': ['repo'],
  785. 'exclude_patterns': None,
  786. },
  787. storage_config={'checkpoint_volume': 1024},
  788. local_borg_version='1.2.3',
  789. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  790. )
  791. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  792. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  793. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  794. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  795. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  796. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  797. flexmock(module).should_receive('expand_directories').and_return(())
  798. flexmock(module).should_receive('pattern_root_directories').and_return([])
  799. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  800. flexmock(module).should_receive('expand_home_directories').and_return(())
  801. flexmock(module).should_receive('write_pattern_file').and_return(None)
  802. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  803. flexmock(module.feature).should_receive('available').and_return(True)
  804. flexmock(module).should_receive('ensure_files_readable')
  805. flexmock(module).should_receive('make_pattern_flags').and_return(())
  806. flexmock(module).should_receive('make_exclude_flags').and_return(())
  807. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  808. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  809. )
  810. flexmock(module.environment).should_receive('make_environment')
  811. flexmock(module).should_receive('execute_command').with_args(
  812. ('borg', 'create', '--chunker-params', '1,2,3,4') + REPO_ARCHIVE_WITH_PATHS,
  813. output_log_level=logging.INFO,
  814. output_file=None,
  815. borg_local_path='borg',
  816. working_directory=None,
  817. extra_environment=None,
  818. )
  819. module.create_archive(
  820. dry_run=False,
  821. repository_path='repo',
  822. location_config={
  823. 'source_directories': ['foo', 'bar'],
  824. 'repositories': ['repo'],
  825. 'exclude_patterns': None,
  826. },
  827. storage_config={'chunker_params': '1,2,3,4'},
  828. local_borg_version='1.2.3',
  829. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  830. )
  831. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  832. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  833. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  834. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  835. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  836. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  837. flexmock(module).should_receive('expand_directories').and_return(())
  838. flexmock(module).should_receive('pattern_root_directories').and_return([])
  839. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  840. flexmock(module).should_receive('expand_home_directories').and_return(())
  841. flexmock(module).should_receive('write_pattern_file').and_return(None)
  842. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  843. flexmock(module.feature).should_receive('available').and_return(True)
  844. flexmock(module).should_receive('ensure_files_readable')
  845. flexmock(module).should_receive('make_pattern_flags').and_return(())
  846. flexmock(module).should_receive('make_exclude_flags').and_return(())
  847. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  848. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  849. )
  850. flexmock(module.environment).should_receive('make_environment')
  851. flexmock(module).should_receive('execute_command').with_args(
  852. ('borg', 'create', '--compression', 'rle') + REPO_ARCHIVE_WITH_PATHS,
  853. output_log_level=logging.INFO,
  854. output_file=None,
  855. borg_local_path='borg',
  856. working_directory=None,
  857. extra_environment=None,
  858. )
  859. module.create_archive(
  860. dry_run=False,
  861. repository_path='repo',
  862. location_config={
  863. 'source_directories': ['foo', 'bar'],
  864. 'repositories': ['repo'],
  865. 'exclude_patterns': None,
  866. },
  867. storage_config={'compression': 'rle'},
  868. local_borg_version='1.2.3',
  869. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  870. )
  871. @pytest.mark.parametrize(
  872. 'feature_available,option_flag',
  873. ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  874. )
  875. def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  876. feature_available, option_flag
  877. ):
  878. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  879. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  880. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  881. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  882. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  883. flexmock(module).should_receive('expand_directories').and_return(())
  884. flexmock(module).should_receive('pattern_root_directories').and_return([])
  885. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  886. flexmock(module).should_receive('expand_home_directories').and_return(())
  887. flexmock(module).should_receive('write_pattern_file').and_return(None)
  888. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  889. flexmock(module.feature).should_receive('available').and_return(feature_available)
  890. flexmock(module).should_receive('ensure_files_readable')
  891. flexmock(module).should_receive('make_pattern_flags').and_return(())
  892. flexmock(module).should_receive('make_exclude_flags').and_return(())
  893. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  894. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  895. )
  896. flexmock(module.environment).should_receive('make_environment')
  897. flexmock(module).should_receive('execute_command').with_args(
  898. ('borg', 'create', option_flag, '100') + REPO_ARCHIVE_WITH_PATHS,
  899. output_log_level=logging.INFO,
  900. output_file=None,
  901. borg_local_path='borg',
  902. working_directory=None,
  903. extra_environment=None,
  904. )
  905. module.create_archive(
  906. dry_run=False,
  907. repository_path='repo',
  908. location_config={
  909. 'source_directories': ['foo', 'bar'],
  910. 'repositories': ['repo'],
  911. 'exclude_patterns': None,
  912. },
  913. storage_config={'upload_rate_limit': 100},
  914. local_borg_version='1.2.3',
  915. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  916. )
  917. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  918. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  919. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  920. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  921. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  922. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  923. flexmock(module).should_receive('expand_directories').and_return(())
  924. flexmock(module).should_receive('pattern_root_directories').and_return([])
  925. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  926. '/working/dir'
  927. )
  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(True)
  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') + REPO_ARCHIVE_WITH_PATHS,
  941. output_log_level=logging.INFO,
  942. output_file=None,
  943. borg_local_path='borg',
  944. working_directory='/working/dir',
  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. 'working_directory': '/working/dir',
  954. 'exclude_patterns': None,
  955. },
  956. storage_config={},
  957. local_borg_version='1.2.3',
  958. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  959. )
  960. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  961. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  962. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  963. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  964. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  965. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  966. flexmock(module).should_receive('expand_directories').and_return(())
  967. flexmock(module).should_receive('pattern_root_directories').and_return([])
  968. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  969. flexmock(module).should_receive('expand_home_directories').and_return(())
  970. flexmock(module).should_receive('write_pattern_file').and_return(None)
  971. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  972. flexmock(module.feature).should_receive('available').and_return(True)
  973. flexmock(module).should_receive('ensure_files_readable')
  974. flexmock(module).should_receive('make_pattern_flags').and_return(())
  975. flexmock(module).should_receive('make_exclude_flags').and_return(())
  976. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  977. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  978. )
  979. flexmock(module.environment).should_receive('make_environment')
  980. flexmock(module).should_receive('execute_command').with_args(
  981. ('borg', 'create', '--one-file-system') + REPO_ARCHIVE_WITH_PATHS,
  982. output_log_level=logging.INFO,
  983. output_file=None,
  984. borg_local_path='borg',
  985. working_directory=None,
  986. extra_environment=None,
  987. )
  988. module.create_archive(
  989. dry_run=False,
  990. repository_path='repo',
  991. location_config={
  992. 'source_directories': ['foo', 'bar'],
  993. 'repositories': ['repo'],
  994. 'one_file_system': True,
  995. 'exclude_patterns': None,
  996. },
  997. storage_config={},
  998. local_borg_version='1.2.3',
  999. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1000. )
  1001. @pytest.mark.parametrize(
  1002. 'feature_available,option_flag',
  1003. ((True, '--numeric-ids'), (False, '--numeric-owner')),
  1004. )
  1005. def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
  1006. feature_available, option_flag
  1007. ):
  1008. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1009. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1010. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1011. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1012. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1013. flexmock(module).should_receive('expand_directories').and_return(())
  1014. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1015. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1016. flexmock(module).should_receive('expand_home_directories').and_return(())
  1017. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1018. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1019. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1020. flexmock(module).should_receive('ensure_files_readable')
  1021. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1022. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1023. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1024. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1025. )
  1026. flexmock(module.environment).should_receive('make_environment')
  1027. flexmock(module).should_receive('execute_command').with_args(
  1028. ('borg', 'create', option_flag) + REPO_ARCHIVE_WITH_PATHS,
  1029. output_log_level=logging.INFO,
  1030. output_file=None,
  1031. borg_local_path='borg',
  1032. working_directory=None,
  1033. extra_environment=None,
  1034. )
  1035. module.create_archive(
  1036. dry_run=False,
  1037. repository_path='repo',
  1038. location_config={
  1039. 'source_directories': ['foo', 'bar'],
  1040. 'repositories': ['repo'],
  1041. 'numeric_ids': True,
  1042. 'exclude_patterns': None,
  1043. },
  1044. storage_config={},
  1045. local_borg_version='1.2.3',
  1046. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1047. )
  1048. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  1049. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1050. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1051. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1052. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1053. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1054. flexmock(module).should_receive('expand_directories').and_return(())
  1055. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1056. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1057. flexmock(module).should_receive('expand_home_directories').and_return(())
  1058. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1059. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1060. flexmock(module.feature).should_receive('available').and_return(True)
  1061. flexmock(module).should_receive('ensure_files_readable')
  1062. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1063. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1064. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1065. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1066. )
  1067. flexmock(module.environment).should_receive('make_environment')
  1068. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1069. create_command = ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS
  1070. flexmock(module).should_receive('execute_command').with_args(
  1071. create_command + ('--dry-run', '--list'),
  1072. output_log_level=logging.INFO,
  1073. output_file=None,
  1074. borg_local_path='borg',
  1075. working_directory=None,
  1076. extra_environment=None,
  1077. )
  1078. flexmock(module).should_receive('execute_command').with_args(
  1079. create_command,
  1080. output_log_level=logging.INFO,
  1081. output_file=None,
  1082. borg_local_path='borg',
  1083. working_directory=None,
  1084. extra_environment=None,
  1085. )
  1086. module.create_archive(
  1087. dry_run=False,
  1088. repository_path='repo',
  1089. location_config={
  1090. 'source_directories': ['foo', 'bar'],
  1091. 'repositories': ['repo'],
  1092. 'read_special': True,
  1093. 'exclude_patterns': None,
  1094. },
  1095. storage_config={},
  1096. local_borg_version='1.2.3',
  1097. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1098. )
  1099. @pytest.mark.parametrize(
  1100. 'option_name,option_value',
  1101. (
  1102. ('ctime', True),
  1103. ('ctime', False),
  1104. ('birthtime', True),
  1105. ('birthtime', False),
  1106. ),
  1107. )
  1108. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  1109. option_name, option_value
  1110. ):
  1111. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1112. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1113. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  1114. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1115. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1116. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1117. flexmock(module).should_receive('expand_directories').and_return(())
  1118. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1119. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1120. flexmock(module).should_receive('expand_home_directories').and_return(())
  1121. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1122. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1123. flexmock(module.feature).should_receive('available').and_return(True)
  1124. flexmock(module).should_receive('ensure_files_readable')
  1125. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1126. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1127. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1128. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1129. )
  1130. flexmock(module.environment).should_receive('make_environment')
  1131. flexmock(module).should_receive('execute_command').with_args(
  1132. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1133. output_log_level=logging.INFO,
  1134. output_file=None,
  1135. borg_local_path='borg',
  1136. working_directory=None,
  1137. extra_environment=None,
  1138. )
  1139. module.create_archive(
  1140. dry_run=False,
  1141. repository_path='repo',
  1142. location_config={
  1143. 'source_directories': ['foo', 'bar'],
  1144. 'repositories': ['repo'],
  1145. option_name: option_value,
  1146. 'exclude_patterns': None,
  1147. },
  1148. storage_config={},
  1149. local_borg_version='1.2.3',
  1150. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1151. )
  1152. @pytest.mark.parametrize(
  1153. 'option_value,feature_available,option_flag',
  1154. (
  1155. (True, True, '--atime'),
  1156. (True, False, None),
  1157. (False, True, None),
  1158. (False, False, '--noatime'),
  1159. ),
  1160. )
  1161. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  1162. option_value, feature_available, option_flag
  1163. ):
  1164. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1165. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1166. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1167. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1168. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1169. flexmock(module).should_receive('expand_directories').and_return(())
  1170. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1171. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1172. flexmock(module).should_receive('expand_home_directories').and_return(())
  1173. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1174. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1175. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1176. flexmock(module).should_receive('ensure_files_readable')
  1177. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1178. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1179. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1180. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1181. )
  1182. flexmock(module.environment).should_receive('make_environment')
  1183. flexmock(module).should_receive('execute_command').with_args(
  1184. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1185. output_log_level=logging.INFO,
  1186. output_file=None,
  1187. borg_local_path='borg',
  1188. working_directory=None,
  1189. extra_environment=None,
  1190. )
  1191. module.create_archive(
  1192. dry_run=False,
  1193. repository_path='repo',
  1194. location_config={
  1195. 'source_directories': ['foo', 'bar'],
  1196. 'repositories': ['repo'],
  1197. 'atime': option_value,
  1198. 'exclude_patterns': None,
  1199. },
  1200. storage_config={},
  1201. local_borg_version='1.2.3',
  1202. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1203. )
  1204. @pytest.mark.parametrize(
  1205. 'option_value,feature_available,option_flag',
  1206. (
  1207. (True, True, None),
  1208. (True, False, None),
  1209. (False, True, '--noflags'),
  1210. (False, False, '--nobsdflags'),
  1211. ),
  1212. )
  1213. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  1214. option_value, feature_available, option_flag
  1215. ):
  1216. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1217. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1218. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1219. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1220. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1221. flexmock(module).should_receive('expand_directories').and_return(())
  1222. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1223. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1224. flexmock(module).should_receive('expand_home_directories').and_return(())
  1225. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1226. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1227. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1228. flexmock(module).should_receive('ensure_files_readable')
  1229. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1230. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1231. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1232. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1233. )
  1234. flexmock(module.environment).should_receive('make_environment')
  1235. flexmock(module).should_receive('execute_command').with_args(
  1236. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1237. output_log_level=logging.INFO,
  1238. output_file=None,
  1239. borg_local_path='borg',
  1240. working_directory=None,
  1241. extra_environment=None,
  1242. )
  1243. module.create_archive(
  1244. dry_run=False,
  1245. repository_path='repo',
  1246. location_config={
  1247. 'source_directories': ['foo', 'bar'],
  1248. 'repositories': ['repo'],
  1249. 'flags': option_value,
  1250. 'exclude_patterns': None,
  1251. },
  1252. storage_config={},
  1253. local_borg_version='1.2.3',
  1254. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1255. )
  1256. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  1257. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1258. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1259. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1260. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1261. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1262. flexmock(module).should_receive('expand_directories').and_return(())
  1263. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1264. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1265. flexmock(module).should_receive('expand_home_directories').and_return(())
  1266. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1267. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1268. flexmock(module.feature).should_receive('available').and_return(True)
  1269. flexmock(module).should_receive('ensure_files_readable')
  1270. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1271. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1272. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1273. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1274. )
  1275. flexmock(module.environment).should_receive('make_environment')
  1276. flexmock(module).should_receive('execute_command').with_args(
  1277. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  1278. output_log_level=logging.INFO,
  1279. output_file=None,
  1280. borg_local_path='borg',
  1281. working_directory=None,
  1282. extra_environment=None,
  1283. )
  1284. module.create_archive(
  1285. dry_run=False,
  1286. repository_path='repo',
  1287. location_config={
  1288. 'source_directories': ['foo', 'bar'],
  1289. 'repositories': ['repo'],
  1290. 'files_cache': 'ctime,size',
  1291. 'exclude_patterns': None,
  1292. },
  1293. storage_config={},
  1294. local_borg_version='1.2.3',
  1295. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1296. )
  1297. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1298. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1299. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1300. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1301. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1302. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1303. flexmock(module).should_receive('expand_directories').and_return(())
  1304. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1305. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1306. flexmock(module).should_receive('expand_home_directories').and_return(())
  1307. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1308. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1309. flexmock(module.feature).should_receive('available').and_return(True)
  1310. flexmock(module).should_receive('ensure_files_readable')
  1311. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1312. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1313. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1314. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1315. )
  1316. flexmock(module.environment).should_receive('make_environment')
  1317. flexmock(module).should_receive('execute_command').with_args(
  1318. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1319. output_log_level=logging.INFO,
  1320. output_file=None,
  1321. borg_local_path='borg1',
  1322. working_directory=None,
  1323. extra_environment=None,
  1324. )
  1325. module.create_archive(
  1326. dry_run=False,
  1327. repository_path='repo',
  1328. location_config={
  1329. 'source_directories': ['foo', 'bar'],
  1330. 'repositories': ['repo'],
  1331. 'exclude_patterns': None,
  1332. },
  1333. storage_config={},
  1334. local_borg_version='1.2.3',
  1335. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1336. local_path='borg1',
  1337. )
  1338. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1339. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1340. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1341. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1342. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1343. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1344. flexmock(module).should_receive('expand_directories').and_return(())
  1345. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1346. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1347. flexmock(module).should_receive('expand_home_directories').and_return(())
  1348. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1349. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1350. flexmock(module.feature).should_receive('available').and_return(True)
  1351. flexmock(module).should_receive('ensure_files_readable')
  1352. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1353. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1354. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1355. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1356. )
  1357. flexmock(module.environment).should_receive('make_environment')
  1358. flexmock(module).should_receive('execute_command').with_args(
  1359. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1360. output_log_level=logging.INFO,
  1361. output_file=None,
  1362. borg_local_path='borg',
  1363. working_directory=None,
  1364. extra_environment=None,
  1365. )
  1366. module.create_archive(
  1367. dry_run=False,
  1368. repository_path='repo',
  1369. location_config={
  1370. 'source_directories': ['foo', 'bar'],
  1371. 'repositories': ['repo'],
  1372. 'exclude_patterns': None,
  1373. },
  1374. storage_config={},
  1375. local_borg_version='1.2.3',
  1376. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1377. remote_path='borg1',
  1378. )
  1379. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1380. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1381. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1382. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1383. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1384. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1385. flexmock(module).should_receive('expand_directories').and_return(())
  1386. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1387. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1388. flexmock(module).should_receive('expand_home_directories').and_return(())
  1389. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1390. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1391. flexmock(module.feature).should_receive('available').and_return(True)
  1392. flexmock(module).should_receive('ensure_files_readable')
  1393. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1394. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1395. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1396. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1397. )
  1398. flexmock(module.environment).should_receive('make_environment')
  1399. flexmock(module).should_receive('execute_command').with_args(
  1400. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1401. output_log_level=logging.INFO,
  1402. output_file=None,
  1403. borg_local_path='borg',
  1404. working_directory=None,
  1405. extra_environment=None,
  1406. )
  1407. module.create_archive(
  1408. dry_run=False,
  1409. repository_path='repo',
  1410. location_config={
  1411. 'source_directories': ['foo', 'bar'],
  1412. 'repositories': ['repo'],
  1413. 'exclude_patterns': None,
  1414. },
  1415. storage_config={'umask': 740},
  1416. local_borg_version='1.2.3',
  1417. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1418. )
  1419. def test_create_archive_with_log_json_calls_borg_with_log_json_parameters():
  1420. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1421. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1422. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1423. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1424. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1425. flexmock(module).should_receive('expand_directories').and_return(())
  1426. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1427. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1428. flexmock(module).should_receive('expand_home_directories').and_return(())
  1429. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1430. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1431. flexmock(module.feature).should_receive('available').and_return(True)
  1432. flexmock(module).should_receive('ensure_files_readable')
  1433. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1434. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1435. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1436. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1437. )
  1438. flexmock(module.environment).should_receive('make_environment')
  1439. flexmock(module).should_receive('execute_command').with_args(
  1440. ('borg', 'create', '--log-json') + REPO_ARCHIVE_WITH_PATHS,
  1441. output_log_level=logging.INFO,
  1442. output_file=None,
  1443. borg_local_path='borg',
  1444. working_directory=None,
  1445. extra_environment=None,
  1446. )
  1447. module.create_archive(
  1448. dry_run=False,
  1449. repository_path='repo',
  1450. location_config={
  1451. 'source_directories': ['foo', 'bar'],
  1452. 'repositories': ['repo'],
  1453. 'exclude_patterns': None,
  1454. },
  1455. storage_config={},
  1456. local_borg_version='1.2.3',
  1457. global_arguments=flexmock(log_json=True, used_config_paths=[]),
  1458. )
  1459. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1460. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1461. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1462. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1463. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1464. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1465. flexmock(module).should_receive('expand_directories').and_return(())
  1466. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1467. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1468. flexmock(module).should_receive('expand_home_directories').and_return(())
  1469. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1470. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1471. flexmock(module.feature).should_receive('available').and_return(True)
  1472. flexmock(module).should_receive('ensure_files_readable')
  1473. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1474. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1475. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1476. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1477. )
  1478. flexmock(module.environment).should_receive('make_environment')
  1479. flexmock(module).should_receive('execute_command').with_args(
  1480. ('borg', 'create', '--lock-wait', '5') + REPO_ARCHIVE_WITH_PATHS,
  1481. output_log_level=logging.INFO,
  1482. output_file=None,
  1483. borg_local_path='borg',
  1484. working_directory=None,
  1485. extra_environment=None,
  1486. )
  1487. module.create_archive(
  1488. dry_run=False,
  1489. repository_path='repo',
  1490. location_config={
  1491. 'source_directories': ['foo', 'bar'],
  1492. 'repositories': ['repo'],
  1493. 'exclude_patterns': None,
  1494. },
  1495. storage_config={'lock_wait': 5},
  1496. local_borg_version='1.2.3',
  1497. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1498. )
  1499. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  1500. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1501. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1502. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1503. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1504. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1505. flexmock(module).should_receive('expand_directories').and_return(())
  1506. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1507. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1508. flexmock(module).should_receive('expand_home_directories').and_return(())
  1509. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1510. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1511. flexmock(module.feature).should_receive('available').and_return(True)
  1512. flexmock(module).should_receive('ensure_files_readable')
  1513. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1514. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1515. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1516. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1517. )
  1518. flexmock(module.environment).should_receive('make_environment')
  1519. flexmock(module).should_receive('execute_command').with_args(
  1520. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--stats',),
  1521. output_log_level=module.borgmatic.logger.ANSWER,
  1522. output_file=None,
  1523. borg_local_path='borg',
  1524. working_directory=None,
  1525. extra_environment=None,
  1526. )
  1527. module.create_archive(
  1528. dry_run=False,
  1529. repository_path='repo',
  1530. location_config={
  1531. 'source_directories': ['foo', 'bar'],
  1532. 'repositories': ['repo'],
  1533. 'exclude_patterns': None,
  1534. },
  1535. storage_config={},
  1536. local_borg_version='1.2.3',
  1537. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1538. stats=True,
  1539. )
  1540. def test_create_archive_with_files_calls_borg_with_list_parameter_and_answer_output_log_level():
  1541. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1542. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1543. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1544. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1545. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1546. flexmock(module).should_receive('expand_directories').and_return(())
  1547. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1548. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1549. flexmock(module).should_receive('expand_home_directories').and_return(())
  1550. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1551. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1552. flexmock(module.feature).should_receive('available').and_return(True)
  1553. flexmock(module).should_receive('ensure_files_readable')
  1554. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1555. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1556. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1557. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1558. )
  1559. flexmock(module.environment).should_receive('make_environment')
  1560. flexmock(module).should_receive('execute_command').with_args(
  1561. ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE_WITH_PATHS,
  1562. output_log_level=module.borgmatic.logger.ANSWER,
  1563. output_file=None,
  1564. borg_local_path='borg',
  1565. working_directory=None,
  1566. extra_environment=None,
  1567. )
  1568. module.create_archive(
  1569. dry_run=False,
  1570. repository_path='repo',
  1571. location_config={
  1572. 'source_directories': ['foo', 'bar'],
  1573. 'repositories': ['repo'],
  1574. 'exclude_patterns': None,
  1575. },
  1576. storage_config={},
  1577. local_borg_version='1.2.3',
  1578. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1579. list_files=True,
  1580. )
  1581. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1582. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1583. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1584. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1585. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1586. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1587. flexmock(module).should_receive('expand_directories').and_return(())
  1588. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1589. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1590. flexmock(module).should_receive('expand_home_directories').and_return(())
  1591. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1592. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1593. flexmock(module.feature).should_receive('available').and_return(True)
  1594. flexmock(module).should_receive('ensure_files_readable')
  1595. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1596. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1597. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1598. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1599. )
  1600. flexmock(module.environment).should_receive('make_environment')
  1601. flexmock(module).should_receive('execute_command').with_args(
  1602. ('borg', 'create')
  1603. + REPO_ARCHIVE_WITH_PATHS
  1604. + (
  1605. '--info',
  1606. '--progress',
  1607. ),
  1608. output_log_level=logging.INFO,
  1609. output_file=module.DO_NOT_CAPTURE,
  1610. borg_local_path='borg',
  1611. working_directory=None,
  1612. extra_environment=None,
  1613. )
  1614. insert_logging_mock(logging.INFO)
  1615. module.create_archive(
  1616. dry_run=False,
  1617. repository_path='repo',
  1618. location_config={
  1619. 'source_directories': ['foo', 'bar'],
  1620. 'repositories': ['repo'],
  1621. 'exclude_patterns': None,
  1622. },
  1623. storage_config={},
  1624. local_borg_version='1.2.3',
  1625. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1626. progress=True,
  1627. )
  1628. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1629. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1630. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1631. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1632. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1633. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1634. flexmock(module).should_receive('expand_directories').and_return(())
  1635. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1636. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1637. flexmock(module).should_receive('expand_home_directories').and_return(())
  1638. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1639. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1640. flexmock(module.feature).should_receive('available').and_return(True)
  1641. flexmock(module).should_receive('ensure_files_readable')
  1642. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1643. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1644. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1645. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1646. )
  1647. flexmock(module.environment).should_receive('make_environment')
  1648. flexmock(module).should_receive('execute_command').with_args(
  1649. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--progress',),
  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. module.create_archive(
  1657. dry_run=False,
  1658. repository_path='repo',
  1659. location_config={
  1660. 'source_directories': ['foo', 'bar'],
  1661. 'repositories': ['repo'],
  1662. 'exclude_patterns': None,
  1663. },
  1664. storage_config={},
  1665. local_borg_version='1.2.3',
  1666. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1667. progress=True,
  1668. )
  1669. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1670. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1671. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1672. processes = flexmock()
  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('collect_special_file_paths').and_return(())
  1691. create_command = (
  1692. ('borg', 'create', '--one-file-system', '--read-special')
  1693. + REPO_ARCHIVE_WITH_PATHS
  1694. + ('--progress',)
  1695. )
  1696. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1697. create_command + ('--dry-run', '--list'),
  1698. processes=processes,
  1699. output_log_level=logging.INFO,
  1700. output_file=module.DO_NOT_CAPTURE,
  1701. borg_local_path='borg',
  1702. working_directory=None,
  1703. extra_environment=None,
  1704. )
  1705. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1706. create_command,
  1707. processes=processes,
  1708. output_log_level=logging.INFO,
  1709. output_file=module.DO_NOT_CAPTURE,
  1710. borg_local_path='borg',
  1711. working_directory=None,
  1712. extra_environment=None,
  1713. )
  1714. module.create_archive(
  1715. dry_run=False,
  1716. repository_path='repo',
  1717. location_config={
  1718. 'source_directories': ['foo', 'bar'],
  1719. 'repositories': ['repo'],
  1720. 'exclude_patterns': None,
  1721. },
  1722. storage_config={},
  1723. local_borg_version='1.2.3',
  1724. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1725. progress=True,
  1726. stream_processes=processes,
  1727. )
  1728. def test_create_archive_with_stream_processes_ignores_read_special_false_and_logs_warnings():
  1729. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1730. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1731. processes = flexmock()
  1732. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1733. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1734. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1735. flexmock(module).should_receive('expand_directories').and_return(())
  1736. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1737. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1738. flexmock(module).should_receive('expand_home_directories').and_return(())
  1739. flexmock(module).should_receive('write_pattern_file').and_return(flexmock(name='/tmp/excludes'))
  1740. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1741. flexmock(module.feature).should_receive('available').and_return(True)
  1742. flexmock(module).should_receive('ensure_files_readable')
  1743. flexmock(module.logger).should_receive('warning').twice()
  1744. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1745. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1746. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1747. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1748. )
  1749. flexmock(module.environment).should_receive('make_environment')
  1750. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',))
  1751. create_command = (
  1752. 'borg',
  1753. 'create',
  1754. '--one-file-system',
  1755. '--read-special',
  1756. ) + REPO_ARCHIVE_WITH_PATHS
  1757. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1758. create_command + ('--dry-run', '--list'),
  1759. processes=processes,
  1760. output_log_level=logging.INFO,
  1761. output_file=None,
  1762. borg_local_path='borg',
  1763. working_directory=None,
  1764. extra_environment=None,
  1765. )
  1766. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1767. create_command,
  1768. processes=processes,
  1769. output_log_level=logging.INFO,
  1770. output_file=None,
  1771. borg_local_path='borg',
  1772. working_directory=None,
  1773. extra_environment=None,
  1774. )
  1775. module.create_archive(
  1776. dry_run=False,
  1777. repository_path='repo',
  1778. location_config={
  1779. 'source_directories': ['foo', 'bar'],
  1780. 'repositories': ['repo'],
  1781. 'exclude_patterns': None,
  1782. 'read_special': False,
  1783. },
  1784. storage_config={},
  1785. local_borg_version='1.2.3',
  1786. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1787. stream_processes=processes,
  1788. )
  1789. def test_create_archive_with_stream_processes_adds_special_files_to_excludes():
  1790. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1791. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1792. processes = flexmock()
  1793. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1794. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1795. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1796. flexmock(module).should_receive('expand_directories').and_return(())
  1797. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1798. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1799. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  1800. ('special',)
  1801. )
  1802. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  1803. flexmock(name='/excludes')
  1804. )
  1805. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1806. flexmock(module.feature).should_receive('available').and_return(True)
  1807. flexmock(module).should_receive('ensure_files_readable')
  1808. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1809. flexmock(module).should_receive('make_exclude_flags').and_return(()).and_return(
  1810. '--exclude-from', '/excludes'
  1811. )
  1812. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1813. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1814. )
  1815. flexmock(module.environment).should_receive('make_environment')
  1816. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  1817. create_command = (
  1818. 'borg',
  1819. 'create',
  1820. '--one-file-system',
  1821. '--read-special',
  1822. ) + REPO_ARCHIVE_WITH_PATHS
  1823. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1824. create_command + ('--dry-run', '--list'),
  1825. processes=processes,
  1826. output_log_level=logging.INFO,
  1827. output_file=None,
  1828. borg_local_path='borg',
  1829. working_directory=None,
  1830. extra_environment=None,
  1831. )
  1832. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1833. create_command + ('--exclude-from', '/excludes'),
  1834. processes=processes,
  1835. output_log_level=logging.INFO,
  1836. output_file=None,
  1837. borg_local_path='borg',
  1838. working_directory=None,
  1839. extra_environment=None,
  1840. )
  1841. module.create_archive(
  1842. dry_run=False,
  1843. repository_path='repo',
  1844. location_config={
  1845. 'source_directories': ['foo', 'bar'],
  1846. 'repositories': ['repo'],
  1847. 'exclude_patterns': None,
  1848. },
  1849. storage_config={},
  1850. local_borg_version='1.2.3',
  1851. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1852. stream_processes=processes,
  1853. )
  1854. def test_create_archive_with_stream_processes_and_read_special_does_not_add_special_files_to_excludes():
  1855. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1856. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1857. processes = flexmock()
  1858. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1859. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1860. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1861. flexmock(module).should_receive('expand_directories').and_return(())
  1862. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1863. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1864. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  1865. ('special',)
  1866. )
  1867. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1868. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1869. flexmock(module.feature).should_receive('available').and_return(True)
  1870. flexmock(module).should_receive('ensure_files_readable')
  1871. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1872. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1873. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1874. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1875. )
  1876. flexmock(module.environment).should_receive('make_environment')
  1877. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  1878. create_command = (
  1879. 'borg',
  1880. 'create',
  1881. '--one-file-system',
  1882. '--read-special',
  1883. ) + REPO_ARCHIVE_WITH_PATHS
  1884. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1885. create_command + ('--dry-run', '--list'),
  1886. processes=processes,
  1887. output_log_level=logging.INFO,
  1888. output_file=None,
  1889. borg_local_path='borg',
  1890. working_directory=None,
  1891. extra_environment=None,
  1892. )
  1893. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1894. create_command,
  1895. processes=processes,
  1896. output_log_level=logging.INFO,
  1897. output_file=None,
  1898. borg_local_path='borg',
  1899. working_directory=None,
  1900. extra_environment=None,
  1901. )
  1902. module.create_archive(
  1903. dry_run=False,
  1904. repository_path='repo',
  1905. location_config={
  1906. 'source_directories': ['foo', 'bar'],
  1907. 'repositories': ['repo'],
  1908. 'exclude_patterns': None,
  1909. 'read_special': True,
  1910. },
  1911. storage_config={},
  1912. local_borg_version='1.2.3',
  1913. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1914. stream_processes=processes,
  1915. )
  1916. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1917. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1918. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1919. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1920. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1921. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1922. flexmock(module).should_receive('expand_directories').and_return(())
  1923. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1924. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1925. flexmock(module).should_receive('expand_home_directories').and_return(())
  1926. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1927. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1928. flexmock(module.feature).should_receive('available').and_return(True)
  1929. flexmock(module).should_receive('ensure_files_readable')
  1930. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1931. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1932. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1933. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1934. )
  1935. flexmock(module.environment).should_receive('make_environment')
  1936. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1937. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1938. working_directory=None,
  1939. extra_environment=None,
  1940. ).and_return('[]')
  1941. json_output = module.create_archive(
  1942. dry_run=False,
  1943. repository_path='repo',
  1944. location_config={
  1945. 'source_directories': ['foo', 'bar'],
  1946. 'repositories': ['repo'],
  1947. 'exclude_patterns': None,
  1948. },
  1949. storage_config={},
  1950. local_borg_version='1.2.3',
  1951. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1952. json=True,
  1953. )
  1954. assert json_output == '[]'
  1955. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1956. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1957. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1958. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1959. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1960. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1961. flexmock(module).should_receive('expand_directories').and_return(())
  1962. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1963. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1964. flexmock(module).should_receive('expand_home_directories').and_return(())
  1965. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1966. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1967. flexmock(module.feature).should_receive('available').and_return(True)
  1968. flexmock(module).should_receive('ensure_files_readable')
  1969. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1970. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1971. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1972. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1973. )
  1974. flexmock(module.environment).should_receive('make_environment')
  1975. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1976. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1977. working_directory=None,
  1978. extra_environment=None,
  1979. ).and_return('[]')
  1980. json_output = module.create_archive(
  1981. dry_run=False,
  1982. repository_path='repo',
  1983. location_config={
  1984. 'source_directories': ['foo', 'bar'],
  1985. 'repositories': ['repo'],
  1986. 'exclude_patterns': None,
  1987. },
  1988. storage_config={},
  1989. local_borg_version='1.2.3',
  1990. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1991. json=True,
  1992. stats=True,
  1993. )
  1994. assert json_output == '[]'
  1995. def test_create_archive_with_source_directories_glob_expands():
  1996. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1997. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1998. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1999. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  2000. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2001. flexmock(module).should_receive('expand_directories').and_return(())
  2002. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2003. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2004. flexmock(module).should_receive('expand_home_directories').and_return(())
  2005. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2006. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2007. flexmock(module.feature).should_receive('available').and_return(True)
  2008. flexmock(module).should_receive('ensure_files_readable')
  2009. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2010. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2011. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2012. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2013. )
  2014. flexmock(module.environment).should_receive('make_environment')
  2015. flexmock(module).should_receive('execute_command').with_args(
  2016. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food'),
  2017. output_log_level=logging.INFO,
  2018. output_file=None,
  2019. borg_local_path='borg',
  2020. working_directory=None,
  2021. extra_environment=None,
  2022. )
  2023. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  2024. module.create_archive(
  2025. dry_run=False,
  2026. repository_path='repo',
  2027. location_config={
  2028. 'source_directories': ['foo*'],
  2029. 'repositories': ['repo'],
  2030. 'exclude_patterns': None,
  2031. },
  2032. storage_config={},
  2033. local_borg_version='1.2.3',
  2034. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2035. )
  2036. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  2037. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2038. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2039. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2040. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  2041. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2042. flexmock(module).should_receive('expand_directories').and_return(())
  2043. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2044. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2045. flexmock(module).should_receive('expand_home_directories').and_return(())
  2046. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2047. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2048. flexmock(module.feature).should_receive('available').and_return(True)
  2049. flexmock(module).should_receive('ensure_files_readable')
  2050. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2051. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2052. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2053. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2054. )
  2055. flexmock(module.environment).should_receive('make_environment')
  2056. flexmock(module).should_receive('execute_command').with_args(
  2057. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo*'),
  2058. output_log_level=logging.INFO,
  2059. output_file=None,
  2060. borg_local_path='borg',
  2061. working_directory=None,
  2062. extra_environment=None,
  2063. )
  2064. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  2065. module.create_archive(
  2066. dry_run=False,
  2067. repository_path='repo',
  2068. location_config={
  2069. 'source_directories': ['foo*'],
  2070. 'repositories': ['repo'],
  2071. 'exclude_patterns': None,
  2072. },
  2073. storage_config={},
  2074. local_borg_version='1.2.3',
  2075. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2076. )
  2077. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  2078. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2079. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2080. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2081. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  2082. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2083. flexmock(module).should_receive('expand_directories').and_return(())
  2084. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2085. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2086. flexmock(module).should_receive('expand_home_directories').and_return(())
  2087. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2088. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2089. flexmock(module.feature).should_receive('available').and_return(True)
  2090. flexmock(module).should_receive('ensure_files_readable')
  2091. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2092. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2093. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2094. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2095. )
  2096. flexmock(module.environment).should_receive('make_environment')
  2097. flexmock(module).should_receive('execute_command').with_args(
  2098. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food'),
  2099. output_log_level=logging.INFO,
  2100. output_file=None,
  2101. borg_local_path='borg',
  2102. working_directory=None,
  2103. extra_environment=None,
  2104. )
  2105. module.create_archive(
  2106. dry_run=False,
  2107. repository_path='repo',
  2108. location_config={
  2109. 'source_directories': ['foo*'],
  2110. 'repositories': ['repo'],
  2111. 'exclude_patterns': None,
  2112. },
  2113. storage_config={},
  2114. local_borg_version='1.2.3',
  2115. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2116. )
  2117. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  2118. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2119. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2120. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2121. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2122. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2123. flexmock(module).should_receive('expand_directories').and_return(())
  2124. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2125. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2126. flexmock(module).should_receive('expand_home_directories').and_return(())
  2127. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2128. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2129. flexmock(module.feature).should_receive('available').and_return(True)
  2130. flexmock(module).should_receive('ensure_files_readable')
  2131. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2132. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2133. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2134. ('repo::ARCHIVE_NAME',)
  2135. )
  2136. flexmock(module.environment).should_receive('make_environment')
  2137. flexmock(module).should_receive('execute_command').with_args(
  2138. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  2139. output_log_level=logging.INFO,
  2140. output_file=None,
  2141. borg_local_path='borg',
  2142. working_directory=None,
  2143. extra_environment=None,
  2144. )
  2145. module.create_archive(
  2146. dry_run=False,
  2147. repository_path='repo',
  2148. location_config={
  2149. 'source_directories': ['foo', 'bar'],
  2150. 'repositories': ['repo'],
  2151. 'exclude_patterns': None,
  2152. },
  2153. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  2154. local_borg_version='1.2.3',
  2155. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2156. )
  2157. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  2158. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2159. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2160. repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
  2161. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2162. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2163. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2164. flexmock(module).should_receive('expand_directories').and_return(())
  2165. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2166. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2167. flexmock(module).should_receive('expand_home_directories').and_return(())
  2168. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2169. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2170. flexmock(module.feature).should_receive('available').and_return(True)
  2171. flexmock(module).should_receive('ensure_files_readable')
  2172. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2173. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2174. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2175. (repository_archive_pattern,)
  2176. )
  2177. flexmock(module.environment).should_receive('make_environment')
  2178. flexmock(module).should_receive('execute_command').with_args(
  2179. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  2180. output_log_level=logging.INFO,
  2181. output_file=None,
  2182. borg_local_path='borg',
  2183. working_directory=None,
  2184. extra_environment=None,
  2185. )
  2186. module.create_archive(
  2187. dry_run=False,
  2188. repository_path='repo',
  2189. location_config={
  2190. 'source_directories': ['foo', 'bar'],
  2191. 'repositories': ['repo'],
  2192. 'exclude_patterns': None,
  2193. },
  2194. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'}, # noqa: FS003
  2195. local_borg_version='1.2.3',
  2196. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2197. )
  2198. def test_create_archive_with_repository_accepts_borg_placeholders():
  2199. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2200. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2201. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
  2202. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2203. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2204. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2205. flexmock(module).should_receive('expand_directories').and_return(())
  2206. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2207. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2208. flexmock(module).should_receive('expand_home_directories').and_return(())
  2209. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2210. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2211. flexmock(module.feature).should_receive('available').and_return(True)
  2212. flexmock(module).should_receive('ensure_files_readable')
  2213. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2214. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2215. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2216. (repository_archive_pattern,)
  2217. )
  2218. flexmock(module.environment).should_receive('make_environment')
  2219. flexmock(module).should_receive('execute_command').with_args(
  2220. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  2221. output_log_level=logging.INFO,
  2222. output_file=None,
  2223. borg_local_path='borg',
  2224. working_directory=None,
  2225. extra_environment=None,
  2226. )
  2227. module.create_archive(
  2228. dry_run=False,
  2229. repository_path='{fqdn}', # noqa: FS003
  2230. location_config={
  2231. 'source_directories': ['foo', 'bar'],
  2232. 'repositories': ['{fqdn}'], # noqa: FS003
  2233. 'exclude_patterns': None,
  2234. },
  2235. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'}, # noqa: FS003
  2236. local_borg_version='1.2.3',
  2237. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2238. )
  2239. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  2240. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2241. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2242. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2243. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2244. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2245. flexmock(module).should_receive('expand_directories').and_return(())
  2246. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2247. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2248. flexmock(module).should_receive('expand_home_directories').and_return(())
  2249. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2250. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2251. flexmock(module.feature).should_receive('available').and_return(True)
  2252. flexmock(module).should_receive('ensure_files_readable')
  2253. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2254. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2255. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2256. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2257. )
  2258. flexmock(module.environment).should_receive('make_environment')
  2259. flexmock(module).should_receive('execute_command').with_args(
  2260. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  2261. output_log_level=logging.INFO,
  2262. output_file=None,
  2263. borg_local_path='borg',
  2264. working_directory=None,
  2265. extra_environment=None,
  2266. )
  2267. module.create_archive(
  2268. dry_run=False,
  2269. repository_path='repo',
  2270. location_config={
  2271. 'source_directories': ['foo', 'bar'],
  2272. 'repositories': ['repo'],
  2273. 'exclude_patterns': None,
  2274. },
  2275. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  2276. local_borg_version='1.2.3',
  2277. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2278. )
  2279. def test_create_archive_with_stream_processes_calls_borg_with_processes_and_read_special():
  2280. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2281. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2282. processes = flexmock()
  2283. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2284. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2285. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2286. flexmock(module).should_receive('expand_directories').and_return(())
  2287. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2288. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2289. flexmock(module).should_receive('expand_home_directories').and_return(())
  2290. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2291. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2292. flexmock(module.feature).should_receive('available').and_return(True)
  2293. flexmock(module).should_receive('ensure_files_readable')
  2294. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2295. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2296. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2297. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2298. )
  2299. flexmock(module.environment).should_receive('make_environment')
  2300. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  2301. create_command = (
  2302. 'borg',
  2303. 'create',
  2304. '--one-file-system',
  2305. '--read-special',
  2306. ) + REPO_ARCHIVE_WITH_PATHS
  2307. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2308. create_command + ('--dry-run', 'list'),
  2309. processes=processes,
  2310. output_log_level=logging.INFO,
  2311. output_file=None,
  2312. borg_local_path='borg',
  2313. working_directory=None,
  2314. extra_environment=None,
  2315. )
  2316. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2317. create_command,
  2318. processes=processes,
  2319. output_log_level=logging.INFO,
  2320. output_file=None,
  2321. borg_local_path='borg',
  2322. working_directory=None,
  2323. extra_environment=None,
  2324. )
  2325. module.create_archive(
  2326. dry_run=False,
  2327. repository_path='repo',
  2328. location_config={
  2329. 'source_directories': ['foo', 'bar'],
  2330. 'repositories': ['repo'],
  2331. 'exclude_patterns': None,
  2332. },
  2333. storage_config={},
  2334. local_borg_version='1.2.3',
  2335. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2336. stream_processes=processes,
  2337. )
  2338. def test_create_archive_with_non_existent_directory_and_source_directories_must_exist_raises_error():
  2339. '''
  2340. If a source directory doesn't exist and source_directories_must_exist is True, raise an error.
  2341. '''
  2342. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2343. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2344. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2345. flexmock(module).should_receive('check_all_source_directories_exist').and_raise(ValueError)
  2346. with pytest.raises(ValueError):
  2347. module.create_archive(
  2348. dry_run=False,
  2349. repository_path='repo',
  2350. location_config={
  2351. 'source_directories': ['foo', 'bar'],
  2352. 'repositories': ['repo'],
  2353. 'exclude_patterns': None,
  2354. 'source_directories_must_exist': True,
  2355. },
  2356. storage_config={},
  2357. local_borg_version='1.2.3',
  2358. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2359. )
  2360. def test_check_all_source_directories_exist_with_glob_and_tilde_directories():
  2361. flexmock(module).should_receive('expand_directory').with_args('foo*').and_return(
  2362. ('foo', 'food')
  2363. )
  2364. flexmock(module).should_receive('expand_directory').with_args('~/bar').and_return(
  2365. ('/root/bar',)
  2366. )
  2367. flexmock(module.os.path).should_receive('exists').and_return(False)
  2368. flexmock(module.os.path).should_receive('exists').with_args('foo').and_return(True)
  2369. flexmock(module.os.path).should_receive('exists').with_args('food').and_return(True)
  2370. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  2371. module.check_all_source_directories_exist(['foo*', '~/bar'])
  2372. def test_check_all_source_directories_exist_with_non_existent_directory_raises():
  2373. flexmock(module).should_receive('expand_directory').with_args('foo').and_return(('foo',))
  2374. flexmock(module.os.path).should_receive('exists').and_return(False)
  2375. with pytest.raises(ValueError):
  2376. module.check_all_source_directories_exist(['foo'])