2
0

test_create.py 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331
  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. def test_create_archive_with_read_special_adds_special_files_to_excludes():
  961. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  962. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  963. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  964. flexmock(module).should_receive('expand_directories').and_return(())
  965. flexmock(module).should_receive('pattern_root_directories').and_return([])
  966. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  967. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  968. ('special',)
  969. )
  970. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  971. flexmock(name='/excludes')
  972. )
  973. flexmock(module.feature).should_receive('available').and_return(True)
  974. flexmock(module).should_receive('ensure_files_readable')
  975. flexmock(module).should_receive('make_pattern_flags').and_return(())
  976. flexmock(module).should_receive('make_exclude_flags').and_return(()).and_return(
  977. '--exclude-from', '/excludes'
  978. )
  979. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  980. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  981. )
  982. flexmock(module.environment).should_receive('make_environment')
  983. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  984. create_command = ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS
  985. flexmock(module).should_receive('execute_command').with_args(
  986. create_command + ('--dry-run', '--list'),
  987. output_log_level=logging.INFO,
  988. output_file=None,
  989. borg_local_path='borg',
  990. working_directory=None,
  991. extra_environment=None,
  992. )
  993. flexmock(module).should_receive('execute_command').with_args(
  994. create_command + ('--exclude-from', '/excludes'),
  995. output_log_level=logging.INFO,
  996. output_file=None,
  997. borg_local_path='borg',
  998. working_directory=None,
  999. extra_environment=None,
  1000. )
  1001. module.create_archive(
  1002. dry_run=False,
  1003. repository='repo',
  1004. location_config={
  1005. 'source_directories': ['foo', 'bar'],
  1006. 'repositories': ['repo'],
  1007. 'read_special': True,
  1008. 'exclude_patterns': None,
  1009. },
  1010. storage_config={},
  1011. local_borg_version='1.2.3',
  1012. )
  1013. @pytest.mark.parametrize(
  1014. 'option_name,option_value',
  1015. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  1016. )
  1017. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  1018. option_name, option_value
  1019. ):
  1020. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  1021. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1022. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1023. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1024. flexmock(module).should_receive('expand_directories').and_return(())
  1025. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1026. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1027. flexmock(module).should_receive('expand_home_directories').and_return(())
  1028. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1029. flexmock(module.feature).should_receive('available').and_return(True)
  1030. flexmock(module).should_receive('ensure_files_readable')
  1031. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1032. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1033. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1034. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1035. )
  1036. flexmock(module.environment).should_receive('make_environment')
  1037. flexmock(module).should_receive('execute_command').with_args(
  1038. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1039. output_log_level=logging.INFO,
  1040. output_file=None,
  1041. borg_local_path='borg',
  1042. working_directory=None,
  1043. extra_environment=None,
  1044. )
  1045. module.create_archive(
  1046. dry_run=False,
  1047. repository='repo',
  1048. location_config={
  1049. 'source_directories': ['foo', 'bar'],
  1050. 'repositories': ['repo'],
  1051. option_name: option_value,
  1052. 'exclude_patterns': None,
  1053. },
  1054. storage_config={},
  1055. local_borg_version='1.2.3',
  1056. )
  1057. @pytest.mark.parametrize(
  1058. 'option_value,feature_available,option_flag',
  1059. (
  1060. (True, True, '--atime'),
  1061. (True, False, None),
  1062. (False, True, None),
  1063. (False, False, '--noatime'),
  1064. ),
  1065. )
  1066. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  1067. option_value, feature_available, option_flag
  1068. ):
  1069. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1070. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1071. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1072. flexmock(module).should_receive('expand_directories').and_return(())
  1073. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1074. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1075. flexmock(module).should_receive('expand_home_directories').and_return(())
  1076. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1077. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1078. flexmock(module).should_receive('ensure_files_readable')
  1079. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1080. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1081. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1082. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1083. )
  1084. flexmock(module.environment).should_receive('make_environment')
  1085. flexmock(module).should_receive('execute_command').with_args(
  1086. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1087. output_log_level=logging.INFO,
  1088. output_file=None,
  1089. borg_local_path='borg',
  1090. working_directory=None,
  1091. extra_environment=None,
  1092. )
  1093. module.create_archive(
  1094. dry_run=False,
  1095. repository='repo',
  1096. location_config={
  1097. 'source_directories': ['foo', 'bar'],
  1098. 'repositories': ['repo'],
  1099. 'atime': option_value,
  1100. 'exclude_patterns': None,
  1101. },
  1102. storage_config={},
  1103. local_borg_version='1.2.3',
  1104. )
  1105. @pytest.mark.parametrize(
  1106. 'option_value,feature_available,option_flag',
  1107. (
  1108. (True, True, None),
  1109. (True, False, None),
  1110. (False, True, '--noflags'),
  1111. (False, False, '--nobsdflags'),
  1112. ),
  1113. )
  1114. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  1115. option_value, feature_available, option_flag
  1116. ):
  1117. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1118. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1119. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1120. flexmock(module).should_receive('expand_directories').and_return(())
  1121. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1122. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1123. flexmock(module).should_receive('expand_home_directories').and_return(())
  1124. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1125. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1126. flexmock(module).should_receive('ensure_files_readable')
  1127. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1128. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1129. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1130. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1131. )
  1132. flexmock(module.environment).should_receive('make_environment')
  1133. flexmock(module).should_receive('execute_command').with_args(
  1134. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1135. output_log_level=logging.INFO,
  1136. output_file=None,
  1137. borg_local_path='borg',
  1138. working_directory=None,
  1139. extra_environment=None,
  1140. )
  1141. module.create_archive(
  1142. dry_run=False,
  1143. repository='repo',
  1144. location_config={
  1145. 'source_directories': ['foo', 'bar'],
  1146. 'repositories': ['repo'],
  1147. 'flags': option_value,
  1148. 'exclude_patterns': None,
  1149. },
  1150. storage_config={},
  1151. local_borg_version='1.2.3',
  1152. )
  1153. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  1154. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1155. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1156. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1157. flexmock(module).should_receive('expand_directories').and_return(())
  1158. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1159. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1160. flexmock(module).should_receive('expand_home_directories').and_return(())
  1161. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1162. flexmock(module.feature).should_receive('available').and_return(True)
  1163. flexmock(module).should_receive('ensure_files_readable')
  1164. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1165. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1166. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1167. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1168. )
  1169. flexmock(module.environment).should_receive('make_environment')
  1170. flexmock(module).should_receive('execute_command').with_args(
  1171. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  1172. output_log_level=logging.INFO,
  1173. output_file=None,
  1174. borg_local_path='borg',
  1175. working_directory=None,
  1176. extra_environment=None,
  1177. )
  1178. module.create_archive(
  1179. dry_run=False,
  1180. repository='repo',
  1181. location_config={
  1182. 'source_directories': ['foo', 'bar'],
  1183. 'repositories': ['repo'],
  1184. 'files_cache': 'ctime,size',
  1185. 'exclude_patterns': None,
  1186. },
  1187. storage_config={},
  1188. local_borg_version='1.2.3',
  1189. )
  1190. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1191. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1192. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1193. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1194. flexmock(module).should_receive('expand_directories').and_return(())
  1195. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1196. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1197. flexmock(module).should_receive('expand_home_directories').and_return(())
  1198. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1199. flexmock(module.feature).should_receive('available').and_return(True)
  1200. flexmock(module).should_receive('ensure_files_readable')
  1201. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1202. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1203. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1204. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1205. )
  1206. flexmock(module.environment).should_receive('make_environment')
  1207. flexmock(module).should_receive('execute_command').with_args(
  1208. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1209. output_log_level=logging.INFO,
  1210. output_file=None,
  1211. borg_local_path='borg1',
  1212. working_directory=None,
  1213. extra_environment=None,
  1214. )
  1215. module.create_archive(
  1216. dry_run=False,
  1217. repository='repo',
  1218. location_config={
  1219. 'source_directories': ['foo', 'bar'],
  1220. 'repositories': ['repo'],
  1221. 'exclude_patterns': None,
  1222. },
  1223. storage_config={},
  1224. local_borg_version='1.2.3',
  1225. local_path='borg1',
  1226. )
  1227. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1228. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1229. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1230. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1231. flexmock(module).should_receive('expand_directories').and_return(())
  1232. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1233. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1234. flexmock(module).should_receive('expand_home_directories').and_return(())
  1235. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1236. flexmock(module.feature).should_receive('available').and_return(True)
  1237. flexmock(module).should_receive('ensure_files_readable')
  1238. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1239. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1240. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1241. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1242. )
  1243. flexmock(module.environment).should_receive('make_environment')
  1244. flexmock(module).should_receive('execute_command').with_args(
  1245. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1246. output_log_level=logging.INFO,
  1247. output_file=None,
  1248. borg_local_path='borg',
  1249. working_directory=None,
  1250. extra_environment=None,
  1251. )
  1252. module.create_archive(
  1253. dry_run=False,
  1254. repository='repo',
  1255. location_config={
  1256. 'source_directories': ['foo', 'bar'],
  1257. 'repositories': ['repo'],
  1258. 'exclude_patterns': None,
  1259. },
  1260. storage_config={},
  1261. local_borg_version='1.2.3',
  1262. remote_path='borg1',
  1263. )
  1264. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1265. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1266. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1267. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1268. flexmock(module).should_receive('expand_directories').and_return(())
  1269. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1270. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1271. flexmock(module).should_receive('expand_home_directories').and_return(())
  1272. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1273. flexmock(module.feature).should_receive('available').and_return(True)
  1274. flexmock(module).should_receive('ensure_files_readable')
  1275. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1276. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1277. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1278. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1279. )
  1280. flexmock(module.environment).should_receive('make_environment')
  1281. flexmock(module).should_receive('execute_command').with_args(
  1282. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1283. output_log_level=logging.INFO,
  1284. output_file=None,
  1285. borg_local_path='borg',
  1286. working_directory=None,
  1287. extra_environment=None,
  1288. )
  1289. module.create_archive(
  1290. dry_run=False,
  1291. repository='repo',
  1292. location_config={
  1293. 'source_directories': ['foo', 'bar'],
  1294. 'repositories': ['repo'],
  1295. 'exclude_patterns': None,
  1296. },
  1297. storage_config={'umask': 740},
  1298. local_borg_version='1.2.3',
  1299. )
  1300. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1301. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1302. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1303. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1304. flexmock(module).should_receive('expand_directories').and_return(())
  1305. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1306. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1307. flexmock(module).should_receive('expand_home_directories').and_return(())
  1308. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1309. flexmock(module.feature).should_receive('available').and_return(True)
  1310. flexmock(module).should_receive('ensure_files_readable')
  1311. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1312. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1313. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1314. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1315. )
  1316. flexmock(module.environment).should_receive('make_environment')
  1317. flexmock(module).should_receive('execute_command').with_args(
  1318. ('borg', 'create', '--lock-wait', '5') + REPO_ARCHIVE_WITH_PATHS,
  1319. output_log_level=logging.INFO,
  1320. output_file=None,
  1321. borg_local_path='borg',
  1322. working_directory=None,
  1323. extra_environment=None,
  1324. )
  1325. module.create_archive(
  1326. dry_run=False,
  1327. repository='repo',
  1328. location_config={
  1329. 'source_directories': ['foo', 'bar'],
  1330. 'repositories': ['repo'],
  1331. 'exclude_patterns': None,
  1332. },
  1333. storage_config={'lock_wait': 5},
  1334. local_borg_version='1.2.3',
  1335. )
  1336. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  1337. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1338. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1339. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1340. flexmock(module).should_receive('expand_directories').and_return(())
  1341. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1342. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1343. flexmock(module).should_receive('expand_home_directories').and_return(())
  1344. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1345. flexmock(module.feature).should_receive('available').and_return(True)
  1346. flexmock(module).should_receive('ensure_files_readable')
  1347. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1348. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1349. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1350. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1351. )
  1352. flexmock(module.environment).should_receive('make_environment')
  1353. flexmock(module).should_receive('execute_command').with_args(
  1354. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--stats',),
  1355. output_log_level=logging.WARNING,
  1356. output_file=None,
  1357. borg_local_path='borg',
  1358. working_directory=None,
  1359. extra_environment=None,
  1360. )
  1361. module.create_archive(
  1362. dry_run=False,
  1363. repository='repo',
  1364. location_config={
  1365. 'source_directories': ['foo', 'bar'],
  1366. 'repositories': ['repo'],
  1367. 'exclude_patterns': None,
  1368. },
  1369. storage_config={},
  1370. local_borg_version='1.2.3',
  1371. stats=True,
  1372. )
  1373. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  1374. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1375. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1376. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1377. flexmock(module).should_receive('expand_directories').and_return(())
  1378. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1379. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1380. flexmock(module).should_receive('expand_home_directories').and_return(())
  1381. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1382. flexmock(module.feature).should_receive('available').and_return(True)
  1383. flexmock(module).should_receive('ensure_files_readable')
  1384. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1385. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1386. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1387. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1388. )
  1389. flexmock(module.environment).should_receive('make_environment')
  1390. flexmock(module).should_receive('execute_command').with_args(
  1391. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info', '--stats'),
  1392. output_log_level=logging.INFO,
  1393. output_file=None,
  1394. borg_local_path='borg',
  1395. working_directory=None,
  1396. extra_environment=None,
  1397. )
  1398. insert_logging_mock(logging.INFO)
  1399. module.create_archive(
  1400. dry_run=False,
  1401. repository='repo',
  1402. location_config={
  1403. 'source_directories': ['foo', 'bar'],
  1404. 'repositories': ['repo'],
  1405. 'exclude_patterns': None,
  1406. },
  1407. storage_config={},
  1408. local_borg_version='1.2.3',
  1409. stats=True,
  1410. )
  1411. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  1412. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1413. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1414. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1415. flexmock(module).should_receive('expand_directories').and_return(())
  1416. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1417. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1418. flexmock(module).should_receive('expand_home_directories').and_return(())
  1419. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1420. flexmock(module.feature).should_receive('available').and_return(True)
  1421. flexmock(module).should_receive('ensure_files_readable')
  1422. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1423. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1424. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1425. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1426. )
  1427. flexmock(module.environment).should_receive('make_environment')
  1428. flexmock(module).should_receive('execute_command').with_args(
  1429. ('borg', 'create', '--list', '--filter', 'AMEx-') + REPO_ARCHIVE_WITH_PATHS,
  1430. output_log_level=logging.WARNING,
  1431. output_file=None,
  1432. borg_local_path='borg',
  1433. working_directory=None,
  1434. extra_environment=None,
  1435. )
  1436. module.create_archive(
  1437. dry_run=False,
  1438. repository='repo',
  1439. location_config={
  1440. 'source_directories': ['foo', 'bar'],
  1441. 'repositories': ['repo'],
  1442. 'exclude_patterns': None,
  1443. },
  1444. storage_config={},
  1445. local_borg_version='1.2.3',
  1446. list_files=True,
  1447. )
  1448. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1449. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1450. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1451. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1452. flexmock(module).should_receive('expand_directories').and_return(())
  1453. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1454. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1455. flexmock(module).should_receive('expand_home_directories').and_return(())
  1456. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1457. flexmock(module.feature).should_receive('available').and_return(True)
  1458. flexmock(module).should_receive('ensure_files_readable')
  1459. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1460. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1461. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1462. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1463. )
  1464. flexmock(module.environment).should_receive('make_environment')
  1465. flexmock(module).should_receive('execute_command').with_args(
  1466. ('borg', 'create', '--list', '--filter', 'AMEx-') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  1467. output_log_level=logging.INFO,
  1468. output_file=None,
  1469. borg_local_path='borg',
  1470. working_directory=None,
  1471. extra_environment=None,
  1472. )
  1473. insert_logging_mock(logging.INFO)
  1474. module.create_archive(
  1475. dry_run=False,
  1476. repository='repo',
  1477. location_config={
  1478. 'source_directories': ['foo', 'bar'],
  1479. 'repositories': ['repo'],
  1480. 'exclude_patterns': None,
  1481. },
  1482. storage_config={},
  1483. local_borg_version='1.2.3',
  1484. list_files=True,
  1485. )
  1486. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1487. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1488. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1489. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1490. flexmock(module).should_receive('expand_directories').and_return(())
  1491. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1492. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1493. flexmock(module).should_receive('expand_home_directories').and_return(())
  1494. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1495. flexmock(module.feature).should_receive('available').and_return(True)
  1496. flexmock(module).should_receive('ensure_files_readable')
  1497. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1498. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1499. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1500. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1501. )
  1502. flexmock(module.environment).should_receive('make_environment')
  1503. flexmock(module).should_receive('execute_command').with_args(
  1504. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info', '--progress',),
  1505. output_log_level=logging.INFO,
  1506. output_file=module.DO_NOT_CAPTURE,
  1507. borg_local_path='borg',
  1508. working_directory=None,
  1509. extra_environment=None,
  1510. )
  1511. insert_logging_mock(logging.INFO)
  1512. module.create_archive(
  1513. dry_run=False,
  1514. repository='repo',
  1515. location_config={
  1516. 'source_directories': ['foo', 'bar'],
  1517. 'repositories': ['repo'],
  1518. 'exclude_patterns': None,
  1519. },
  1520. storage_config={},
  1521. local_borg_version='1.2.3',
  1522. progress=True,
  1523. )
  1524. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1525. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1526. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1527. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1528. flexmock(module).should_receive('expand_directories').and_return(())
  1529. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1530. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1531. flexmock(module).should_receive('expand_home_directories').and_return(())
  1532. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1533. flexmock(module.feature).should_receive('available').and_return(True)
  1534. flexmock(module).should_receive('ensure_files_readable')
  1535. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1536. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1537. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1538. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1539. )
  1540. flexmock(module.environment).should_receive('make_environment')
  1541. flexmock(module).should_receive('execute_command').with_args(
  1542. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--progress',),
  1543. output_log_level=logging.INFO,
  1544. output_file=module.DO_NOT_CAPTURE,
  1545. borg_local_path='borg',
  1546. working_directory=None,
  1547. extra_environment=None,
  1548. )
  1549. module.create_archive(
  1550. dry_run=False,
  1551. repository='repo',
  1552. location_config={
  1553. 'source_directories': ['foo', 'bar'],
  1554. 'repositories': ['repo'],
  1555. 'exclude_patterns': None,
  1556. },
  1557. storage_config={},
  1558. local_borg_version='1.2.3',
  1559. progress=True,
  1560. )
  1561. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1562. processes = flexmock()
  1563. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1564. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1565. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1566. flexmock(module).should_receive('expand_directories').and_return(())
  1567. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1568. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1569. flexmock(module).should_receive('expand_home_directories').and_return(())
  1570. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1571. flexmock(module.feature).should_receive('available').and_return(True)
  1572. flexmock(module).should_receive('ensure_files_readable')
  1573. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1574. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1575. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1576. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1577. )
  1578. flexmock(module.environment).should_receive('make_environment')
  1579. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1580. create_command = (
  1581. ('borg', 'create', '--one-file-system', '--read-special')
  1582. + REPO_ARCHIVE_WITH_PATHS
  1583. + ('--progress',)
  1584. )
  1585. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1586. create_command + ('--dry-run', '--list'),
  1587. processes=processes,
  1588. output_log_level=logging.INFO,
  1589. output_file=module.DO_NOT_CAPTURE,
  1590. borg_local_path='borg',
  1591. working_directory=None,
  1592. extra_environment=None,
  1593. )
  1594. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1595. create_command,
  1596. processes=processes,
  1597. output_log_level=logging.INFO,
  1598. output_file=module.DO_NOT_CAPTURE,
  1599. borg_local_path='borg',
  1600. working_directory=None,
  1601. extra_environment=None,
  1602. )
  1603. module.create_archive(
  1604. dry_run=False,
  1605. repository='repo',
  1606. location_config={
  1607. 'source_directories': ['foo', 'bar'],
  1608. 'repositories': ['repo'],
  1609. 'exclude_patterns': None,
  1610. },
  1611. storage_config={},
  1612. local_borg_version='1.2.3',
  1613. progress=True,
  1614. stream_processes=processes,
  1615. )
  1616. def test_create_archive_with_stream_processes_ignores_read_special_false_and_logs_warnings():
  1617. processes = flexmock()
  1618. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1619. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1620. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1621. flexmock(module).should_receive('expand_directories').and_return(())
  1622. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1623. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1624. flexmock(module).should_receive('expand_home_directories').and_return(())
  1625. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1626. flexmock(module.feature).should_receive('available').and_return(True)
  1627. flexmock(module).should_receive('ensure_files_readable')
  1628. flexmock(module.logger).should_receive('warning').twice()
  1629. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1630. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1631. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1632. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1633. )
  1634. flexmock(module.environment).should_receive('make_environment')
  1635. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1636. create_command = (
  1637. 'borg',
  1638. 'create',
  1639. '--one-file-system',
  1640. '--read-special',
  1641. ) + REPO_ARCHIVE_WITH_PATHS
  1642. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1643. create_command + ('--dry-run', '--list'),
  1644. processes=processes,
  1645. output_log_level=logging.INFO,
  1646. output_file=None,
  1647. borg_local_path='borg',
  1648. working_directory=None,
  1649. extra_environment=None,
  1650. )
  1651. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1652. create_command,
  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. module.create_archive(
  1661. dry_run=False,
  1662. repository='repo',
  1663. location_config={
  1664. 'source_directories': ['foo', 'bar'],
  1665. 'repositories': ['repo'],
  1666. 'exclude_patterns': None,
  1667. 'read_special': False,
  1668. },
  1669. storage_config={},
  1670. local_borg_version='1.2.3',
  1671. stream_processes=processes,
  1672. )
  1673. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1674. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1675. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1676. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1677. flexmock(module).should_receive('expand_directories').and_return(())
  1678. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1679. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1680. flexmock(module).should_receive('expand_home_directories').and_return(())
  1681. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1682. flexmock(module.feature).should_receive('available').and_return(True)
  1683. flexmock(module).should_receive('ensure_files_readable')
  1684. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1685. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1686. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1687. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1688. )
  1689. flexmock(module.environment).should_receive('make_environment')
  1690. flexmock(module).should_receive('execute_command').with_args(
  1691. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1692. output_log_level=None,
  1693. output_file=None,
  1694. borg_local_path='borg',
  1695. working_directory=None,
  1696. extra_environment=None,
  1697. ).and_return('[]')
  1698. json_output = module.create_archive(
  1699. dry_run=False,
  1700. repository='repo',
  1701. location_config={
  1702. 'source_directories': ['foo', 'bar'],
  1703. 'repositories': ['repo'],
  1704. 'exclude_patterns': None,
  1705. },
  1706. storage_config={},
  1707. local_borg_version='1.2.3',
  1708. json=True,
  1709. )
  1710. assert json_output == '[]'
  1711. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1712. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1713. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1714. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1715. flexmock(module).should_receive('expand_directories').and_return(())
  1716. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1717. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1718. flexmock(module).should_receive('expand_home_directories').and_return(())
  1719. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1720. flexmock(module.feature).should_receive('available').and_return(True)
  1721. flexmock(module).should_receive('ensure_files_readable')
  1722. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1723. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1724. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1725. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1726. )
  1727. flexmock(module.environment).should_receive('make_environment')
  1728. flexmock(module).should_receive('execute_command').with_args(
  1729. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1730. output_log_level=None,
  1731. output_file=None,
  1732. borg_local_path='borg',
  1733. working_directory=None,
  1734. extra_environment=None,
  1735. ).and_return('[]')
  1736. json_output = module.create_archive(
  1737. dry_run=False,
  1738. repository='repo',
  1739. location_config={
  1740. 'source_directories': ['foo', 'bar'],
  1741. 'repositories': ['repo'],
  1742. 'exclude_patterns': None,
  1743. },
  1744. storage_config={},
  1745. local_borg_version='1.2.3',
  1746. json=True,
  1747. stats=True,
  1748. )
  1749. assert json_output == '[]'
  1750. def test_create_archive_with_source_directories_glob_expands():
  1751. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1752. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1753. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1754. flexmock(module).should_receive('expand_directories').and_return(())
  1755. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1756. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1757. flexmock(module).should_receive('expand_home_directories').and_return(())
  1758. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1759. flexmock(module.feature).should_receive('available').and_return(True)
  1760. flexmock(module).should_receive('ensure_files_readable')
  1761. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1762. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1763. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1764. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1765. )
  1766. flexmock(module.environment).should_receive('make_environment')
  1767. flexmock(module).should_receive('execute_command').with_args(
  1768. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1769. output_log_level=logging.INFO,
  1770. output_file=None,
  1771. borg_local_path='borg',
  1772. working_directory=None,
  1773. extra_environment=None,
  1774. )
  1775. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1776. module.create_archive(
  1777. dry_run=False,
  1778. repository='repo',
  1779. location_config={
  1780. 'source_directories': ['foo*'],
  1781. 'repositories': ['repo'],
  1782. 'exclude_patterns': None,
  1783. },
  1784. storage_config={},
  1785. local_borg_version='1.2.3',
  1786. )
  1787. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1788. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1789. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1790. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1791. flexmock(module).should_receive('expand_directories').and_return(())
  1792. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1793. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1794. flexmock(module).should_receive('expand_home_directories').and_return(())
  1795. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1796. flexmock(module.feature).should_receive('available').and_return(True)
  1797. flexmock(module).should_receive('ensure_files_readable')
  1798. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1799. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1800. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1801. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1802. )
  1803. flexmock(module.environment).should_receive('make_environment')
  1804. flexmock(module).should_receive('execute_command').with_args(
  1805. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1806. output_log_level=logging.INFO,
  1807. output_file=None,
  1808. borg_local_path='borg',
  1809. working_directory=None,
  1810. extra_environment=None,
  1811. )
  1812. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1813. module.create_archive(
  1814. dry_run=False,
  1815. repository='repo',
  1816. location_config={
  1817. 'source_directories': ['foo*'],
  1818. 'repositories': ['repo'],
  1819. 'exclude_patterns': None,
  1820. },
  1821. storage_config={},
  1822. local_borg_version='1.2.3',
  1823. )
  1824. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1825. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1826. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1827. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1828. flexmock(module).should_receive('expand_directories').and_return(())
  1829. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1830. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1831. flexmock(module).should_receive('expand_home_directories').and_return(())
  1832. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1833. flexmock(module.feature).should_receive('available').and_return(True)
  1834. flexmock(module).should_receive('ensure_files_readable')
  1835. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1836. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1837. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1838. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1839. )
  1840. flexmock(module.environment).should_receive('make_environment')
  1841. flexmock(module).should_receive('execute_command').with_args(
  1842. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1843. output_log_level=logging.INFO,
  1844. output_file=None,
  1845. borg_local_path='borg',
  1846. working_directory=None,
  1847. extra_environment=None,
  1848. )
  1849. module.create_archive(
  1850. dry_run=False,
  1851. repository='repo',
  1852. location_config={
  1853. 'source_directories': ['foo*'],
  1854. 'repositories': ['repo'],
  1855. 'exclude_patterns': None,
  1856. },
  1857. storage_config={},
  1858. local_borg_version='1.2.3',
  1859. )
  1860. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1861. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1862. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1863. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1864. flexmock(module).should_receive('expand_directories').and_return(())
  1865. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1866. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1867. flexmock(module).should_receive('expand_home_directories').and_return(())
  1868. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1869. flexmock(module.feature).should_receive('available').and_return(True)
  1870. flexmock(module).should_receive('ensure_files_readable')
  1871. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1872. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1873. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1874. ('repo::ARCHIVE_NAME',)
  1875. )
  1876. flexmock(module.environment).should_receive('make_environment')
  1877. flexmock(module).should_receive('execute_command').with_args(
  1878. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1879. output_log_level=logging.INFO,
  1880. output_file=None,
  1881. borg_local_path='borg',
  1882. working_directory=None,
  1883. extra_environment=None,
  1884. )
  1885. module.create_archive(
  1886. dry_run=False,
  1887. repository='repo',
  1888. location_config={
  1889. 'source_directories': ['foo', 'bar'],
  1890. 'repositories': ['repo'],
  1891. 'exclude_patterns': None,
  1892. },
  1893. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1894. local_borg_version='1.2.3',
  1895. )
  1896. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1897. repository_archive_pattern = 'repo::Documents_{hostname}-{now}'
  1898. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1899. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1900. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1901. flexmock(module).should_receive('expand_directories').and_return(())
  1902. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1903. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1904. flexmock(module).should_receive('expand_home_directories').and_return(())
  1905. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1906. flexmock(module.feature).should_receive('available').and_return(True)
  1907. flexmock(module).should_receive('ensure_files_readable')
  1908. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1909. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1910. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1911. (repository_archive_pattern,)
  1912. )
  1913. flexmock(module.environment).should_receive('make_environment')
  1914. flexmock(module).should_receive('execute_command').with_args(
  1915. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1916. output_log_level=logging.INFO,
  1917. output_file=None,
  1918. borg_local_path='borg',
  1919. working_directory=None,
  1920. extra_environment=None,
  1921. )
  1922. module.create_archive(
  1923. dry_run=False,
  1924. repository='repo',
  1925. location_config={
  1926. 'source_directories': ['foo', 'bar'],
  1927. 'repositories': ['repo'],
  1928. 'exclude_patterns': None,
  1929. },
  1930. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1931. local_borg_version='1.2.3',
  1932. )
  1933. def test_create_archive_with_repository_accepts_borg_placeholders():
  1934. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}'
  1935. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1936. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1937. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1938. flexmock(module).should_receive('expand_directories').and_return(())
  1939. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1940. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1941. flexmock(module).should_receive('expand_home_directories').and_return(())
  1942. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1943. flexmock(module.feature).should_receive('available').and_return(True)
  1944. flexmock(module).should_receive('ensure_files_readable')
  1945. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1946. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1947. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1948. (repository_archive_pattern,)
  1949. )
  1950. flexmock(module.environment).should_receive('make_environment')
  1951. flexmock(module).should_receive('execute_command').with_args(
  1952. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1953. output_log_level=logging.INFO,
  1954. output_file=None,
  1955. borg_local_path='borg',
  1956. working_directory=None,
  1957. extra_environment=None,
  1958. )
  1959. module.create_archive(
  1960. dry_run=False,
  1961. repository='{fqdn}',
  1962. location_config={
  1963. 'source_directories': ['foo', 'bar'],
  1964. 'repositories': ['{fqdn}'],
  1965. 'exclude_patterns': None,
  1966. },
  1967. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1968. local_borg_version='1.2.3',
  1969. )
  1970. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1971. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1972. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1973. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1974. flexmock(module).should_receive('expand_directories').and_return(())
  1975. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1976. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1977. flexmock(module).should_receive('expand_home_directories').and_return(())
  1978. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1979. flexmock(module.feature).should_receive('available').and_return(True)
  1980. flexmock(module).should_receive('ensure_files_readable')
  1981. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1982. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1983. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1984. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1985. )
  1986. flexmock(module.environment).should_receive('make_environment')
  1987. flexmock(module).should_receive('execute_command').with_args(
  1988. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  1989. output_log_level=logging.INFO,
  1990. output_file=None,
  1991. borg_local_path='borg',
  1992. working_directory=None,
  1993. extra_environment=None,
  1994. )
  1995. module.create_archive(
  1996. dry_run=False,
  1997. repository='repo',
  1998. location_config={
  1999. 'source_directories': ['foo', 'bar'],
  2000. 'repositories': ['repo'],
  2001. 'exclude_patterns': None,
  2002. },
  2003. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  2004. local_borg_version='1.2.3',
  2005. )
  2006. def test_create_archive_with_stream_processes_calls_borg_with_processes_and_read_special():
  2007. processes = flexmock()
  2008. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2009. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2010. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2011. flexmock(module).should_receive('expand_directories').and_return(())
  2012. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2013. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2014. flexmock(module).should_receive('expand_home_directories').and_return(())
  2015. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2016. flexmock(module.feature).should_receive('available').and_return(True)
  2017. flexmock(module).should_receive('ensure_files_readable')
  2018. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2019. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2020. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2021. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2022. )
  2023. flexmock(module.environment).should_receive('make_environment')
  2024. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  2025. create_command = (
  2026. 'borg',
  2027. 'create',
  2028. '--one-file-system',
  2029. '--read-special',
  2030. ) + REPO_ARCHIVE_WITH_PATHS
  2031. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2032. create_command + ('--dry-run', 'list'),
  2033. processes=processes,
  2034. output_log_level=logging.INFO,
  2035. output_file=None,
  2036. borg_local_path='borg',
  2037. working_directory=None,
  2038. extra_environment=None,
  2039. )
  2040. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2041. create_command,
  2042. processes=processes,
  2043. output_log_level=logging.INFO,
  2044. output_file=None,
  2045. borg_local_path='borg',
  2046. working_directory=None,
  2047. extra_environment=None,
  2048. )
  2049. module.create_archive(
  2050. dry_run=False,
  2051. repository='repo',
  2052. location_config={
  2053. 'source_directories': ['foo', 'bar'],
  2054. 'repositories': ['repo'],
  2055. 'exclude_patterns': None,
  2056. },
  2057. storage_config={},
  2058. local_borg_version='1.2.3',
  2059. stream_processes=processes,
  2060. )