test_create.py 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590
  1. import logging
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import create as module
  6. from ..test_verbosity import insert_logging_mock
  7. def test_expand_directory_with_basic_path_passes_it_through():
  8. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  9. flexmock(module.glob).should_receive('glob').and_return([])
  10. paths = module.expand_directory('foo')
  11. assert paths == ['foo']
  12. def test_expand_directory_with_glob_expands():
  13. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  14. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  15. paths = module.expand_directory('foo*')
  16. assert paths == ['foo', 'food']
  17. def test_expand_directories_flattens_expanded_directories():
  18. flexmock(module).should_receive('expand_directory').with_args('~/foo').and_return(['/root/foo'])
  19. flexmock(module).should_receive('expand_directory').with_args('bar*').and_return(
  20. ['bar', 'barf']
  21. )
  22. paths = module.expand_directories(('~/foo', 'bar*'))
  23. assert paths == ('/root/foo', 'bar', 'barf')
  24. def test_expand_directories_considers_none_as_no_directories():
  25. paths = module.expand_directories(None)
  26. assert paths == ()
  27. def test_expand_home_directories_expands_tildes():
  28. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  29. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  30. paths = module.expand_home_directories(('~/bar', 'baz'))
  31. assert paths == ('/foo/bar', 'baz')
  32. def test_expand_home_directories_considers_none_as_no_directories():
  33. paths = module.expand_home_directories(None)
  34. assert paths == ()
  35. def test_map_directories_to_devices_gives_device_id_per_path():
  36. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  37. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=66))
  38. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  39. assert device_map == {
  40. '/foo': 55,
  41. '/bar': 66,
  42. }
  43. def test_map_directories_to_devices_with_missing_path_does_not_error():
  44. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  45. flexmock(module.os).should_receive('stat').with_args('/bar').and_raise(FileNotFoundError)
  46. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  47. assert device_map == {
  48. '/foo': 55,
  49. '/bar': None,
  50. }
  51. @pytest.mark.parametrize(
  52. 'directories,additional_directories,expected_directories',
  53. (
  54. ({'/': 1, '/root': 1}, {}, ('/',)),
  55. ({'/': 1, '/root/': 1}, {}, ('/',)),
  56. ({'/': 1, '/root': 2}, {}, ('/', '/root')),
  57. ({'/root': 1, '/': 1}, {}, ('/',)),
  58. ({'/root': 1, '/root/foo': 1}, {}, ('/root',)),
  59. ({'/root/': 1, '/root/foo': 1}, {}, ('/root/',)),
  60. ({'/root': 1, '/root/foo/': 1}, {}, ('/root',)),
  61. ({'/root': 1, '/root/foo': 2}, {}, ('/root', '/root/foo')),
  62. ({'/root/foo': 1, '/root': 1}, {}, ('/root',)),
  63. ({'/root': None, '/root/foo': None}, {}, ('/root', '/root/foo')),
  64. ({'/root': 1, '/etc': 1, '/root/foo/bar': 1}, {}, ('/etc', '/root')),
  65. ({'/root': 1, '/root/foo': 1, '/root/foo/bar': 1}, {}, ('/root',)),
  66. ({'/dup': 1, '/dup': 1}, {}, ('/dup',)),
  67. ({'/foo': 1, '/bar': 1}, {}, ('/bar', '/foo')),
  68. ({'/foo': 1, '/bar': 2}, {}, ('/bar', '/foo')),
  69. ({'/root/foo': 1}, {'/root': 1}, ()),
  70. ({'/root/foo': 1}, {'/root': 2}, ('/root/foo',)),
  71. ({'/root/foo': 1}, {}, ('/root/foo',)),
  72. ),
  73. )
  74. def test_deduplicate_directories_removes_child_paths_on_the_same_filesystem(
  75. directories, additional_directories, expected_directories
  76. ):
  77. assert (
  78. module.deduplicate_directories(directories, additional_directories) == expected_directories
  79. )
  80. def test_write_pattern_file_writes_pattern_lines():
  81. temporary_file = flexmock(name='filename', flush=lambda: None)
  82. temporary_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  83. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  84. module.write_pattern_file(['R /foo', '+ /foo/bar'])
  85. def test_write_pattern_file_with_sources_writes_sources_as_roots():
  86. temporary_file = flexmock(name='filename', flush=lambda: None)
  87. temporary_file.should_receive('write').with_args('R /foo\n+ /foo/bar\nR /baz\nR /quux')
  88. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  89. module.write_pattern_file(['R /foo', '+ /foo/bar'], sources=['/baz', '/quux'])
  90. def test_write_pattern_file_without_patterns_but_with_sources_writes_sources_as_roots():
  91. temporary_file = flexmock(name='filename', flush=lambda: None)
  92. temporary_file.should_receive('write').with_args('R /baz\nR /quux')
  93. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  94. module.write_pattern_file([], sources=['/baz', '/quux'])
  95. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  96. module.write_pattern_file([])
  97. def test_write_pattern_file_overwrites_existing_file():
  98. pattern_file = flexmock(name='filename', flush=lambda: None)
  99. pattern_file.should_receive('seek').with_args(0).once()
  100. pattern_file.should_receive('write').with_args('R /foo\n+ /foo/bar')
  101. flexmock(module.tempfile).should_receive('NamedTemporaryFile').never()
  102. module.write_pattern_file(['R /foo', '+ /foo/bar'], pattern_file=pattern_file)
  103. @pytest.mark.parametrize(
  104. 'filename_lists,opened_filenames',
  105. (
  106. ([('foo', 'bar'), ('baz', 'quux')], ('foo', 'bar', 'baz', 'quux')),
  107. ([None, ('foo', 'bar')], ('foo', 'bar')),
  108. ([None, None], ()),
  109. ),
  110. )
  111. def test_ensure_files_readable_opens_filenames(filename_lists, opened_filenames):
  112. for expected_filename in opened_filenames:
  113. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  114. expected_filename
  115. ).and_return(flexmock(close=lambda: None))
  116. module.ensure_files_readable(*filename_lists)
  117. def test_make_pattern_flags_includes_pattern_filename_when_given():
  118. pattern_flags = module.make_pattern_flags(
  119. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  120. )
  121. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  122. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  123. pattern_flags = module.make_pattern_flags(
  124. location_config={'patterns_from': ['patterns', 'other']}
  125. )
  126. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  127. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  128. pattern_flags = module.make_pattern_flags(
  129. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  130. )
  131. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  132. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  133. pattern_flags = module.make_pattern_flags(location_config={'patterns_from': None})
  134. assert pattern_flags == ()
  135. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  136. exclude_flags = module.make_exclude_flags(
  137. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  138. )
  139. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  140. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  141. exclude_flags = module.make_exclude_flags(
  142. location_config={'exclude_from': ['excludes', 'other']}
  143. )
  144. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  145. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  146. exclude_flags = module.make_exclude_flags(
  147. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  148. )
  149. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  150. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  151. exclude_flags = module.make_exclude_flags(location_config={'exclude_from': None})
  152. assert exclude_flags == ()
  153. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  154. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': True})
  155. assert exclude_flags == ('--exclude-caches',)
  156. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  157. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': False})
  158. assert exclude_flags == ()
  159. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  160. exclude_flags = module.make_exclude_flags(
  161. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  162. )
  163. assert exclude_flags == (
  164. '--exclude-if-present',
  165. 'exclude_me',
  166. '--exclude-if-present',
  167. 'also_me',
  168. )
  169. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  170. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': True})
  171. assert exclude_flags == ('--keep-exclude-tags',)
  172. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  173. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': False})
  174. assert exclude_flags == ()
  175. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  176. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': True})
  177. assert exclude_flags == ('--exclude-nodump',)
  178. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  179. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': False})
  180. assert exclude_flags == ()
  181. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  182. exclude_flags = module.make_exclude_flags(location_config={})
  183. assert exclude_flags == ()
  184. def test_make_list_filter_flags_with_debug_and_feature_available_includes_plus_and_minus():
  185. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  186. flexmock(module.feature).should_receive('available').and_return(True)
  187. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME+-'
  188. def test_make_list_filter_flags_with_info_and_feature_available_omits_plus_and_minus():
  189. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  190. flexmock(module.feature).should_receive('available').and_return(True)
  191. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME'
  192. def test_make_list_filter_flags_with_debug_and_feature_available_and_dry_run_includes_plus_and_minus():
  193. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  194. flexmock(module.feature).should_receive('available').and_return(True)
  195. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
  196. def test_make_list_filter_flags_with_info_and_feature_available_and_dry_run_includes_plus_and_minus():
  197. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  198. flexmock(module.feature).should_receive('available').and_return(True)
  199. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=True) == 'AME+-'
  200. def test_make_list_filter_flags_with_debug_and_feature_not_available_includes_x():
  201. flexmock(module.logger).should_receive('isEnabledFor').and_return(True)
  202. flexmock(module.feature).should_receive('available').and_return(False)
  203. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AMEx-'
  204. def test_make_list_filter_flags_with_info_and_feature_not_available_omits_x():
  205. flexmock(module.logger).should_receive('isEnabledFor').and_return(False)
  206. flexmock(module.feature).should_receive('available').and_return(False)
  207. assert module.make_list_filter_flags(local_borg_version=flexmock(), dry_run=False) == 'AME-'
  208. def test_collect_borgmatic_source_directories_set_when_directory_exists():
  209. flexmock(module.os.path).should_receive('exists').and_return(True)
  210. flexmock(module.os.path).should_receive('expanduser')
  211. assert module.collect_borgmatic_source_directories('/tmp') == ['/tmp']
  212. def test_collect_borgmatic_source_directories_empty_when_directory_does_not_exist():
  213. flexmock(module.os.path).should_receive('exists').and_return(False)
  214. flexmock(module.os.path).should_receive('expanduser')
  215. assert module.collect_borgmatic_source_directories('/tmp') == []
  216. def test_collect_borgmatic_source_directories_defaults_when_directory_not_given():
  217. flexmock(module.os.path).should_receive('exists').and_return(True)
  218. flexmock(module.os.path).should_receive('expanduser')
  219. assert module.collect_borgmatic_source_directories(None) == [
  220. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  221. ]
  222. def test_pattern_root_directories_deals_with_none_patterns():
  223. assert module.pattern_root_directories(patterns=None) == []
  224. def test_pattern_root_directories_parses_roots_and_ignores_others():
  225. assert module.pattern_root_directories(
  226. ['R /root', '+ /root/foo', '- /root/foo/bar', 'R /baz']
  227. ) == ['/root', '/baz']
  228. @pytest.mark.parametrize(
  229. 'character_device,block_device,fifo,expected_result',
  230. (
  231. (False, False, False, False),
  232. (True, False, False, True),
  233. (False, True, False, True),
  234. (True, True, False, True),
  235. (False, False, True, True),
  236. (False, True, True, True),
  237. (True, False, True, True),
  238. ),
  239. )
  240. def test_special_file_looks_at_file_type(character_device, block_device, fifo, expected_result):
  241. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mode=flexmock()))
  242. flexmock(module.stat).should_receive('S_ISCHR').and_return(character_device)
  243. flexmock(module.stat).should_receive('S_ISBLK').and_return(block_device)
  244. flexmock(module.stat).should_receive('S_ISFIFO').and_return(fifo)
  245. assert module.special_file('/dev/special') == expected_result
  246. def test_special_file_treats_broken_symlink_as_non_special():
  247. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  248. assert module.special_file('/broken/symlink') is False
  249. def test_any_parent_directories_treats_parents_as_match():
  250. module.any_parent_directories('/foo/bar.txt', ('/foo', '/etc'))
  251. def test_any_parent_directories_treats_grandparents_as_match():
  252. module.any_parent_directories('/foo/bar/baz.txt', ('/foo', '/etc'))
  253. def test_any_parent_directories_treats_unrelated_paths_as_non_match():
  254. module.any_parent_directories('/foo/bar.txt', ('/usr', '/etc'))
  255. def test_collect_special_file_paths_parses_special_files_from_borg_dry_run_file_list():
  256. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  257. 'Processing files ...\n- /foo\n+ /bar\n- /baz'
  258. )
  259. flexmock(module).should_receive('special_file').and_return(True)
  260. flexmock(module).should_receive('any_parent_directories').and_return(False)
  261. assert module.collect_special_file_paths(
  262. ('borg', 'create'),
  263. local_path=None,
  264. working_directory=None,
  265. borg_environment=None,
  266. skip_directories=flexmock(),
  267. ) == ('/foo', '/bar', '/baz')
  268. def test_collect_special_file_paths_excludes_requested_directories():
  269. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  270. '+ /foo\n- /bar\n- /baz'
  271. )
  272. flexmock(module).should_receive('special_file').and_return(True)
  273. flexmock(module).should_receive('any_parent_directories').and_return(False).and_return(
  274. True
  275. ).and_return(False)
  276. assert module.collect_special_file_paths(
  277. ('borg', 'create'),
  278. local_path=None,
  279. working_directory=None,
  280. borg_environment=None,
  281. skip_directories=flexmock(),
  282. ) == ('/foo', '/baz')
  283. def test_collect_special_file_paths_excludes_non_special_files():
  284. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  285. '+ /foo\n+ /bar\n+ /baz'
  286. )
  287. flexmock(module).should_receive('special_file').and_return(True).and_return(False).and_return(
  288. True
  289. )
  290. flexmock(module).should_receive('any_parent_directories').and_return(False)
  291. assert module.collect_special_file_paths(
  292. ('borg', 'create'),
  293. local_path=None,
  294. working_directory=None,
  295. borg_environment=None,
  296. skip_directories=flexmock(),
  297. ) == ('/foo', '/baz')
  298. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}' # noqa: FS003
  299. REPO_ARCHIVE_WITH_PATHS = (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'bar')
  300. def test_create_archive_calls_borg_with_parameters():
  301. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  302. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  303. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  304. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  305. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  306. flexmock(module).should_receive('expand_directories').and_return(())
  307. flexmock(module).should_receive('pattern_root_directories').and_return([])
  308. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  309. flexmock(module).should_receive('expand_home_directories').and_return(())
  310. flexmock(module).should_receive('write_pattern_file').and_return(None)
  311. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  312. flexmock(module.feature).should_receive('available').and_return(True)
  313. flexmock(module).should_receive('ensure_files_readable')
  314. flexmock(module).should_receive('make_pattern_flags').and_return(())
  315. flexmock(module).should_receive('make_exclude_flags').and_return(())
  316. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  317. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  318. )
  319. flexmock(module.environment).should_receive('make_environment')
  320. flexmock(module).should_receive('execute_command').with_args(
  321. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  322. output_log_level=logging.INFO,
  323. output_file=None,
  324. borg_local_path='borg',
  325. working_directory=None,
  326. extra_environment=None,
  327. )
  328. module.create_archive(
  329. dry_run=False,
  330. repository_path='repo',
  331. location_config={
  332. 'source_directories': ['foo', 'bar'],
  333. 'repositories': ['repo'],
  334. 'exclude_patterns': None,
  335. },
  336. storage_config={},
  337. local_borg_version='1.2.3',
  338. )
  339. def test_create_archive_calls_borg_with_environment():
  340. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  341. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  342. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  343. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  344. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  345. flexmock(module).should_receive('expand_directories').and_return(())
  346. flexmock(module).should_receive('pattern_root_directories').and_return([])
  347. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  348. flexmock(module).should_receive('expand_home_directories').and_return(())
  349. flexmock(module).should_receive('write_pattern_file').and_return(None)
  350. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  351. flexmock(module.feature).should_receive('available').and_return(True)
  352. flexmock(module).should_receive('ensure_files_readable')
  353. flexmock(module).should_receive('make_pattern_flags').and_return(())
  354. flexmock(module).should_receive('make_exclude_flags').and_return(())
  355. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  356. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  357. )
  358. environment = {'BORG_THINGY': 'YUP'}
  359. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  360. flexmock(module).should_receive('execute_command').with_args(
  361. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  362. output_log_level=logging.INFO,
  363. output_file=None,
  364. borg_local_path='borg',
  365. working_directory=None,
  366. extra_environment=environment,
  367. )
  368. module.create_archive(
  369. dry_run=False,
  370. repository_path='repo',
  371. location_config={
  372. 'source_directories': ['foo', 'bar'],
  373. 'repositories': ['repo'],
  374. 'exclude_patterns': None,
  375. },
  376. storage_config={},
  377. local_borg_version='1.2.3',
  378. )
  379. def test_create_archive_with_patterns_calls_borg_with_patterns_including_converted_source_directories():
  380. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  381. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  382. pattern_flags = ('--patterns-from', 'patterns')
  383. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  384. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  385. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  386. flexmock(module).should_receive('expand_directories').and_return(())
  387. flexmock(module).should_receive('pattern_root_directories').and_return([])
  388. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  389. flexmock(module).should_receive('expand_home_directories').and_return(())
  390. flexmock(module).should_receive('write_pattern_file').and_return(
  391. flexmock(name='/tmp/patterns')
  392. ).and_return(None)
  393. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  394. flexmock(module.feature).should_receive('available').and_return(True)
  395. flexmock(module).should_receive('ensure_files_readable')
  396. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  397. flexmock(module).should_receive('make_exclude_flags').and_return(())
  398. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  399. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  400. )
  401. flexmock(module.environment).should_receive('make_environment')
  402. flexmock(module).should_receive('execute_command').with_args(
  403. ('borg', 'create') + pattern_flags + (f'repo::{DEFAULT_ARCHIVE_NAME}',),
  404. output_log_level=logging.INFO,
  405. output_file=None,
  406. borg_local_path='borg',
  407. working_directory=None,
  408. extra_environment=None,
  409. )
  410. module.create_archive(
  411. dry_run=False,
  412. repository_path='repo',
  413. location_config={
  414. 'source_directories': ['foo', 'bar'],
  415. 'repositories': ['repo'],
  416. 'patterns': ['pattern'],
  417. },
  418. storage_config={},
  419. local_borg_version='1.2.3',
  420. )
  421. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  422. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  423. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  424. exclude_flags = ('--exclude-from', 'excludes')
  425. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  426. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  427. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  428. flexmock(module).should_receive('expand_directories').and_return(())
  429. flexmock(module).should_receive('pattern_root_directories').and_return([])
  430. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  431. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  432. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  433. flexmock(name='/tmp/excludes')
  434. )
  435. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  436. flexmock(module.feature).should_receive('available').and_return(True)
  437. flexmock(module).should_receive('ensure_files_readable')
  438. flexmock(module).should_receive('make_pattern_flags').and_return(())
  439. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  440. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  441. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  442. )
  443. flexmock(module.environment).should_receive('make_environment')
  444. flexmock(module).should_receive('execute_command').with_args(
  445. ('borg', 'create') + exclude_flags + REPO_ARCHIVE_WITH_PATHS,
  446. output_log_level=logging.INFO,
  447. output_file=None,
  448. borg_local_path='borg',
  449. working_directory=None,
  450. extra_environment=None,
  451. )
  452. module.create_archive(
  453. dry_run=False,
  454. repository_path='repo',
  455. location_config={
  456. 'source_directories': ['foo', 'bar'],
  457. 'repositories': ['repo'],
  458. 'exclude_patterns': ['exclude'],
  459. },
  460. storage_config={},
  461. local_borg_version='1.2.3',
  462. )
  463. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  464. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  465. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  466. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  467. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  468. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  469. flexmock(module).should_receive('expand_directories').and_return(())
  470. flexmock(module).should_receive('pattern_root_directories').and_return([])
  471. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  472. flexmock(module).should_receive('expand_home_directories').and_return(())
  473. flexmock(module).should_receive('write_pattern_file').and_return(None)
  474. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  475. flexmock(module.feature).should_receive('available').and_return(True)
  476. flexmock(module).should_receive('ensure_files_readable')
  477. flexmock(module).should_receive('make_pattern_flags').and_return(())
  478. flexmock(module).should_receive('make_exclude_flags').and_return(())
  479. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  480. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  481. )
  482. flexmock(module.environment).should_receive('make_environment')
  483. flexmock(module).should_receive('execute_command').with_args(
  484. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  485. output_log_level=logging.INFO,
  486. output_file=None,
  487. borg_local_path='borg',
  488. working_directory=None,
  489. extra_environment=None,
  490. )
  491. insert_logging_mock(logging.INFO)
  492. module.create_archive(
  493. dry_run=False,
  494. repository_path='repo',
  495. location_config={
  496. 'source_directories': ['foo', 'bar'],
  497. 'repositories': ['repo'],
  498. 'exclude_patterns': None,
  499. },
  500. storage_config={},
  501. local_borg_version='1.2.3',
  502. )
  503. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  504. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  505. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  506. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  507. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  508. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  509. flexmock(module).should_receive('expand_directories').and_return(())
  510. flexmock(module).should_receive('pattern_root_directories').and_return([])
  511. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  512. flexmock(module).should_receive('expand_home_directories').and_return(())
  513. flexmock(module).should_receive('write_pattern_file').and_return(None)
  514. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  515. flexmock(module.feature).should_receive('available').and_return(True)
  516. flexmock(module).should_receive('ensure_files_readable')
  517. flexmock(module).should_receive('make_pattern_flags').and_return(())
  518. flexmock(module).should_receive('make_exclude_flags').and_return(())
  519. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  520. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  521. )
  522. flexmock(module.environment).should_receive('make_environment')
  523. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  524. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  525. working_directory=None,
  526. extra_environment=None,
  527. )
  528. insert_logging_mock(logging.INFO)
  529. module.create_archive(
  530. dry_run=False,
  531. repository_path='repo',
  532. location_config={
  533. 'source_directories': ['foo', 'bar'],
  534. 'repositories': ['repo'],
  535. 'exclude_patterns': None,
  536. },
  537. storage_config={},
  538. local_borg_version='1.2.3',
  539. json=True,
  540. )
  541. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  542. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  543. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  544. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  545. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  546. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  547. flexmock(module).should_receive('expand_directories').and_return(())
  548. flexmock(module).should_receive('pattern_root_directories').and_return([])
  549. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  550. flexmock(module).should_receive('expand_home_directories').and_return(())
  551. flexmock(module).should_receive('write_pattern_file').and_return(None)
  552. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  553. flexmock(module.feature).should_receive('available').and_return(True)
  554. flexmock(module).should_receive('ensure_files_readable')
  555. flexmock(module).should_receive('make_pattern_flags').and_return(())
  556. flexmock(module).should_receive('make_exclude_flags').and_return(())
  557. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  558. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  559. )
  560. flexmock(module.environment).should_receive('make_environment')
  561. flexmock(module).should_receive('execute_command').with_args(
  562. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--debug', '--show-rc'),
  563. output_log_level=logging.INFO,
  564. output_file=None,
  565. borg_local_path='borg',
  566. working_directory=None,
  567. extra_environment=None,
  568. )
  569. insert_logging_mock(logging.DEBUG)
  570. module.create_archive(
  571. dry_run=False,
  572. repository_path='repo',
  573. location_config={
  574. 'source_directories': ['foo', 'bar'],
  575. 'repositories': ['repo'],
  576. 'exclude_patterns': None,
  577. },
  578. storage_config={},
  579. local_borg_version='1.2.3',
  580. )
  581. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  582. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  583. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  584. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  585. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  586. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  587. flexmock(module).should_receive('expand_directories').and_return(())
  588. flexmock(module).should_receive('pattern_root_directories').and_return([])
  589. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  590. flexmock(module).should_receive('expand_home_directories').and_return(())
  591. flexmock(module).should_receive('write_pattern_file').and_return(None)
  592. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  593. flexmock(module.feature).should_receive('available').and_return(True)
  594. flexmock(module).should_receive('ensure_files_readable')
  595. flexmock(module).should_receive('make_pattern_flags').and_return(())
  596. flexmock(module).should_receive('make_exclude_flags').and_return(())
  597. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  598. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  599. )
  600. flexmock(module.environment).should_receive('make_environment')
  601. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  602. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  603. working_directory=None,
  604. extra_environment=None,
  605. )
  606. insert_logging_mock(logging.DEBUG)
  607. module.create_archive(
  608. dry_run=False,
  609. repository_path='repo',
  610. location_config={
  611. 'source_directories': ['foo', 'bar'],
  612. 'repositories': ['repo'],
  613. 'exclude_patterns': None,
  614. },
  615. storage_config={},
  616. local_borg_version='1.2.3',
  617. json=True,
  618. )
  619. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  620. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  621. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  622. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  623. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  624. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  625. flexmock(module).should_receive('expand_directories').and_return(())
  626. flexmock(module).should_receive('pattern_root_directories').and_return([])
  627. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  628. flexmock(module).should_receive('expand_home_directories').and_return(())
  629. flexmock(module).should_receive('write_pattern_file').and_return(None)
  630. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  631. flexmock(module.feature).should_receive('available').and_return(True)
  632. flexmock(module).should_receive('ensure_files_readable')
  633. flexmock(module).should_receive('make_pattern_flags').and_return(())
  634. flexmock(module).should_receive('make_exclude_flags').and_return(())
  635. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  636. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  637. )
  638. flexmock(module.environment).should_receive('make_environment')
  639. flexmock(module).should_receive('execute_command').with_args(
  640. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  641. output_log_level=logging.INFO,
  642. output_file=None,
  643. borg_local_path='borg',
  644. working_directory=None,
  645. extra_environment=None,
  646. )
  647. module.create_archive(
  648. dry_run=True,
  649. repository_path='repo',
  650. location_config={
  651. 'source_directories': ['foo', 'bar'],
  652. 'repositories': ['repo'],
  653. 'exclude_patterns': None,
  654. },
  655. storage_config={},
  656. local_borg_version='1.2.3',
  657. )
  658. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  659. # --dry-run and --stats are mutually exclusive, see:
  660. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  661. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  662. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  663. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  664. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  665. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  666. flexmock(module).should_receive('expand_directories').and_return(())
  667. flexmock(module).should_receive('pattern_root_directories').and_return([])
  668. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  669. flexmock(module).should_receive('expand_home_directories').and_return(())
  670. flexmock(module).should_receive('write_pattern_file').and_return(None)
  671. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  672. flexmock(module.feature).should_receive('available').and_return(True)
  673. flexmock(module).should_receive('ensure_files_readable')
  674. flexmock(module).should_receive('make_pattern_flags').and_return(())
  675. flexmock(module).should_receive('make_exclude_flags').and_return(())
  676. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  677. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  678. )
  679. flexmock(module.environment).should_receive('make_environment')
  680. flexmock(module).should_receive('execute_command').with_args(
  681. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  682. output_log_level=logging.INFO,
  683. output_file=None,
  684. borg_local_path='borg',
  685. working_directory=None,
  686. extra_environment=None,
  687. )
  688. insert_logging_mock(logging.INFO)
  689. module.create_archive(
  690. dry_run=True,
  691. repository_path='repo',
  692. location_config={
  693. 'source_directories': ['foo', 'bar'],
  694. 'repositories': ['repo'],
  695. 'exclude_patterns': None,
  696. },
  697. storage_config={},
  698. local_borg_version='1.2.3',
  699. stats=True,
  700. )
  701. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  702. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  703. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  704. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  705. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  706. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  707. flexmock(module).should_receive('expand_directories').and_return(())
  708. flexmock(module).should_receive('pattern_root_directories').and_return([])
  709. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  710. flexmock(module).should_receive('expand_home_directories').and_return(())
  711. flexmock(module).should_receive('write_pattern_file').and_return(None)
  712. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  713. flexmock(module.feature).should_receive('available').and_return(True)
  714. flexmock(module).should_receive('ensure_files_readable')
  715. flexmock(module).should_receive('make_pattern_flags').and_return(())
  716. flexmock(module).should_receive('make_exclude_flags').and_return(())
  717. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  718. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  719. )
  720. flexmock(module.environment).should_receive('make_environment')
  721. flexmock(module).should_receive('execute_command').with_args(
  722. ('borg', 'create', '--checkpoint-interval', '600') + REPO_ARCHIVE_WITH_PATHS,
  723. output_log_level=logging.INFO,
  724. output_file=None,
  725. borg_local_path='borg',
  726. working_directory=None,
  727. extra_environment=None,
  728. )
  729. module.create_archive(
  730. dry_run=False,
  731. repository_path='repo',
  732. location_config={
  733. 'source_directories': ['foo', 'bar'],
  734. 'repositories': ['repo'],
  735. 'exclude_patterns': None,
  736. },
  737. storage_config={'checkpoint_interval': 600},
  738. local_borg_version='1.2.3',
  739. )
  740. def test_create_archive_with_checkpoint_volume_calls_borg_with_checkpoint_volume_parameters():
  741. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  742. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  743. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  744. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  745. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  746. flexmock(module).should_receive('expand_directories').and_return(())
  747. flexmock(module).should_receive('pattern_root_directories').and_return([])
  748. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  749. flexmock(module).should_receive('expand_home_directories').and_return(())
  750. flexmock(module).should_receive('write_pattern_file').and_return(None)
  751. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  752. flexmock(module.feature).should_receive('available').and_return(True)
  753. flexmock(module).should_receive('ensure_files_readable')
  754. flexmock(module).should_receive('make_pattern_flags').and_return(())
  755. flexmock(module).should_receive('make_exclude_flags').and_return(())
  756. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  757. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  758. )
  759. flexmock(module.environment).should_receive('make_environment')
  760. flexmock(module).should_receive('execute_command').with_args(
  761. ('borg', 'create', '--checkpoint-volume', '1024') + REPO_ARCHIVE_WITH_PATHS,
  762. output_log_level=logging.INFO,
  763. output_file=None,
  764. borg_local_path='borg',
  765. working_directory=None,
  766. extra_environment=None,
  767. )
  768. module.create_archive(
  769. dry_run=False,
  770. repository_path='repo',
  771. location_config={
  772. 'source_directories': ['foo', 'bar'],
  773. 'repositories': ['repo'],
  774. 'exclude_patterns': None,
  775. },
  776. storage_config={'checkpoint_volume': 1024},
  777. local_borg_version='1.2.3',
  778. )
  779. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  780. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  781. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  782. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  783. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  784. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  785. flexmock(module).should_receive('expand_directories').and_return(())
  786. flexmock(module).should_receive('pattern_root_directories').and_return([])
  787. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  788. flexmock(module).should_receive('expand_home_directories').and_return(())
  789. flexmock(module).should_receive('write_pattern_file').and_return(None)
  790. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  791. flexmock(module.feature).should_receive('available').and_return(True)
  792. flexmock(module).should_receive('ensure_files_readable')
  793. flexmock(module).should_receive('make_pattern_flags').and_return(())
  794. flexmock(module).should_receive('make_exclude_flags').and_return(())
  795. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  796. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  797. )
  798. flexmock(module.environment).should_receive('make_environment')
  799. flexmock(module).should_receive('execute_command').with_args(
  800. ('borg', 'create', '--chunker-params', '1,2,3,4') + REPO_ARCHIVE_WITH_PATHS,
  801. output_log_level=logging.INFO,
  802. output_file=None,
  803. borg_local_path='borg',
  804. working_directory=None,
  805. extra_environment=None,
  806. )
  807. module.create_archive(
  808. dry_run=False,
  809. repository_path='repo',
  810. location_config={
  811. 'source_directories': ['foo', 'bar'],
  812. 'repositories': ['repo'],
  813. 'exclude_patterns': None,
  814. },
  815. storage_config={'chunker_params': '1,2,3,4'},
  816. local_borg_version='1.2.3',
  817. )
  818. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  819. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  820. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  821. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  822. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  823. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  824. flexmock(module).should_receive('expand_directories').and_return(())
  825. flexmock(module).should_receive('pattern_root_directories').and_return([])
  826. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  827. flexmock(module).should_receive('expand_home_directories').and_return(())
  828. flexmock(module).should_receive('write_pattern_file').and_return(None)
  829. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  830. flexmock(module.feature).should_receive('available').and_return(True)
  831. flexmock(module).should_receive('ensure_files_readable')
  832. flexmock(module).should_receive('make_pattern_flags').and_return(())
  833. flexmock(module).should_receive('make_exclude_flags').and_return(())
  834. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  835. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  836. )
  837. flexmock(module.environment).should_receive('make_environment')
  838. flexmock(module).should_receive('execute_command').with_args(
  839. ('borg', 'create', '--compression', 'rle') + REPO_ARCHIVE_WITH_PATHS,
  840. output_log_level=logging.INFO,
  841. output_file=None,
  842. borg_local_path='borg',
  843. working_directory=None,
  844. extra_environment=None,
  845. )
  846. module.create_archive(
  847. dry_run=False,
  848. repository_path='repo',
  849. location_config={
  850. 'source_directories': ['foo', 'bar'],
  851. 'repositories': ['repo'],
  852. 'exclude_patterns': None,
  853. },
  854. storage_config={'compression': 'rle'},
  855. local_borg_version='1.2.3',
  856. )
  857. @pytest.mark.parametrize(
  858. 'feature_available,option_flag',
  859. ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  860. )
  861. def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  862. feature_available, option_flag
  863. ):
  864. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  865. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  866. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  867. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  868. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  869. flexmock(module).should_receive('expand_directories').and_return(())
  870. flexmock(module).should_receive('pattern_root_directories').and_return([])
  871. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  872. flexmock(module).should_receive('expand_home_directories').and_return(())
  873. flexmock(module).should_receive('write_pattern_file').and_return(None)
  874. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  875. flexmock(module.feature).should_receive('available').and_return(feature_available)
  876. flexmock(module).should_receive('ensure_files_readable')
  877. flexmock(module).should_receive('make_pattern_flags').and_return(())
  878. flexmock(module).should_receive('make_exclude_flags').and_return(())
  879. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  880. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  881. )
  882. flexmock(module.environment).should_receive('make_environment')
  883. flexmock(module).should_receive('execute_command').with_args(
  884. ('borg', 'create', option_flag, '100') + REPO_ARCHIVE_WITH_PATHS,
  885. output_log_level=logging.INFO,
  886. output_file=None,
  887. borg_local_path='borg',
  888. working_directory=None,
  889. extra_environment=None,
  890. )
  891. module.create_archive(
  892. dry_run=False,
  893. repository_path='repo',
  894. location_config={
  895. 'source_directories': ['foo', 'bar'],
  896. 'repositories': ['repo'],
  897. 'exclude_patterns': None,
  898. },
  899. storage_config={'upload_rate_limit': 100},
  900. local_borg_version='1.2.3',
  901. )
  902. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  903. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  904. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  905. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  906. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  907. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  908. flexmock(module).should_receive('expand_directories').and_return(())
  909. flexmock(module).should_receive('pattern_root_directories').and_return([])
  910. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  911. '/working/dir'
  912. )
  913. flexmock(module).should_receive('expand_home_directories').and_return(())
  914. flexmock(module).should_receive('write_pattern_file').and_return(None)
  915. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  916. flexmock(module.feature).should_receive('available').and_return(True)
  917. flexmock(module).should_receive('ensure_files_readable')
  918. flexmock(module).should_receive('make_pattern_flags').and_return(())
  919. flexmock(module).should_receive('make_exclude_flags').and_return(())
  920. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  921. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  922. )
  923. flexmock(module.environment).should_receive('make_environment')
  924. flexmock(module).should_receive('execute_command').with_args(
  925. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  926. output_log_level=logging.INFO,
  927. output_file=None,
  928. borg_local_path='borg',
  929. working_directory='/working/dir',
  930. extra_environment=None,
  931. )
  932. module.create_archive(
  933. dry_run=False,
  934. repository_path='repo',
  935. location_config={
  936. 'source_directories': ['foo', 'bar'],
  937. 'repositories': ['repo'],
  938. 'working_directory': '/working/dir',
  939. 'exclude_patterns': None,
  940. },
  941. storage_config={},
  942. local_borg_version='1.2.3',
  943. )
  944. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  945. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  946. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  947. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  948. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  949. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  950. flexmock(module).should_receive('expand_directories').and_return(())
  951. flexmock(module).should_receive('pattern_root_directories').and_return([])
  952. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  953. flexmock(module).should_receive('expand_home_directories').and_return(())
  954. flexmock(module).should_receive('write_pattern_file').and_return(None)
  955. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  956. flexmock(module.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. flexmock(module.environment).should_receive('make_environment')
  964. flexmock(module).should_receive('execute_command').with_args(
  965. ('borg', 'create', '--one-file-system') + REPO_ARCHIVE_WITH_PATHS,
  966. output_log_level=logging.INFO,
  967. output_file=None,
  968. borg_local_path='borg',
  969. working_directory=None,
  970. extra_environment=None,
  971. )
  972. module.create_archive(
  973. dry_run=False,
  974. repository_path='repo',
  975. location_config={
  976. 'source_directories': ['foo', 'bar'],
  977. 'repositories': ['repo'],
  978. 'one_file_system': True,
  979. 'exclude_patterns': None,
  980. },
  981. storage_config={},
  982. local_borg_version='1.2.3',
  983. )
  984. @pytest.mark.parametrize(
  985. 'feature_available,option_flag',
  986. ((True, '--numeric-ids'), (False, '--numeric-owner')),
  987. )
  988. def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
  989. feature_available, option_flag
  990. ):
  991. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  992. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  993. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  994. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  995. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  996. flexmock(module).should_receive('expand_directories').and_return(())
  997. flexmock(module).should_receive('pattern_root_directories').and_return([])
  998. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  999. flexmock(module).should_receive('expand_home_directories').and_return(())
  1000. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1001. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1002. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1003. flexmock(module).should_receive('ensure_files_readable')
  1004. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1005. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1006. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1007. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1008. )
  1009. flexmock(module.environment).should_receive('make_environment')
  1010. flexmock(module).should_receive('execute_command').with_args(
  1011. ('borg', 'create', option_flag) + REPO_ARCHIVE_WITH_PATHS,
  1012. output_log_level=logging.INFO,
  1013. output_file=None,
  1014. borg_local_path='borg',
  1015. working_directory=None,
  1016. extra_environment=None,
  1017. )
  1018. module.create_archive(
  1019. dry_run=False,
  1020. repository_path='repo',
  1021. location_config={
  1022. 'source_directories': ['foo', 'bar'],
  1023. 'repositories': ['repo'],
  1024. 'numeric_ids': True,
  1025. 'exclude_patterns': None,
  1026. },
  1027. storage_config={},
  1028. local_borg_version='1.2.3',
  1029. )
  1030. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  1031. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1032. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1033. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1034. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1035. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1036. flexmock(module).should_receive('expand_directories').and_return(())
  1037. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1038. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1039. flexmock(module).should_receive('expand_home_directories').and_return(())
  1040. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1041. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1042. flexmock(module.feature).should_receive('available').and_return(True)
  1043. flexmock(module).should_receive('ensure_files_readable')
  1044. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1045. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1046. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1047. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1048. )
  1049. flexmock(module.environment).should_receive('make_environment')
  1050. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1051. create_command = ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS
  1052. flexmock(module).should_receive('execute_command').with_args(
  1053. create_command + ('--dry-run', '--list'),
  1054. output_log_level=logging.INFO,
  1055. output_file=None,
  1056. borg_local_path='borg',
  1057. working_directory=None,
  1058. extra_environment=None,
  1059. )
  1060. flexmock(module).should_receive('execute_command').with_args(
  1061. create_command,
  1062. output_log_level=logging.INFO,
  1063. output_file=None,
  1064. borg_local_path='borg',
  1065. working_directory=None,
  1066. extra_environment=None,
  1067. )
  1068. module.create_archive(
  1069. dry_run=False,
  1070. repository_path='repo',
  1071. location_config={
  1072. 'source_directories': ['foo', 'bar'],
  1073. 'repositories': ['repo'],
  1074. 'read_special': True,
  1075. 'exclude_patterns': None,
  1076. },
  1077. storage_config={},
  1078. local_borg_version='1.2.3',
  1079. )
  1080. @pytest.mark.parametrize(
  1081. 'option_name,option_value',
  1082. (
  1083. ('ctime', True),
  1084. ('ctime', False),
  1085. ('birthtime', True),
  1086. ('birthtime', False),
  1087. ),
  1088. )
  1089. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  1090. option_name, option_value
  1091. ):
  1092. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1093. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1094. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  1095. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1096. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1097. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1098. flexmock(module).should_receive('expand_directories').and_return(())
  1099. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1100. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1101. flexmock(module).should_receive('expand_home_directories').and_return(())
  1102. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1103. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1104. flexmock(module.feature).should_receive('available').and_return(True)
  1105. flexmock(module).should_receive('ensure_files_readable')
  1106. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1107. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1108. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1109. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1110. )
  1111. flexmock(module.environment).should_receive('make_environment')
  1112. flexmock(module).should_receive('execute_command').with_args(
  1113. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1114. output_log_level=logging.INFO,
  1115. output_file=None,
  1116. borg_local_path='borg',
  1117. working_directory=None,
  1118. extra_environment=None,
  1119. )
  1120. module.create_archive(
  1121. dry_run=False,
  1122. repository_path='repo',
  1123. location_config={
  1124. 'source_directories': ['foo', 'bar'],
  1125. 'repositories': ['repo'],
  1126. option_name: option_value,
  1127. 'exclude_patterns': None,
  1128. },
  1129. storage_config={},
  1130. local_borg_version='1.2.3',
  1131. )
  1132. @pytest.mark.parametrize(
  1133. 'option_value,feature_available,option_flag',
  1134. (
  1135. (True, True, '--atime'),
  1136. (True, False, None),
  1137. (False, True, None),
  1138. (False, False, '--noatime'),
  1139. ),
  1140. )
  1141. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  1142. option_value, feature_available, option_flag
  1143. ):
  1144. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1145. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1146. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1147. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1148. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1149. flexmock(module).should_receive('expand_directories').and_return(())
  1150. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1151. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1152. flexmock(module).should_receive('expand_home_directories').and_return(())
  1153. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1154. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1155. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1156. flexmock(module).should_receive('ensure_files_readable')
  1157. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1158. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1159. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1160. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1161. )
  1162. flexmock(module.environment).should_receive('make_environment')
  1163. flexmock(module).should_receive('execute_command').with_args(
  1164. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1165. output_log_level=logging.INFO,
  1166. output_file=None,
  1167. borg_local_path='borg',
  1168. working_directory=None,
  1169. extra_environment=None,
  1170. )
  1171. module.create_archive(
  1172. dry_run=False,
  1173. repository_path='repo',
  1174. location_config={
  1175. 'source_directories': ['foo', 'bar'],
  1176. 'repositories': ['repo'],
  1177. 'atime': option_value,
  1178. 'exclude_patterns': None,
  1179. },
  1180. storage_config={},
  1181. local_borg_version='1.2.3',
  1182. )
  1183. @pytest.mark.parametrize(
  1184. 'option_value,feature_available,option_flag',
  1185. (
  1186. (True, True, None),
  1187. (True, False, None),
  1188. (False, True, '--noflags'),
  1189. (False, False, '--nobsdflags'),
  1190. ),
  1191. )
  1192. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  1193. option_value, feature_available, option_flag
  1194. ):
  1195. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1196. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1197. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1198. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1199. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1200. flexmock(module).should_receive('expand_directories').and_return(())
  1201. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1202. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1203. flexmock(module).should_receive('expand_home_directories').and_return(())
  1204. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1205. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1206. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1207. flexmock(module).should_receive('ensure_files_readable')
  1208. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1209. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1210. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1211. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1212. )
  1213. flexmock(module.environment).should_receive('make_environment')
  1214. flexmock(module).should_receive('execute_command').with_args(
  1215. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1216. output_log_level=logging.INFO,
  1217. output_file=None,
  1218. borg_local_path='borg',
  1219. working_directory=None,
  1220. extra_environment=None,
  1221. )
  1222. module.create_archive(
  1223. dry_run=False,
  1224. repository_path='repo',
  1225. location_config={
  1226. 'source_directories': ['foo', 'bar'],
  1227. 'repositories': ['repo'],
  1228. 'flags': option_value,
  1229. 'exclude_patterns': None,
  1230. },
  1231. storage_config={},
  1232. local_borg_version='1.2.3',
  1233. )
  1234. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  1235. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1236. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1237. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1238. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1239. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1240. flexmock(module).should_receive('expand_directories').and_return(())
  1241. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1242. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1243. flexmock(module).should_receive('expand_home_directories').and_return(())
  1244. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1245. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1246. flexmock(module.feature).should_receive('available').and_return(True)
  1247. flexmock(module).should_receive('ensure_files_readable')
  1248. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1249. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1250. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1251. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1252. )
  1253. flexmock(module.environment).should_receive('make_environment')
  1254. flexmock(module).should_receive('execute_command').with_args(
  1255. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  1256. output_log_level=logging.INFO,
  1257. output_file=None,
  1258. borg_local_path='borg',
  1259. working_directory=None,
  1260. extra_environment=None,
  1261. )
  1262. module.create_archive(
  1263. dry_run=False,
  1264. repository_path='repo',
  1265. location_config={
  1266. 'source_directories': ['foo', 'bar'],
  1267. 'repositories': ['repo'],
  1268. 'files_cache': 'ctime,size',
  1269. 'exclude_patterns': None,
  1270. },
  1271. storage_config={},
  1272. local_borg_version='1.2.3',
  1273. )
  1274. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1275. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1276. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1277. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1278. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1279. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1280. flexmock(module).should_receive('expand_directories').and_return(())
  1281. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1282. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1283. flexmock(module).should_receive('expand_home_directories').and_return(())
  1284. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1285. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1286. flexmock(module.feature).should_receive('available').and_return(True)
  1287. flexmock(module).should_receive('ensure_files_readable')
  1288. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1289. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1290. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1291. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1292. )
  1293. flexmock(module.environment).should_receive('make_environment')
  1294. flexmock(module).should_receive('execute_command').with_args(
  1295. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1296. output_log_level=logging.INFO,
  1297. output_file=None,
  1298. borg_local_path='borg1',
  1299. working_directory=None,
  1300. extra_environment=None,
  1301. )
  1302. module.create_archive(
  1303. dry_run=False,
  1304. repository_path='repo',
  1305. location_config={
  1306. 'source_directories': ['foo', 'bar'],
  1307. 'repositories': ['repo'],
  1308. 'exclude_patterns': None,
  1309. },
  1310. storage_config={},
  1311. local_borg_version='1.2.3',
  1312. local_path='borg1',
  1313. )
  1314. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1315. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1316. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1317. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1318. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1319. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1320. flexmock(module).should_receive('expand_directories').and_return(())
  1321. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1322. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1323. flexmock(module).should_receive('expand_home_directories').and_return(())
  1324. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1325. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1326. flexmock(module.feature).should_receive('available').and_return(True)
  1327. flexmock(module).should_receive('ensure_files_readable')
  1328. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1329. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1330. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1331. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1332. )
  1333. flexmock(module.environment).should_receive('make_environment')
  1334. flexmock(module).should_receive('execute_command').with_args(
  1335. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1336. output_log_level=logging.INFO,
  1337. output_file=None,
  1338. borg_local_path='borg',
  1339. working_directory=None,
  1340. extra_environment=None,
  1341. )
  1342. module.create_archive(
  1343. dry_run=False,
  1344. repository_path='repo',
  1345. location_config={
  1346. 'source_directories': ['foo', 'bar'],
  1347. 'repositories': ['repo'],
  1348. 'exclude_patterns': None,
  1349. },
  1350. storage_config={},
  1351. local_borg_version='1.2.3',
  1352. remote_path='borg1',
  1353. )
  1354. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1355. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1356. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1357. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1358. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1359. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1360. flexmock(module).should_receive('expand_directories').and_return(())
  1361. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1362. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1363. flexmock(module).should_receive('expand_home_directories').and_return(())
  1364. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1365. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1366. flexmock(module.feature).should_receive('available').and_return(True)
  1367. flexmock(module).should_receive('ensure_files_readable')
  1368. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1369. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1370. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1371. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1372. )
  1373. flexmock(module.environment).should_receive('make_environment')
  1374. flexmock(module).should_receive('execute_command').with_args(
  1375. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1376. output_log_level=logging.INFO,
  1377. output_file=None,
  1378. borg_local_path='borg',
  1379. working_directory=None,
  1380. extra_environment=None,
  1381. )
  1382. module.create_archive(
  1383. dry_run=False,
  1384. repository_path='repo',
  1385. location_config={
  1386. 'source_directories': ['foo', 'bar'],
  1387. 'repositories': ['repo'],
  1388. 'exclude_patterns': None,
  1389. },
  1390. storage_config={'umask': 740},
  1391. local_borg_version='1.2.3',
  1392. )
  1393. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1394. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1395. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1396. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1397. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1398. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1399. flexmock(module).should_receive('expand_directories').and_return(())
  1400. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1401. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1402. flexmock(module).should_receive('expand_home_directories').and_return(())
  1403. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1404. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1405. flexmock(module.feature).should_receive('available').and_return(True)
  1406. flexmock(module).should_receive('ensure_files_readable')
  1407. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1408. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1409. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1410. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1411. )
  1412. flexmock(module.environment).should_receive('make_environment')
  1413. flexmock(module).should_receive('execute_command').with_args(
  1414. ('borg', 'create', '--lock-wait', '5') + REPO_ARCHIVE_WITH_PATHS,
  1415. output_log_level=logging.INFO,
  1416. output_file=None,
  1417. borg_local_path='borg',
  1418. working_directory=None,
  1419. extra_environment=None,
  1420. )
  1421. module.create_archive(
  1422. dry_run=False,
  1423. repository_path='repo',
  1424. location_config={
  1425. 'source_directories': ['foo', 'bar'],
  1426. 'repositories': ['repo'],
  1427. 'exclude_patterns': None,
  1428. },
  1429. storage_config={'lock_wait': 5},
  1430. local_borg_version='1.2.3',
  1431. )
  1432. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  1433. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1434. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1435. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1436. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1437. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1438. flexmock(module).should_receive('expand_directories').and_return(())
  1439. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1440. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1441. flexmock(module).should_receive('expand_home_directories').and_return(())
  1442. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1443. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1444. flexmock(module.feature).should_receive('available').and_return(True)
  1445. flexmock(module).should_receive('ensure_files_readable')
  1446. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1447. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1448. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1449. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1450. )
  1451. flexmock(module.environment).should_receive('make_environment')
  1452. flexmock(module).should_receive('execute_command').with_args(
  1453. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--stats',),
  1454. output_log_level=module.borgmatic.logger.ANSWER,
  1455. output_file=None,
  1456. borg_local_path='borg',
  1457. working_directory=None,
  1458. extra_environment=None,
  1459. )
  1460. module.create_archive(
  1461. dry_run=False,
  1462. repository_path='repo',
  1463. location_config={
  1464. 'source_directories': ['foo', 'bar'],
  1465. 'repositories': ['repo'],
  1466. 'exclude_patterns': None,
  1467. },
  1468. storage_config={},
  1469. local_borg_version='1.2.3',
  1470. stats=True,
  1471. )
  1472. def test_create_archive_with_files_calls_borg_with_list_parameter_and_answer_output_log_level():
  1473. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1474. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1475. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1476. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1477. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1478. flexmock(module).should_receive('expand_directories').and_return(())
  1479. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1480. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1481. flexmock(module).should_receive('expand_home_directories').and_return(())
  1482. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1483. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1484. flexmock(module.feature).should_receive('available').and_return(True)
  1485. flexmock(module).should_receive('ensure_files_readable')
  1486. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1487. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1488. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1489. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1490. )
  1491. flexmock(module.environment).should_receive('make_environment')
  1492. flexmock(module).should_receive('execute_command').with_args(
  1493. ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE_WITH_PATHS,
  1494. output_log_level=module.borgmatic.logger.ANSWER,
  1495. output_file=None,
  1496. borg_local_path='borg',
  1497. working_directory=None,
  1498. extra_environment=None,
  1499. )
  1500. module.create_archive(
  1501. dry_run=False,
  1502. repository_path='repo',
  1503. location_config={
  1504. 'source_directories': ['foo', 'bar'],
  1505. 'repositories': ['repo'],
  1506. 'exclude_patterns': None,
  1507. },
  1508. storage_config={},
  1509. local_borg_version='1.2.3',
  1510. list_files=True,
  1511. )
  1512. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1513. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1514. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1515. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1516. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1517. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1518. flexmock(module).should_receive('expand_directories').and_return(())
  1519. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1520. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1521. flexmock(module).should_receive('expand_home_directories').and_return(())
  1522. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1523. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1524. flexmock(module.feature).should_receive('available').and_return(True)
  1525. flexmock(module).should_receive('ensure_files_readable')
  1526. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1527. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1528. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1529. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1530. )
  1531. flexmock(module.environment).should_receive('make_environment')
  1532. flexmock(module).should_receive('execute_command').with_args(
  1533. ('borg', 'create')
  1534. + REPO_ARCHIVE_WITH_PATHS
  1535. + (
  1536. '--info',
  1537. '--progress',
  1538. ),
  1539. output_log_level=logging.INFO,
  1540. output_file=module.DO_NOT_CAPTURE,
  1541. borg_local_path='borg',
  1542. working_directory=None,
  1543. extra_environment=None,
  1544. )
  1545. insert_logging_mock(logging.INFO)
  1546. module.create_archive(
  1547. dry_run=False,
  1548. repository_path='repo',
  1549. location_config={
  1550. 'source_directories': ['foo', 'bar'],
  1551. 'repositories': ['repo'],
  1552. 'exclude_patterns': None,
  1553. },
  1554. storage_config={},
  1555. local_borg_version='1.2.3',
  1556. progress=True,
  1557. )
  1558. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1559. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1560. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1561. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1562. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1563. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1564. flexmock(module).should_receive('expand_directories').and_return(())
  1565. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1566. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1567. flexmock(module).should_receive('expand_home_directories').and_return(())
  1568. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1569. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1570. flexmock(module.feature).should_receive('available').and_return(True)
  1571. flexmock(module).should_receive('ensure_files_readable')
  1572. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1573. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1574. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1575. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1576. )
  1577. flexmock(module.environment).should_receive('make_environment')
  1578. flexmock(module).should_receive('execute_command').with_args(
  1579. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--progress',),
  1580. output_log_level=logging.INFO,
  1581. output_file=module.DO_NOT_CAPTURE,
  1582. borg_local_path='borg',
  1583. working_directory=None,
  1584. extra_environment=None,
  1585. )
  1586. module.create_archive(
  1587. dry_run=False,
  1588. repository_path='repo',
  1589. location_config={
  1590. 'source_directories': ['foo', 'bar'],
  1591. 'repositories': ['repo'],
  1592. 'exclude_patterns': None,
  1593. },
  1594. storage_config={},
  1595. local_borg_version='1.2.3',
  1596. progress=True,
  1597. )
  1598. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1599. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1600. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1601. processes = flexmock()
  1602. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1603. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1604. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1605. flexmock(module).should_receive('expand_directories').and_return(())
  1606. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1607. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1608. flexmock(module).should_receive('expand_home_directories').and_return(())
  1609. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1610. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1611. flexmock(module.feature).should_receive('available').and_return(True)
  1612. flexmock(module).should_receive('ensure_files_readable')
  1613. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1614. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1615. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1616. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1617. )
  1618. flexmock(module.environment).should_receive('make_environment')
  1619. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1620. create_command = (
  1621. ('borg', 'create', '--one-file-system', '--read-special')
  1622. + REPO_ARCHIVE_WITH_PATHS
  1623. + ('--progress',)
  1624. )
  1625. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1626. create_command + ('--dry-run', '--list'),
  1627. processes=processes,
  1628. output_log_level=logging.INFO,
  1629. output_file=module.DO_NOT_CAPTURE,
  1630. borg_local_path='borg',
  1631. working_directory=None,
  1632. extra_environment=None,
  1633. )
  1634. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1635. create_command,
  1636. processes=processes,
  1637. output_log_level=logging.INFO,
  1638. output_file=module.DO_NOT_CAPTURE,
  1639. borg_local_path='borg',
  1640. working_directory=None,
  1641. extra_environment=None,
  1642. )
  1643. module.create_archive(
  1644. dry_run=False,
  1645. repository_path='repo',
  1646. location_config={
  1647. 'source_directories': ['foo', 'bar'],
  1648. 'repositories': ['repo'],
  1649. 'exclude_patterns': None,
  1650. },
  1651. storage_config={},
  1652. local_borg_version='1.2.3',
  1653. progress=True,
  1654. stream_processes=processes,
  1655. )
  1656. def test_create_archive_with_stream_processes_ignores_read_special_false_and_logs_warnings():
  1657. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1658. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1659. processes = flexmock()
  1660. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1661. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1662. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1663. flexmock(module).should_receive('expand_directories').and_return(())
  1664. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1665. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1666. flexmock(module).should_receive('expand_home_directories').and_return(())
  1667. flexmock(module).should_receive('write_pattern_file').and_return(flexmock(name='/tmp/excludes'))
  1668. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1669. flexmock(module.feature).should_receive('available').and_return(True)
  1670. flexmock(module).should_receive('ensure_files_readable')
  1671. flexmock(module.logger).should_receive('warning').twice()
  1672. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1673. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1674. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1675. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1676. )
  1677. flexmock(module.environment).should_receive('make_environment')
  1678. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',))
  1679. create_command = (
  1680. 'borg',
  1681. 'create',
  1682. '--one-file-system',
  1683. '--read-special',
  1684. ) + REPO_ARCHIVE_WITH_PATHS
  1685. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1686. create_command + ('--dry-run', '--list'),
  1687. processes=processes,
  1688. output_log_level=logging.INFO,
  1689. output_file=None,
  1690. borg_local_path='borg',
  1691. working_directory=None,
  1692. extra_environment=None,
  1693. )
  1694. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1695. create_command,
  1696. processes=processes,
  1697. output_log_level=logging.INFO,
  1698. output_file=None,
  1699. borg_local_path='borg',
  1700. working_directory=None,
  1701. extra_environment=None,
  1702. )
  1703. module.create_archive(
  1704. dry_run=False,
  1705. repository_path='repo',
  1706. location_config={
  1707. 'source_directories': ['foo', 'bar'],
  1708. 'repositories': ['repo'],
  1709. 'exclude_patterns': None,
  1710. 'read_special': False,
  1711. },
  1712. storage_config={},
  1713. local_borg_version='1.2.3',
  1714. stream_processes=processes,
  1715. )
  1716. def test_create_archive_with_stream_processes_adds_special_files_to_excludes():
  1717. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1718. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1719. processes = flexmock()
  1720. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1721. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1722. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1723. flexmock(module).should_receive('expand_directories').and_return(())
  1724. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1725. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1726. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  1727. ('special',)
  1728. )
  1729. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  1730. flexmock(name='/excludes')
  1731. )
  1732. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1733. flexmock(module.feature).should_receive('available').and_return(True)
  1734. flexmock(module).should_receive('ensure_files_readable')
  1735. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1736. flexmock(module).should_receive('make_exclude_flags').and_return(()).and_return(
  1737. '--exclude-from', '/excludes'
  1738. )
  1739. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1740. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1741. )
  1742. flexmock(module.environment).should_receive('make_environment')
  1743. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  1744. create_command = (
  1745. 'borg',
  1746. 'create',
  1747. '--one-file-system',
  1748. '--read-special',
  1749. ) + REPO_ARCHIVE_WITH_PATHS
  1750. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1751. create_command + ('--dry-run', '--list'),
  1752. processes=processes,
  1753. output_log_level=logging.INFO,
  1754. output_file=None,
  1755. borg_local_path='borg',
  1756. working_directory=None,
  1757. extra_environment=None,
  1758. )
  1759. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1760. create_command + ('--exclude-from', '/excludes'),
  1761. processes=processes,
  1762. output_log_level=logging.INFO,
  1763. output_file=None,
  1764. borg_local_path='borg',
  1765. working_directory=None,
  1766. extra_environment=None,
  1767. )
  1768. module.create_archive(
  1769. dry_run=False,
  1770. repository_path='repo',
  1771. location_config={
  1772. 'source_directories': ['foo', 'bar'],
  1773. 'repositories': ['repo'],
  1774. 'exclude_patterns': None,
  1775. },
  1776. storage_config={},
  1777. local_borg_version='1.2.3',
  1778. stream_processes=processes,
  1779. )
  1780. def test_create_archive_with_stream_processes_and_read_special_does_not_add_special_files_to_excludes():
  1781. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1782. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1783. processes = flexmock()
  1784. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1785. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1786. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1787. flexmock(module).should_receive('expand_directories').and_return(())
  1788. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1789. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1790. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  1791. ('special',)
  1792. )
  1793. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1794. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1795. flexmock(module.feature).should_receive('available').and_return(True)
  1796. flexmock(module).should_receive('ensure_files_readable')
  1797. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1798. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1799. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1800. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1801. )
  1802. flexmock(module.environment).should_receive('make_environment')
  1803. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  1804. create_command = (
  1805. 'borg',
  1806. 'create',
  1807. '--one-file-system',
  1808. '--read-special',
  1809. ) + REPO_ARCHIVE_WITH_PATHS
  1810. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1811. create_command + ('--dry-run', '--list'),
  1812. processes=processes,
  1813. output_log_level=logging.INFO,
  1814. output_file=None,
  1815. borg_local_path='borg',
  1816. working_directory=None,
  1817. extra_environment=None,
  1818. )
  1819. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1820. create_command,
  1821. processes=processes,
  1822. output_log_level=logging.INFO,
  1823. output_file=None,
  1824. borg_local_path='borg',
  1825. working_directory=None,
  1826. extra_environment=None,
  1827. )
  1828. module.create_archive(
  1829. dry_run=False,
  1830. repository_path='repo',
  1831. location_config={
  1832. 'source_directories': ['foo', 'bar'],
  1833. 'repositories': ['repo'],
  1834. 'exclude_patterns': None,
  1835. 'read_special': True,
  1836. },
  1837. storage_config={},
  1838. local_borg_version='1.2.3',
  1839. stream_processes=processes,
  1840. )
  1841. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1842. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1843. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1844. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1845. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1846. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1847. flexmock(module).should_receive('expand_directories').and_return(())
  1848. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1849. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1850. flexmock(module).should_receive('expand_home_directories').and_return(())
  1851. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1852. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1853. flexmock(module.feature).should_receive('available').and_return(True)
  1854. flexmock(module).should_receive('ensure_files_readable')
  1855. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1856. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1857. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1858. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1859. )
  1860. flexmock(module.environment).should_receive('make_environment')
  1861. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1862. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1863. working_directory=None,
  1864. extra_environment=None,
  1865. ).and_return('[]')
  1866. json_output = module.create_archive(
  1867. dry_run=False,
  1868. repository_path='repo',
  1869. location_config={
  1870. 'source_directories': ['foo', 'bar'],
  1871. 'repositories': ['repo'],
  1872. 'exclude_patterns': None,
  1873. },
  1874. storage_config={},
  1875. local_borg_version='1.2.3',
  1876. json=True,
  1877. )
  1878. assert json_output == '[]'
  1879. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1880. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1881. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1882. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1883. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1884. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1885. flexmock(module).should_receive('expand_directories').and_return(())
  1886. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1887. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1888. flexmock(module).should_receive('expand_home_directories').and_return(())
  1889. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1890. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1891. flexmock(module.feature).should_receive('available').and_return(True)
  1892. flexmock(module).should_receive('ensure_files_readable')
  1893. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1894. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1895. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1896. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1897. )
  1898. flexmock(module.environment).should_receive('make_environment')
  1899. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1900. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1901. working_directory=None,
  1902. extra_environment=None,
  1903. ).and_return('[]')
  1904. json_output = module.create_archive(
  1905. dry_run=False,
  1906. repository_path='repo',
  1907. location_config={
  1908. 'source_directories': ['foo', 'bar'],
  1909. 'repositories': ['repo'],
  1910. 'exclude_patterns': None,
  1911. },
  1912. storage_config={},
  1913. local_borg_version='1.2.3',
  1914. json=True,
  1915. stats=True,
  1916. )
  1917. assert json_output == '[]'
  1918. def test_create_archive_with_source_directories_glob_expands():
  1919. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1920. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1921. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1922. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1923. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1924. flexmock(module).should_receive('expand_directories').and_return(())
  1925. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1926. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1927. flexmock(module).should_receive('expand_home_directories').and_return(())
  1928. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1929. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1930. flexmock(module.feature).should_receive('available').and_return(True)
  1931. flexmock(module).should_receive('ensure_files_readable')
  1932. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1933. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1934. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1935. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1936. )
  1937. flexmock(module.environment).should_receive('make_environment')
  1938. flexmock(module).should_receive('execute_command').with_args(
  1939. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food'),
  1940. output_log_level=logging.INFO,
  1941. output_file=None,
  1942. borg_local_path='borg',
  1943. working_directory=None,
  1944. extra_environment=None,
  1945. )
  1946. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1947. module.create_archive(
  1948. dry_run=False,
  1949. repository_path='repo',
  1950. location_config={
  1951. 'source_directories': ['foo*'],
  1952. 'repositories': ['repo'],
  1953. 'exclude_patterns': None,
  1954. },
  1955. storage_config={},
  1956. local_borg_version='1.2.3',
  1957. )
  1958. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1959. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1960. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1961. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1962. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1963. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1964. flexmock(module).should_receive('expand_directories').and_return(())
  1965. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1966. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1967. flexmock(module).should_receive('expand_home_directories').and_return(())
  1968. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1969. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1970. flexmock(module.feature).should_receive('available').and_return(True)
  1971. flexmock(module).should_receive('ensure_files_readable')
  1972. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1973. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1974. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1975. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1976. )
  1977. flexmock(module.environment).should_receive('make_environment')
  1978. flexmock(module).should_receive('execute_command').with_args(
  1979. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo*'),
  1980. output_log_level=logging.INFO,
  1981. output_file=None,
  1982. borg_local_path='borg',
  1983. working_directory=None,
  1984. extra_environment=None,
  1985. )
  1986. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1987. module.create_archive(
  1988. dry_run=False,
  1989. repository_path='repo',
  1990. location_config={
  1991. 'source_directories': ['foo*'],
  1992. 'repositories': ['repo'],
  1993. 'exclude_patterns': None,
  1994. },
  1995. storage_config={},
  1996. local_borg_version='1.2.3',
  1997. )
  1998. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1999. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2000. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2001. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2002. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  2003. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2004. flexmock(module).should_receive('expand_directories').and_return(())
  2005. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2006. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2007. flexmock(module).should_receive('expand_home_directories').and_return(())
  2008. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2009. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2010. flexmock(module.feature).should_receive('available').and_return(True)
  2011. flexmock(module).should_receive('ensure_files_readable')
  2012. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2013. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2014. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2015. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2016. )
  2017. flexmock(module.environment).should_receive('make_environment')
  2018. flexmock(module).should_receive('execute_command').with_args(
  2019. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food'),
  2020. output_log_level=logging.INFO,
  2021. output_file=None,
  2022. borg_local_path='borg',
  2023. working_directory=None,
  2024. extra_environment=None,
  2025. )
  2026. module.create_archive(
  2027. dry_run=False,
  2028. repository_path='repo',
  2029. location_config={
  2030. 'source_directories': ['foo*'],
  2031. 'repositories': ['repo'],
  2032. 'exclude_patterns': None,
  2033. },
  2034. storage_config={},
  2035. local_borg_version='1.2.3',
  2036. )
  2037. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  2038. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2039. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2040. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2041. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2042. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2043. flexmock(module).should_receive('expand_directories').and_return(())
  2044. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2045. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2046. flexmock(module).should_receive('expand_home_directories').and_return(())
  2047. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2048. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2049. flexmock(module.feature).should_receive('available').and_return(True)
  2050. flexmock(module).should_receive('ensure_files_readable')
  2051. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2052. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2053. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2054. ('repo::ARCHIVE_NAME',)
  2055. )
  2056. flexmock(module.environment).should_receive('make_environment')
  2057. flexmock(module).should_receive('execute_command').with_args(
  2058. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  2059. output_log_level=logging.INFO,
  2060. output_file=None,
  2061. borg_local_path='borg',
  2062. working_directory=None,
  2063. extra_environment=None,
  2064. )
  2065. module.create_archive(
  2066. dry_run=False,
  2067. repository_path='repo',
  2068. location_config={
  2069. 'source_directories': ['foo', 'bar'],
  2070. 'repositories': ['repo'],
  2071. 'exclude_patterns': None,
  2072. },
  2073. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  2074. local_borg_version='1.2.3',
  2075. )
  2076. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  2077. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2078. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2079. repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
  2080. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2081. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2082. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2083. flexmock(module).should_receive('expand_directories').and_return(())
  2084. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2085. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2086. flexmock(module).should_receive('expand_home_directories').and_return(())
  2087. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2088. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2089. flexmock(module.feature).should_receive('available').and_return(True)
  2090. flexmock(module).should_receive('ensure_files_readable')
  2091. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2092. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2093. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2094. (repository_archive_pattern,)
  2095. )
  2096. flexmock(module.environment).should_receive('make_environment')
  2097. flexmock(module).should_receive('execute_command').with_args(
  2098. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  2099. output_log_level=logging.INFO,
  2100. output_file=None,
  2101. borg_local_path='borg',
  2102. working_directory=None,
  2103. extra_environment=None,
  2104. )
  2105. module.create_archive(
  2106. dry_run=False,
  2107. repository_path='repo',
  2108. location_config={
  2109. 'source_directories': ['foo', 'bar'],
  2110. 'repositories': ['repo'],
  2111. 'exclude_patterns': None,
  2112. },
  2113. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'}, # noqa: FS003
  2114. local_borg_version='1.2.3',
  2115. )
  2116. def test_create_archive_with_repository_accepts_borg_placeholders():
  2117. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2118. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2119. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
  2120. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2121. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2122. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2123. flexmock(module).should_receive('expand_directories').and_return(())
  2124. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2125. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2126. flexmock(module).should_receive('expand_home_directories').and_return(())
  2127. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2128. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2129. flexmock(module.feature).should_receive('available').and_return(True)
  2130. flexmock(module).should_receive('ensure_files_readable')
  2131. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2132. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2133. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2134. (repository_archive_pattern,)
  2135. )
  2136. flexmock(module.environment).should_receive('make_environment')
  2137. flexmock(module).should_receive('execute_command').with_args(
  2138. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  2139. output_log_level=logging.INFO,
  2140. output_file=None,
  2141. borg_local_path='borg',
  2142. working_directory=None,
  2143. extra_environment=None,
  2144. )
  2145. module.create_archive(
  2146. dry_run=False,
  2147. repository_path='{fqdn}', # noqa: FS003
  2148. location_config={
  2149. 'source_directories': ['foo', 'bar'],
  2150. 'repositories': ['{fqdn}'], # noqa: FS003
  2151. 'exclude_patterns': None,
  2152. },
  2153. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'}, # noqa: FS003
  2154. local_borg_version='1.2.3',
  2155. )
  2156. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  2157. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2158. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2159. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2160. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2161. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2162. flexmock(module).should_receive('expand_directories').and_return(())
  2163. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2164. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2165. flexmock(module).should_receive('expand_home_directories').and_return(())
  2166. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2167. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2168. flexmock(module.feature).should_receive('available').and_return(True)
  2169. flexmock(module).should_receive('ensure_files_readable')
  2170. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2171. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2172. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2173. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2174. )
  2175. flexmock(module.environment).should_receive('make_environment')
  2176. flexmock(module).should_receive('execute_command').with_args(
  2177. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  2178. output_log_level=logging.INFO,
  2179. output_file=None,
  2180. borg_local_path='borg',
  2181. working_directory=None,
  2182. extra_environment=None,
  2183. )
  2184. module.create_archive(
  2185. dry_run=False,
  2186. repository_path='repo',
  2187. location_config={
  2188. 'source_directories': ['foo', 'bar'],
  2189. 'repositories': ['repo'],
  2190. 'exclude_patterns': None,
  2191. },
  2192. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  2193. local_borg_version='1.2.3',
  2194. )
  2195. def test_create_archive_with_stream_processes_calls_borg_with_processes_and_read_special():
  2196. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2197. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2198. processes = flexmock()
  2199. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2200. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2201. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2202. flexmock(module).should_receive('expand_directories').and_return(())
  2203. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2204. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2205. flexmock(module).should_receive('expand_home_directories').and_return(())
  2206. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2207. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2208. flexmock(module.feature).should_receive('available').and_return(True)
  2209. flexmock(module).should_receive('ensure_files_readable')
  2210. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2211. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2212. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2213. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2214. )
  2215. flexmock(module.environment).should_receive('make_environment')
  2216. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  2217. create_command = (
  2218. 'borg',
  2219. 'create',
  2220. '--one-file-system',
  2221. '--read-special',
  2222. ) + REPO_ARCHIVE_WITH_PATHS
  2223. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2224. create_command + ('--dry-run', 'list'),
  2225. processes=processes,
  2226. output_log_level=logging.INFO,
  2227. output_file=None,
  2228. borg_local_path='borg',
  2229. working_directory=None,
  2230. extra_environment=None,
  2231. )
  2232. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2233. create_command,
  2234. processes=processes,
  2235. output_log_level=logging.INFO,
  2236. output_file=None,
  2237. borg_local_path='borg',
  2238. working_directory=None,
  2239. extra_environment=None,
  2240. )
  2241. module.create_archive(
  2242. dry_run=False,
  2243. repository_path='repo',
  2244. location_config={
  2245. 'source_directories': ['foo', 'bar'],
  2246. 'repositories': ['repo'],
  2247. 'exclude_patterns': None,
  2248. },
  2249. storage_config={},
  2250. local_borg_version='1.2.3',
  2251. stream_processes=processes,
  2252. )
  2253. def test_create_archive_with_non_existent_directory_and_source_directories_must_exist_raises_error():
  2254. '''
  2255. If a source directory doesn't exist and source_directories_must_exist is True, raise an error.
  2256. '''
  2257. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2258. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2259. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2260. flexmock(module).should_receive('check_all_source_directories_exist').and_raise(ValueError)
  2261. with pytest.raises(ValueError):
  2262. module.create_archive(
  2263. dry_run=False,
  2264. repository_path='repo',
  2265. location_config={
  2266. 'source_directories': ['foo', 'bar'],
  2267. 'repositories': ['repo'],
  2268. 'exclude_patterns': None,
  2269. 'source_directories_must_exist': True,
  2270. },
  2271. storage_config={},
  2272. local_borg_version='1.2.3',
  2273. )
  2274. def test_check_all_source_directories_exist_with_glob_and_tilde_directories():
  2275. flexmock(module).should_receive('expand_directory').with_args('foo*').and_return(
  2276. ('foo', 'food')
  2277. )
  2278. flexmock(module).should_receive('expand_directory').with_args('~/bar').and_return(
  2279. ('/root/bar',)
  2280. )
  2281. flexmock(module.os.path).should_receive('exists').and_return(False)
  2282. flexmock(module.os.path).should_receive('exists').with_args('foo').and_return(True)
  2283. flexmock(module.os.path).should_receive('exists').with_args('food').and_return(True)
  2284. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  2285. module.check_all_source_directories_exist(['foo*', '~/bar'])
  2286. def test_check_all_source_directories_exist_with_non_existent_directory_raises():
  2287. flexmock(module).should_receive('expand_directory').with_args('foo').and_return(('foo',))
  2288. flexmock(module.os.path).should_receive('exists').and_return(False)
  2289. with pytest.raises(ValueError):
  2290. module.check_all_source_directories_exist(['foo'])