test_create.py 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  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', None)
  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*', None)
  16. assert paths == ['foo', 'food']
  17. def test_expand_directory_with_working_directory_passes_it_through():
  18. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  19. flexmock(module.glob).should_receive('glob').with_args('/working/dir/foo').and_return([]).once()
  20. paths = module.expand_directory('foo', working_directory='/working/dir')
  21. assert paths == ['/working/dir/foo']
  22. def test_expand_directory_with_glob_passes_through_working_directory():
  23. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  24. flexmock(module.glob).should_receive('glob').with_args('/working/dir/foo*').and_return(
  25. ['/working/dir/foo', '/working/dir/food']
  26. ).once()
  27. paths = module.expand_directory('foo*', working_directory='/working/dir')
  28. assert paths == ['/working/dir/foo', '/working/dir/food']
  29. def test_expand_directories_flattens_expanded_directories():
  30. flexmock(module).should_receive('expand_directory').with_args('~/foo', None).and_return(
  31. ['/root/foo']
  32. )
  33. flexmock(module).should_receive('expand_directory').with_args('bar*', None).and_return(
  34. ['bar', 'barf']
  35. )
  36. paths = module.expand_directories(('~/foo', 'bar*'))
  37. assert paths == ('/root/foo', 'bar', 'barf')
  38. def test_expand_directories_with_working_directory_passes_it_through():
  39. flexmock(module).should_receive('expand_directory').with_args('foo', '/working/dir').and_return(
  40. ['/working/dir/foo']
  41. )
  42. paths = module.expand_directories(('foo',), working_directory='/working/dir')
  43. assert paths == ('/working/dir/foo',)
  44. def test_expand_directories_considers_none_as_no_directories():
  45. paths = module.expand_directories(None, None)
  46. assert paths == ()
  47. def test_expand_home_directories_expands_tildes():
  48. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  49. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  50. paths = module.expand_home_directories(('~/bar', 'baz'))
  51. assert paths == ('/foo/bar', 'baz')
  52. def test_expand_home_directories_considers_none_as_no_directories():
  53. paths = module.expand_home_directories(None)
  54. assert paths == ()
  55. def test_map_directories_to_devices_gives_device_id_per_path():
  56. flexmock(module.os.path).should_receive('exists').and_return(True)
  57. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  58. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=66))
  59. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  60. assert device_map == {
  61. '/foo': 55,
  62. '/bar': 66,
  63. }
  64. def test_map_directories_to_devices_with_missing_path_does_not_error():
  65. flexmock(module.os.path).should_receive('exists').and_return(True).and_return(False)
  66. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  67. flexmock(module.os).should_receive('stat').with_args('/bar').never()
  68. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  69. assert device_map == {
  70. '/foo': 55,
  71. '/bar': None,
  72. }
  73. def test_map_directories_to_devices_uses_working_directory_to_construct_path():
  74. flexmock(module.os.path).should_receive('exists').and_return(True)
  75. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  76. flexmock(module.os).should_receive('stat').with_args('/working/dir/bar').and_return(
  77. flexmock(st_dev=66)
  78. )
  79. device_map = module.map_directories_to_devices(
  80. ('/foo', 'bar'), working_directory='/working/dir'
  81. )
  82. assert device_map == {
  83. '/foo': 55,
  84. 'bar': 66,
  85. }
  86. @pytest.mark.parametrize(
  87. 'directories,additional_directories,expected_directories',
  88. (
  89. ({'/': 1, '/root': 1}, {}, ('/',)),
  90. ({'/': 1, '/root/': 1}, {}, ('/',)),
  91. ({'/': 1, '/root': 2}, {}, ('/', '/root')),
  92. ({'/root': 1, '/': 1}, {}, ('/',)),
  93. ({'/root': 1, '/root/foo': 1}, {}, ('/root',)),
  94. ({'/root/': 1, '/root/foo': 1}, {}, ('/root/',)),
  95. ({'/root': 1, '/root/foo/': 1}, {}, ('/root',)),
  96. ({'/root': 1, '/root/foo': 2}, {}, ('/root', '/root/foo')),
  97. ({'/root/foo': 1, '/root': 1}, {}, ('/root',)),
  98. ({'/root': None, '/root/foo': None}, {}, ('/root', '/root/foo')),
  99. ({'/root': 1, '/etc': 1, '/root/foo/bar': 1}, {}, ('/etc', '/root')),
  100. ({'/root': 1, '/root/foo': 1, '/root/foo/bar': 1}, {}, ('/root',)),
  101. ({'/dup': 1, '/dup': 1}, {}, ('/dup',)),
  102. ({'/foo': 1, '/bar': 1}, {}, ('/bar', '/foo')),
  103. ({'/foo': 1, '/bar': 2}, {}, ('/bar', '/foo')),
  104. ({'/root/foo': 1}, {'/root': 1}, ()),
  105. ({'/root/foo': 1}, {'/root': 2}, ('/root/foo',)),
  106. ({'/root/foo': 1}, {}, ('/root/foo',)),
  107. ),
  108. )
  109. def test_deduplicate_directories_removes_child_paths_on_the_same_filesystem(
  110. directories, additional_directories, expected_directories
  111. ):
  112. assert (
  113. module.deduplicate_directories(directories, additional_directories) == expected_directories
  114. )
  115. def test_write_pattern_file_writes_pattern_lines():
  116. temporary_file = flexmock(name='filename', flush=lambda: None)
  117. temporary_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  118. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  119. module.write_pattern_file(['R /foo', '+ /foo/bar'])
  120. def test_write_pattern_file_with_sources_writes_sources_as_roots():
  121. temporary_file = flexmock(name='filename', flush=lambda: None)
  122. temporary_file.should_receive('write').with_args('R /foo\n+ /foo/bar\nR /baz\nR /quux')
  123. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  124. module.write_pattern_file(['R /foo', '+ /foo/bar'], sources=['/baz', '/quux'])
  125. def test_write_pattern_file_without_patterns_but_with_sources_writes_sources_as_roots():
  126. temporary_file = flexmock(name='filename', flush=lambda: None)
  127. temporary_file.should_receive('write').with_args('R /baz\nR /quux')
  128. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  129. module.write_pattern_file([], sources=['/baz', '/quux'])
  130. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  131. module.write_pattern_file([])
  132. def test_write_pattern_file_overwrites_existing_file():
  133. pattern_file = flexmock(name='filename', flush=lambda: None)
  134. pattern_file.should_receive('seek').with_args(0).once()
  135. pattern_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  136. flexmock(module.tempfile).should_receive('NamedTemporaryFile').never()
  137. module.write_pattern_file(['R /foo', '+ /foo/bar'], pattern_file=pattern_file)
  138. @pytest.mark.parametrize(
  139. 'filename_lists,opened_filenames',
  140. (
  141. ([('foo', 'bar'), ('baz', 'quux')], ('foo', 'bar', 'baz', 'quux')),
  142. ([None, ('foo', 'bar')], ('foo', 'bar')),
  143. ([None, None], ()),
  144. ),
  145. )
  146. def test_ensure_files_readable_opens_filenames(filename_lists, opened_filenames):
  147. for expected_filename in opened_filenames:
  148. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  149. expected_filename
  150. ).and_return(flexmock(close=lambda: None))
  151. module.ensure_files_readable(*filename_lists)
  152. def test_make_pattern_flags_includes_pattern_filename_when_given():
  153. pattern_flags = module.make_pattern_flags(
  154. config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  155. )
  156. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  157. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  158. pattern_flags = module.make_pattern_flags(config={'patterns_from': ['patterns', 'other']})
  159. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  160. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  161. pattern_flags = module.make_pattern_flags(
  162. config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  163. )
  164. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  165. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  166. pattern_flags = module.make_pattern_flags(config={'patterns_from': None})
  167. assert pattern_flags == ()
  168. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  169. exclude_flags = module.make_exclude_flags(
  170. config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  171. )
  172. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  173. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  174. exclude_flags = module.make_exclude_flags(config={'exclude_from': ['excludes', 'other']})
  175. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  176. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  177. exclude_flags = module.make_exclude_flags(
  178. config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  179. )
  180. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  181. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  182. exclude_flags = module.make_exclude_flags(config={'exclude_from': None})
  183. assert exclude_flags == ()
  184. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  185. exclude_flags = module.make_exclude_flags(config={'exclude_caches': True})
  186. assert exclude_flags == ('--exclude-caches',)
  187. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  188. exclude_flags = module.make_exclude_flags(config={'exclude_caches': False})
  189. assert exclude_flags == ()
  190. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  191. exclude_flags = module.make_exclude_flags(
  192. config={'exclude_if_present': ['exclude_me', 'also_me']}
  193. )
  194. assert exclude_flags == (
  195. '--exclude-if-present',
  196. 'exclude_me',
  197. '--exclude-if-present',
  198. 'also_me',
  199. )
  200. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  201. exclude_flags = module.make_exclude_flags(config={'keep_exclude_tags': True})
  202. assert exclude_flags == ('--keep-exclude-tags',)
  203. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  204. exclude_flags = module.make_exclude_flags(config={'keep_exclude_tags': False})
  205. assert exclude_flags == ()
  206. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  207. exclude_flags = module.make_exclude_flags(config={'exclude_nodump': True})
  208. assert exclude_flags == ('--exclude-nodump',)
  209. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  210. exclude_flags = module.make_exclude_flags(config={'exclude_nodump': False})
  211. assert exclude_flags == ()
  212. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  213. exclude_flags = module.make_exclude_flags(config={})
  214. assert exclude_flags == ()
  215. def test_make_list_filter_flags_with_debug_and_feature_available_includes_plus_and_minus():
  216. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  217. flexmock(module.feature).should_receive('available').and_return(True)
  218. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME+-'
  219. def test_make_list_filter_flags_with_info_and_feature_available_omits_plus_and_minus():
  220. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  221. flexmock(module.feature).should_receive('available').and_return(True)
  222. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME'
  223. def test_make_list_filter_flags_with_debug_and_feature_available_and_dry_run_includes_plus_and_minus():
  224. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  225. flexmock(module.feature).should_receive('available').and_return(True)
  226. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
  227. def test_make_list_filter_flags_with_info_and_feature_available_and_dry_run_includes_plus_and_minus():
  228. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  229. flexmock(module.feature).should_receive('available').and_return(True)
  230. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
  231. def test_make_list_filter_flags_with_debug_and_feature_not_available_includes_x():
  232. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  233. flexmock(module.feature).should_receive('available').and_return(False)
  234. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AMEx-'
  235. def test_make_list_filter_flags_with_info_and_feature_not_available_omits_x():
  236. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  237. flexmock(module.feature).should_receive('available').and_return(False)
  238. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME-'
  239. def test_collect_borgmatic_runtime_directories_set_when_directory_exists():
  240. flexmock(module.os.path).should_receive('exists').and_return(True)
  241. assert module.collect_borgmatic_runtime_directories('/tmp') == ['/tmp']
  242. def test_collect_borgmatic_runtime_directories_empty_when_directory_does_not_exist():
  243. flexmock(module.os.path).should_receive('exists').and_return(False)
  244. assert module.collect_borgmatic_runtime_directories('/tmp') == []
  245. def test_pattern_root_directories_deals_with_none_patterns():
  246. assert module.pattern_root_directories(patterns=None) == []
  247. def test_pattern_root_directories_parses_roots_and_ignores_others():
  248. assert module.pattern_root_directories(
  249. ['R /root', '+ /root/foo', '- /root/foo/bar', 'R /baz']
  250. ) == ['/root', '/baz']
  251. @pytest.mark.parametrize(
  252. 'character_device,block_device,fifo,expected_result',
  253. (
  254. (False, False, False, False),
  255. (True, False, False, True),
  256. (False, True, False, True),
  257. (True, True, False, True),
  258. (False, False, True, True),
  259. (False, True, True, True),
  260. (True, False, True, True),
  261. ),
  262. )
  263. def test_special_file_looks_at_file_type(character_device, block_device, fifo, expected_result):
  264. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mode=flexmock()))
  265. flexmock(module.stat).should_receive('S_ISCHR').and_return(character_device)
  266. flexmock(module.stat).should_receive('S_ISBLK').and_return(block_device)
  267. flexmock(module.stat).should_receive('S_ISFIFO').and_return(fifo)
  268. assert module.special_file('/dev/special') == expected_result
  269. def test_special_file_treats_broken_symlink_as_non_special():
  270. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  271. assert module.special_file('/broken/symlink') is False
  272. def test_any_parent_directories_treats_parents_as_match():
  273. module.any_parent_directories('/foo/bar.txt', ('/foo', '/etc'))
  274. def test_any_parent_directories_treats_grandparents_as_match():
  275. module.any_parent_directories('/foo/bar/baz.txt', ('/foo', '/etc'))
  276. def test_any_parent_directories_treats_unrelated_paths_as_non_match():
  277. module.any_parent_directories('/foo/bar.txt', ('/usr', '/etc'))
  278. def test_collect_special_file_paths_parses_special_files_from_borg_dry_run_file_list():
  279. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  280. 'Processing files ...\n- /foo\n+ /bar\n- /baz'
  281. )
  282. flexmock(module).should_receive('special_file').and_return(True)
  283. flexmock(module).should_receive('any_parent_directories').and_return(False)
  284. assert module.collect_special_file_paths(
  285. ('borg', 'create'),
  286. config={},
  287. local_path=None,
  288. working_directory=None,
  289. borg_environment=None,
  290. skip_directories=flexmock(),
  291. ) == ('/foo', '/bar', '/baz')
  292. def test_collect_special_file_paths_excludes_requested_directories():
  293. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  294. '+ /foo\n- /bar\n- /baz'
  295. )
  296. flexmock(module).should_receive('special_file').and_return(True)
  297. flexmock(module).should_receive('any_parent_directories').and_return(False).and_return(
  298. True
  299. ).and_return(False)
  300. assert module.collect_special_file_paths(
  301. ('borg', 'create'),
  302. config={},
  303. local_path=None,
  304. working_directory=None,
  305. borg_environment=None,
  306. skip_directories=flexmock(),
  307. ) == ('/foo', '/baz')
  308. def test_collect_special_file_paths_excludes_non_special_files():
  309. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  310. '+ /foo\n+ /bar\n+ /baz'
  311. )
  312. flexmock(module).should_receive('special_file').and_return(True).and_return(False).and_return(
  313. True
  314. )
  315. flexmock(module).should_receive('any_parent_directories').and_return(False)
  316. assert module.collect_special_file_paths(
  317. ('borg', 'create'),
  318. config={},
  319. local_path=None,
  320. working_directory=None,
  321. borg_environment=None,
  322. skip_directories=flexmock(),
  323. ) == ('/foo', '/baz')
  324. def test_collect_special_file_paths_omits_exclude_no_dump_flag_from_command():
  325. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  326. ('borg', 'create', '--dry-run', '--list'),
  327. capture_stderr=True,
  328. working_directory=None,
  329. extra_environment=None,
  330. borg_local_path='borg',
  331. borg_exit_codes=None,
  332. ).and_return('Processing files ...\n- /foo\n+ /bar\n- /baz').once()
  333. flexmock(module).should_receive('special_file').and_return(True)
  334. flexmock(module).should_receive('any_parent_directories').and_return(False)
  335. module.collect_special_file_paths(
  336. ('borg', 'create', '--exclude-nodump'),
  337. config={},
  338. local_path='borg',
  339. working_directory=None,
  340. borg_environment=None,
  341. skip_directories=flexmock(),
  342. )
  343. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003
  344. REPO_ARCHIVE_WITH_PATHS = (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'bar')
  345. def test_make_base_create_produces_borg_command():
  346. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  347. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  348. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  349. flexmock(module).should_receive('expand_directories').and_return(())
  350. flexmock(module).should_receive('pattern_root_directories').and_return([])
  351. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  352. flexmock(module).should_receive('expand_home_directories').and_return(())
  353. flexmock(module).should_receive('ensure_files_readable')
  354. flexmock(module).should_receive('write_pattern_file').and_return(None)
  355. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  356. flexmock(module.feature).should_receive('available').and_return(True)
  357. flexmock(module).should_receive('make_pattern_flags').and_return(())
  358. flexmock(module).should_receive('make_exclude_flags').and_return(())
  359. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  360. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  361. )
  362. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  363. module.make_base_create_command(
  364. dry_run=False,
  365. repository_path='repo',
  366. config={
  367. 'source_directories': ['foo', 'bar'],
  368. 'repositories': ['repo'],
  369. },
  370. config_paths=['/tmp/test.yaml'],
  371. local_borg_version='1.2.3',
  372. global_arguments=flexmock(log_json=False),
  373. borgmatic_runtime_directories=(),
  374. )
  375. )
  376. assert create_flags == ('borg', 'create')
  377. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  378. assert not pattern_file
  379. assert not exclude_file
  380. def test_make_base_create_command_includes_patterns_file_in_borg_command():
  381. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  382. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  383. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  384. flexmock(module).should_receive('expand_directories').and_return(())
  385. flexmock(module).should_receive('pattern_root_directories').and_return([])
  386. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  387. flexmock(module).should_receive('expand_home_directories').and_return(())
  388. mock_pattern_file = flexmock(name='/tmp/patterns')
  389. flexmock(module).should_receive('write_pattern_file').and_return(mock_pattern_file).and_return(
  390. None
  391. )
  392. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  393. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  394. '{hostname}'
  395. )
  396. flexmock(module.feature).should_receive('available').and_return(True)
  397. flexmock(module).should_receive('ensure_files_readable')
  398. pattern_flags = ('--patterns-from', mock_pattern_file.name)
  399. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  400. flexmock(module).should_receive('make_exclude_flags').and_return(())
  401. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  402. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  403. )
  404. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  405. module.make_base_create_command(
  406. dry_run=False,
  407. repository_path='repo',
  408. config={
  409. 'source_directories': ['foo', 'bar'],
  410. 'repositories': ['repo'],
  411. 'patterns': ['pattern'],
  412. },
  413. config_paths=['/tmp/test.yaml'],
  414. local_borg_version='1.2.3',
  415. global_arguments=flexmock(log_json=False),
  416. borgmatic_runtime_directories=(),
  417. )
  418. )
  419. assert create_flags == ('borg', 'create') + pattern_flags
  420. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  421. assert pattern_file == mock_pattern_file
  422. assert not exclude_file
  423. def test_make_base_create_command_includes_sources_and_config_paths_in_borg_command():
  424. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  425. flexmock(module).should_receive('deduplicate_directories').and_return(
  426. ('foo', 'bar', '/tmp/test.yaml')
  427. )
  428. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  429. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  430. flexmock(module).should_receive('expand_directories').with_args(
  431. ('foo', 'bar', '/tmp/test.yaml'),
  432. None,
  433. ).and_return(('foo', 'bar', '/tmp/test.yaml'))
  434. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  435. flexmock(module).should_receive('pattern_root_directories').and_return([])
  436. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  437. flexmock(module).should_receive('expand_home_directories').and_return(())
  438. flexmock(module).should_receive('write_pattern_file').and_return(None)
  439. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  440. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  441. '{hostname}'
  442. )
  443. flexmock(module.feature).should_receive('available').and_return(True)
  444. flexmock(module).should_receive('ensure_files_readable')
  445. flexmock(module).should_receive('make_pattern_flags').and_return(())
  446. flexmock(module).should_receive('make_exclude_flags').and_return(())
  447. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  448. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  449. )
  450. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  451. module.make_base_create_command(
  452. dry_run=False,
  453. repository_path='repo',
  454. config={
  455. 'source_directories': ['foo', 'bar'],
  456. 'repositories': ['repo'],
  457. },
  458. config_paths=['/tmp/test.yaml'],
  459. local_borg_version='1.2.3',
  460. global_arguments=flexmock(log_json=False),
  461. borgmatic_runtime_directories=(),
  462. )
  463. )
  464. assert create_flags == ('borg', 'create')
  465. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS + ('/tmp/test.yaml',)
  466. assert not pattern_file
  467. assert not exclude_file
  468. def test_make_base_create_command_with_store_config_false_omits_config_files():
  469. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  470. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  471. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  472. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  473. flexmock(module).should_receive('expand_directories').with_args(
  474. ('foo', 'bar'), None
  475. ).and_return(('foo', 'bar'))
  476. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  477. flexmock(module).should_receive('pattern_root_directories').and_return([])
  478. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  479. flexmock(module).should_receive('expand_home_directories').and_return(())
  480. flexmock(module).should_receive('write_pattern_file').and_return(None)
  481. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  482. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  483. '{hostname}'
  484. )
  485. flexmock(module.feature).should_receive('available').and_return(True)
  486. flexmock(module).should_receive('ensure_files_readable')
  487. flexmock(module).should_receive('make_pattern_flags').and_return(())
  488. flexmock(module).should_receive('make_exclude_flags').and_return(())
  489. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  490. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  491. )
  492. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  493. module.make_base_create_command(
  494. dry_run=False,
  495. repository_path='repo',
  496. config={
  497. 'source_directories': ['foo', 'bar'],
  498. 'repositories': ['repo'],
  499. 'store_config_files': False,
  500. },
  501. config_paths=['/tmp/test.yaml'],
  502. local_borg_version='1.2.3',
  503. global_arguments=flexmock(log_json=False),
  504. borgmatic_runtime_directories=(),
  505. )
  506. )
  507. assert create_flags == ('borg', 'create')
  508. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  509. assert not pattern_file
  510. assert not exclude_file
  511. def test_make_base_create_command_includes_exclude_patterns_in_borg_command():
  512. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  513. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  514. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  515. flexmock(module).should_receive('expand_directories').and_return(())
  516. flexmock(module).should_receive('pattern_root_directories').and_return([])
  517. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  518. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  519. mock_exclude_file = flexmock(name='/tmp/excludes')
  520. flexmock(module).should_receive('write_pattern_file').and_return(mock_exclude_file)
  521. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  522. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  523. '{hostname}'
  524. )
  525. flexmock(module.feature).should_receive('available').and_return(True)
  526. flexmock(module).should_receive('ensure_files_readable')
  527. flexmock(module).should_receive('make_pattern_flags').and_return(())
  528. exclude_flags = ('--exclude-from', 'excludes')
  529. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  530. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  531. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  532. )
  533. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  534. module.make_base_create_command(
  535. dry_run=False,
  536. repository_path='repo',
  537. config={
  538. 'source_directories': ['foo', 'bar'],
  539. 'repositories': ['repo'],
  540. 'exclude_patterns': ['exclude'],
  541. },
  542. config_paths=['/tmp/test.yaml'],
  543. local_borg_version='1.2.3',
  544. global_arguments=flexmock(log_json=False),
  545. borgmatic_runtime_directories=(),
  546. )
  547. )
  548. assert create_flags == ('borg', 'create') + exclude_flags
  549. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  550. assert not pattern_file
  551. assert exclude_file == mock_exclude_file
  552. @pytest.mark.parametrize(
  553. 'option_name,option_value,feature_available,option_flags',
  554. (
  555. ('checkpoint_interval', 600, True, ('--checkpoint-interval', '600')),
  556. ('checkpoint_volume', 1024, True, ('--checkpoint-volume', '1024')),
  557. ('chunker_params', '1,2,3,4', True, ('--chunker-params', '1,2,3,4')),
  558. ('compression', 'rle', True, ('--compression', 'rle')),
  559. ('one_file_system', True, True, ('--one-file-system',)),
  560. ('upload_rate_limit', 100, True, ('--upload-ratelimit', '100')),
  561. ('upload_rate_limit', 100, False, ('--remote-ratelimit', '100')),
  562. ('upload_buffer_size', 160, True, ('--upload-buffer', '160')),
  563. ('numeric_ids', True, True, ('--numeric-ids',)),
  564. ('numeric_ids', True, False, ('--numeric-owner',)),
  565. ('read_special', True, True, ('--read-special',)),
  566. ('ctime', True, True, ()),
  567. ('ctime', False, True, ('--noctime',)),
  568. ('birthtime', True, True, ()),
  569. ('birthtime', False, True, ('--nobirthtime',)),
  570. ('atime', True, True, ('--atime',)),
  571. ('atime', True, False, ()),
  572. ('atime', False, True, ()),
  573. ('atime', False, False, ('--noatime',)),
  574. ('flags', True, True, ()),
  575. ('flags', True, False, ()),
  576. ('flags', False, True, ('--noflags',)),
  577. ('flags', False, False, ('--nobsdflags',)),
  578. ('files_cache', 'ctime,size', True, ('--files-cache', 'ctime,size')),
  579. ('umask', 740, True, ('--umask', '740')),
  580. ('lock_wait', 5, True, ('--lock-wait', '5')),
  581. ),
  582. )
  583. def test_make_base_create_command_includes_configuration_option_as_command_flag(
  584. option_name, option_value, feature_available, option_flags
  585. ):
  586. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  587. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  588. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  589. flexmock(module).should_receive('expand_directories').and_return(())
  590. flexmock(module).should_receive('pattern_root_directories').and_return([])
  591. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  592. flexmock(module).should_receive('expand_home_directories').and_return(())
  593. flexmock(module).should_receive('write_pattern_file').and_return(None)
  594. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  595. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  596. '{hostname}'
  597. )
  598. flexmock(module.feature).should_receive('available').and_return(feature_available)
  599. flexmock(module).should_receive('ensure_files_readable')
  600. flexmock(module).should_receive('make_pattern_flags').and_return(())
  601. flexmock(module).should_receive('make_exclude_flags').and_return(())
  602. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  603. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  604. )
  605. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  606. module.make_base_create_command(
  607. dry_run=False,
  608. repository_path='repo',
  609. config={
  610. 'source_directories': ['foo', 'bar'],
  611. 'repositories': ['repo'],
  612. option_name: option_value,
  613. },
  614. config_paths=['/tmp/test.yaml'],
  615. local_borg_version='1.2.3',
  616. global_arguments=flexmock(log_json=False),
  617. borgmatic_runtime_directories=(),
  618. )
  619. )
  620. assert create_flags == ('borg', 'create') + option_flags
  621. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  622. assert not pattern_file
  623. assert not exclude_file
  624. def test_make_base_create_command_includes_dry_run_in_borg_command():
  625. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  626. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  627. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  628. flexmock(module).should_receive('expand_directories').and_return(())
  629. flexmock(module).should_receive('pattern_root_directories').and_return([])
  630. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  631. flexmock(module).should_receive('expand_home_directories').and_return(())
  632. flexmock(module).should_receive('write_pattern_file').and_return(None)
  633. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  634. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  635. '{hostname}'
  636. )
  637. flexmock(module.feature).should_receive('available').and_return(True)
  638. flexmock(module).should_receive('ensure_files_readable')
  639. flexmock(module).should_receive('make_pattern_flags').and_return(())
  640. flexmock(module).should_receive('make_exclude_flags').and_return(())
  641. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  642. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  643. )
  644. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  645. module.make_base_create_command(
  646. dry_run=True,
  647. repository_path='repo',
  648. config={
  649. 'source_directories': ['foo', 'bar'],
  650. 'repositories': ['repo'],
  651. 'exclude_patterns': ['exclude'],
  652. },
  653. config_paths=['/tmp/test.yaml'],
  654. local_borg_version='1.2.3',
  655. global_arguments=flexmock(log_json=False),
  656. borgmatic_runtime_directories=(),
  657. )
  658. )
  659. assert create_flags == ('borg', 'create', '--dry-run')
  660. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  661. assert not pattern_file
  662. assert not exclude_file
  663. def test_make_base_create_command_includes_local_path_in_borg_command():
  664. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  665. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  666. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  667. flexmock(module).should_receive('expand_directories').and_return(())
  668. flexmock(module).should_receive('pattern_root_directories').and_return([])
  669. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  670. flexmock(module).should_receive('expand_home_directories').and_return(())
  671. flexmock(module).should_receive('write_pattern_file').and_return(None)
  672. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  673. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  674. '{hostname}'
  675. )
  676. flexmock(module.feature).should_receive('available').and_return(True)
  677. flexmock(module).should_receive('ensure_files_readable')
  678. flexmock(module).should_receive('make_pattern_flags').and_return(())
  679. flexmock(module).should_receive('make_exclude_flags').and_return(())
  680. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  681. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  682. )
  683. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  684. module.make_base_create_command(
  685. dry_run=False,
  686. repository_path='repo',
  687. config={
  688. 'source_directories': ['foo', 'bar'],
  689. 'repositories': ['repo'],
  690. },
  691. config_paths=['/tmp/test.yaml'],
  692. local_borg_version='1.2.3',
  693. global_arguments=flexmock(log_json=False),
  694. borgmatic_runtime_directories=(),
  695. local_path='borg1',
  696. )
  697. )
  698. assert create_flags == ('borg1', 'create')
  699. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  700. assert not pattern_file
  701. assert not exclude_file
  702. def test_make_base_create_command_includes_remote_path_in_borg_command():
  703. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  704. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  705. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  706. flexmock(module).should_receive('expand_directories').and_return(())
  707. flexmock(module).should_receive('pattern_root_directories').and_return([])
  708. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  709. flexmock(module).should_receive('expand_home_directories').and_return(())
  710. flexmock(module).should_receive('write_pattern_file').and_return(None)
  711. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  712. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  713. '{hostname}'
  714. )
  715. flexmock(module.feature).should_receive('available').and_return(True)
  716. flexmock(module).should_receive('ensure_files_readable')
  717. flexmock(module).should_receive('make_pattern_flags').and_return(())
  718. flexmock(module).should_receive('make_exclude_flags').and_return(())
  719. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  720. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  721. )
  722. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  723. module.make_base_create_command(
  724. dry_run=False,
  725. repository_path='repo',
  726. config={
  727. 'source_directories': ['foo', 'bar'],
  728. 'repositories': ['repo'],
  729. },
  730. config_paths=['/tmp/test.yaml'],
  731. local_borg_version='1.2.3',
  732. global_arguments=flexmock(log_json=False),
  733. borgmatic_runtime_directories=(),
  734. remote_path='borg1',
  735. )
  736. )
  737. assert create_flags == ('borg', 'create', '--remote-path', 'borg1')
  738. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  739. assert not pattern_file
  740. assert not exclude_file
  741. def test_make_base_create_command_includes_log_json_in_borg_command():
  742. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  743. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  744. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  745. flexmock(module).should_receive('expand_directories').and_return(())
  746. flexmock(module).should_receive('pattern_root_directories').and_return([])
  747. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  748. flexmock(module).should_receive('expand_home_directories').and_return(())
  749. flexmock(module).should_receive('write_pattern_file').and_return(None)
  750. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  751. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  752. '{hostname}'
  753. )
  754. flexmock(module.feature).should_receive('available').and_return(True)
  755. flexmock(module).should_receive('ensure_files_readable')
  756. flexmock(module).should_receive('make_pattern_flags').and_return(())
  757. flexmock(module).should_receive('make_exclude_flags').and_return(())
  758. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  759. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  760. )
  761. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  762. module.make_base_create_command(
  763. dry_run=False,
  764. repository_path='repo',
  765. config={
  766. 'source_directories': ['foo', 'bar'],
  767. 'repositories': ['repo'],
  768. },
  769. config_paths=['/tmp/test.yaml'],
  770. local_borg_version='1.2.3',
  771. global_arguments=flexmock(log_json=True),
  772. borgmatic_runtime_directories=(),
  773. )
  774. )
  775. assert create_flags == ('borg', 'create', '--log-json')
  776. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  777. assert not pattern_file
  778. assert not exclude_file
  779. def test_make_base_create_command_includes_list_flags_in_borg_command():
  780. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  781. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  782. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  783. flexmock(module).should_receive('expand_directories').and_return(())
  784. flexmock(module).should_receive('pattern_root_directories').and_return([])
  785. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  786. flexmock(module).should_receive('expand_home_directories').and_return(())
  787. flexmock(module).should_receive('write_pattern_file').and_return(None)
  788. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  789. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  790. '{hostname}'
  791. )
  792. flexmock(module.feature).should_receive('available').and_return(True)
  793. flexmock(module).should_receive('ensure_files_readable')
  794. flexmock(module).should_receive('make_pattern_flags').and_return(())
  795. flexmock(module).should_receive('make_exclude_flags').and_return(())
  796. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  797. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  798. )
  799. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  800. module.make_base_create_command(
  801. dry_run=False,
  802. repository_path='repo',
  803. config={
  804. 'source_directories': ['foo', 'bar'],
  805. 'repositories': ['repo'],
  806. },
  807. config_paths=['/tmp/test.yaml'],
  808. local_borg_version='1.2.3',
  809. global_arguments=flexmock(log_json=False),
  810. borgmatic_runtime_directories=(),
  811. list_files=True,
  812. )
  813. )
  814. assert create_flags == ('borg', 'create', '--list', '--filter', 'FOO')
  815. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  816. assert not pattern_file
  817. assert not exclude_file
  818. def test_make_base_create_command_with_stream_processes_ignores_read_special_false_and_excludes_special_files():
  819. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  820. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  821. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  822. flexmock(module).should_receive('expand_directories').and_return(())
  823. flexmock(module).should_receive('pattern_root_directories').and_return([])
  824. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  825. flexmock(module).should_receive('expand_home_directories').and_return(())
  826. flexmock(module).should_receive('write_pattern_file').and_return(None)
  827. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  828. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  829. '{hostname}'
  830. )
  831. flexmock(module.feature).should_receive('available').and_return(True)
  832. flexmock(module).should_receive('ensure_files_readable')
  833. flexmock(module).should_receive('make_pattern_flags').and_return(())
  834. flexmock(module).should_receive('make_exclude_flags').and_return(())
  835. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  836. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  837. )
  838. flexmock(module.logger).should_receive('warning').twice()
  839. flexmock(module.environment).should_receive('make_environment')
  840. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',)).once()
  841. flexmock(module).should_receive('expand_home_directories').and_return(())
  842. flexmock(module).should_receive('write_pattern_file').and_return(flexmock(name='patterns'))
  843. flexmock(module).should_receive('make_exclude_flags').and_return(())
  844. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  845. module.make_base_create_command(
  846. dry_run=False,
  847. repository_path='repo',
  848. config={
  849. 'source_directories': ['foo', 'bar'],
  850. 'repositories': ['repo'],
  851. 'read_special': False,
  852. },
  853. config_paths=['/tmp/test.yaml'],
  854. local_borg_version='1.2.3',
  855. global_arguments=flexmock(log_json=False),
  856. borgmatic_runtime_directories=(),
  857. stream_processes=flexmock(),
  858. )
  859. )
  860. assert create_flags == ('borg', 'create', '--read-special')
  861. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  862. assert not pattern_file
  863. assert exclude_file
  864. def test_make_base_create_command_with_stream_processes_and_read_special_true_skip_special_files_excludes():
  865. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  866. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  867. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  868. flexmock(module).should_receive('expand_directories').and_return(())
  869. flexmock(module).should_receive('pattern_root_directories').and_return([])
  870. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  871. flexmock(module).should_receive('expand_home_directories').and_return(())
  872. flexmock(module).should_receive('write_pattern_file').and_return(None)
  873. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  874. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  875. '{hostname}'
  876. )
  877. flexmock(module.feature).should_receive('available').and_return(True)
  878. flexmock(module).should_receive('ensure_files_readable')
  879. flexmock(module).should_receive('make_pattern_flags').and_return(())
  880. flexmock(module).should_receive('make_exclude_flags').and_return(())
  881. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  882. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  883. )
  884. flexmock(module.logger).should_receive('warning').never()
  885. flexmock(module).should_receive('collect_special_file_paths').never()
  886. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  887. module.make_base_create_command(
  888. dry_run=False,
  889. repository_path='repo',
  890. config={
  891. 'source_directories': ['foo', 'bar'],
  892. 'repositories': ['repo'],
  893. 'read_special': True,
  894. },
  895. config_paths=['/tmp/test.yaml'],
  896. local_borg_version='1.2.3',
  897. global_arguments=flexmock(log_json=False),
  898. borgmatic_runtime_directories=(),
  899. stream_processes=flexmock(),
  900. )
  901. )
  902. assert create_flags == ('borg', 'create', '--read-special')
  903. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  904. assert not pattern_file
  905. assert not exclude_file
  906. def test_make_base_create_command_with_non_matching_source_directories_glob_passes_through():
  907. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  908. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  909. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  910. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  911. flexmock(module).should_receive('expand_directories').and_return(())
  912. flexmock(module).should_receive('pattern_root_directories').and_return([])
  913. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  914. flexmock(module).should_receive('expand_home_directories').and_return(())
  915. flexmock(module).should_receive('write_pattern_file').and_return(None)
  916. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  917. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  918. '{hostname}'
  919. )
  920. flexmock(module.feature).should_receive('available').and_return(True)
  921. flexmock(module).should_receive('ensure_files_readable')
  922. flexmock(module).should_receive('make_pattern_flags').and_return(())
  923. flexmock(module).should_receive('make_exclude_flags').and_return(())
  924. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  925. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  926. )
  927. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  928. module.make_base_create_command(
  929. dry_run=False,
  930. repository_path='repo',
  931. config={
  932. 'source_directories': ['foo*'],
  933. 'repositories': ['repo'],
  934. },
  935. config_paths=['/tmp/test.yaml'],
  936. local_borg_version='1.2.3',
  937. global_arguments=flexmock(log_json=False),
  938. borgmatic_runtime_directories=(),
  939. )
  940. )
  941. assert create_flags == ('borg', 'create')
  942. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo*')
  943. assert not pattern_file
  944. assert not exclude_file
  945. def test_make_base_create_command_expands_glob_in_source_directories():
  946. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  947. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  948. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  949. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  950. flexmock(module).should_receive('expand_directories').and_return(())
  951. flexmock(module).should_receive('pattern_root_directories').and_return([])
  952. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  953. flexmock(module).should_receive('expand_home_directories').and_return(())
  954. flexmock(module).should_receive('write_pattern_file').and_return(None)
  955. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  956. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  957. '{hostname}'
  958. )
  959. flexmock(module.feature).should_receive('available').and_return(True)
  960. flexmock(module).should_receive('ensure_files_readable')
  961. flexmock(module).should_receive('make_pattern_flags').and_return(())
  962. flexmock(module).should_receive('make_exclude_flags').and_return(())
  963. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  964. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  965. )
  966. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  967. module.make_base_create_command(
  968. dry_run=False,
  969. repository_path='repo',
  970. config={
  971. 'source_directories': ['foo*'],
  972. 'repositories': ['repo'],
  973. },
  974. config_paths=['/tmp/test.yaml'],
  975. local_borg_version='1.2.3',
  976. global_arguments=flexmock(log_json=False),
  977. borgmatic_runtime_directories=(),
  978. )
  979. )
  980. assert create_flags == ('borg', 'create')
  981. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food')
  982. assert not pattern_file
  983. assert not exclude_file
  984. def test_make_base_create_command_includes_archive_name_format_in_borg_command():
  985. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  986. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  987. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  988. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  989. flexmock(module).should_receive('expand_directories').and_return(())
  990. flexmock(module).should_receive('pattern_root_directories').and_return([])
  991. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  992. flexmock(module).should_receive('expand_home_directories').and_return(())
  993. flexmock(module).should_receive('write_pattern_file').and_return(None)
  994. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  995. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  996. '{hostname}'
  997. )
  998. flexmock(module.feature).should_receive('available').and_return(True)
  999. flexmock(module).should_receive('ensure_files_readable')
  1000. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1001. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1002. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1003. ('repo::ARCHIVE_NAME',)
  1004. )
  1005. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1006. module.make_base_create_command(
  1007. dry_run=False,
  1008. repository_path='repo',
  1009. config={
  1010. 'source_directories': ['foo', 'bar'],
  1011. 'repositories': ['repo'],
  1012. 'archive_name_format': 'ARCHIVE_NAME',
  1013. },
  1014. config_paths=['/tmp/test.yaml'],
  1015. local_borg_version='1.2.3',
  1016. global_arguments=flexmock(log_json=False),
  1017. borgmatic_runtime_directories=(),
  1018. )
  1019. )
  1020. assert create_flags == ('borg', 'create')
  1021. assert create_positional_arguments == ('repo::ARCHIVE_NAME', 'foo', 'bar')
  1022. assert not pattern_file
  1023. assert not exclude_file
  1024. def test_make_base_create_command_includes_default_archive_name_format_in_borg_command():
  1025. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1026. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1027. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1028. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1029. flexmock(module).should_receive('expand_directories').and_return(())
  1030. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1031. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1032. flexmock(module).should_receive('expand_home_directories').and_return(())
  1033. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1034. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1035. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  1036. '{hostname}'
  1037. )
  1038. flexmock(module.feature).should_receive('available').and_return(True)
  1039. flexmock(module).should_receive('ensure_files_readable')
  1040. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1041. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1042. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1043. ('repo::{hostname}',)
  1044. )
  1045. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1046. module.make_base_create_command(
  1047. dry_run=False,
  1048. repository_path='repo',
  1049. config={
  1050. 'source_directories': ['foo', 'bar'],
  1051. 'repositories': ['repo'],
  1052. },
  1053. config_paths=['/tmp/test.yaml'],
  1054. local_borg_version='1.2.3',
  1055. global_arguments=flexmock(log_json=False),
  1056. borgmatic_runtime_directories=(),
  1057. )
  1058. )
  1059. assert create_flags == ('borg', 'create')
  1060. assert create_positional_arguments == ('repo::{hostname}', 'foo', 'bar')
  1061. assert not pattern_file
  1062. assert not exclude_file
  1063. def test_make_base_create_command_includes_archive_name_format_with_placeholders_in_borg_command():
  1064. repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
  1065. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1066. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1067. flexmock(module).should_receive('expand_directories').and_return(())
  1068. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1069. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1070. flexmock(module).should_receive('expand_home_directories').and_return(())
  1071. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1072. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1073. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  1074. '{hostname}'
  1075. )
  1076. flexmock(module.feature).should_receive('available').and_return(True)
  1077. flexmock(module).should_receive('ensure_files_readable')
  1078. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1079. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1080. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1081. (repository_archive_pattern,)
  1082. )
  1083. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1084. module.make_base_create_command(
  1085. dry_run=False,
  1086. repository_path='repo',
  1087. config={
  1088. 'source_directories': ['foo', 'bar'],
  1089. 'repositories': ['repo'],
  1090. 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
  1091. },
  1092. config_paths=['/tmp/test.yaml'],
  1093. local_borg_version='1.2.3',
  1094. global_arguments=flexmock(log_json=False),
  1095. borgmatic_runtime_directories=(),
  1096. )
  1097. )
  1098. assert create_flags == ('borg', 'create')
  1099. assert create_positional_arguments == (repository_archive_pattern, 'foo', 'bar')
  1100. assert not pattern_file
  1101. assert not exclude_file
  1102. def test_make_base_create_command_includes_repository_and_archive_name_format_with_placeholders_in_borg_command():
  1103. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
  1104. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1105. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1106. flexmock(module).should_receive('expand_directories').and_return(())
  1107. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1108. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1109. flexmock(module).should_receive('expand_home_directories').and_return(())
  1110. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1111. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1112. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  1113. '{hostname}'
  1114. )
  1115. flexmock(module.feature).should_receive('available').and_return(True)
  1116. flexmock(module).should_receive('ensure_files_readable')
  1117. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1118. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1119. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1120. (repository_archive_pattern,)
  1121. )
  1122. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1123. module.make_base_create_command(
  1124. dry_run=False,
  1125. repository_path='{fqdn}', # noqa: FS003
  1126. config={
  1127. 'source_directories': ['foo', 'bar'],
  1128. 'repositories': ['{fqdn}'], # noqa: FS003
  1129. 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
  1130. },
  1131. config_paths=['/tmp/test.yaml'],
  1132. local_borg_version='1.2.3',
  1133. global_arguments=flexmock(log_json=False),
  1134. borgmatic_runtime_directories=(),
  1135. )
  1136. )
  1137. assert create_flags == ('borg', 'create')
  1138. assert create_positional_arguments == (repository_archive_pattern, 'foo', 'bar')
  1139. assert not pattern_file
  1140. assert not exclude_file
  1141. def test_make_base_create_command_includes_extra_borg_options_in_borg_command():
  1142. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1143. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1144. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1145. flexmock(module).should_receive('expand_directories').and_return(())
  1146. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1147. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1148. flexmock(module).should_receive('expand_home_directories').and_return(())
  1149. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1150. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1151. flexmock(module.flags).should_receive('get_default_archive_name_format').and_return(
  1152. '{hostname}'
  1153. )
  1154. flexmock(module.feature).should_receive('available').and_return(True)
  1155. flexmock(module).should_receive('ensure_files_readable')
  1156. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1157. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1158. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1159. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1160. )
  1161. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1162. module.make_base_create_command(
  1163. dry_run=False,
  1164. repository_path='repo',
  1165. config={
  1166. 'source_directories': ['foo', 'bar'],
  1167. 'repositories': ['repo'],
  1168. 'extra_borg_options': {'create': '--extra --options'},
  1169. },
  1170. config_paths=['/tmp/test.yaml'],
  1171. local_borg_version='1.2.3',
  1172. global_arguments=flexmock(log_json=False),
  1173. borgmatic_runtime_directories=(),
  1174. )
  1175. )
  1176. assert create_flags == ('borg', 'create', '--extra', '--options')
  1177. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  1178. assert not pattern_file
  1179. assert not exclude_file
  1180. def test_make_base_create_command_with_non_existent_directory_and_source_directories_must_exist_raises():
  1181. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1182. flexmock(module).should_receive('check_all_source_directories_exist').and_raise(ValueError)
  1183. with pytest.raises(ValueError):
  1184. module.make_base_create_command(
  1185. dry_run=False,
  1186. repository_path='repo',
  1187. config={
  1188. 'source_directories': ['foo', 'bar'],
  1189. 'repositories': ['repo'],
  1190. 'source_directories_must_exist': True,
  1191. },
  1192. config_paths=['/tmp/test.yaml'],
  1193. local_borg_version='1.2.3',
  1194. global_arguments=flexmock(log_json=False),
  1195. borgmatic_runtime_directories=(),
  1196. )
  1197. def test_create_archive_calls_borg_with_parameters():
  1198. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1199. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1200. flexmock(module).should_receive('expand_directories').and_return(())
  1201. flexmock(module.borgmatic.config.paths).should_receive(
  1202. 'get_borgmatic_runtime_directory'
  1203. ).and_return('/var/run/0/borgmatic')
  1204. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1205. flexmock(module).should_receive('make_base_create_command').and_return(
  1206. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1207. )
  1208. flexmock(module.environment).should_receive('make_environment')
  1209. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1210. flexmock(module).should_receive('execute_command').with_args(
  1211. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1212. output_log_level=logging.INFO,
  1213. output_file=None,
  1214. borg_local_path='borg',
  1215. borg_exit_codes=None,
  1216. working_directory=None,
  1217. extra_environment=None,
  1218. )
  1219. module.create_archive(
  1220. dry_run=False,
  1221. repository_path='repo',
  1222. config={
  1223. 'source_directories': ['foo', 'bar'],
  1224. 'repositories': ['repo'],
  1225. 'exclude_patterns': None,
  1226. },
  1227. config_paths=['/tmp/test.yaml'],
  1228. local_borg_version='1.2.3',
  1229. global_arguments=flexmock(log_json=False),
  1230. )
  1231. def test_create_archive_calls_borg_with_environment():
  1232. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1233. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1234. flexmock(module).should_receive('expand_directories').and_return(())
  1235. flexmock(module.borgmatic.config.paths).should_receive(
  1236. 'get_borgmatic_runtime_directory'
  1237. ).and_return('/var/run/0/borgmatic')
  1238. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1239. flexmock(module).should_receive('make_base_create_command').and_return(
  1240. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1241. )
  1242. environment = {'BORG_THINGY': 'YUP'}
  1243. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  1244. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1245. flexmock(module).should_receive('execute_command').with_args(
  1246. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1247. output_log_level=logging.INFO,
  1248. output_file=None,
  1249. borg_local_path='borg',
  1250. borg_exit_codes=None,
  1251. working_directory=None,
  1252. extra_environment=environment,
  1253. )
  1254. module.create_archive(
  1255. dry_run=False,
  1256. repository_path='repo',
  1257. config={
  1258. 'source_directories': ['foo', 'bar'],
  1259. 'repositories': ['repo'],
  1260. 'exclude_patterns': None,
  1261. },
  1262. config_paths=['/tmp/test.yaml'],
  1263. local_borg_version='1.2.3',
  1264. global_arguments=flexmock(log_json=False),
  1265. )
  1266. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  1267. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1268. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1269. flexmock(module).should_receive('expand_directories').and_return(())
  1270. flexmock(module.borgmatic.config.paths).should_receive(
  1271. 'get_borgmatic_runtime_directory'
  1272. ).and_return('/var/run/0/borgmatic')
  1273. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1274. flexmock(module).should_receive('make_base_create_command').and_return(
  1275. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1276. )
  1277. flexmock(module.environment).should_receive('make_environment')
  1278. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1279. flexmock(module).should_receive('execute_command').with_args(
  1280. ('borg', 'create', '--info') + REPO_ARCHIVE_WITH_PATHS,
  1281. output_log_level=logging.INFO,
  1282. output_file=None,
  1283. borg_local_path='borg',
  1284. borg_exit_codes=None,
  1285. working_directory=None,
  1286. extra_environment=None,
  1287. )
  1288. insert_logging_mock(logging.INFO)
  1289. module.create_archive(
  1290. dry_run=False,
  1291. repository_path='repo',
  1292. config={
  1293. 'source_directories': ['foo', 'bar'],
  1294. 'repositories': ['repo'],
  1295. 'exclude_patterns': None,
  1296. },
  1297. config_paths=['/tmp/test.yaml'],
  1298. local_borg_version='1.2.3',
  1299. global_arguments=flexmock(log_json=False),
  1300. )
  1301. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  1302. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1303. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1304. flexmock(module).should_receive('expand_directories').and_return(())
  1305. flexmock(module.borgmatic.config.paths).should_receive(
  1306. 'get_borgmatic_runtime_directory'
  1307. ).and_return('/var/run/0/borgmatic')
  1308. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1309. flexmock(module).should_receive('make_base_create_command').and_return(
  1310. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1311. )
  1312. flexmock(module.environment).should_receive('make_environment')
  1313. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1314. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1315. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1316. working_directory=None,
  1317. extra_environment=None,
  1318. borg_local_path='borg',
  1319. borg_exit_codes=None,
  1320. )
  1321. insert_logging_mock(logging.INFO)
  1322. module.create_archive(
  1323. dry_run=False,
  1324. repository_path='repo',
  1325. config={
  1326. 'source_directories': ['foo', 'bar'],
  1327. 'repositories': ['repo'],
  1328. 'exclude_patterns': None,
  1329. },
  1330. config_paths=['/tmp/test.yaml'],
  1331. local_borg_version='1.2.3',
  1332. global_arguments=flexmock(log_json=False),
  1333. json=True,
  1334. )
  1335. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  1336. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1337. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1338. flexmock(module).should_receive('expand_directories').and_return(())
  1339. flexmock(module.borgmatic.config.paths).should_receive(
  1340. 'get_borgmatic_runtime_directory'
  1341. ).and_return('/var/run/0/borgmatic')
  1342. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1343. flexmock(module).should_receive('make_base_create_command').and_return(
  1344. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1345. )
  1346. flexmock(module.environment).should_receive('make_environment')
  1347. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1348. flexmock(module).should_receive('execute_command').with_args(
  1349. ('borg', 'create', '--debug', '--show-rc') + REPO_ARCHIVE_WITH_PATHS,
  1350. output_log_level=logging.INFO,
  1351. output_file=None,
  1352. borg_local_path='borg',
  1353. borg_exit_codes=None,
  1354. working_directory=None,
  1355. extra_environment=None,
  1356. )
  1357. insert_logging_mock(logging.DEBUG)
  1358. module.create_archive(
  1359. dry_run=False,
  1360. repository_path='repo',
  1361. config={
  1362. 'source_directories': ['foo', 'bar'],
  1363. 'repositories': ['repo'],
  1364. 'exclude_patterns': None,
  1365. },
  1366. config_paths=['/tmp/test.yaml'],
  1367. local_borg_version='1.2.3',
  1368. global_arguments=flexmock(log_json=False),
  1369. )
  1370. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  1371. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1372. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1373. flexmock(module).should_receive('expand_directories').and_return(())
  1374. flexmock(module.borgmatic.config.paths).should_receive(
  1375. 'get_borgmatic_runtime_directory'
  1376. ).and_return('/var/run/0/borgmatic')
  1377. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1378. flexmock(module).should_receive('make_base_create_command').and_return(
  1379. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1380. )
  1381. flexmock(module.environment).should_receive('make_environment')
  1382. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1383. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1384. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1385. working_directory=None,
  1386. extra_environment=None,
  1387. borg_local_path='borg',
  1388. borg_exit_codes=None,
  1389. )
  1390. insert_logging_mock(logging.DEBUG)
  1391. module.create_archive(
  1392. dry_run=False,
  1393. repository_path='repo',
  1394. config={
  1395. 'source_directories': ['foo', 'bar'],
  1396. 'repositories': ['repo'],
  1397. 'exclude_patterns': None,
  1398. },
  1399. config_paths=['/tmp/test.yaml'],
  1400. local_borg_version='1.2.3',
  1401. global_arguments=flexmock(log_json=False),
  1402. json=True,
  1403. )
  1404. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats():
  1405. # --dry-run and --stats are mutually exclusive, see:
  1406. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  1407. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1408. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1409. flexmock(module).should_receive('expand_directories').and_return(())
  1410. flexmock(module.borgmatic.config.paths).should_receive(
  1411. 'get_borgmatic_runtime_directory'
  1412. ).and_return('/var/run/0/borgmatic')
  1413. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1414. flexmock(module).should_receive('make_base_create_command').and_return(
  1415. (('borg', 'create', '--dry-run'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1416. )
  1417. flexmock(module.environment).should_receive('make_environment')
  1418. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1419. flexmock(module).should_receive('execute_command').with_args(
  1420. ('borg', 'create', '--dry-run', '--info') + REPO_ARCHIVE_WITH_PATHS,
  1421. output_log_level=logging.INFO,
  1422. output_file=None,
  1423. borg_local_path='borg',
  1424. borg_exit_codes=None,
  1425. working_directory=None,
  1426. extra_environment=None,
  1427. )
  1428. insert_logging_mock(logging.INFO)
  1429. module.create_archive(
  1430. dry_run=True,
  1431. repository_path='repo',
  1432. config={
  1433. 'source_directories': ['foo', 'bar'],
  1434. 'repositories': ['repo'],
  1435. 'exclude_patterns': None,
  1436. },
  1437. config_paths=['/tmp/test.yaml'],
  1438. local_borg_version='1.2.3',
  1439. global_arguments=flexmock(log_json=False),
  1440. stats=True,
  1441. )
  1442. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  1443. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1444. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1445. flexmock(module).should_receive('expand_directories').and_return(())
  1446. flexmock(module.borgmatic.config.paths).should_receive(
  1447. 'get_borgmatic_runtime_directory'
  1448. ).and_return('/var/run/0/borgmatic')
  1449. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1450. flexmock(module).should_receive('make_base_create_command').and_return(
  1451. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1452. )
  1453. flexmock(module.environment).should_receive('make_environment')
  1454. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1455. '/working/dir'
  1456. )
  1457. flexmock(module).should_receive('execute_command').with_args(
  1458. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1459. output_log_level=logging.INFO,
  1460. output_file=None,
  1461. borg_local_path='borg',
  1462. borg_exit_codes=None,
  1463. working_directory='/working/dir',
  1464. extra_environment=None,
  1465. )
  1466. module.create_archive(
  1467. dry_run=False,
  1468. repository_path='repo',
  1469. config={
  1470. 'source_directories': ['foo', 'bar'],
  1471. 'repositories': ['repo'],
  1472. 'working_directory': '/working/dir',
  1473. 'exclude_patterns': None,
  1474. },
  1475. config_paths=['/tmp/test.yaml'],
  1476. local_borg_version='1.2.3',
  1477. global_arguments=flexmock(log_json=False),
  1478. )
  1479. def test_create_archive_with_exit_codes_calls_borg_using_them():
  1480. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1481. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1482. flexmock(module).should_receive('expand_directories').and_return(())
  1483. flexmock(module.borgmatic.config.paths).should_receive(
  1484. 'get_borgmatic_runtime_directory'
  1485. ).and_return('/var/run/0/borgmatic')
  1486. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1487. flexmock(module).should_receive('make_base_create_command').and_return(
  1488. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1489. )
  1490. flexmock(module.environment).should_receive('make_environment')
  1491. borg_exit_codes = flexmock()
  1492. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1493. flexmock(module).should_receive('execute_command').with_args(
  1494. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1495. output_log_level=logging.INFO,
  1496. output_file=None,
  1497. borg_local_path='borg',
  1498. borg_exit_codes=borg_exit_codes,
  1499. working_directory=None,
  1500. extra_environment=None,
  1501. )
  1502. module.create_archive(
  1503. dry_run=False,
  1504. repository_path='repo',
  1505. config={
  1506. 'source_directories': ['foo', 'bar'],
  1507. 'repositories': ['repo'],
  1508. 'exclude_patterns': None,
  1509. 'borg_exit_codes': borg_exit_codes,
  1510. },
  1511. config_paths=['/tmp/test.yaml'],
  1512. local_borg_version='1.2.3',
  1513. global_arguments=flexmock(log_json=False),
  1514. )
  1515. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  1516. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1517. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1518. flexmock(module).should_receive('expand_directories').and_return(())
  1519. flexmock(module.borgmatic.config.paths).should_receive(
  1520. 'get_borgmatic_runtime_directory'
  1521. ).and_return('/var/run/0/borgmatic')
  1522. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1523. flexmock(module).should_receive('make_base_create_command').and_return(
  1524. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1525. )
  1526. flexmock(module.environment).should_receive('make_environment')
  1527. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1528. flexmock(module).should_receive('execute_command').with_args(
  1529. ('borg', 'create', '--stats') + REPO_ARCHIVE_WITH_PATHS,
  1530. output_log_level=module.borgmatic.logger.ANSWER,
  1531. output_file=None,
  1532. borg_local_path='borg',
  1533. borg_exit_codes=None,
  1534. working_directory=None,
  1535. extra_environment=None,
  1536. )
  1537. module.create_archive(
  1538. dry_run=False,
  1539. repository_path='repo',
  1540. config={
  1541. 'source_directories': ['foo', 'bar'],
  1542. 'repositories': ['repo'],
  1543. 'exclude_patterns': None,
  1544. },
  1545. config_paths=['/tmp/test.yaml'],
  1546. local_borg_version='1.2.3',
  1547. global_arguments=flexmock(log_json=False),
  1548. stats=True,
  1549. )
  1550. def test_create_archive_with_files_calls_borg_with_answer_output_log_level():
  1551. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1552. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1553. flexmock(module).should_receive('expand_directories').and_return(())
  1554. flexmock(module.borgmatic.config.paths).should_receive(
  1555. 'get_borgmatic_runtime_directory'
  1556. ).and_return('/var/run/0/borgmatic')
  1557. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1558. flexmock(module).should_receive('make_base_create_command').and_return(
  1559. (
  1560. ('borg', 'create', '--list', '--filter', 'FOO'),
  1561. REPO_ARCHIVE_WITH_PATHS,
  1562. flexmock(),
  1563. flexmock(),
  1564. )
  1565. )
  1566. flexmock(module.environment).should_receive('make_environment')
  1567. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1568. flexmock(module).should_receive('execute_command').with_args(
  1569. ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE_WITH_PATHS,
  1570. output_log_level=module.borgmatic.logger.ANSWER,
  1571. output_file=None,
  1572. borg_local_path='borg',
  1573. borg_exit_codes=None,
  1574. working_directory=None,
  1575. extra_environment=None,
  1576. )
  1577. module.create_archive(
  1578. dry_run=False,
  1579. repository_path='repo',
  1580. config={
  1581. 'source_directories': ['foo', 'bar'],
  1582. 'repositories': ['repo'],
  1583. 'exclude_patterns': None,
  1584. },
  1585. config_paths=['/tmp/test.yaml'],
  1586. local_borg_version='1.2.3',
  1587. global_arguments=flexmock(log_json=False),
  1588. list_files=True,
  1589. )
  1590. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1591. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1592. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1593. flexmock(module).should_receive('expand_directories').and_return(())
  1594. flexmock(module.borgmatic.config.paths).should_receive(
  1595. 'get_borgmatic_runtime_directory'
  1596. ).and_return('/var/run/0/borgmatic')
  1597. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1598. flexmock(module).should_receive('make_base_create_command').and_return(
  1599. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1600. )
  1601. flexmock(module.environment).should_receive('make_environment')
  1602. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1603. flexmock(module).should_receive('execute_command').with_args(
  1604. ('borg', 'create', '--info', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1605. output_log_level=logging.INFO,
  1606. output_file=module.DO_NOT_CAPTURE,
  1607. borg_local_path='borg',
  1608. borg_exit_codes=None,
  1609. working_directory=None,
  1610. extra_environment=None,
  1611. )
  1612. insert_logging_mock(logging.INFO)
  1613. module.create_archive(
  1614. dry_run=False,
  1615. repository_path='repo',
  1616. config={
  1617. 'source_directories': ['foo', 'bar'],
  1618. 'repositories': ['repo'],
  1619. 'exclude_patterns': None,
  1620. },
  1621. config_paths=['/tmp/test.yaml'],
  1622. local_borg_version='1.2.3',
  1623. global_arguments=flexmock(log_json=False),
  1624. progress=True,
  1625. )
  1626. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1627. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1628. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1629. flexmock(module).should_receive('expand_directories').and_return(())
  1630. flexmock(module.borgmatic.config.paths).should_receive(
  1631. 'get_borgmatic_runtime_directory'
  1632. ).and_return('/var/run/0/borgmatic')
  1633. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1634. flexmock(module).should_receive('make_base_create_command').and_return(
  1635. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1636. )
  1637. flexmock(module.environment).should_receive('make_environment')
  1638. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1639. flexmock(module).should_receive('execute_command').with_args(
  1640. ('borg', 'create', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1641. output_log_level=logging.INFO,
  1642. output_file=module.DO_NOT_CAPTURE,
  1643. borg_local_path='borg',
  1644. borg_exit_codes=None,
  1645. working_directory=None,
  1646. extra_environment=None,
  1647. )
  1648. module.create_archive(
  1649. dry_run=False,
  1650. repository_path='repo',
  1651. config={
  1652. 'source_directories': ['foo', 'bar'],
  1653. 'repositories': ['repo'],
  1654. 'exclude_patterns': None,
  1655. },
  1656. config_paths=['/tmp/test.yaml'],
  1657. local_borg_version='1.2.3',
  1658. global_arguments=flexmock(log_json=False),
  1659. progress=True,
  1660. )
  1661. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1662. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1663. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1664. processes = flexmock()
  1665. flexmock(module).should_receive('expand_directories').and_return(())
  1666. flexmock(module.borgmatic.config.paths).should_receive(
  1667. 'get_borgmatic_runtime_directory'
  1668. ).and_return('/var/run/0/borgmatic')
  1669. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1670. flexmock(module).should_receive('make_base_create_command').and_return(
  1671. (
  1672. ('borg', 'create', '--read-special'),
  1673. REPO_ARCHIVE_WITH_PATHS,
  1674. flexmock(),
  1675. flexmock(),
  1676. )
  1677. )
  1678. flexmock(module.environment).should_receive('make_environment')
  1679. create_command = (
  1680. 'borg',
  1681. 'create',
  1682. '--read-special',
  1683. '--progress',
  1684. ) + REPO_ARCHIVE_WITH_PATHS
  1685. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1686. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1687. create_command + ('--dry-run', '--list'),
  1688. processes=processes,
  1689. output_log_level=logging.INFO,
  1690. output_file=module.DO_NOT_CAPTURE,
  1691. borg_local_path='borg',
  1692. borg_exit_codes=None,
  1693. working_directory=None,
  1694. extra_environment=None,
  1695. )
  1696. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1697. create_command,
  1698. processes=processes,
  1699. output_log_level=logging.INFO,
  1700. output_file=module.DO_NOT_CAPTURE,
  1701. borg_local_path='borg',
  1702. borg_exit_codes=None,
  1703. working_directory=None,
  1704. extra_environment=None,
  1705. )
  1706. module.create_archive(
  1707. dry_run=False,
  1708. repository_path='repo',
  1709. config={
  1710. 'source_directories': ['foo', 'bar'],
  1711. 'repositories': ['repo'],
  1712. 'exclude_patterns': None,
  1713. },
  1714. config_paths=['/tmp/test.yaml'],
  1715. local_borg_version='1.2.3',
  1716. global_arguments=flexmock(log_json=False),
  1717. progress=True,
  1718. stream_processes=processes,
  1719. )
  1720. def test_create_archive_with_json_calls_borg_with_json_flag():
  1721. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1722. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1723. flexmock(module).should_receive('expand_directories').and_return(())
  1724. flexmock(module.borgmatic.config.paths).should_receive(
  1725. 'get_borgmatic_runtime_directory'
  1726. ).and_return('/var/run/0/borgmatic')
  1727. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1728. flexmock(module).should_receive('make_base_create_command').and_return(
  1729. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1730. )
  1731. flexmock(module.environment).should_receive('make_environment')
  1732. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1733. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1734. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1735. working_directory=None,
  1736. extra_environment=None,
  1737. borg_local_path='borg',
  1738. borg_exit_codes=None,
  1739. ).and_return('[]')
  1740. json_output = module.create_archive(
  1741. dry_run=False,
  1742. repository_path='repo',
  1743. config={
  1744. 'source_directories': ['foo', 'bar'],
  1745. 'repositories': ['repo'],
  1746. 'exclude_patterns': None,
  1747. },
  1748. config_paths=['/tmp/test.yaml'],
  1749. local_borg_version='1.2.3',
  1750. global_arguments=flexmock(log_json=False),
  1751. json=True,
  1752. )
  1753. assert json_output == '[]'
  1754. def test_create_archive_with_stats_and_json_calls_borg_without_stats_flag():
  1755. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1756. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1757. flexmock(module).should_receive('expand_directories').and_return(())
  1758. flexmock(module.borgmatic.config.paths).should_receive(
  1759. 'get_borgmatic_runtime_directory'
  1760. ).and_return('/var/run/0/borgmatic')
  1761. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1762. flexmock(module).should_receive('make_base_create_command').and_return(
  1763. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1764. )
  1765. flexmock(module.environment).should_receive('make_environment')
  1766. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1767. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1768. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1769. working_directory=None,
  1770. extra_environment=None,
  1771. borg_local_path='borg',
  1772. borg_exit_codes=None,
  1773. ).and_return('[]')
  1774. json_output = module.create_archive(
  1775. dry_run=False,
  1776. repository_path='repo',
  1777. config={
  1778. 'source_directories': ['foo*'],
  1779. 'repositories': ['repo'],
  1780. 'exclude_patterns': None,
  1781. },
  1782. config_paths=['/tmp/test.yaml'],
  1783. local_borg_version='1.2.3',
  1784. global_arguments=flexmock(log_json=False),
  1785. json=True,
  1786. stats=True,
  1787. )
  1788. assert json_output == '[]'
  1789. def test_create_archive_calls_borg_with_working_directory():
  1790. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1791. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1792. flexmock(module).should_receive('expand_directories').and_return(())
  1793. flexmock(module.borgmatic.config.paths).should_receive(
  1794. 'get_borgmatic_runtime_directory'
  1795. ).and_return('/var/run/0/borgmatic')
  1796. flexmock(module).should_receive('collect_borgmatic_runtime_directories').and_return([])
  1797. flexmock(module).should_receive('make_base_create_command').and_return(
  1798. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1799. )
  1800. flexmock(module.environment).should_receive('make_environment')
  1801. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1802. '/working/dir'
  1803. )
  1804. flexmock(module).should_receive('execute_command').with_args(
  1805. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1806. output_log_level=logging.INFO,
  1807. output_file=None,
  1808. borg_local_path='borg',
  1809. borg_exit_codes=None,
  1810. working_directory='/working/dir',
  1811. extra_environment=None,
  1812. )
  1813. module.create_archive(
  1814. dry_run=False,
  1815. repository_path='repo',
  1816. config={
  1817. 'source_directories': ['foo', 'bar'],
  1818. 'repositories': ['repo'],
  1819. 'exclude_patterns': None,
  1820. 'working_directory': '/working/dir',
  1821. },
  1822. config_paths=['/tmp/test.yaml'],
  1823. local_borg_version='1.2.3',
  1824. global_arguments=flexmock(log_json=False),
  1825. )
  1826. def test_check_all_source_directories_exist_with_glob_and_tilde_directories():
  1827. flexmock(module).should_receive('expand_directory').with_args('foo*', None).and_return(
  1828. ('foo', 'food')
  1829. )
  1830. flexmock(module).should_receive('expand_directory').with_args('~/bar', None).and_return(
  1831. ('/root/bar',)
  1832. )
  1833. flexmock(module.os.path).should_receive('exists').and_return(False)
  1834. flexmock(module.os.path).should_receive('exists').with_args('foo').and_return(True)
  1835. flexmock(module.os.path).should_receive('exists').with_args('food').and_return(True)
  1836. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  1837. module.check_all_source_directories_exist(['foo*', '~/bar'])
  1838. def test_check_all_source_directories_exist_with_non_existent_directory_raises():
  1839. flexmock(module).should_receive('expand_directory').with_args('foo', None).and_return(('foo',))
  1840. flexmock(module.os.path).should_receive('exists').and_return(False)
  1841. with pytest.raises(ValueError):
  1842. module.check_all_source_directories_exist(['foo'])
  1843. def test_check_all_source_directories_exist_with_working_directory_applies_to_relative_source_directories():
  1844. flexmock(module).should_receive('expand_directory').with_args(
  1845. 'foo*', working_directory='/tmp'
  1846. ).and_return(('/tmp/foo', '/tmp/food'))
  1847. flexmock(module).should_receive('expand_directory').with_args(
  1848. '/root/bar', working_directory='/tmp'
  1849. ).and_return(('/root/bar',))
  1850. flexmock(module.os.path).should_receive('exists').and_return(False)
  1851. flexmock(module.os.path).should_receive('exists').with_args('/tmp/foo').and_return(True)
  1852. flexmock(module.os.path).should_receive('exists').with_args('/tmp/food').and_return(True)
  1853. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  1854. module.check_all_source_directories_exist(['foo*', '/root/bar'], working_directory='/tmp')