2
0

test_create.py 106 KB

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