test_create.py 80 KB

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