test_create.py 126 KB

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