test_create.py 104 KB

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