test_create.py 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  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_source_directories_set_when_directory_exists():
  240. flexmock(module.os.path).should_receive('exists').and_return(True)
  241. flexmock(module.os.path).should_receive('expanduser')
  242. assert module.collect_borgmatic_source_directories('/tmp') == ['/tmp']
  243. def test_collect_borgmatic_source_directories_empty_when_directory_does_not_exist():
  244. flexmock(module.os.path).should_receive('exists').and_return(False)
  245. flexmock(module.os.path).should_receive('expanduser')
  246. assert module.collect_borgmatic_source_directories('/tmp') == []
  247. def test_collect_borgmatic_source_directories_defaults_when_directory_not_given():
  248. flexmock(module.os.path).should_receive('exists').and_return(True)
  249. flexmock(module.os.path).should_receive('expanduser')
  250. assert module.collect_borgmatic_source_directories(None) == [
  251. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  252. ]
  253. def test_pattern_root_directories_deals_with_none_patterns():
  254. assert module.pattern_root_directories(patterns=None) == []
  255. def test_pattern_root_directories_parses_roots_and_ignores_others():
  256. assert module.pattern_root_directories(
  257. ['R /root', '+ /root/foo', '- /root/foo/bar', 'R /baz']
  258. ) == ['/root', '/baz']
  259. @pytest.mark.parametrize(
  260. 'character_device,block_device,fifo,expected_result',
  261. (
  262. (False, False, False, False),
  263. (True, False, False, True),
  264. (False, True, False, True),
  265. (True, True, False, True),
  266. (False, False, True, True),
  267. (False, True, True, True),
  268. (True, False, True, True),
  269. ),
  270. )
  271. def test_special_file_looks_at_file_type(character_device, block_device, fifo, expected_result):
  272. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mode=flexmock()))
  273. flexmock(module.stat).should_receive('S_ISCHR').and_return(character_device)
  274. flexmock(module.stat).should_receive('S_ISBLK').and_return(block_device)
  275. flexmock(module.stat).should_receive('S_ISFIFO').and_return(fifo)
  276. assert module.special_file('/dev/special') == expected_result
  277. def test_special_file_treats_broken_symlink_as_non_special():
  278. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  279. assert module.special_file('/broken/symlink') is False
  280. def test_any_parent_directories_treats_parents_as_match():
  281. module.any_parent_directories('/foo/bar.txt', ('/foo', '/etc'))
  282. def test_any_parent_directories_treats_grandparents_as_match():
  283. module.any_parent_directories('/foo/bar/baz.txt', ('/foo', '/etc'))
  284. def test_any_parent_directories_treats_unrelated_paths_as_non_match():
  285. module.any_parent_directories('/foo/bar.txt', ('/usr', '/etc'))
  286. def test_collect_special_file_paths_parses_special_files_from_borg_dry_run_file_list():
  287. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  288. 'Processing files ...\n- /foo\n+ /bar\n- /baz'
  289. )
  290. flexmock(module).should_receive('special_file').and_return(True)
  291. flexmock(module).should_receive('any_parent_directories').and_return(False)
  292. assert module.collect_special_file_paths(
  293. ('borg', 'create'),
  294. config={},
  295. local_path=None,
  296. working_directory=None,
  297. borg_environment=None,
  298. skip_directories=flexmock(),
  299. ) == ('/foo', '/bar', '/baz')
  300. def test_collect_special_file_paths_excludes_requested_directories():
  301. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  302. '+ /foo\n- /bar\n- /baz'
  303. )
  304. flexmock(module).should_receive('special_file').and_return(True)
  305. flexmock(module).should_receive('any_parent_directories').and_return(False).and_return(
  306. True
  307. ).and_return(False)
  308. assert module.collect_special_file_paths(
  309. ('borg', 'create'),
  310. config={},
  311. local_path=None,
  312. working_directory=None,
  313. borg_environment=None,
  314. skip_directories=flexmock(),
  315. ) == ('/foo', '/baz')
  316. def test_collect_special_file_paths_excludes_non_special_files():
  317. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  318. '+ /foo\n+ /bar\n+ /baz'
  319. )
  320. flexmock(module).should_receive('special_file').and_return(True).and_return(False).and_return(
  321. True
  322. )
  323. flexmock(module).should_receive('any_parent_directories').and_return(False)
  324. assert module.collect_special_file_paths(
  325. ('borg', 'create'),
  326. config={},
  327. local_path=None,
  328. working_directory=None,
  329. borg_environment=None,
  330. skip_directories=flexmock(),
  331. ) == ('/foo', '/baz')
  332. def test_collect_special_file_paths_omits_exclude_no_dump_flag_from_command():
  333. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  334. ('borg', 'create', '--dry-run', '--list'),
  335. capture_stderr=True,
  336. working_directory=None,
  337. extra_environment=None,
  338. borg_local_path='borg',
  339. borg_exit_codes=None,
  340. ).and_return('Processing files ...\n- /foo\n+ /bar\n- /baz').once()
  341. flexmock(module).should_receive('special_file').and_return(True)
  342. flexmock(module).should_receive('any_parent_directories').and_return(False)
  343. module.collect_special_file_paths(
  344. ('borg', 'create', '--exclude-nodump'),
  345. config={},
  346. local_path='borg',
  347. working_directory=None,
  348. borg_environment=None,
  349. skip_directories=flexmock(),
  350. )
  351. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003
  352. REPO_ARCHIVE_WITH_PATHS = (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'bar')
  353. def test_make_base_create_produces_borg_command():
  354. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  355. None
  356. )
  357. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  358. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  359. flexmock(module).should_receive('expand_directories').and_return(())
  360. flexmock(module).should_receive('pattern_root_directories').and_return([])
  361. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  362. flexmock(module).should_receive('expand_home_directories').and_return(())
  363. flexmock(module).should_receive('ensure_files_readable')
  364. flexmock(module).should_receive('write_pattern_file').and_return(None)
  365. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  366. flexmock(module.feature).should_receive('available').and_return(True)
  367. flexmock(module).should_receive('make_pattern_flags').and_return(())
  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. },
  380. config_paths=['/tmp/test.yaml'],
  381. local_borg_version='1.2.3',
  382. global_arguments=flexmock(log_json=False),
  383. borgmatic_source_directories=(),
  384. )
  385. )
  386. assert create_flags == ('borg', 'create')
  387. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  388. assert not pattern_file
  389. assert not exclude_file
  390. def test_make_base_create_command_includes_patterns_file_in_borg_command():
  391. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  392. None
  393. )
  394. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  395. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  396. flexmock(module).should_receive('expand_directories').and_return(())
  397. flexmock(module).should_receive('pattern_root_directories').and_return([])
  398. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  399. flexmock(module).should_receive('expand_home_directories').and_return(())
  400. mock_pattern_file = flexmock(name='/tmp/patterns')
  401. flexmock(module).should_receive('write_pattern_file').and_return(mock_pattern_file).and_return(
  402. None
  403. )
  404. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  405. flexmock(module.feature).should_receive('available').and_return(True)
  406. flexmock(module).should_receive('ensure_files_readable')
  407. pattern_flags = ('--patterns-from', mock_pattern_file.name)
  408. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  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. 'patterns': ['pattern'],
  421. },
  422. config_paths=['/tmp/test.yaml'],
  423. local_borg_version='1.2.3',
  424. global_arguments=flexmock(log_json=False),
  425. borgmatic_source_directories=(),
  426. )
  427. )
  428. assert create_flags == ('borg', 'create') + pattern_flags
  429. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  430. assert pattern_file == mock_pattern_file
  431. assert not exclude_file
  432. def test_make_base_create_command_includes_sources_and_config_paths_in_borg_command():
  433. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  434. None
  435. )
  436. flexmock(module).should_receive('deduplicate_directories').and_return(
  437. ('foo', 'bar', '/tmp/test.yaml')
  438. )
  439. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  440. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  441. flexmock(module).should_receive('expand_directories').with_args(
  442. ('foo', 'bar', '/tmp/test.yaml'),
  443. None,
  444. ).and_return(('foo', 'bar', '/tmp/test.yaml'))
  445. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  446. flexmock(module).should_receive('pattern_root_directories').and_return([])
  447. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  448. flexmock(module).should_receive('expand_home_directories').and_return(())
  449. flexmock(module).should_receive('write_pattern_file').and_return(None)
  450. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  451. flexmock(module.feature).should_receive('available').and_return(True)
  452. flexmock(module).should_receive('ensure_files_readable')
  453. flexmock(module).should_receive('make_pattern_flags').and_return(())
  454. flexmock(module).should_receive('make_exclude_flags').and_return(())
  455. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  456. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  457. )
  458. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  459. module.make_base_create_command(
  460. dry_run=False,
  461. repository_path='repo',
  462. config={
  463. 'source_directories': ['foo', 'bar'],
  464. 'repositories': ['repo'],
  465. },
  466. config_paths=['/tmp/test.yaml'],
  467. local_borg_version='1.2.3',
  468. global_arguments=flexmock(log_json=False),
  469. borgmatic_source_directories=(),
  470. )
  471. )
  472. assert create_flags == ('borg', 'create')
  473. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS + ('/tmp/test.yaml',)
  474. assert not pattern_file
  475. assert not exclude_file
  476. def test_make_base_create_command_with_store_config_false_omits_config_files():
  477. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  478. None
  479. )
  480. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  481. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  482. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  483. flexmock(module).should_receive('expand_directories').with_args(
  484. ('foo', 'bar'), None
  485. ).and_return(('foo', 'bar'))
  486. flexmock(module).should_receive('expand_directories').with_args([], None).and_return(())
  487. flexmock(module).should_receive('pattern_root_directories').and_return([])
  488. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  489. flexmock(module).should_receive('expand_home_directories').and_return(())
  490. flexmock(module).should_receive('write_pattern_file').and_return(None)
  491. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  492. flexmock(module.feature).should_receive('available').and_return(True)
  493. flexmock(module).should_receive('ensure_files_readable')
  494. flexmock(module).should_receive('make_pattern_flags').and_return(())
  495. flexmock(module).should_receive('make_exclude_flags').and_return(())
  496. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  497. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  498. )
  499. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  500. module.make_base_create_command(
  501. dry_run=False,
  502. repository_path='repo',
  503. config={
  504. 'source_directories': ['foo', 'bar'],
  505. 'repositories': ['repo'],
  506. 'store_config_files': False,
  507. },
  508. config_paths=['/tmp/test.yaml'],
  509. local_borg_version='1.2.3',
  510. global_arguments=flexmock(log_json=False),
  511. borgmatic_source_directories=(),
  512. )
  513. )
  514. assert create_flags == ('borg', 'create')
  515. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  516. assert not pattern_file
  517. assert not exclude_file
  518. def test_make_base_create_command_includes_exclude_patterns_in_borg_command():
  519. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  520. None
  521. )
  522. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  523. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  524. flexmock(module).should_receive('expand_directories').and_return(())
  525. flexmock(module).should_receive('pattern_root_directories').and_return([])
  526. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  527. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  528. mock_exclude_file = flexmock(name='/tmp/excludes')
  529. flexmock(module).should_receive('write_pattern_file').and_return(mock_exclude_file)
  530. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  531. flexmock(module.feature).should_receive('available').and_return(True)
  532. flexmock(module).should_receive('ensure_files_readable')
  533. flexmock(module).should_receive('make_pattern_flags').and_return(())
  534. exclude_flags = ('--exclude-from', 'excludes')
  535. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  536. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  537. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  538. )
  539. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  540. module.make_base_create_command(
  541. dry_run=False,
  542. repository_path='repo',
  543. config={
  544. 'source_directories': ['foo', 'bar'],
  545. 'repositories': ['repo'],
  546. 'exclude_patterns': ['exclude'],
  547. },
  548. config_paths=['/tmp/test.yaml'],
  549. local_borg_version='1.2.3',
  550. global_arguments=flexmock(log_json=False),
  551. borgmatic_source_directories=(),
  552. )
  553. )
  554. assert create_flags == ('borg', 'create') + exclude_flags
  555. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  556. assert not pattern_file
  557. assert exclude_file == mock_exclude_file
  558. @pytest.mark.parametrize(
  559. 'option_name,option_value,feature_available,option_flags',
  560. (
  561. ('checkpoint_interval', 600, True, ('--checkpoint-interval', '600')),
  562. ('checkpoint_volume', 1024, True, ('--checkpoint-volume', '1024')),
  563. ('chunker_params', '1,2,3,4', True, ('--chunker-params', '1,2,3,4')),
  564. ('compression', 'rle', True, ('--compression', 'rle')),
  565. ('one_file_system', True, True, ('--one-file-system',)),
  566. ('upload_rate_limit', 100, True, ('--upload-ratelimit', '100')),
  567. ('upload_rate_limit', 100, False, ('--remote-ratelimit', '100')),
  568. ('upload_buffer_size', 160, True, ('--upload-buffer', '160')),
  569. ('numeric_ids', True, True, ('--numeric-ids',)),
  570. ('numeric_ids', True, False, ('--numeric-owner',)),
  571. ('read_special', True, True, ('--read-special',)),
  572. ('ctime', True, True, ()),
  573. ('ctime', False, True, ('--noctime',)),
  574. ('birthtime', True, True, ()),
  575. ('birthtime', False, True, ('--nobirthtime',)),
  576. ('atime', True, True, ('--atime',)),
  577. ('atime', True, False, ()),
  578. ('atime', False, True, ()),
  579. ('atime', False, False, ('--noatime',)),
  580. ('flags', True, True, ()),
  581. ('flags', True, False, ()),
  582. ('flags', False, True, ('--noflags',)),
  583. ('flags', False, False, ('--nobsdflags',)),
  584. ('files_cache', 'ctime,size', True, ('--files-cache', 'ctime,size')),
  585. ('umask', 740, True, ('--umask', '740')),
  586. ('lock_wait', 5, True, ('--lock-wait', '5')),
  587. ),
  588. )
  589. def test_make_base_create_command_includes_configuration_option_as_command_flag(
  590. option_name, option_value, feature_available, option_flags
  591. ):
  592. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  593. None
  594. )
  595. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  596. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  597. flexmock(module).should_receive('expand_directories').and_return(())
  598. flexmock(module).should_receive('pattern_root_directories').and_return([])
  599. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  600. flexmock(module).should_receive('expand_home_directories').and_return(())
  601. flexmock(module).should_receive('write_pattern_file').and_return(None)
  602. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  603. flexmock(module.feature).should_receive('available').and_return(feature_available)
  604. flexmock(module).should_receive('ensure_files_readable')
  605. flexmock(module).should_receive('make_pattern_flags').and_return(())
  606. flexmock(module).should_receive('make_exclude_flags').and_return(())
  607. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  608. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  609. )
  610. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  611. module.make_base_create_command(
  612. dry_run=False,
  613. repository_path='repo',
  614. config={
  615. 'source_directories': ['foo', 'bar'],
  616. 'repositories': ['repo'],
  617. option_name: option_value,
  618. },
  619. config_paths=['/tmp/test.yaml'],
  620. local_borg_version='1.2.3',
  621. global_arguments=flexmock(log_json=False),
  622. borgmatic_source_directories=(),
  623. )
  624. )
  625. assert create_flags == ('borg', 'create') + option_flags
  626. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  627. assert not pattern_file
  628. assert not exclude_file
  629. def test_make_base_create_command_includes_dry_run_in_borg_command():
  630. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  631. None
  632. )
  633. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  634. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  635. flexmock(module).should_receive('expand_directories').and_return(())
  636. flexmock(module).should_receive('pattern_root_directories').and_return([])
  637. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  638. flexmock(module).should_receive('expand_home_directories').and_return(())
  639. flexmock(module).should_receive('write_pattern_file').and_return(None)
  640. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  641. flexmock(module.feature).should_receive('available').and_return(True)
  642. flexmock(module).should_receive('ensure_files_readable')
  643. flexmock(module).should_receive('make_pattern_flags').and_return(())
  644. flexmock(module).should_receive('make_exclude_flags').and_return(())
  645. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  646. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  647. )
  648. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  649. module.make_base_create_command(
  650. dry_run=True,
  651. repository_path='repo',
  652. config={
  653. 'source_directories': ['foo', 'bar'],
  654. 'repositories': ['repo'],
  655. 'exclude_patterns': ['exclude'],
  656. },
  657. config_paths=['/tmp/test.yaml'],
  658. local_borg_version='1.2.3',
  659. global_arguments=flexmock(log_json=False),
  660. borgmatic_source_directories=(),
  661. )
  662. )
  663. assert create_flags == ('borg', 'create', '--dry-run')
  664. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  665. assert not pattern_file
  666. assert not exclude_file
  667. def test_make_base_create_command_includes_local_path_in_borg_command():
  668. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  669. None
  670. )
  671. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  672. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  673. flexmock(module).should_receive('expand_directories').and_return(())
  674. flexmock(module).should_receive('pattern_root_directories').and_return([])
  675. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  676. flexmock(module).should_receive('expand_home_directories').and_return(())
  677. flexmock(module).should_receive('write_pattern_file').and_return(None)
  678. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  679. flexmock(module.feature).should_receive('available').and_return(True)
  680. flexmock(module).should_receive('ensure_files_readable')
  681. flexmock(module).should_receive('make_pattern_flags').and_return(())
  682. flexmock(module).should_receive('make_exclude_flags').and_return(())
  683. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  684. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  685. )
  686. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  687. module.make_base_create_command(
  688. dry_run=False,
  689. repository_path='repo',
  690. config={
  691. 'source_directories': ['foo', 'bar'],
  692. 'repositories': ['repo'],
  693. },
  694. config_paths=['/tmp/test.yaml'],
  695. local_borg_version='1.2.3',
  696. global_arguments=flexmock(log_json=False),
  697. borgmatic_source_directories=(),
  698. local_path='borg1',
  699. )
  700. )
  701. assert create_flags == ('borg1', 'create')
  702. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  703. assert not pattern_file
  704. assert not exclude_file
  705. def test_make_base_create_command_includes_remote_path_in_borg_command():
  706. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  707. None
  708. )
  709. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  710. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  711. flexmock(module).should_receive('expand_directories').and_return(())
  712. flexmock(module).should_receive('pattern_root_directories').and_return([])
  713. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  714. flexmock(module).should_receive('expand_home_directories').and_return(())
  715. flexmock(module).should_receive('write_pattern_file').and_return(None)
  716. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  717. flexmock(module.feature).should_receive('available').and_return(True)
  718. flexmock(module).should_receive('ensure_files_readable')
  719. flexmock(module).should_receive('make_pattern_flags').and_return(())
  720. flexmock(module).should_receive('make_exclude_flags').and_return(())
  721. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  722. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  723. )
  724. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  725. module.make_base_create_command(
  726. dry_run=False,
  727. repository_path='repo',
  728. config={
  729. 'source_directories': ['foo', 'bar'],
  730. 'repositories': ['repo'],
  731. },
  732. config_paths=['/tmp/test.yaml'],
  733. local_borg_version='1.2.3',
  734. global_arguments=flexmock(log_json=False),
  735. borgmatic_source_directories=(),
  736. remote_path='borg1',
  737. )
  738. )
  739. assert create_flags == ('borg', 'create', '--remote-path', 'borg1')
  740. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  741. assert not pattern_file
  742. assert not exclude_file
  743. def test_make_base_create_command_includes_log_json_in_borg_command():
  744. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  745. None
  746. )
  747. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  748. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  749. flexmock(module).should_receive('expand_directories').and_return(())
  750. flexmock(module).should_receive('pattern_root_directories').and_return([])
  751. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  752. flexmock(module).should_receive('expand_home_directories').and_return(())
  753. flexmock(module).should_receive('write_pattern_file').and_return(None)
  754. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  755. flexmock(module.feature).should_receive('available').and_return(True)
  756. flexmock(module).should_receive('ensure_files_readable')
  757. flexmock(module).should_receive('make_pattern_flags').and_return(())
  758. flexmock(module).should_receive('make_exclude_flags').and_return(())
  759. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  760. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  761. )
  762. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  763. module.make_base_create_command(
  764. dry_run=False,
  765. repository_path='repo',
  766. config={
  767. 'source_directories': ['foo', 'bar'],
  768. 'repositories': ['repo'],
  769. },
  770. config_paths=['/tmp/test.yaml'],
  771. local_borg_version='1.2.3',
  772. global_arguments=flexmock(log_json=True),
  773. borgmatic_source_directories=(),
  774. )
  775. )
  776. assert create_flags == ('borg', 'create', '--log-json')
  777. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  778. assert not pattern_file
  779. assert not exclude_file
  780. def test_make_base_create_command_includes_list_flags_in_borg_command():
  781. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  782. None
  783. )
  784. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  785. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  786. flexmock(module).should_receive('expand_directories').and_return(())
  787. flexmock(module).should_receive('pattern_root_directories').and_return([])
  788. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  789. flexmock(module).should_receive('expand_home_directories').and_return(())
  790. flexmock(module).should_receive('write_pattern_file').and_return(None)
  791. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  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_source_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.options).should_receive('get_working_directory').and_return(
  820. None
  821. )
  822. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  823. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  824. flexmock(module).should_receive('expand_directories').and_return(())
  825. flexmock(module).should_receive('pattern_root_directories').and_return([])
  826. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  827. flexmock(module).should_receive('expand_home_directories').and_return(())
  828. flexmock(module).should_receive('write_pattern_file').and_return(None)
  829. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  830. flexmock(module.feature).should_receive('available').and_return(True)
  831. flexmock(module).should_receive('ensure_files_readable')
  832. flexmock(module).should_receive('make_pattern_flags').and_return(())
  833. flexmock(module).should_receive('make_exclude_flags').and_return(())
  834. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  835. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  836. )
  837. flexmock(module.logger).should_receive('warning').twice()
  838. flexmock(module.environment).should_receive('make_environment')
  839. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',)).once()
  840. flexmock(module).should_receive('expand_home_directories').and_return(())
  841. flexmock(module).should_receive('write_pattern_file').and_return(flexmock(name='patterns'))
  842. flexmock(module).should_receive('make_exclude_flags').and_return(())
  843. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  844. module.make_base_create_command(
  845. dry_run=False,
  846. repository_path='repo',
  847. config={
  848. 'source_directories': ['foo', 'bar'],
  849. 'repositories': ['repo'],
  850. 'read_special': False,
  851. },
  852. config_paths=['/tmp/test.yaml'],
  853. local_borg_version='1.2.3',
  854. global_arguments=flexmock(log_json=False),
  855. borgmatic_source_directories=(),
  856. stream_processes=flexmock(),
  857. )
  858. )
  859. assert create_flags == ('borg', 'create', '--read-special')
  860. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  861. assert not pattern_file
  862. assert exclude_file
  863. def test_make_base_create_command_with_stream_processes_and_read_special_true_skip_special_files_excludes():
  864. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  865. None
  866. )
  867. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  868. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  869. flexmock(module).should_receive('expand_directories').and_return(())
  870. flexmock(module).should_receive('pattern_root_directories').and_return([])
  871. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  872. flexmock(module).should_receive('expand_home_directories').and_return(())
  873. flexmock(module).should_receive('write_pattern_file').and_return(None)
  874. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  875. flexmock(module.feature).should_receive('available').and_return(True)
  876. flexmock(module).should_receive('ensure_files_readable')
  877. flexmock(module).should_receive('make_pattern_flags').and_return(())
  878. flexmock(module).should_receive('make_exclude_flags').and_return(())
  879. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  880. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  881. )
  882. flexmock(module.logger).should_receive('warning').never()
  883. flexmock(module).should_receive('collect_special_file_paths').never()
  884. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  885. module.make_base_create_command(
  886. dry_run=False,
  887. repository_path='repo',
  888. config={
  889. 'source_directories': ['foo', 'bar'],
  890. 'repositories': ['repo'],
  891. 'read_special': True,
  892. },
  893. config_paths=['/tmp/test.yaml'],
  894. local_borg_version='1.2.3',
  895. global_arguments=flexmock(log_json=False),
  896. borgmatic_source_directories=(),
  897. stream_processes=flexmock(),
  898. )
  899. )
  900. assert create_flags == ('borg', 'create', '--read-special')
  901. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  902. assert not pattern_file
  903. assert not exclude_file
  904. def test_make_base_create_command_with_non_matching_source_directories_glob_passes_through():
  905. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  906. None
  907. )
  908. flexmock(module).should_receive('collect_borgmatic_source_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.feature).should_receive('available').and_return(True)
  918. flexmock(module).should_receive('ensure_files_readable')
  919. flexmock(module).should_receive('make_pattern_flags').and_return(())
  920. flexmock(module).should_receive('make_exclude_flags').and_return(())
  921. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  922. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  923. )
  924. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  925. module.make_base_create_command(
  926. dry_run=False,
  927. repository_path='repo',
  928. config={
  929. 'source_directories': ['foo*'],
  930. 'repositories': ['repo'],
  931. },
  932. config_paths=['/tmp/test.yaml'],
  933. local_borg_version='1.2.3',
  934. global_arguments=flexmock(log_json=False),
  935. borgmatic_source_directories=(),
  936. )
  937. )
  938. assert create_flags == ('borg', 'create')
  939. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo*')
  940. assert not pattern_file
  941. assert not exclude_file
  942. def test_make_base_create_command_expands_glob_in_source_directories():
  943. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  944. None
  945. )
  946. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  947. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  948. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  949. flexmock(module).should_receive('expand_directories').and_return(())
  950. flexmock(module).should_receive('pattern_root_directories').and_return([])
  951. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  952. flexmock(module).should_receive('expand_home_directories').and_return(())
  953. flexmock(module).should_receive('write_pattern_file').and_return(None)
  954. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  955. flexmock(module.feature).should_receive('available').and_return(True)
  956. flexmock(module).should_receive('ensure_files_readable')
  957. flexmock(module).should_receive('make_pattern_flags').and_return(())
  958. flexmock(module).should_receive('make_exclude_flags').and_return(())
  959. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  960. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  961. )
  962. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  963. module.make_base_create_command(
  964. dry_run=False,
  965. repository_path='repo',
  966. config={
  967. 'source_directories': ['foo*'],
  968. 'repositories': ['repo'],
  969. },
  970. config_paths=['/tmp/test.yaml'],
  971. local_borg_version='1.2.3',
  972. global_arguments=flexmock(log_json=False),
  973. borgmatic_source_directories=(),
  974. )
  975. )
  976. assert create_flags == ('borg', 'create')
  977. assert create_positional_arguments == (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food')
  978. assert not pattern_file
  979. assert not exclude_file
  980. def test_make_base_create_command_includes_archive_name_format_in_borg_command():
  981. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  982. None
  983. )
  984. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  985. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  986. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  987. flexmock(module).should_receive('expand_directories').and_return(())
  988. flexmock(module).should_receive('pattern_root_directories').and_return([])
  989. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  990. flexmock(module).should_receive('expand_home_directories').and_return(())
  991. flexmock(module).should_receive('write_pattern_file').and_return(None)
  992. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  993. flexmock(module.feature).should_receive('available').and_return(True)
  994. flexmock(module).should_receive('ensure_files_readable')
  995. flexmock(module).should_receive('make_pattern_flags').and_return(())
  996. flexmock(module).should_receive('make_exclude_flags').and_return(())
  997. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  998. ('repo::ARCHIVE_NAME',)
  999. )
  1000. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1001. module.make_base_create_command(
  1002. dry_run=False,
  1003. repository_path='repo',
  1004. config={
  1005. 'source_directories': ['foo', 'bar'],
  1006. 'repositories': ['repo'],
  1007. 'archive_name_format': 'ARCHIVE_NAME',
  1008. },
  1009. config_paths=['/tmp/test.yaml'],
  1010. local_borg_version='1.2.3',
  1011. global_arguments=flexmock(log_json=False),
  1012. borgmatic_source_directories=(),
  1013. )
  1014. )
  1015. assert create_flags == ('borg', 'create')
  1016. assert create_positional_arguments == ('repo::ARCHIVE_NAME', 'foo', 'bar')
  1017. assert not pattern_file
  1018. assert not exclude_file
  1019. def test_base_create_command_includes_archive_name_format_with_placeholders_in_borg_command():
  1020. repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
  1021. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1022. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1023. flexmock(module).should_receive('expand_directories').and_return(())
  1024. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1025. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1026. flexmock(module).should_receive('expand_home_directories').and_return(())
  1027. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1028. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1029. flexmock(module.feature).should_receive('available').and_return(True)
  1030. flexmock(module).should_receive('ensure_files_readable')
  1031. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1032. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1033. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1034. (repository_archive_pattern,)
  1035. )
  1036. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1037. module.make_base_create_command(
  1038. dry_run=False,
  1039. repository_path='repo',
  1040. config={
  1041. 'source_directories': ['foo', 'bar'],
  1042. 'repositories': ['repo'],
  1043. 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
  1044. },
  1045. config_paths=['/tmp/test.yaml'],
  1046. local_borg_version='1.2.3',
  1047. global_arguments=flexmock(log_json=False),
  1048. borgmatic_source_directories=(),
  1049. )
  1050. )
  1051. assert create_flags == ('borg', 'create')
  1052. assert create_positional_arguments == (repository_archive_pattern, 'foo', 'bar')
  1053. assert not pattern_file
  1054. assert not exclude_file
  1055. def test_base_create_command_includes_repository_and_archive_name_format_with_placeholders_in_borg_command():
  1056. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
  1057. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1058. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1059. flexmock(module).should_receive('expand_directories').and_return(())
  1060. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1061. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1062. flexmock(module).should_receive('expand_home_directories').and_return(())
  1063. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1064. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1065. flexmock(module.feature).should_receive('available').and_return(True)
  1066. flexmock(module).should_receive('ensure_files_readable')
  1067. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1068. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1069. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1070. (repository_archive_pattern,)
  1071. )
  1072. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1073. module.make_base_create_command(
  1074. dry_run=False,
  1075. repository_path='{fqdn}', # noqa: FS003
  1076. config={
  1077. 'source_directories': ['foo', 'bar'],
  1078. 'repositories': ['{fqdn}'], # noqa: FS003
  1079. 'archive_name_format': 'Documents_{hostname}-{now}', # noqa: FS003
  1080. },
  1081. config_paths=['/tmp/test.yaml'],
  1082. local_borg_version='1.2.3',
  1083. global_arguments=flexmock(log_json=False),
  1084. borgmatic_source_directories=(),
  1085. )
  1086. )
  1087. assert create_flags == ('borg', 'create')
  1088. assert create_positional_arguments == (repository_archive_pattern, 'foo', 'bar')
  1089. assert not pattern_file
  1090. assert not exclude_file
  1091. def test_make_base_create_command_includes_extra_borg_options_in_borg_command():
  1092. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1093. None
  1094. )
  1095. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1096. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1097. flexmock(module).should_receive('expand_directories').and_return(())
  1098. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1099. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1100. flexmock(module).should_receive('expand_home_directories').and_return(())
  1101. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1102. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1103. flexmock(module.feature).should_receive('available').and_return(True)
  1104. flexmock(module).should_receive('ensure_files_readable')
  1105. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1106. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1107. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1108. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1109. )
  1110. (create_flags, create_positional_arguments, pattern_file, exclude_file) = (
  1111. module.make_base_create_command(
  1112. dry_run=False,
  1113. repository_path='repo',
  1114. config={
  1115. 'source_directories': ['foo', 'bar'],
  1116. 'repositories': ['repo'],
  1117. 'extra_borg_options': {'create': '--extra --options'},
  1118. },
  1119. config_paths=['/tmp/test.yaml'],
  1120. local_borg_version='1.2.3',
  1121. global_arguments=flexmock(log_json=False),
  1122. borgmatic_source_directories=(),
  1123. )
  1124. )
  1125. assert create_flags == ('borg', 'create', '--extra', '--options')
  1126. assert create_positional_arguments == REPO_ARCHIVE_WITH_PATHS
  1127. assert not pattern_file
  1128. assert not exclude_file
  1129. def test_make_base_create_command_with_non_existent_directory_and_source_directories_must_exist_raises():
  1130. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1131. None
  1132. )
  1133. flexmock(module).should_receive('check_all_source_directories_exist').and_raise(ValueError)
  1134. with pytest.raises(ValueError):
  1135. module.make_base_create_command(
  1136. dry_run=False,
  1137. repository_path='repo',
  1138. config={
  1139. 'source_directories': ['foo', 'bar'],
  1140. 'repositories': ['repo'],
  1141. 'source_directories_must_exist': True,
  1142. },
  1143. config_paths=['/tmp/test.yaml'],
  1144. local_borg_version='1.2.3',
  1145. global_arguments=flexmock(log_json=False),
  1146. borgmatic_source_directories=(),
  1147. )
  1148. def test_create_archive_calls_borg_with_parameters():
  1149. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1150. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1151. flexmock(module).should_receive('expand_directories').and_return(())
  1152. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1153. flexmock(module).should_receive('make_base_create_command').and_return(
  1154. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1155. )
  1156. flexmock(module.environment).should_receive('make_environment')
  1157. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1158. None
  1159. )
  1160. flexmock(module).should_receive('execute_command').with_args(
  1161. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1162. output_log_level=logging.INFO,
  1163. output_file=None,
  1164. borg_local_path='borg',
  1165. borg_exit_codes=None,
  1166. working_directory=None,
  1167. extra_environment=None,
  1168. )
  1169. module.create_archive(
  1170. dry_run=False,
  1171. repository_path='repo',
  1172. config={
  1173. 'source_directories': ['foo', 'bar'],
  1174. 'repositories': ['repo'],
  1175. 'exclude_patterns': None,
  1176. },
  1177. config_paths=['/tmp/test.yaml'],
  1178. local_borg_version='1.2.3',
  1179. global_arguments=flexmock(log_json=False),
  1180. )
  1181. def test_create_archive_calls_borg_with_environment():
  1182. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1183. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1184. flexmock(module).should_receive('expand_directories').and_return(())
  1185. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1186. flexmock(module).should_receive('make_base_create_command').and_return(
  1187. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1188. )
  1189. environment = {'BORG_THINGY': 'YUP'}
  1190. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  1191. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1192. None
  1193. )
  1194. flexmock(module).should_receive('execute_command').with_args(
  1195. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1196. output_log_level=logging.INFO,
  1197. output_file=None,
  1198. borg_local_path='borg',
  1199. borg_exit_codes=None,
  1200. working_directory=None,
  1201. extra_environment=environment,
  1202. )
  1203. module.create_archive(
  1204. dry_run=False,
  1205. repository_path='repo',
  1206. config={
  1207. 'source_directories': ['foo', 'bar'],
  1208. 'repositories': ['repo'],
  1209. 'exclude_patterns': None,
  1210. },
  1211. config_paths=['/tmp/test.yaml'],
  1212. local_borg_version='1.2.3',
  1213. global_arguments=flexmock(log_json=False),
  1214. )
  1215. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  1216. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1217. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1218. flexmock(module).should_receive('expand_directories').and_return(())
  1219. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1220. flexmock(module).should_receive('make_base_create_command').and_return(
  1221. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1222. )
  1223. flexmock(module.environment).should_receive('make_environment')
  1224. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1225. None
  1226. )
  1227. flexmock(module).should_receive('execute_command').with_args(
  1228. ('borg', 'create', '--info') + REPO_ARCHIVE_WITH_PATHS,
  1229. output_log_level=logging.INFO,
  1230. output_file=None,
  1231. borg_local_path='borg',
  1232. borg_exit_codes=None,
  1233. working_directory=None,
  1234. extra_environment=None,
  1235. )
  1236. insert_logging_mock(logging.INFO)
  1237. module.create_archive(
  1238. dry_run=False,
  1239. repository_path='repo',
  1240. config={
  1241. 'source_directories': ['foo', 'bar'],
  1242. 'repositories': ['repo'],
  1243. 'exclude_patterns': None,
  1244. },
  1245. config_paths=['/tmp/test.yaml'],
  1246. local_borg_version='1.2.3',
  1247. global_arguments=flexmock(log_json=False),
  1248. )
  1249. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  1250. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1251. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1252. flexmock(module).should_receive('expand_directories').and_return(())
  1253. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1254. flexmock(module).should_receive('make_base_create_command').and_return(
  1255. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1256. )
  1257. flexmock(module.environment).should_receive('make_environment')
  1258. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1259. None
  1260. )
  1261. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1262. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1263. working_directory=None,
  1264. extra_environment=None,
  1265. borg_local_path='borg',
  1266. borg_exit_codes=None,
  1267. )
  1268. insert_logging_mock(logging.INFO)
  1269. module.create_archive(
  1270. dry_run=False,
  1271. repository_path='repo',
  1272. config={
  1273. 'source_directories': ['foo', 'bar'],
  1274. 'repositories': ['repo'],
  1275. 'exclude_patterns': None,
  1276. },
  1277. config_paths=['/tmp/test.yaml'],
  1278. local_borg_version='1.2.3',
  1279. global_arguments=flexmock(log_json=False),
  1280. json=True,
  1281. )
  1282. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  1283. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1284. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1285. flexmock(module).should_receive('expand_directories').and_return(())
  1286. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1287. flexmock(module).should_receive('make_base_create_command').and_return(
  1288. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1289. )
  1290. flexmock(module.environment).should_receive('make_environment')
  1291. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1292. None
  1293. )
  1294. flexmock(module).should_receive('execute_command').with_args(
  1295. ('borg', 'create', '--debug', '--show-rc') + REPO_ARCHIVE_WITH_PATHS,
  1296. output_log_level=logging.INFO,
  1297. output_file=None,
  1298. borg_local_path='borg',
  1299. borg_exit_codes=None,
  1300. working_directory=None,
  1301. extra_environment=None,
  1302. )
  1303. insert_logging_mock(logging.DEBUG)
  1304. module.create_archive(
  1305. dry_run=False,
  1306. repository_path='repo',
  1307. config={
  1308. 'source_directories': ['foo', 'bar'],
  1309. 'repositories': ['repo'],
  1310. 'exclude_patterns': None,
  1311. },
  1312. config_paths=['/tmp/test.yaml'],
  1313. local_borg_version='1.2.3',
  1314. global_arguments=flexmock(log_json=False),
  1315. )
  1316. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  1317. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1318. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1319. flexmock(module).should_receive('expand_directories').and_return(())
  1320. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1321. flexmock(module).should_receive('make_base_create_command').and_return(
  1322. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1323. )
  1324. flexmock(module.environment).should_receive('make_environment')
  1325. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1326. None
  1327. )
  1328. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1329. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1330. working_directory=None,
  1331. extra_environment=None,
  1332. borg_local_path='borg',
  1333. borg_exit_codes=None,
  1334. )
  1335. insert_logging_mock(logging.DEBUG)
  1336. module.create_archive(
  1337. dry_run=False,
  1338. repository_path='repo',
  1339. config={
  1340. 'source_directories': ['foo', 'bar'],
  1341. 'repositories': ['repo'],
  1342. 'exclude_patterns': None,
  1343. },
  1344. config_paths=['/tmp/test.yaml'],
  1345. local_borg_version='1.2.3',
  1346. global_arguments=flexmock(log_json=False),
  1347. json=True,
  1348. )
  1349. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats():
  1350. # --dry-run and --stats are mutually exclusive, see:
  1351. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  1352. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1353. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1354. flexmock(module).should_receive('expand_directories').and_return(())
  1355. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1356. flexmock(module).should_receive('make_base_create_command').and_return(
  1357. (('borg', 'create', '--dry-run'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1358. )
  1359. flexmock(module.environment).should_receive('make_environment')
  1360. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1361. None
  1362. )
  1363. flexmock(module).should_receive('execute_command').with_args(
  1364. ('borg', 'create', '--dry-run', '--info') + REPO_ARCHIVE_WITH_PATHS,
  1365. output_log_level=logging.INFO,
  1366. output_file=None,
  1367. borg_local_path='borg',
  1368. borg_exit_codes=None,
  1369. working_directory=None,
  1370. extra_environment=None,
  1371. )
  1372. insert_logging_mock(logging.INFO)
  1373. module.create_archive(
  1374. dry_run=True,
  1375. repository_path='repo',
  1376. config={
  1377. 'source_directories': ['foo', 'bar'],
  1378. 'repositories': ['repo'],
  1379. 'exclude_patterns': None,
  1380. },
  1381. config_paths=['/tmp/test.yaml'],
  1382. local_borg_version='1.2.3',
  1383. global_arguments=flexmock(log_json=False),
  1384. stats=True,
  1385. )
  1386. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  1387. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1388. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1389. flexmock(module).should_receive('expand_directories').and_return(())
  1390. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1391. flexmock(module).should_receive('make_base_create_command').and_return(
  1392. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1393. )
  1394. flexmock(module.environment).should_receive('make_environment')
  1395. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1396. '/working/dir'
  1397. )
  1398. flexmock(module).should_receive('execute_command').with_args(
  1399. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1400. output_log_level=logging.INFO,
  1401. output_file=None,
  1402. borg_local_path='borg',
  1403. borg_exit_codes=None,
  1404. working_directory='/working/dir',
  1405. extra_environment=None,
  1406. )
  1407. module.create_archive(
  1408. dry_run=False,
  1409. repository_path='repo',
  1410. config={
  1411. 'source_directories': ['foo', 'bar'],
  1412. 'repositories': ['repo'],
  1413. 'working_directory': '/working/dir',
  1414. 'exclude_patterns': None,
  1415. },
  1416. config_paths=['/tmp/test.yaml'],
  1417. local_borg_version='1.2.3',
  1418. global_arguments=flexmock(log_json=False),
  1419. )
  1420. def test_create_archive_with_exit_codes_calls_borg_using_them():
  1421. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1422. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1423. flexmock(module).should_receive('expand_directories').and_return(())
  1424. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1425. flexmock(module).should_receive('make_base_create_command').and_return(
  1426. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1427. )
  1428. flexmock(module.environment).should_receive('make_environment')
  1429. borg_exit_codes = flexmock()
  1430. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1431. None
  1432. )
  1433. flexmock(module).should_receive('execute_command').with_args(
  1434. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1435. output_log_level=logging.INFO,
  1436. output_file=None,
  1437. borg_local_path='borg',
  1438. borg_exit_codes=borg_exit_codes,
  1439. working_directory=None,
  1440. extra_environment=None,
  1441. )
  1442. module.create_archive(
  1443. dry_run=False,
  1444. repository_path='repo',
  1445. config={
  1446. 'source_directories': ['foo', 'bar'],
  1447. 'repositories': ['repo'],
  1448. 'exclude_patterns': None,
  1449. 'borg_exit_codes': borg_exit_codes,
  1450. },
  1451. config_paths=['/tmp/test.yaml'],
  1452. local_borg_version='1.2.3',
  1453. global_arguments=flexmock(log_json=False),
  1454. )
  1455. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  1456. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1457. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1458. flexmock(module).should_receive('expand_directories').and_return(())
  1459. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1460. flexmock(module).should_receive('make_base_create_command').and_return(
  1461. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1462. )
  1463. flexmock(module.environment).should_receive('make_environment')
  1464. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1465. None
  1466. )
  1467. flexmock(module).should_receive('execute_command').with_args(
  1468. ('borg', 'create', '--stats') + REPO_ARCHIVE_WITH_PATHS,
  1469. output_log_level=module.borgmatic.logger.ANSWER,
  1470. output_file=None,
  1471. borg_local_path='borg',
  1472. borg_exit_codes=None,
  1473. working_directory=None,
  1474. extra_environment=None,
  1475. )
  1476. module.create_archive(
  1477. dry_run=False,
  1478. repository_path='repo',
  1479. config={
  1480. 'source_directories': ['foo', 'bar'],
  1481. 'repositories': ['repo'],
  1482. 'exclude_patterns': None,
  1483. },
  1484. config_paths=['/tmp/test.yaml'],
  1485. local_borg_version='1.2.3',
  1486. global_arguments=flexmock(log_json=False),
  1487. stats=True,
  1488. )
  1489. def test_create_archive_with_files_calls_borg_with_answer_output_log_level():
  1490. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1491. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1492. flexmock(module).should_receive('expand_directories').and_return(())
  1493. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1494. flexmock(module).should_receive('make_base_create_command').and_return(
  1495. (
  1496. ('borg', 'create', '--list', '--filter', 'FOO'),
  1497. REPO_ARCHIVE_WITH_PATHS,
  1498. flexmock(),
  1499. flexmock(),
  1500. )
  1501. )
  1502. flexmock(module.environment).should_receive('make_environment')
  1503. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1504. None
  1505. )
  1506. flexmock(module).should_receive('execute_command').with_args(
  1507. ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE_WITH_PATHS,
  1508. output_log_level=module.borgmatic.logger.ANSWER,
  1509. output_file=None,
  1510. borg_local_path='borg',
  1511. borg_exit_codes=None,
  1512. working_directory=None,
  1513. extra_environment=None,
  1514. )
  1515. module.create_archive(
  1516. dry_run=False,
  1517. repository_path='repo',
  1518. config={
  1519. 'source_directories': ['foo', 'bar'],
  1520. 'repositories': ['repo'],
  1521. 'exclude_patterns': None,
  1522. },
  1523. config_paths=['/tmp/test.yaml'],
  1524. local_borg_version='1.2.3',
  1525. global_arguments=flexmock(log_json=False),
  1526. list_files=True,
  1527. )
  1528. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1529. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1530. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1531. flexmock(module).should_receive('expand_directories').and_return(())
  1532. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1533. flexmock(module).should_receive('make_base_create_command').and_return(
  1534. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1535. )
  1536. flexmock(module.environment).should_receive('make_environment')
  1537. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1538. None
  1539. )
  1540. flexmock(module).should_receive('execute_command').with_args(
  1541. ('borg', 'create', '--info', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1542. output_log_level=logging.INFO,
  1543. output_file=module.DO_NOT_CAPTURE,
  1544. borg_local_path='borg',
  1545. borg_exit_codes=None,
  1546. working_directory=None,
  1547. extra_environment=None,
  1548. )
  1549. insert_logging_mock(logging.INFO)
  1550. module.create_archive(
  1551. dry_run=False,
  1552. repository_path='repo',
  1553. config={
  1554. 'source_directories': ['foo', 'bar'],
  1555. 'repositories': ['repo'],
  1556. 'exclude_patterns': None,
  1557. },
  1558. config_paths=['/tmp/test.yaml'],
  1559. local_borg_version='1.2.3',
  1560. global_arguments=flexmock(log_json=False),
  1561. progress=True,
  1562. )
  1563. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1564. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1565. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1566. flexmock(module).should_receive('expand_directories').and_return(())
  1567. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1568. flexmock(module).should_receive('make_base_create_command').and_return(
  1569. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1570. )
  1571. flexmock(module.environment).should_receive('make_environment')
  1572. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1573. None
  1574. )
  1575. flexmock(module).should_receive('execute_command').with_args(
  1576. ('borg', 'create', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1577. output_log_level=logging.INFO,
  1578. output_file=module.DO_NOT_CAPTURE,
  1579. borg_local_path='borg',
  1580. borg_exit_codes=None,
  1581. working_directory=None,
  1582. extra_environment=None,
  1583. )
  1584. module.create_archive(
  1585. dry_run=False,
  1586. repository_path='repo',
  1587. config={
  1588. 'source_directories': ['foo', 'bar'],
  1589. 'repositories': ['repo'],
  1590. 'exclude_patterns': None,
  1591. },
  1592. config_paths=['/tmp/test.yaml'],
  1593. local_borg_version='1.2.3',
  1594. global_arguments=flexmock(log_json=False),
  1595. progress=True,
  1596. )
  1597. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1598. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1599. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1600. processes = flexmock()
  1601. flexmock(module).should_receive('expand_directories').and_return(())
  1602. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1603. flexmock(module).should_receive('make_base_create_command').and_return(
  1604. (
  1605. ('borg', 'create', '--read-special'),
  1606. REPO_ARCHIVE_WITH_PATHS,
  1607. flexmock(),
  1608. flexmock(),
  1609. )
  1610. )
  1611. flexmock(module.environment).should_receive('make_environment')
  1612. create_command = (
  1613. 'borg',
  1614. 'create',
  1615. '--read-special',
  1616. '--progress',
  1617. ) + REPO_ARCHIVE_WITH_PATHS
  1618. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1619. None
  1620. )
  1621. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1622. create_command + ('--dry-run', '--list'),
  1623. processes=processes,
  1624. output_log_level=logging.INFO,
  1625. output_file=module.DO_NOT_CAPTURE,
  1626. borg_local_path='borg',
  1627. borg_exit_codes=None,
  1628. working_directory=None,
  1629. extra_environment=None,
  1630. )
  1631. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1632. create_command,
  1633. processes=processes,
  1634. output_log_level=logging.INFO,
  1635. output_file=module.DO_NOT_CAPTURE,
  1636. borg_local_path='borg',
  1637. borg_exit_codes=None,
  1638. working_directory=None,
  1639. extra_environment=None,
  1640. )
  1641. module.create_archive(
  1642. dry_run=False,
  1643. repository_path='repo',
  1644. config={
  1645. 'source_directories': ['foo', 'bar'],
  1646. 'repositories': ['repo'],
  1647. 'exclude_patterns': None,
  1648. },
  1649. config_paths=['/tmp/test.yaml'],
  1650. local_borg_version='1.2.3',
  1651. global_arguments=flexmock(log_json=False),
  1652. progress=True,
  1653. stream_processes=processes,
  1654. )
  1655. def test_create_archive_with_json_calls_borg_with_json_flag():
  1656. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1657. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1658. flexmock(module).should_receive('expand_directories').and_return(())
  1659. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1660. flexmock(module).should_receive('make_base_create_command').and_return(
  1661. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1662. )
  1663. flexmock(module.environment).should_receive('make_environment')
  1664. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1665. None
  1666. )
  1667. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1668. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1669. working_directory=None,
  1670. extra_environment=None,
  1671. borg_local_path='borg',
  1672. borg_exit_codes=None,
  1673. ).and_return('[]')
  1674. json_output = module.create_archive(
  1675. dry_run=False,
  1676. repository_path='repo',
  1677. config={
  1678. 'source_directories': ['foo', 'bar'],
  1679. 'repositories': ['repo'],
  1680. 'exclude_patterns': None,
  1681. },
  1682. config_paths=['/tmp/test.yaml'],
  1683. local_borg_version='1.2.3',
  1684. global_arguments=flexmock(log_json=False),
  1685. json=True,
  1686. )
  1687. assert json_output == '[]'
  1688. def test_create_archive_with_stats_and_json_calls_borg_without_stats_flag():
  1689. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1690. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1691. flexmock(module).should_receive('expand_directories').and_return(())
  1692. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1693. flexmock(module).should_receive('make_base_create_command').and_return(
  1694. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1695. )
  1696. flexmock(module.environment).should_receive('make_environment')
  1697. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1698. None
  1699. )
  1700. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1701. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1702. working_directory=None,
  1703. extra_environment=None,
  1704. borg_local_path='borg',
  1705. borg_exit_codes=None,
  1706. ).and_return('[]')
  1707. json_output = module.create_archive(
  1708. dry_run=False,
  1709. repository_path='repo',
  1710. config={
  1711. 'source_directories': ['foo*'],
  1712. 'repositories': ['repo'],
  1713. 'exclude_patterns': None,
  1714. },
  1715. config_paths=['/tmp/test.yaml'],
  1716. local_borg_version='1.2.3',
  1717. global_arguments=flexmock(log_json=False),
  1718. json=True,
  1719. stats=True,
  1720. )
  1721. assert json_output == '[]'
  1722. def test_create_archive_calls_borg_with_working_directory():
  1723. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1724. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1725. flexmock(module).should_receive('expand_directories').and_return(())
  1726. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1727. flexmock(module).should_receive('make_base_create_command').and_return(
  1728. (('borg', 'create'), REPO_ARCHIVE_WITH_PATHS, flexmock(), flexmock())
  1729. )
  1730. flexmock(module.environment).should_receive('make_environment')
  1731. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  1732. '/working/dir'
  1733. )
  1734. flexmock(module).should_receive('execute_command').with_args(
  1735. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1736. output_log_level=logging.INFO,
  1737. output_file=None,
  1738. borg_local_path='borg',
  1739. borg_exit_codes=None,
  1740. working_directory='/working/dir',
  1741. extra_environment=None,
  1742. )
  1743. module.create_archive(
  1744. dry_run=False,
  1745. repository_path='repo',
  1746. config={
  1747. 'source_directories': ['foo', 'bar'],
  1748. 'repositories': ['repo'],
  1749. 'exclude_patterns': None,
  1750. 'working_directory': '/working/dir',
  1751. },
  1752. config_paths=['/tmp/test.yaml'],
  1753. local_borg_version='1.2.3',
  1754. global_arguments=flexmock(log_json=False),
  1755. )
  1756. def test_check_all_source_directories_exist_with_glob_and_tilde_directories():
  1757. flexmock(module).should_receive('expand_directory').with_args('foo*', None).and_return(
  1758. ('foo', 'food')
  1759. )
  1760. flexmock(module).should_receive('expand_directory').with_args('~/bar', None).and_return(
  1761. ('/root/bar',)
  1762. )
  1763. flexmock(module.os.path).should_receive('exists').and_return(False)
  1764. flexmock(module.os.path).should_receive('exists').with_args('foo').and_return(True)
  1765. flexmock(module.os.path).should_receive('exists').with_args('food').and_return(True)
  1766. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  1767. module.check_all_source_directories_exist(['foo*', '~/bar'])
  1768. def test_check_all_source_directories_exist_with_non_existent_directory_raises():
  1769. flexmock(module).should_receive('expand_directory').with_args('foo', None).and_return(('foo',))
  1770. flexmock(module.os.path).should_receive('exists').and_return(False)
  1771. with pytest.raises(ValueError):
  1772. module.check_all_source_directories_exist(['foo'])
  1773. def test_check_all_source_directories_exist_with_working_directory_applies_to_relative_source_directories():
  1774. flexmock(module).should_receive('expand_directory').with_args(
  1775. 'foo*', working_directory='/tmp'
  1776. ).and_return(('/tmp/foo', '/tmp/food'))
  1777. flexmock(module).should_receive('expand_directory').with_args(
  1778. '/root/bar', working_directory='/tmp'
  1779. ).and_return(('/root/bar',))
  1780. flexmock(module.os.path).should_receive('exists').and_return(False)
  1781. flexmock(module.os.path).should_receive('exists').with_args('/tmp/foo').and_return(True)
  1782. flexmock(module.os.path).should_receive('exists').with_args('/tmp/food').and_return(True)
  1783. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  1784. module.check_all_source_directories_exist(['foo*', '/root/bar'], working_directory='/tmp')