test_create.py 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  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. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  339. )
  340. def test_create_archive_calls_borg_with_environment():
  341. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  342. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  343. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  344. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  345. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  346. flexmock(module).should_receive('expand_directories').and_return(())
  347. flexmock(module).should_receive('pattern_root_directories').and_return([])
  348. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  349. flexmock(module).should_receive('expand_home_directories').and_return(())
  350. flexmock(module).should_receive('write_pattern_file').and_return(None)
  351. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  352. flexmock(module.feature).should_receive('available').and_return(True)
  353. flexmock(module).should_receive('ensure_files_readable')
  354. flexmock(module).should_receive('make_pattern_flags').and_return(())
  355. flexmock(module).should_receive('make_exclude_flags').and_return(())
  356. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  357. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  358. )
  359. environment = {'BORG_THINGY': 'YUP'}
  360. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  361. flexmock(module).should_receive('execute_command').with_args(
  362. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  363. output_log_level=logging.INFO,
  364. output_file=None,
  365. borg_local_path='borg',
  366. working_directory=None,
  367. extra_environment=environment,
  368. )
  369. module.create_archive(
  370. dry_run=False,
  371. repository_path='repo',
  372. location_config={
  373. 'source_directories': ['foo', 'bar'],
  374. 'repositories': ['repo'],
  375. 'exclude_patterns': None,
  376. },
  377. storage_config={},
  378. local_borg_version='1.2.3',
  379. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  380. )
  381. def test_create_archive_with_patterns_calls_borg_with_patterns_including_converted_source_directories():
  382. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  383. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  384. pattern_flags = ('--patterns-from', 'patterns')
  385. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  386. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  387. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  388. flexmock(module).should_receive('expand_directories').and_return(())
  389. flexmock(module).should_receive('pattern_root_directories').and_return([])
  390. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  391. flexmock(module).should_receive('expand_home_directories').and_return(())
  392. flexmock(module).should_receive('write_pattern_file').and_return(
  393. flexmock(name='/tmp/patterns')
  394. ).and_return(None)
  395. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  396. flexmock(module.feature).should_receive('available').and_return(True)
  397. flexmock(module).should_receive('ensure_files_readable')
  398. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  399. flexmock(module).should_receive('make_exclude_flags').and_return(())
  400. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  401. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  402. )
  403. flexmock(module.environment).should_receive('make_environment')
  404. flexmock(module).should_receive('execute_command').with_args(
  405. ('borg', 'create') + pattern_flags + (f'repo::{DEFAULT_ARCHIVE_NAME}',),
  406. output_log_level=logging.INFO,
  407. output_file=None,
  408. borg_local_path='borg',
  409. working_directory=None,
  410. extra_environment=None,
  411. )
  412. module.create_archive(
  413. dry_run=False,
  414. repository_path='repo',
  415. location_config={
  416. 'source_directories': ['foo', 'bar'],
  417. 'repositories': ['repo'],
  418. 'patterns': ['pattern'],
  419. },
  420. storage_config={},
  421. local_borg_version='1.2.3',
  422. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  423. )
  424. def test_create_archive_with_sources_and_used_config_paths_calls_borg_with_sources_and_config_paths():
  425. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  426. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  427. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  428. flexmock(module).should_receive('deduplicate_directories').and_return(
  429. ('foo', 'bar', '/etc/borgmatic/config.yaml')
  430. )
  431. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  432. flexmock(module).should_receive('expand_directories').with_args([]).and_return(())
  433. flexmock(module).should_receive('expand_directories').with_args(
  434. ('foo', 'bar', '/etc/borgmatic/config.yaml')
  435. ).and_return(('foo', 'bar', '/etc/borgmatic/config.yaml'))
  436. flexmock(module).should_receive('expand_directories').with_args([]).and_return(())
  437. flexmock(module).should_receive('pattern_root_directories').and_return([])
  438. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  439. flexmock(module).should_receive('expand_home_directories').and_return(())
  440. flexmock(module).should_receive('write_pattern_file').and_return(None)
  441. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  442. flexmock(module.feature).should_receive('available').and_return(True)
  443. flexmock(module).should_receive('ensure_files_readable')
  444. flexmock(module).should_receive('make_pattern_flags').and_return(())
  445. flexmock(module).should_receive('make_exclude_flags').and_return(())
  446. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  447. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  448. )
  449. environment = {'BORG_THINGY': 'YUP'}
  450. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  451. flexmock(module).should_receive('execute_command').with_args(
  452. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('/etc/borgmatic/config.yaml',),
  453. output_log_level=logging.INFO,
  454. output_file=None,
  455. borg_local_path='borg',
  456. working_directory=None,
  457. extra_environment=environment,
  458. )
  459. module.create_archive(
  460. dry_run=False,
  461. repository_path='repo',
  462. location_config={
  463. 'source_directories': ['foo', 'bar'],
  464. 'repositories': ['repo'],
  465. },
  466. storage_config={},
  467. local_borg_version='1.2.3',
  468. global_arguments=flexmock(log_json=False, used_config_paths=['/etc/borgmatic/config.yaml']),
  469. )
  470. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  471. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  472. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  473. exclude_flags = ('--exclude-from', 'excludes')
  474. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  475. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  476. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  477. flexmock(module).should_receive('expand_directories').and_return(())
  478. flexmock(module).should_receive('pattern_root_directories').and_return([])
  479. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  480. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  481. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  482. flexmock(name='/tmp/excludes')
  483. )
  484. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  485. flexmock(module.feature).should_receive('available').and_return(True)
  486. flexmock(module).should_receive('ensure_files_readable')
  487. flexmock(module).should_receive('make_pattern_flags').and_return(())
  488. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  489. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  490. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  491. )
  492. flexmock(module.environment).should_receive('make_environment')
  493. flexmock(module).should_receive('execute_command').with_args(
  494. ('borg', 'create') + exclude_flags + REPO_ARCHIVE_WITH_PATHS,
  495. output_log_level=logging.INFO,
  496. output_file=None,
  497. borg_local_path='borg',
  498. working_directory=None,
  499. extra_environment=None,
  500. )
  501. module.create_archive(
  502. dry_run=False,
  503. repository_path='repo',
  504. location_config={
  505. 'source_directories': ['foo', 'bar'],
  506. 'repositories': ['repo'],
  507. 'exclude_patterns': ['exclude'],
  508. },
  509. storage_config={},
  510. local_borg_version='1.2.3',
  511. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  512. )
  513. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  514. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  515. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  516. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  517. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  518. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  519. flexmock(module).should_receive('expand_directories').and_return(())
  520. flexmock(module).should_receive('pattern_root_directories').and_return([])
  521. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  522. flexmock(module).should_receive('expand_home_directories').and_return(())
  523. flexmock(module).should_receive('write_pattern_file').and_return(None)
  524. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  525. flexmock(module.feature).should_receive('available').and_return(True)
  526. flexmock(module).should_receive('ensure_files_readable')
  527. flexmock(module).should_receive('make_pattern_flags').and_return(())
  528. flexmock(module).should_receive('make_exclude_flags').and_return(())
  529. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  530. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  531. )
  532. flexmock(module.environment).should_receive('make_environment')
  533. flexmock(module).should_receive('execute_command').with_args(
  534. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  535. output_log_level=logging.INFO,
  536. output_file=None,
  537. borg_local_path='borg',
  538. working_directory=None,
  539. extra_environment=None,
  540. )
  541. insert_logging_mock(logging.INFO)
  542. module.create_archive(
  543. dry_run=False,
  544. repository_path='repo',
  545. location_config={
  546. 'source_directories': ['foo', 'bar'],
  547. 'repositories': ['repo'],
  548. 'exclude_patterns': None,
  549. },
  550. storage_config={},
  551. local_borg_version='1.2.3',
  552. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  553. )
  554. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  555. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  556. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  557. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  558. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  559. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  560. flexmock(module).should_receive('expand_directories').and_return(())
  561. flexmock(module).should_receive('pattern_root_directories').and_return([])
  562. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  563. flexmock(module).should_receive('expand_home_directories').and_return(())
  564. flexmock(module).should_receive('write_pattern_file').and_return(None)
  565. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  566. flexmock(module.feature).should_receive('available').and_return(True)
  567. flexmock(module).should_receive('ensure_files_readable')
  568. flexmock(module).should_receive('make_pattern_flags').and_return(())
  569. flexmock(module).should_receive('make_exclude_flags').and_return(())
  570. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  571. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  572. )
  573. flexmock(module.environment).should_receive('make_environment')
  574. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  575. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  576. working_directory=None,
  577. extra_environment=None,
  578. )
  579. insert_logging_mock(logging.INFO)
  580. module.create_archive(
  581. dry_run=False,
  582. repository_path='repo',
  583. location_config={
  584. 'source_directories': ['foo', 'bar'],
  585. 'repositories': ['repo'],
  586. 'exclude_patterns': None,
  587. },
  588. storage_config={},
  589. local_borg_version='1.2.3',
  590. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  591. json=True,
  592. )
  593. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  594. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  595. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  596. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  597. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  598. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  599. flexmock(module).should_receive('expand_directories').and_return(())
  600. flexmock(module).should_receive('pattern_root_directories').and_return([])
  601. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  602. flexmock(module).should_receive('expand_home_directories').and_return(())
  603. flexmock(module).should_receive('write_pattern_file').and_return(None)
  604. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  605. flexmock(module.feature).should_receive('available').and_return(True)
  606. flexmock(module).should_receive('ensure_files_readable')
  607. flexmock(module).should_receive('make_pattern_flags').and_return(())
  608. flexmock(module).should_receive('make_exclude_flags').and_return(())
  609. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  610. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  611. )
  612. flexmock(module.environment).should_receive('make_environment')
  613. flexmock(module).should_receive('execute_command').with_args(
  614. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--debug', '--show-rc'),
  615. output_log_level=logging.INFO,
  616. output_file=None,
  617. borg_local_path='borg',
  618. working_directory=None,
  619. extra_environment=None,
  620. )
  621. insert_logging_mock(logging.DEBUG)
  622. module.create_archive(
  623. dry_run=False,
  624. repository_path='repo',
  625. location_config={
  626. 'source_directories': ['foo', 'bar'],
  627. 'repositories': ['repo'],
  628. 'exclude_patterns': None,
  629. },
  630. storage_config={},
  631. local_borg_version='1.2.3',
  632. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  633. )
  634. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  635. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  636. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  637. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  638. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  639. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  640. flexmock(module).should_receive('expand_directories').and_return(())
  641. flexmock(module).should_receive('pattern_root_directories').and_return([])
  642. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  643. flexmock(module).should_receive('expand_home_directories').and_return(())
  644. flexmock(module).should_receive('write_pattern_file').and_return(None)
  645. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  646. flexmock(module.feature).should_receive('available').and_return(True)
  647. flexmock(module).should_receive('ensure_files_readable')
  648. flexmock(module).should_receive('make_pattern_flags').and_return(())
  649. flexmock(module).should_receive('make_exclude_flags').and_return(())
  650. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  651. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  652. )
  653. flexmock(module.environment).should_receive('make_environment')
  654. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  655. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  656. working_directory=None,
  657. extra_environment=None,
  658. )
  659. insert_logging_mock(logging.DEBUG)
  660. module.create_archive(
  661. dry_run=False,
  662. repository_path='repo',
  663. location_config={
  664. 'source_directories': ['foo', 'bar'],
  665. 'repositories': ['repo'],
  666. 'exclude_patterns': None,
  667. },
  668. storage_config={},
  669. local_borg_version='1.2.3',
  670. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  671. json=True,
  672. )
  673. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  674. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  675. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  676. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  677. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  678. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  679. flexmock(module).should_receive('expand_directories').and_return(())
  680. flexmock(module).should_receive('pattern_root_directories').and_return([])
  681. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  682. flexmock(module).should_receive('expand_home_directories').and_return(())
  683. flexmock(module).should_receive('write_pattern_file').and_return(None)
  684. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  685. flexmock(module.feature).should_receive('available').and_return(True)
  686. flexmock(module).should_receive('ensure_files_readable')
  687. flexmock(module).should_receive('make_pattern_flags').and_return(())
  688. flexmock(module).should_receive('make_exclude_flags').and_return(())
  689. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  690. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  691. )
  692. flexmock(module.environment).should_receive('make_environment')
  693. flexmock(module).should_receive('execute_command').with_args(
  694. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  695. output_log_level=logging.INFO,
  696. output_file=None,
  697. borg_local_path='borg',
  698. working_directory=None,
  699. extra_environment=None,
  700. )
  701. module.create_archive(
  702. dry_run=True,
  703. repository_path='repo',
  704. location_config={
  705. 'source_directories': ['foo', 'bar'],
  706. 'repositories': ['repo'],
  707. 'exclude_patterns': None,
  708. },
  709. storage_config={},
  710. local_borg_version='1.2.3',
  711. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  712. )
  713. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  714. # --dry-run and --stats are mutually exclusive, see:
  715. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  716. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  717. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  718. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  719. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  720. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  721. flexmock(module).should_receive('expand_directories').and_return(())
  722. flexmock(module).should_receive('pattern_root_directories').and_return([])
  723. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  724. flexmock(module).should_receive('expand_home_directories').and_return(())
  725. flexmock(module).should_receive('write_pattern_file').and_return(None)
  726. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  727. flexmock(module.feature).should_receive('available').and_return(True)
  728. flexmock(module).should_receive('ensure_files_readable')
  729. flexmock(module).should_receive('make_pattern_flags').and_return(())
  730. flexmock(module).should_receive('make_exclude_flags').and_return(())
  731. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  732. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  733. )
  734. flexmock(module.environment).should_receive('make_environment')
  735. flexmock(module).should_receive('execute_command').with_args(
  736. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS + ('--info',),
  737. output_log_level=logging.INFO,
  738. output_file=None,
  739. borg_local_path='borg',
  740. working_directory=None,
  741. extra_environment=None,
  742. )
  743. insert_logging_mock(logging.INFO)
  744. module.create_archive(
  745. dry_run=True,
  746. repository_path='repo',
  747. location_config={
  748. 'source_directories': ['foo', 'bar'],
  749. 'repositories': ['repo'],
  750. 'exclude_patterns': None,
  751. },
  752. storage_config={},
  753. local_borg_version='1.2.3',
  754. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  755. stats=True,
  756. )
  757. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  758. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  759. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  760. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  761. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  762. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  763. flexmock(module).should_receive('expand_directories').and_return(())
  764. flexmock(module).should_receive('pattern_root_directories').and_return([])
  765. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  766. flexmock(module).should_receive('expand_home_directories').and_return(())
  767. flexmock(module).should_receive('write_pattern_file').and_return(None)
  768. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  769. flexmock(module.feature).should_receive('available').and_return(True)
  770. flexmock(module).should_receive('ensure_files_readable')
  771. flexmock(module).should_receive('make_pattern_flags').and_return(())
  772. flexmock(module).should_receive('make_exclude_flags').and_return(())
  773. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  774. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  775. )
  776. flexmock(module.environment).should_receive('make_environment')
  777. flexmock(module).should_receive('execute_command').with_args(
  778. ('borg', 'create', '--checkpoint-interval', '600') + REPO_ARCHIVE_WITH_PATHS,
  779. output_log_level=logging.INFO,
  780. output_file=None,
  781. borg_local_path='borg',
  782. working_directory=None,
  783. extra_environment=None,
  784. )
  785. module.create_archive(
  786. dry_run=False,
  787. repository_path='repo',
  788. location_config={
  789. 'source_directories': ['foo', 'bar'],
  790. 'repositories': ['repo'],
  791. 'exclude_patterns': None,
  792. },
  793. storage_config={'checkpoint_interval': 600},
  794. local_borg_version='1.2.3',
  795. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  796. )
  797. def test_create_archive_with_checkpoint_volume_calls_borg_with_checkpoint_volume_parameters():
  798. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  799. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  800. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  801. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  802. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  803. flexmock(module).should_receive('expand_directories').and_return(())
  804. flexmock(module).should_receive('pattern_root_directories').and_return([])
  805. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  806. flexmock(module).should_receive('expand_home_directories').and_return(())
  807. flexmock(module).should_receive('write_pattern_file').and_return(None)
  808. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  809. flexmock(module.feature).should_receive('available').and_return(True)
  810. flexmock(module).should_receive('ensure_files_readable')
  811. flexmock(module).should_receive('make_pattern_flags').and_return(())
  812. flexmock(module).should_receive('make_exclude_flags').and_return(())
  813. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  814. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  815. )
  816. flexmock(module.environment).should_receive('make_environment')
  817. flexmock(module).should_receive('execute_command').with_args(
  818. ('borg', 'create', '--checkpoint-volume', '1024') + REPO_ARCHIVE_WITH_PATHS,
  819. output_log_level=logging.INFO,
  820. output_file=None,
  821. borg_local_path='borg',
  822. working_directory=None,
  823. extra_environment=None,
  824. )
  825. module.create_archive(
  826. dry_run=False,
  827. repository_path='repo',
  828. location_config={
  829. 'source_directories': ['foo', 'bar'],
  830. 'repositories': ['repo'],
  831. 'exclude_patterns': None,
  832. },
  833. storage_config={'checkpoint_volume': 1024},
  834. local_borg_version='1.2.3',
  835. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  836. )
  837. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  838. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  839. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  840. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  841. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  842. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  843. flexmock(module).should_receive('expand_directories').and_return(())
  844. flexmock(module).should_receive('pattern_root_directories').and_return([])
  845. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  846. flexmock(module).should_receive('expand_home_directories').and_return(())
  847. flexmock(module).should_receive('write_pattern_file').and_return(None)
  848. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  849. flexmock(module.feature).should_receive('available').and_return(True)
  850. flexmock(module).should_receive('ensure_files_readable')
  851. flexmock(module).should_receive('make_pattern_flags').and_return(())
  852. flexmock(module).should_receive('make_exclude_flags').and_return(())
  853. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  854. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  855. )
  856. flexmock(module.environment).should_receive('make_environment')
  857. flexmock(module).should_receive('execute_command').with_args(
  858. ('borg', 'create', '--chunker-params', '1,2,3,4') + REPO_ARCHIVE_WITH_PATHS,
  859. output_log_level=logging.INFO,
  860. output_file=None,
  861. borg_local_path='borg',
  862. working_directory=None,
  863. extra_environment=None,
  864. )
  865. module.create_archive(
  866. dry_run=False,
  867. repository_path='repo',
  868. location_config={
  869. 'source_directories': ['foo', 'bar'],
  870. 'repositories': ['repo'],
  871. 'exclude_patterns': None,
  872. },
  873. storage_config={'chunker_params': '1,2,3,4'},
  874. local_borg_version='1.2.3',
  875. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  876. )
  877. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  878. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  879. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  880. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  881. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  882. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  883. flexmock(module).should_receive('expand_directories').and_return(())
  884. flexmock(module).should_receive('pattern_root_directories').and_return([])
  885. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  886. flexmock(module).should_receive('expand_home_directories').and_return(())
  887. flexmock(module).should_receive('write_pattern_file').and_return(None)
  888. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  889. flexmock(module.feature).should_receive('available').and_return(True)
  890. flexmock(module).should_receive('ensure_files_readable')
  891. flexmock(module).should_receive('make_pattern_flags').and_return(())
  892. flexmock(module).should_receive('make_exclude_flags').and_return(())
  893. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  894. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  895. )
  896. flexmock(module.environment).should_receive('make_environment')
  897. flexmock(module).should_receive('execute_command').with_args(
  898. ('borg', 'create', '--compression', 'rle') + REPO_ARCHIVE_WITH_PATHS,
  899. output_log_level=logging.INFO,
  900. output_file=None,
  901. borg_local_path='borg',
  902. working_directory=None,
  903. extra_environment=None,
  904. )
  905. module.create_archive(
  906. dry_run=False,
  907. repository_path='repo',
  908. location_config={
  909. 'source_directories': ['foo', 'bar'],
  910. 'repositories': ['repo'],
  911. 'exclude_patterns': None,
  912. },
  913. storage_config={'compression': 'rle'},
  914. local_borg_version='1.2.3',
  915. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  916. )
  917. @pytest.mark.parametrize(
  918. 'feature_available,option_flag',
  919. ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  920. )
  921. def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  922. feature_available, option_flag
  923. ):
  924. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  925. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  926. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  927. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  928. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  929. flexmock(module).should_receive('expand_directories').and_return(())
  930. flexmock(module).should_receive('pattern_root_directories').and_return([])
  931. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  932. flexmock(module).should_receive('expand_home_directories').and_return(())
  933. flexmock(module).should_receive('write_pattern_file').and_return(None)
  934. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  935. flexmock(module.feature).should_receive('available').and_return(feature_available)
  936. flexmock(module).should_receive('ensure_files_readable')
  937. flexmock(module).should_receive('make_pattern_flags').and_return(())
  938. flexmock(module).should_receive('make_exclude_flags').and_return(())
  939. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  940. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  941. )
  942. flexmock(module.environment).should_receive('make_environment')
  943. flexmock(module).should_receive('execute_command').with_args(
  944. ('borg', 'create', option_flag, '100') + REPO_ARCHIVE_WITH_PATHS,
  945. output_log_level=logging.INFO,
  946. output_file=None,
  947. borg_local_path='borg',
  948. working_directory=None,
  949. extra_environment=None,
  950. )
  951. module.create_archive(
  952. dry_run=False,
  953. repository_path='repo',
  954. location_config={
  955. 'source_directories': ['foo', 'bar'],
  956. 'repositories': ['repo'],
  957. 'exclude_patterns': None,
  958. },
  959. storage_config={'upload_rate_limit': 100},
  960. local_borg_version='1.2.3',
  961. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  962. )
  963. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  964. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  965. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  966. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  967. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  968. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  969. flexmock(module).should_receive('expand_directories').and_return(())
  970. flexmock(module).should_receive('pattern_root_directories').and_return([])
  971. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  972. '/working/dir'
  973. )
  974. flexmock(module).should_receive('expand_home_directories').and_return(())
  975. flexmock(module).should_receive('write_pattern_file').and_return(None)
  976. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  977. flexmock(module.feature).should_receive('available').and_return(True)
  978. flexmock(module).should_receive('ensure_files_readable')
  979. flexmock(module).should_receive('make_pattern_flags').and_return(())
  980. flexmock(module).should_receive('make_exclude_flags').and_return(())
  981. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  982. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  983. )
  984. flexmock(module.environment).should_receive('make_environment')
  985. flexmock(module).should_receive('execute_command').with_args(
  986. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  987. output_log_level=logging.INFO,
  988. output_file=None,
  989. borg_local_path='borg',
  990. working_directory='/working/dir',
  991. extra_environment=None,
  992. )
  993. module.create_archive(
  994. dry_run=False,
  995. repository_path='repo',
  996. location_config={
  997. 'source_directories': ['foo', 'bar'],
  998. 'repositories': ['repo'],
  999. 'working_directory': '/working/dir',
  1000. 'exclude_patterns': None,
  1001. },
  1002. storage_config={},
  1003. local_borg_version='1.2.3',
  1004. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1005. )
  1006. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  1007. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1008. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1009. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1010. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1011. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1012. flexmock(module).should_receive('expand_directories').and_return(())
  1013. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1014. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1015. flexmock(module).should_receive('expand_home_directories').and_return(())
  1016. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1017. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1018. flexmock(module.feature).should_receive('available').and_return(True)
  1019. flexmock(module).should_receive('ensure_files_readable')
  1020. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1021. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1022. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1023. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1024. )
  1025. flexmock(module.environment).should_receive('make_environment')
  1026. flexmock(module).should_receive('execute_command').with_args(
  1027. ('borg', 'create', '--one-file-system') + REPO_ARCHIVE_WITH_PATHS,
  1028. output_log_level=logging.INFO,
  1029. output_file=None,
  1030. borg_local_path='borg',
  1031. working_directory=None,
  1032. extra_environment=None,
  1033. )
  1034. module.create_archive(
  1035. dry_run=False,
  1036. repository_path='repo',
  1037. location_config={
  1038. 'source_directories': ['foo', 'bar'],
  1039. 'repositories': ['repo'],
  1040. 'one_file_system': True,
  1041. 'exclude_patterns': None,
  1042. },
  1043. storage_config={},
  1044. local_borg_version='1.2.3',
  1045. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1046. )
  1047. @pytest.mark.parametrize(
  1048. 'feature_available,option_flag',
  1049. ((True, '--numeric-ids'), (False, '--numeric-owner')),
  1050. )
  1051. def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
  1052. feature_available, option_flag
  1053. ):
  1054. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1055. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1056. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1057. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1058. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1059. flexmock(module).should_receive('expand_directories').and_return(())
  1060. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1061. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1062. flexmock(module).should_receive('expand_home_directories').and_return(())
  1063. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1064. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1065. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1066. flexmock(module).should_receive('ensure_files_readable')
  1067. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1068. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1069. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1070. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1071. )
  1072. flexmock(module.environment).should_receive('make_environment')
  1073. flexmock(module).should_receive('execute_command').with_args(
  1074. ('borg', 'create', option_flag) + REPO_ARCHIVE_WITH_PATHS,
  1075. output_log_level=logging.INFO,
  1076. output_file=None,
  1077. borg_local_path='borg',
  1078. working_directory=None,
  1079. extra_environment=None,
  1080. )
  1081. module.create_archive(
  1082. dry_run=False,
  1083. repository_path='repo',
  1084. location_config={
  1085. 'source_directories': ['foo', 'bar'],
  1086. 'repositories': ['repo'],
  1087. 'numeric_ids': True,
  1088. 'exclude_patterns': None,
  1089. },
  1090. storage_config={},
  1091. local_borg_version='1.2.3',
  1092. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1093. )
  1094. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  1095. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1096. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1097. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1098. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1099. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1100. flexmock(module).should_receive('expand_directories').and_return(())
  1101. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1102. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1103. flexmock(module).should_receive('expand_home_directories').and_return(())
  1104. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1105. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1106. flexmock(module.feature).should_receive('available').and_return(True)
  1107. flexmock(module).should_receive('ensure_files_readable')
  1108. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1109. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1110. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1111. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1112. )
  1113. flexmock(module.environment).should_receive('make_environment')
  1114. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1115. create_command = ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS
  1116. flexmock(module).should_receive('execute_command').with_args(
  1117. create_command + ('--dry-run', '--list'),
  1118. output_log_level=logging.INFO,
  1119. output_file=None,
  1120. borg_local_path='borg',
  1121. working_directory=None,
  1122. extra_environment=None,
  1123. )
  1124. flexmock(module).should_receive('execute_command').with_args(
  1125. create_command,
  1126. output_log_level=logging.INFO,
  1127. output_file=None,
  1128. borg_local_path='borg',
  1129. working_directory=None,
  1130. extra_environment=None,
  1131. )
  1132. module.create_archive(
  1133. dry_run=False,
  1134. repository_path='repo',
  1135. location_config={
  1136. 'source_directories': ['foo', 'bar'],
  1137. 'repositories': ['repo'],
  1138. 'read_special': True,
  1139. 'exclude_patterns': None,
  1140. },
  1141. storage_config={},
  1142. local_borg_version='1.2.3',
  1143. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1144. )
  1145. @pytest.mark.parametrize(
  1146. 'option_name,option_value',
  1147. (
  1148. ('ctime', True),
  1149. ('ctime', False),
  1150. ('birthtime', True),
  1151. ('birthtime', False),
  1152. ),
  1153. )
  1154. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  1155. option_name, option_value
  1156. ):
  1157. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1158. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1159. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  1160. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1161. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1162. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1163. flexmock(module).should_receive('expand_directories').and_return(())
  1164. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1165. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1166. flexmock(module).should_receive('expand_home_directories').and_return(())
  1167. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1168. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1169. flexmock(module.feature).should_receive('available').and_return(True)
  1170. flexmock(module).should_receive('ensure_files_readable')
  1171. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1172. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1173. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1174. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1175. )
  1176. flexmock(module.environment).should_receive('make_environment')
  1177. flexmock(module).should_receive('execute_command').with_args(
  1178. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1179. output_log_level=logging.INFO,
  1180. output_file=None,
  1181. borg_local_path='borg',
  1182. working_directory=None,
  1183. extra_environment=None,
  1184. )
  1185. module.create_archive(
  1186. dry_run=False,
  1187. repository_path='repo',
  1188. location_config={
  1189. 'source_directories': ['foo', 'bar'],
  1190. 'repositories': ['repo'],
  1191. option_name: option_value,
  1192. 'exclude_patterns': None,
  1193. },
  1194. storage_config={},
  1195. local_borg_version='1.2.3',
  1196. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1197. )
  1198. @pytest.mark.parametrize(
  1199. 'option_value,feature_available,option_flag',
  1200. (
  1201. (True, True, '--atime'),
  1202. (True, False, None),
  1203. (False, True, None),
  1204. (False, False, '--noatime'),
  1205. ),
  1206. )
  1207. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  1208. option_value, feature_available, option_flag
  1209. ):
  1210. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1211. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1212. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1213. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1214. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1215. flexmock(module).should_receive('expand_directories').and_return(())
  1216. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1217. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1218. flexmock(module).should_receive('expand_home_directories').and_return(())
  1219. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1220. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1221. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1222. flexmock(module).should_receive('ensure_files_readable')
  1223. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1224. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1225. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1226. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1227. )
  1228. flexmock(module.environment).should_receive('make_environment')
  1229. flexmock(module).should_receive('execute_command').with_args(
  1230. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1231. output_log_level=logging.INFO,
  1232. output_file=None,
  1233. borg_local_path='borg',
  1234. working_directory=None,
  1235. extra_environment=None,
  1236. )
  1237. module.create_archive(
  1238. dry_run=False,
  1239. repository_path='repo',
  1240. location_config={
  1241. 'source_directories': ['foo', 'bar'],
  1242. 'repositories': ['repo'],
  1243. 'atime': option_value,
  1244. 'exclude_patterns': None,
  1245. },
  1246. storage_config={},
  1247. local_borg_version='1.2.3',
  1248. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1249. )
  1250. @pytest.mark.parametrize(
  1251. 'option_value,feature_available,option_flag',
  1252. (
  1253. (True, True, None),
  1254. (True, False, None),
  1255. (False, True, '--noflags'),
  1256. (False, False, '--nobsdflags'),
  1257. ),
  1258. )
  1259. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  1260. option_value, feature_available, option_flag
  1261. ):
  1262. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1263. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1264. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1265. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1266. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1267. flexmock(module).should_receive('expand_directories').and_return(())
  1268. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1269. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1270. flexmock(module).should_receive('expand_home_directories').and_return(())
  1271. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1272. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1273. flexmock(module.feature).should_receive('available').and_return(feature_available)
  1274. flexmock(module).should_receive('ensure_files_readable')
  1275. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1276. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1277. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1278. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1279. )
  1280. flexmock(module.environment).should_receive('make_environment')
  1281. flexmock(module).should_receive('execute_command').with_args(
  1282. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  1283. output_log_level=logging.INFO,
  1284. output_file=None,
  1285. borg_local_path='borg',
  1286. working_directory=None,
  1287. extra_environment=None,
  1288. )
  1289. module.create_archive(
  1290. dry_run=False,
  1291. repository_path='repo',
  1292. location_config={
  1293. 'source_directories': ['foo', 'bar'],
  1294. 'repositories': ['repo'],
  1295. 'flags': option_value,
  1296. 'exclude_patterns': None,
  1297. },
  1298. storage_config={},
  1299. local_borg_version='1.2.3',
  1300. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1301. )
  1302. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  1303. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1304. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1305. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1306. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1307. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1308. flexmock(module).should_receive('expand_directories').and_return(())
  1309. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1310. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1311. flexmock(module).should_receive('expand_home_directories').and_return(())
  1312. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1313. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1314. flexmock(module.feature).should_receive('available').and_return(True)
  1315. flexmock(module).should_receive('ensure_files_readable')
  1316. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1317. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1318. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1319. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1320. )
  1321. flexmock(module.environment).should_receive('make_environment')
  1322. flexmock(module).should_receive('execute_command').with_args(
  1323. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  1324. output_log_level=logging.INFO,
  1325. output_file=None,
  1326. borg_local_path='borg',
  1327. working_directory=None,
  1328. extra_environment=None,
  1329. )
  1330. module.create_archive(
  1331. dry_run=False,
  1332. repository_path='repo',
  1333. location_config={
  1334. 'source_directories': ['foo', 'bar'],
  1335. 'repositories': ['repo'],
  1336. 'files_cache': 'ctime,size',
  1337. 'exclude_patterns': None,
  1338. },
  1339. storage_config={},
  1340. local_borg_version='1.2.3',
  1341. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1342. )
  1343. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1344. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1345. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1346. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1347. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1348. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1349. flexmock(module).should_receive('expand_directories').and_return(())
  1350. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1351. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1352. flexmock(module).should_receive('expand_home_directories').and_return(())
  1353. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1354. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1355. flexmock(module.feature).should_receive('available').and_return(True)
  1356. flexmock(module).should_receive('ensure_files_readable')
  1357. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1358. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1359. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1360. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1361. )
  1362. flexmock(module.environment).should_receive('make_environment')
  1363. flexmock(module).should_receive('execute_command').with_args(
  1364. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1365. output_log_level=logging.INFO,
  1366. output_file=None,
  1367. borg_local_path='borg1',
  1368. working_directory=None,
  1369. extra_environment=None,
  1370. )
  1371. module.create_archive(
  1372. dry_run=False,
  1373. repository_path='repo',
  1374. location_config={
  1375. 'source_directories': ['foo', 'bar'],
  1376. 'repositories': ['repo'],
  1377. 'exclude_patterns': None,
  1378. },
  1379. storage_config={},
  1380. local_borg_version='1.2.3',
  1381. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1382. local_path='borg1',
  1383. )
  1384. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1385. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1386. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1387. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1388. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1389. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1390. flexmock(module).should_receive('expand_directories').and_return(())
  1391. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1392. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1393. flexmock(module).should_receive('expand_home_directories').and_return(())
  1394. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1395. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1396. flexmock(module.feature).should_receive('available').and_return(True)
  1397. flexmock(module).should_receive('ensure_files_readable')
  1398. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1399. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1400. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1401. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1402. )
  1403. flexmock(module.environment).should_receive('make_environment')
  1404. flexmock(module).should_receive('execute_command').with_args(
  1405. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1406. output_log_level=logging.INFO,
  1407. output_file=None,
  1408. borg_local_path='borg',
  1409. working_directory=None,
  1410. extra_environment=None,
  1411. )
  1412. module.create_archive(
  1413. dry_run=False,
  1414. repository_path='repo',
  1415. location_config={
  1416. 'source_directories': ['foo', 'bar'],
  1417. 'repositories': ['repo'],
  1418. 'exclude_patterns': None,
  1419. },
  1420. storage_config={},
  1421. local_borg_version='1.2.3',
  1422. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1423. remote_path='borg1',
  1424. )
  1425. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1426. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1427. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1428. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1429. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1430. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1431. flexmock(module).should_receive('expand_directories').and_return(())
  1432. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1433. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1434. flexmock(module).should_receive('expand_home_directories').and_return(())
  1435. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1436. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1437. flexmock(module.feature).should_receive('available').and_return(True)
  1438. flexmock(module).should_receive('ensure_files_readable')
  1439. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1440. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1441. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1442. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1443. )
  1444. flexmock(module.environment).should_receive('make_environment')
  1445. flexmock(module).should_receive('execute_command').with_args(
  1446. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1447. output_log_level=logging.INFO,
  1448. output_file=None,
  1449. borg_local_path='borg',
  1450. working_directory=None,
  1451. extra_environment=None,
  1452. )
  1453. module.create_archive(
  1454. dry_run=False,
  1455. repository_path='repo',
  1456. location_config={
  1457. 'source_directories': ['foo', 'bar'],
  1458. 'repositories': ['repo'],
  1459. 'exclude_patterns': None,
  1460. },
  1461. storage_config={'umask': 740},
  1462. local_borg_version='1.2.3',
  1463. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1464. )
  1465. def test_create_archive_with_log_json_calls_borg_with_log_json_parameters():
  1466. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1467. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1468. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1469. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1470. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1471. flexmock(module).should_receive('expand_directories').and_return(())
  1472. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1473. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1474. flexmock(module).should_receive('expand_home_directories').and_return(())
  1475. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1476. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1477. flexmock(module.feature).should_receive('available').and_return(True)
  1478. flexmock(module).should_receive('ensure_files_readable')
  1479. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1480. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1481. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1482. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1483. )
  1484. flexmock(module.environment).should_receive('make_environment')
  1485. flexmock(module).should_receive('execute_command').with_args(
  1486. ('borg', 'create', '--log-json') + REPO_ARCHIVE_WITH_PATHS,
  1487. output_log_level=logging.INFO,
  1488. output_file=None,
  1489. borg_local_path='borg',
  1490. working_directory=None,
  1491. extra_environment=None,
  1492. )
  1493. module.create_archive(
  1494. dry_run=False,
  1495. repository_path='repo',
  1496. location_config={
  1497. 'source_directories': ['foo', 'bar'],
  1498. 'repositories': ['repo'],
  1499. 'exclude_patterns': None,
  1500. },
  1501. storage_config={},
  1502. local_borg_version='1.2.3',
  1503. global_arguments=flexmock(log_json=True, used_config_paths=[]),
  1504. )
  1505. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1506. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1507. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1508. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1509. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1510. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1511. flexmock(module).should_receive('expand_directories').and_return(())
  1512. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1513. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1514. flexmock(module).should_receive('expand_home_directories').and_return(())
  1515. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1516. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1517. flexmock(module.feature).should_receive('available').and_return(True)
  1518. flexmock(module).should_receive('ensure_files_readable')
  1519. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1520. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1521. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1522. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1523. )
  1524. flexmock(module.environment).should_receive('make_environment')
  1525. flexmock(module).should_receive('execute_command').with_args(
  1526. ('borg', 'create', '--lock-wait', '5') + REPO_ARCHIVE_WITH_PATHS,
  1527. output_log_level=logging.INFO,
  1528. output_file=None,
  1529. borg_local_path='borg',
  1530. working_directory=None,
  1531. extra_environment=None,
  1532. )
  1533. module.create_archive(
  1534. dry_run=False,
  1535. repository_path='repo',
  1536. location_config={
  1537. 'source_directories': ['foo', 'bar'],
  1538. 'repositories': ['repo'],
  1539. 'exclude_patterns': None,
  1540. },
  1541. storage_config={'lock_wait': 5},
  1542. local_borg_version='1.2.3',
  1543. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1544. )
  1545. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_answer_output_log_level():
  1546. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1547. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1548. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1549. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1550. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1551. flexmock(module).should_receive('expand_directories').and_return(())
  1552. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1553. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1554. flexmock(module).should_receive('expand_home_directories').and_return(())
  1555. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1556. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1557. flexmock(module.feature).should_receive('available').and_return(True)
  1558. flexmock(module).should_receive('ensure_files_readable')
  1559. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1560. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1561. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1562. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1563. )
  1564. flexmock(module.environment).should_receive('make_environment')
  1565. flexmock(module).should_receive('execute_command').with_args(
  1566. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--stats',),
  1567. output_log_level=module.borgmatic.logger.ANSWER,
  1568. output_file=None,
  1569. borg_local_path='borg',
  1570. working_directory=None,
  1571. extra_environment=None,
  1572. )
  1573. module.create_archive(
  1574. dry_run=False,
  1575. repository_path='repo',
  1576. location_config={
  1577. 'source_directories': ['foo', 'bar'],
  1578. 'repositories': ['repo'],
  1579. 'exclude_patterns': None,
  1580. },
  1581. storage_config={},
  1582. local_borg_version='1.2.3',
  1583. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1584. stats=True,
  1585. )
  1586. def test_create_archive_with_files_calls_borg_with_list_parameter_and_answer_output_log_level():
  1587. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1588. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1589. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1590. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1591. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1592. flexmock(module).should_receive('expand_directories').and_return(())
  1593. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1594. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1595. flexmock(module).should_receive('expand_home_directories').and_return(())
  1596. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1597. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1598. flexmock(module.feature).should_receive('available').and_return(True)
  1599. flexmock(module).should_receive('ensure_files_readable')
  1600. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1601. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1602. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1603. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1604. )
  1605. flexmock(module.environment).should_receive('make_environment')
  1606. flexmock(module).should_receive('execute_command').with_args(
  1607. ('borg', 'create', '--list', '--filter', 'FOO') + REPO_ARCHIVE_WITH_PATHS,
  1608. output_log_level=module.borgmatic.logger.ANSWER,
  1609. output_file=None,
  1610. borg_local_path='borg',
  1611. working_directory=None,
  1612. extra_environment=None,
  1613. )
  1614. module.create_archive(
  1615. dry_run=False,
  1616. repository_path='repo',
  1617. location_config={
  1618. 'source_directories': ['foo', 'bar'],
  1619. 'repositories': ['repo'],
  1620. 'exclude_patterns': None,
  1621. },
  1622. storage_config={},
  1623. local_borg_version='1.2.3',
  1624. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1625. list_files=True,
  1626. )
  1627. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1628. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1629. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1630. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1631. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1632. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1633. flexmock(module).should_receive('expand_directories').and_return(())
  1634. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1635. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1636. flexmock(module).should_receive('expand_home_directories').and_return(())
  1637. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1638. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1639. flexmock(module.feature).should_receive('available').and_return(True)
  1640. flexmock(module).should_receive('ensure_files_readable')
  1641. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1642. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1643. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1644. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1645. )
  1646. flexmock(module.environment).should_receive('make_environment')
  1647. flexmock(module).should_receive('execute_command').with_args(
  1648. ('borg', 'create')
  1649. + REPO_ARCHIVE_WITH_PATHS
  1650. + (
  1651. '--info',
  1652. '--progress',
  1653. ),
  1654. output_log_level=logging.INFO,
  1655. output_file=module.DO_NOT_CAPTURE,
  1656. borg_local_path='borg',
  1657. working_directory=None,
  1658. extra_environment=None,
  1659. )
  1660. insert_logging_mock(logging.INFO)
  1661. module.create_archive(
  1662. dry_run=False,
  1663. repository_path='repo',
  1664. location_config={
  1665. 'source_directories': ['foo', 'bar'],
  1666. 'repositories': ['repo'],
  1667. 'exclude_patterns': None,
  1668. },
  1669. storage_config={},
  1670. local_borg_version='1.2.3',
  1671. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1672. progress=True,
  1673. )
  1674. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1675. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1676. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1677. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1678. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1679. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1680. flexmock(module).should_receive('expand_directories').and_return(())
  1681. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1682. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1683. flexmock(module).should_receive('expand_home_directories').and_return(())
  1684. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1685. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1686. flexmock(module.feature).should_receive('available').and_return(True)
  1687. flexmock(module).should_receive('ensure_files_readable')
  1688. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1689. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1690. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1691. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1692. )
  1693. flexmock(module.environment).should_receive('make_environment')
  1694. flexmock(module).should_receive('execute_command').with_args(
  1695. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--progress',),
  1696. output_log_level=logging.INFO,
  1697. output_file=module.DO_NOT_CAPTURE,
  1698. borg_local_path='borg',
  1699. working_directory=None,
  1700. extra_environment=None,
  1701. )
  1702. module.create_archive(
  1703. dry_run=False,
  1704. repository_path='repo',
  1705. location_config={
  1706. 'source_directories': ['foo', 'bar'],
  1707. 'repositories': ['repo'],
  1708. 'exclude_patterns': None,
  1709. },
  1710. storage_config={},
  1711. local_borg_version='1.2.3',
  1712. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1713. progress=True,
  1714. )
  1715. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1716. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1717. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1718. processes = flexmock()
  1719. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1720. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1721. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1722. flexmock(module).should_receive('expand_directories').and_return(())
  1723. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1724. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1725. flexmock(module).should_receive('expand_home_directories').and_return(())
  1726. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1727. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1728. flexmock(module.feature).should_receive('available').and_return(True)
  1729. flexmock(module).should_receive('ensure_files_readable')
  1730. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1731. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1732. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1733. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1734. )
  1735. flexmock(module.environment).should_receive('make_environment')
  1736. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  1737. create_command = (
  1738. ('borg', 'create', '--one-file-system', '--read-special')
  1739. + REPO_ARCHIVE_WITH_PATHS
  1740. + ('--progress',)
  1741. )
  1742. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1743. create_command + ('--dry-run', '--list'),
  1744. processes=processes,
  1745. output_log_level=logging.INFO,
  1746. output_file=module.DO_NOT_CAPTURE,
  1747. borg_local_path='borg',
  1748. working_directory=None,
  1749. extra_environment=None,
  1750. )
  1751. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1752. create_command,
  1753. processes=processes,
  1754. output_log_level=logging.INFO,
  1755. output_file=module.DO_NOT_CAPTURE,
  1756. borg_local_path='borg',
  1757. working_directory=None,
  1758. extra_environment=None,
  1759. )
  1760. module.create_archive(
  1761. dry_run=False,
  1762. repository_path='repo',
  1763. location_config={
  1764. 'source_directories': ['foo', 'bar'],
  1765. 'repositories': ['repo'],
  1766. 'exclude_patterns': None,
  1767. },
  1768. storage_config={},
  1769. local_borg_version='1.2.3',
  1770. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1771. progress=True,
  1772. stream_processes=processes,
  1773. )
  1774. def test_create_archive_with_stream_processes_ignores_read_special_false_and_logs_warnings():
  1775. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1776. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1777. processes = flexmock()
  1778. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1779. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1780. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1781. flexmock(module).should_receive('expand_directories').and_return(())
  1782. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1783. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1784. flexmock(module).should_receive('expand_home_directories').and_return(())
  1785. flexmock(module).should_receive('write_pattern_file').and_return(flexmock(name='/tmp/excludes'))
  1786. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1787. flexmock(module.feature).should_receive('available').and_return(True)
  1788. flexmock(module).should_receive('ensure_files_readable')
  1789. flexmock(module.logger).should_receive('warning').twice()
  1790. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1791. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1792. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1793. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1794. )
  1795. flexmock(module.environment).should_receive('make_environment')
  1796. flexmock(module).should_receive('collect_special_file_paths').and_return(('/dev/null',))
  1797. create_command = (
  1798. 'borg',
  1799. 'create',
  1800. '--one-file-system',
  1801. '--read-special',
  1802. ) + REPO_ARCHIVE_WITH_PATHS
  1803. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1804. create_command + ('--dry-run', '--list'),
  1805. processes=processes,
  1806. output_log_level=logging.INFO,
  1807. output_file=None,
  1808. borg_local_path='borg',
  1809. working_directory=None,
  1810. extra_environment=None,
  1811. )
  1812. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1813. create_command,
  1814. processes=processes,
  1815. output_log_level=logging.INFO,
  1816. output_file=None,
  1817. borg_local_path='borg',
  1818. working_directory=None,
  1819. extra_environment=None,
  1820. )
  1821. module.create_archive(
  1822. dry_run=False,
  1823. repository_path='repo',
  1824. location_config={
  1825. 'source_directories': ['foo', 'bar'],
  1826. 'repositories': ['repo'],
  1827. 'exclude_patterns': None,
  1828. 'read_special': False,
  1829. },
  1830. storage_config={},
  1831. local_borg_version='1.2.3',
  1832. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1833. stream_processes=processes,
  1834. )
  1835. def test_create_archive_with_stream_processes_adds_special_files_to_excludes():
  1836. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1837. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1838. processes = flexmock()
  1839. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1840. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1841. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1842. flexmock(module).should_receive('expand_directories').and_return(())
  1843. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1844. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1845. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  1846. ('special',)
  1847. )
  1848. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  1849. flexmock(name='/excludes')
  1850. )
  1851. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1852. flexmock(module.feature).should_receive('available').and_return(True)
  1853. flexmock(module).should_receive('ensure_files_readable')
  1854. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1855. flexmock(module).should_receive('make_exclude_flags').and_return(()).and_return(
  1856. '--exclude-from', '/excludes'
  1857. )
  1858. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1859. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1860. )
  1861. flexmock(module.environment).should_receive('make_environment')
  1862. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  1863. create_command = (
  1864. 'borg',
  1865. 'create',
  1866. '--one-file-system',
  1867. '--read-special',
  1868. ) + REPO_ARCHIVE_WITH_PATHS
  1869. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1870. create_command + ('--dry-run', '--list'),
  1871. processes=processes,
  1872. output_log_level=logging.INFO,
  1873. output_file=None,
  1874. borg_local_path='borg',
  1875. working_directory=None,
  1876. extra_environment=None,
  1877. )
  1878. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1879. create_command + ('--exclude-from', '/excludes'),
  1880. processes=processes,
  1881. output_log_level=logging.INFO,
  1882. output_file=None,
  1883. borg_local_path='borg',
  1884. working_directory=None,
  1885. extra_environment=None,
  1886. )
  1887. module.create_archive(
  1888. dry_run=False,
  1889. repository_path='repo',
  1890. location_config={
  1891. 'source_directories': ['foo', 'bar'],
  1892. 'repositories': ['repo'],
  1893. 'exclude_patterns': None,
  1894. },
  1895. storage_config={},
  1896. local_borg_version='1.2.3',
  1897. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1898. stream_processes=processes,
  1899. )
  1900. def test_create_archive_with_stream_processes_and_read_special_does_not_add_special_files_to_excludes():
  1901. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1902. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1903. processes = flexmock()
  1904. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1905. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1906. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1907. flexmock(module).should_receive('expand_directories').and_return(())
  1908. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1909. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1910. flexmock(module).should_receive('expand_home_directories').and_return(()).and_return(
  1911. ('special',)
  1912. )
  1913. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1914. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1915. flexmock(module.feature).should_receive('available').and_return(True)
  1916. flexmock(module).should_receive('ensure_files_readable')
  1917. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1918. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1919. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1920. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1921. )
  1922. flexmock(module.environment).should_receive('make_environment')
  1923. flexmock(module).should_receive('collect_special_file_paths').and_return(('special',))
  1924. create_command = (
  1925. 'borg',
  1926. 'create',
  1927. '--one-file-system',
  1928. '--read-special',
  1929. ) + REPO_ARCHIVE_WITH_PATHS
  1930. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1931. create_command + ('--dry-run', '--list'),
  1932. processes=processes,
  1933. output_log_level=logging.INFO,
  1934. output_file=None,
  1935. borg_local_path='borg',
  1936. working_directory=None,
  1937. extra_environment=None,
  1938. )
  1939. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1940. create_command,
  1941. processes=processes,
  1942. output_log_level=logging.INFO,
  1943. output_file=None,
  1944. borg_local_path='borg',
  1945. working_directory=None,
  1946. extra_environment=None,
  1947. )
  1948. module.create_archive(
  1949. dry_run=False,
  1950. repository_path='repo',
  1951. location_config={
  1952. 'source_directories': ['foo', 'bar'],
  1953. 'repositories': ['repo'],
  1954. 'exclude_patterns': None,
  1955. 'read_special': True,
  1956. },
  1957. storage_config={},
  1958. local_borg_version='1.2.3',
  1959. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1960. stream_processes=processes,
  1961. )
  1962. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1963. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  1964. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  1965. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  1966. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1967. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1968. flexmock(module).should_receive('expand_directories').and_return(())
  1969. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1970. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1971. flexmock(module).should_receive('expand_home_directories').and_return(())
  1972. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1973. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  1974. flexmock(module.feature).should_receive('available').and_return(True)
  1975. flexmock(module).should_receive('ensure_files_readable')
  1976. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1977. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1978. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1979. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1980. )
  1981. flexmock(module.environment).should_receive('make_environment')
  1982. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  1983. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  1984. working_directory=None,
  1985. extra_environment=None,
  1986. ).and_return('[]')
  1987. json_output = module.create_archive(
  1988. dry_run=False,
  1989. repository_path='repo',
  1990. location_config={
  1991. 'source_directories': ['foo', 'bar'],
  1992. 'repositories': ['repo'],
  1993. 'exclude_patterns': None,
  1994. },
  1995. storage_config={},
  1996. local_borg_version='1.2.3',
  1997. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  1998. json=True,
  1999. )
  2000. assert json_output == '[]'
  2001. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  2002. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2003. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2004. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2005. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2006. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2007. flexmock(module).should_receive('expand_directories').and_return(())
  2008. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2009. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2010. flexmock(module).should_receive('expand_home_directories').and_return(())
  2011. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2012. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2013. flexmock(module.feature).should_receive('available').and_return(True)
  2014. flexmock(module).should_receive('ensure_files_readable')
  2015. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2016. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2017. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2018. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2019. )
  2020. flexmock(module.environment).should_receive('make_environment')
  2021. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  2022. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS + ('--json',),
  2023. working_directory=None,
  2024. extra_environment=None,
  2025. ).and_return('[]')
  2026. json_output = module.create_archive(
  2027. dry_run=False,
  2028. repository_path='repo',
  2029. location_config={
  2030. 'source_directories': ['foo', 'bar'],
  2031. 'repositories': ['repo'],
  2032. 'exclude_patterns': None,
  2033. },
  2034. storage_config={},
  2035. local_borg_version='1.2.3',
  2036. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2037. json=True,
  2038. stats=True,
  2039. )
  2040. assert json_output == '[]'
  2041. def test_create_archive_with_source_directories_glob_expands():
  2042. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2043. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2044. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2045. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  2046. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2047. flexmock(module).should_receive('expand_directories').and_return(())
  2048. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2049. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2050. flexmock(module).should_receive('expand_home_directories').and_return(())
  2051. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2052. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2053. flexmock(module.feature).should_receive('available').and_return(True)
  2054. flexmock(module).should_receive('ensure_files_readable')
  2055. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2056. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2057. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2058. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2059. )
  2060. flexmock(module.environment).should_receive('make_environment')
  2061. flexmock(module).should_receive('execute_command').with_args(
  2062. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food'),
  2063. output_log_level=logging.INFO,
  2064. output_file=None,
  2065. borg_local_path='borg',
  2066. working_directory=None,
  2067. extra_environment=None,
  2068. )
  2069. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  2070. module.create_archive(
  2071. dry_run=False,
  2072. repository_path='repo',
  2073. location_config={
  2074. 'source_directories': ['foo*'],
  2075. 'repositories': ['repo'],
  2076. 'exclude_patterns': None,
  2077. },
  2078. storage_config={},
  2079. local_borg_version='1.2.3',
  2080. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2081. )
  2082. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  2083. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2084. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2085. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2086. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  2087. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2088. flexmock(module).should_receive('expand_directories').and_return(())
  2089. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2090. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2091. flexmock(module).should_receive('expand_home_directories').and_return(())
  2092. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2093. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2094. flexmock(module.feature).should_receive('available').and_return(True)
  2095. flexmock(module).should_receive('ensure_files_readable')
  2096. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2097. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2098. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2099. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2100. )
  2101. flexmock(module.environment).should_receive('make_environment')
  2102. flexmock(module).should_receive('execute_command').with_args(
  2103. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo*'),
  2104. output_log_level=logging.INFO,
  2105. output_file=None,
  2106. borg_local_path='borg',
  2107. working_directory=None,
  2108. extra_environment=None,
  2109. )
  2110. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  2111. module.create_archive(
  2112. dry_run=False,
  2113. repository_path='repo',
  2114. location_config={
  2115. 'source_directories': ['foo*'],
  2116. 'repositories': ['repo'],
  2117. 'exclude_patterns': None,
  2118. },
  2119. storage_config={},
  2120. local_borg_version='1.2.3',
  2121. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2122. )
  2123. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  2124. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2125. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2126. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2127. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  2128. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2129. flexmock(module).should_receive('expand_directories').and_return(())
  2130. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2131. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2132. flexmock(module).should_receive('expand_home_directories').and_return(())
  2133. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2134. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2135. flexmock(module.feature).should_receive('available').and_return(True)
  2136. flexmock(module).should_receive('ensure_files_readable')
  2137. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2138. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2139. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2140. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2141. )
  2142. flexmock(module.environment).should_receive('make_environment')
  2143. flexmock(module).should_receive('execute_command').with_args(
  2144. ('borg', 'create', f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'food'),
  2145. output_log_level=logging.INFO,
  2146. output_file=None,
  2147. borg_local_path='borg',
  2148. working_directory=None,
  2149. extra_environment=None,
  2150. )
  2151. module.create_archive(
  2152. dry_run=False,
  2153. repository_path='repo',
  2154. location_config={
  2155. 'source_directories': ['foo*'],
  2156. 'repositories': ['repo'],
  2157. 'exclude_patterns': None,
  2158. },
  2159. storage_config={},
  2160. local_borg_version='1.2.3',
  2161. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2162. )
  2163. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  2164. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2165. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2166. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2167. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2168. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2169. flexmock(module).should_receive('expand_directories').and_return(())
  2170. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2171. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2172. flexmock(module).should_receive('expand_home_directories').and_return(())
  2173. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2174. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2175. flexmock(module.feature).should_receive('available').and_return(True)
  2176. flexmock(module).should_receive('ensure_files_readable')
  2177. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2178. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2179. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2180. ('repo::ARCHIVE_NAME',)
  2181. )
  2182. flexmock(module.environment).should_receive('make_environment')
  2183. flexmock(module).should_receive('execute_command').with_args(
  2184. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  2185. output_log_level=logging.INFO,
  2186. output_file=None,
  2187. borg_local_path='borg',
  2188. working_directory=None,
  2189. extra_environment=None,
  2190. )
  2191. module.create_archive(
  2192. dry_run=False,
  2193. repository_path='repo',
  2194. location_config={
  2195. 'source_directories': ['foo', 'bar'],
  2196. 'repositories': ['repo'],
  2197. 'exclude_patterns': None,
  2198. },
  2199. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  2200. local_borg_version='1.2.3',
  2201. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2202. )
  2203. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  2204. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2205. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2206. repository_archive_pattern = 'repo::Documents_{hostname}-{now}' # noqa: FS003
  2207. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2208. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2209. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2210. flexmock(module).should_receive('expand_directories').and_return(())
  2211. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2212. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2213. flexmock(module).should_receive('expand_home_directories').and_return(())
  2214. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2215. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2216. flexmock(module.feature).should_receive('available').and_return(True)
  2217. flexmock(module).should_receive('ensure_files_readable')
  2218. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2219. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2220. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2221. (repository_archive_pattern,)
  2222. )
  2223. flexmock(module.environment).should_receive('make_environment')
  2224. flexmock(module).should_receive('execute_command').with_args(
  2225. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  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. module.create_archive(
  2233. dry_run=False,
  2234. repository_path='repo',
  2235. location_config={
  2236. 'source_directories': ['foo', 'bar'],
  2237. 'repositories': ['repo'],
  2238. 'exclude_patterns': None,
  2239. },
  2240. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'}, # noqa: FS003
  2241. local_borg_version='1.2.3',
  2242. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2243. )
  2244. def test_create_archive_with_repository_accepts_borg_placeholders():
  2245. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2246. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2247. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}' # noqa: FS003
  2248. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2249. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2250. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2251. flexmock(module).should_receive('expand_directories').and_return(())
  2252. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2253. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2254. flexmock(module).should_receive('expand_home_directories').and_return(())
  2255. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2256. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2257. flexmock(module.feature).should_receive('available').and_return(True)
  2258. flexmock(module).should_receive('ensure_files_readable')
  2259. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2260. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2261. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2262. (repository_archive_pattern,)
  2263. )
  2264. flexmock(module.environment).should_receive('make_environment')
  2265. flexmock(module).should_receive('execute_command').with_args(
  2266. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  2267. output_log_level=logging.INFO,
  2268. output_file=None,
  2269. borg_local_path='borg',
  2270. working_directory=None,
  2271. extra_environment=None,
  2272. )
  2273. module.create_archive(
  2274. dry_run=False,
  2275. repository_path='{fqdn}', # noqa: FS003
  2276. location_config={
  2277. 'source_directories': ['foo', 'bar'],
  2278. 'repositories': ['{fqdn}'], # noqa: FS003
  2279. 'exclude_patterns': None,
  2280. },
  2281. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'}, # noqa: FS003
  2282. local_borg_version='1.2.3',
  2283. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2284. )
  2285. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  2286. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2287. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2288. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2289. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2290. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2291. flexmock(module).should_receive('expand_directories').and_return(())
  2292. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2293. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2294. flexmock(module).should_receive('expand_home_directories').and_return(())
  2295. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2296. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2297. flexmock(module.feature).should_receive('available').and_return(True)
  2298. flexmock(module).should_receive('ensure_files_readable')
  2299. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2300. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2301. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2302. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2303. )
  2304. flexmock(module.environment).should_receive('make_environment')
  2305. flexmock(module).should_receive('execute_command').with_args(
  2306. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  2307. output_log_level=logging.INFO,
  2308. output_file=None,
  2309. borg_local_path='borg',
  2310. working_directory=None,
  2311. extra_environment=None,
  2312. )
  2313. module.create_archive(
  2314. dry_run=False,
  2315. repository_path='repo',
  2316. location_config={
  2317. 'source_directories': ['foo', 'bar'],
  2318. 'repositories': ['repo'],
  2319. 'exclude_patterns': None,
  2320. },
  2321. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  2322. local_borg_version='1.2.3',
  2323. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2324. )
  2325. def test_create_archive_with_stream_processes_calls_borg_with_processes_and_read_special():
  2326. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2327. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2328. processes = flexmock()
  2329. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2330. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  2331. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  2332. flexmock(module).should_receive('expand_directories').and_return(())
  2333. flexmock(module).should_receive('pattern_root_directories').and_return([])
  2334. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  2335. flexmock(module).should_receive('expand_home_directories').and_return(())
  2336. flexmock(module).should_receive('write_pattern_file').and_return(None)
  2337. flexmock(module).should_receive('make_list_filter_flags').and_return('FOO')
  2338. flexmock(module.feature).should_receive('available').and_return(True)
  2339. flexmock(module).should_receive('ensure_files_readable')
  2340. flexmock(module).should_receive('make_pattern_flags').and_return(())
  2341. flexmock(module).should_receive('make_exclude_flags').and_return(())
  2342. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  2343. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  2344. )
  2345. flexmock(module.environment).should_receive('make_environment')
  2346. flexmock(module).should_receive('collect_special_file_paths').and_return(())
  2347. create_command = (
  2348. 'borg',
  2349. 'create',
  2350. '--one-file-system',
  2351. '--read-special',
  2352. ) + REPO_ARCHIVE_WITH_PATHS
  2353. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2354. create_command + ('--dry-run', 'list'),
  2355. processes=processes,
  2356. output_log_level=logging.INFO,
  2357. output_file=None,
  2358. borg_local_path='borg',
  2359. working_directory=None,
  2360. extra_environment=None,
  2361. )
  2362. flexmock(module).should_receive('execute_command_with_processes').with_args(
  2363. create_command,
  2364. processes=processes,
  2365. output_log_level=logging.INFO,
  2366. output_file=None,
  2367. borg_local_path='borg',
  2368. working_directory=None,
  2369. extra_environment=None,
  2370. )
  2371. module.create_archive(
  2372. dry_run=False,
  2373. repository_path='repo',
  2374. location_config={
  2375. 'source_directories': ['foo', 'bar'],
  2376. 'repositories': ['repo'],
  2377. 'exclude_patterns': None,
  2378. },
  2379. storage_config={},
  2380. local_borg_version='1.2.3',
  2381. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2382. stream_processes=processes,
  2383. )
  2384. def test_create_archive_with_non_existent_directory_and_source_directories_must_exist_raises_error():
  2385. '''
  2386. If a source directory doesn't exist and source_directories_must_exist is True, raise an error.
  2387. '''
  2388. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  2389. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  2390. flexmock(module).should_receive('collect_borgmatic_source_directories').and_return([])
  2391. flexmock(module).should_receive('check_all_source_directories_exist').and_raise(ValueError)
  2392. with pytest.raises(ValueError):
  2393. module.create_archive(
  2394. dry_run=False,
  2395. repository_path='repo',
  2396. location_config={
  2397. 'source_directories': ['foo', 'bar'],
  2398. 'repositories': ['repo'],
  2399. 'exclude_patterns': None,
  2400. 'source_directories_must_exist': True,
  2401. },
  2402. storage_config={},
  2403. local_borg_version='1.2.3',
  2404. global_arguments=flexmock(log_json=False, used_config_paths=[]),
  2405. )
  2406. def test_check_all_source_directories_exist_with_glob_and_tilde_directories():
  2407. flexmock(module).should_receive('expand_directory').with_args('foo*').and_return(
  2408. ('foo', 'food')
  2409. )
  2410. flexmock(module).should_receive('expand_directory').with_args('~/bar').and_return(
  2411. ('/root/bar',)
  2412. )
  2413. flexmock(module.os.path).should_receive('exists').and_return(False)
  2414. flexmock(module.os.path).should_receive('exists').with_args('foo').and_return(True)
  2415. flexmock(module.os.path).should_receive('exists').with_args('food').and_return(True)
  2416. flexmock(module.os.path).should_receive('exists').with_args('/root/bar').and_return(True)
  2417. module.check_all_source_directories_exist(['foo*', '~/bar'])
  2418. def test_check_all_source_directories_exist_with_non_existent_directory_raises():
  2419. flexmock(module).should_receive('expand_directory').with_args('foo').and_return(('foo',))
  2420. flexmock(module.os.path).should_receive('exists').and_return(False)
  2421. with pytest.raises(ValueError):
  2422. module.check_all_source_directories_exist(['foo'])