test_create.py 126 KB

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