test_create.py 92 KB

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