test_create.py 114 KB

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