test_create.py 104 KB

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