test_create.py 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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_with_empty_exclude_patterns_does_not_raise():
  91. module.write_pattern_file([])
  92. @pytest.mark.parametrize(
  93. 'filename_lists,opened_filenames',
  94. (
  95. ([('foo', 'bar'), ('baz', 'quux')], ('foo', 'bar', 'baz', 'quux')),
  96. ([None, ('foo', 'bar')], ('foo', 'bar')),
  97. ([None, None], ()),
  98. ),
  99. )
  100. def test_ensure_files_readable_opens_filenames(filename_lists, opened_filenames):
  101. for expected_filename in opened_filenames:
  102. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  103. expected_filename
  104. ).and_return(flexmock(close=lambda: None))
  105. module.ensure_files_readable(*filename_lists)
  106. def test_make_pattern_flags_includes_pattern_filename_when_given():
  107. pattern_flags = module.make_pattern_flags(
  108. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  109. )
  110. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  111. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  112. pattern_flags = module.make_pattern_flags(
  113. location_config={'patterns_from': ['patterns', 'other']}
  114. )
  115. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  116. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  117. pattern_flags = module.make_pattern_flags(
  118. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  119. )
  120. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  121. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  122. pattern_flags = module.make_pattern_flags(location_config={'patterns_from': None})
  123. assert pattern_flags == ()
  124. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  125. exclude_flags = module.make_exclude_flags(
  126. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  127. )
  128. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  129. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  130. exclude_flags = module.make_exclude_flags(
  131. location_config={'exclude_from': ['excludes', 'other']}
  132. )
  133. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  134. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  135. exclude_flags = module.make_exclude_flags(
  136. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  137. )
  138. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  139. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  140. exclude_flags = module.make_exclude_flags(location_config={'exclude_from': None})
  141. assert exclude_flags == ()
  142. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  143. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': True})
  144. assert exclude_flags == ('--exclude-caches',)
  145. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  146. exclude_flags = module.make_exclude_flags(location_config={'exclude_caches': False})
  147. assert exclude_flags == ()
  148. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  149. exclude_flags = module.make_exclude_flags(
  150. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  151. )
  152. assert exclude_flags == (
  153. '--exclude-if-present',
  154. 'exclude_me',
  155. '--exclude-if-present',
  156. 'also_me',
  157. )
  158. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  159. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': True})
  160. assert exclude_flags == ('--keep-exclude-tags',)
  161. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  162. exclude_flags = module.make_exclude_flags(location_config={'keep_exclude_tags': False})
  163. assert exclude_flags == ()
  164. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  165. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': True})
  166. assert exclude_flags == ('--exclude-nodump',)
  167. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  168. exclude_flags = module.make_exclude_flags(location_config={'exclude_nodump': False})
  169. assert exclude_flags == ()
  170. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  171. exclude_flags = module.make_exclude_flags(location_config={})
  172. assert exclude_flags == ()
  173. def test_borgmatic_source_directories_set_when_directory_exists():
  174. flexmock(module.os.path).should_receive('exists').and_return(True)
  175. flexmock(module.os.path).should_receive('expanduser')
  176. assert module.borgmatic_source_directories('/tmp') == ['/tmp']
  177. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  178. flexmock(module.os.path).should_receive('exists').and_return(False)
  179. flexmock(module.os.path).should_receive('expanduser')
  180. assert module.borgmatic_source_directories('/tmp') == []
  181. def test_borgmatic_source_directories_defaults_when_directory_not_given():
  182. flexmock(module.os.path).should_receive('exists').and_return(True)
  183. flexmock(module.os.path).should_receive('expanduser')
  184. assert module.borgmatic_source_directories(None) == [
  185. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  186. ]
  187. def test_pattern_root_directories_deals_with_none_patterns():
  188. assert module.pattern_root_directories(patterns=None) == []
  189. def test_pattern_root_directories_parses_roots_and_ignores_others():
  190. assert module.pattern_root_directories(
  191. ['R /root', '+ /root/foo', '- /root/foo/bar', 'R /baz']
  192. ) == ['/root', '/baz']
  193. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  194. REPO_ARCHIVE_WITH_PATHS = (f'repo::{DEFAULT_ARCHIVE_NAME}', 'foo', 'bar')
  195. def test_create_archive_calls_borg_with_parameters():
  196. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  197. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  198. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  199. flexmock(module).should_receive('expand_directories').and_return(())
  200. flexmock(module).should_receive('pattern_root_directories').and_return([])
  201. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  202. flexmock(module).should_receive('expand_home_directories').and_return(())
  203. flexmock(module).should_receive('write_pattern_file').and_return(None)
  204. flexmock(module.feature).should_receive('available').and_return(True)
  205. flexmock(module).should_receive('ensure_files_readable')
  206. flexmock(module).should_receive('make_pattern_flags').and_return(())
  207. flexmock(module).should_receive('make_exclude_flags').and_return(())
  208. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  209. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  210. )
  211. flexmock(module.environment).should_receive('make_environment')
  212. flexmock(module).should_receive('execute_command').with_args(
  213. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  214. output_log_level=logging.INFO,
  215. output_file=None,
  216. borg_local_path='borg',
  217. working_directory=None,
  218. extra_environment=None,
  219. )
  220. module.create_archive(
  221. dry_run=False,
  222. repository='repo',
  223. location_config={
  224. 'source_directories': ['foo', 'bar'],
  225. 'repositories': ['repo'],
  226. 'exclude_patterns': None,
  227. },
  228. storage_config={},
  229. local_borg_version='1.2.3',
  230. )
  231. def test_create_archive_calls_borg_with_environment():
  232. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  233. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  234. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  235. flexmock(module).should_receive('expand_directories').and_return(())
  236. flexmock(module).should_receive('pattern_root_directories').and_return([])
  237. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  238. flexmock(module).should_receive('expand_home_directories').and_return(())
  239. flexmock(module).should_receive('write_pattern_file').and_return(None)
  240. flexmock(module.feature).should_receive('available').and_return(True)
  241. flexmock(module).should_receive('ensure_files_readable')
  242. flexmock(module).should_receive('make_pattern_flags').and_return(())
  243. flexmock(module).should_receive('make_exclude_flags').and_return(())
  244. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  245. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  246. )
  247. environment = {'BORG_THINGY': 'YUP'}
  248. flexmock(module.environment).should_receive('make_environment').and_return(environment)
  249. flexmock(module).should_receive('execute_command').with_args(
  250. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  251. output_log_level=logging.INFO,
  252. output_file=None,
  253. borg_local_path='borg',
  254. working_directory=None,
  255. extra_environment=environment,
  256. )
  257. module.create_archive(
  258. dry_run=False,
  259. repository='repo',
  260. location_config={
  261. 'source_directories': ['foo', 'bar'],
  262. 'repositories': ['repo'],
  263. 'exclude_patterns': None,
  264. },
  265. storage_config={},
  266. local_borg_version='1.2.3',
  267. )
  268. def test_create_archive_with_patterns_calls_borg_with_patterns_including_converted_source_directories():
  269. pattern_flags = ('--patterns-from', 'patterns')
  270. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  271. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  272. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  273. flexmock(module).should_receive('expand_directories').and_return(())
  274. flexmock(module).should_receive('pattern_root_directories').and_return([])
  275. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  276. flexmock(module).should_receive('expand_home_directories').and_return(())
  277. flexmock(module).should_receive('write_pattern_file').and_return(
  278. flexmock(name='/tmp/patterns')
  279. ).and_return(None)
  280. flexmock(module.feature).should_receive('available').and_return(True)
  281. flexmock(module).should_receive('ensure_files_readable')
  282. flexmock(module).should_receive('make_pattern_flags').and_return(pattern_flags)
  283. flexmock(module).should_receive('make_exclude_flags').and_return(())
  284. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  285. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  286. )
  287. flexmock(module.environment).should_receive('make_environment')
  288. flexmock(module).should_receive('execute_command').with_args(
  289. ('borg', 'create') + pattern_flags + (f'repo::{DEFAULT_ARCHIVE_NAME}',),
  290. output_log_level=logging.INFO,
  291. output_file=None,
  292. borg_local_path='borg',
  293. working_directory=None,
  294. extra_environment=None,
  295. )
  296. module.create_archive(
  297. dry_run=False,
  298. repository='repo',
  299. location_config={
  300. 'source_directories': ['foo', 'bar'],
  301. 'repositories': ['repo'],
  302. 'patterns': ['pattern'],
  303. },
  304. storage_config={},
  305. local_borg_version='1.2.3',
  306. )
  307. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  308. exclude_flags = ('--exclude-from', 'excludes')
  309. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  310. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  311. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  312. flexmock(module).should_receive('expand_directories').and_return(())
  313. flexmock(module).should_receive('pattern_root_directories').and_return([])
  314. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  315. flexmock(module).should_receive('expand_home_directories').and_return(('exclude',))
  316. flexmock(module).should_receive('write_pattern_file').and_return(None).and_return(
  317. flexmock(name='/tmp/excludes')
  318. )
  319. flexmock(module.feature).should_receive('available').and_return(True)
  320. flexmock(module).should_receive('ensure_files_readable')
  321. flexmock(module).should_receive('make_pattern_flags').and_return(())
  322. flexmock(module).should_receive('make_exclude_flags').and_return(exclude_flags)
  323. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  324. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  325. )
  326. flexmock(module.environment).should_receive('make_environment')
  327. flexmock(module).should_receive('execute_command').with_args(
  328. ('borg', 'create') + exclude_flags + REPO_ARCHIVE_WITH_PATHS,
  329. output_log_level=logging.INFO,
  330. output_file=None,
  331. borg_local_path='borg',
  332. working_directory=None,
  333. extra_environment=None,
  334. )
  335. module.create_archive(
  336. dry_run=False,
  337. repository='repo',
  338. location_config={
  339. 'source_directories': ['foo', 'bar'],
  340. 'repositories': ['repo'],
  341. 'exclude_patterns': ['exclude'],
  342. },
  343. storage_config={},
  344. local_borg_version='1.2.3',
  345. )
  346. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  347. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  348. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  349. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  350. flexmock(module).should_receive('expand_directories').and_return(())
  351. flexmock(module).should_receive('pattern_root_directories').and_return([])
  352. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  353. flexmock(module).should_receive('expand_home_directories').and_return(())
  354. flexmock(module).should_receive('write_pattern_file').and_return(None)
  355. flexmock(module.feature).should_receive('available').and_return(True)
  356. flexmock(module).should_receive('ensure_files_readable')
  357. flexmock(module).should_receive('make_pattern_flags').and_return(())
  358. flexmock(module).should_receive('make_exclude_flags').and_return(())
  359. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  360. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  361. )
  362. flexmock(module.environment).should_receive('make_environment')
  363. flexmock(module).should_receive('execute_command').with_args(
  364. ('borg', 'create', '--info') + REPO_ARCHIVE_WITH_PATHS,
  365. output_log_level=logging.INFO,
  366. output_file=None,
  367. borg_local_path='borg',
  368. working_directory=None,
  369. extra_environment=None,
  370. )
  371. insert_logging_mock(logging.INFO)
  372. module.create_archive(
  373. dry_run=False,
  374. repository='repo',
  375. location_config={
  376. 'source_directories': ['foo', 'bar'],
  377. 'repositories': ['repo'],
  378. 'exclude_patterns': None,
  379. },
  380. storage_config={},
  381. local_borg_version='1.2.3',
  382. )
  383. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  384. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  385. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  386. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  387. flexmock(module).should_receive('expand_directories').and_return(())
  388. flexmock(module).should_receive('pattern_root_directories').and_return([])
  389. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  390. flexmock(module).should_receive('expand_home_directories').and_return(())
  391. flexmock(module).should_receive('write_pattern_file').and_return(None)
  392. flexmock(module.feature).should_receive('available').and_return(True)
  393. flexmock(module).should_receive('ensure_files_readable')
  394. flexmock(module).should_receive('make_pattern_flags').and_return(())
  395. flexmock(module).should_receive('make_exclude_flags').and_return(())
  396. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  397. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  398. )
  399. flexmock(module.environment).should_receive('make_environment')
  400. flexmock(module).should_receive('execute_command').with_args(
  401. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  402. output_log_level=None,
  403. output_file=None,
  404. borg_local_path='borg',
  405. working_directory=None,
  406. extra_environment=None,
  407. )
  408. insert_logging_mock(logging.INFO)
  409. module.create_archive(
  410. dry_run=False,
  411. repository='repo',
  412. location_config={
  413. 'source_directories': ['foo', 'bar'],
  414. 'repositories': ['repo'],
  415. 'exclude_patterns': None,
  416. },
  417. storage_config={},
  418. local_borg_version='1.2.3',
  419. json=True,
  420. )
  421. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  422. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  423. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  424. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  425. flexmock(module).should_receive('expand_directories').and_return(())
  426. flexmock(module).should_receive('pattern_root_directories').and_return([])
  427. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  428. flexmock(module).should_receive('expand_home_directories').and_return(())
  429. flexmock(module).should_receive('write_pattern_file').and_return(None)
  430. flexmock(module.feature).should_receive('available').and_return(True)
  431. flexmock(module).should_receive('ensure_files_readable')
  432. flexmock(module).should_receive('make_pattern_flags').and_return(())
  433. flexmock(module).should_receive('make_exclude_flags').and_return(())
  434. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  435. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  436. )
  437. flexmock(module.environment).should_receive('make_environment')
  438. flexmock(module).should_receive('execute_command').with_args(
  439. ('borg', 'create', '--debug', '--show-rc') + REPO_ARCHIVE_WITH_PATHS,
  440. output_log_level=logging.INFO,
  441. output_file=None,
  442. borg_local_path='borg',
  443. working_directory=None,
  444. extra_environment=None,
  445. )
  446. insert_logging_mock(logging.DEBUG)
  447. module.create_archive(
  448. dry_run=False,
  449. repository='repo',
  450. location_config={
  451. 'source_directories': ['foo', 'bar'],
  452. 'repositories': ['repo'],
  453. 'exclude_patterns': None,
  454. },
  455. storage_config={},
  456. local_borg_version='1.2.3',
  457. )
  458. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  459. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  460. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  461. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  462. flexmock(module).should_receive('expand_directories').and_return(())
  463. flexmock(module).should_receive('pattern_root_directories').and_return([])
  464. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  465. flexmock(module).should_receive('expand_home_directories').and_return(())
  466. flexmock(module).should_receive('write_pattern_file').and_return(None)
  467. flexmock(module.feature).should_receive('available').and_return(True)
  468. flexmock(module).should_receive('ensure_files_readable')
  469. flexmock(module).should_receive('make_pattern_flags').and_return(())
  470. flexmock(module).should_receive('make_exclude_flags').and_return(())
  471. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  472. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  473. )
  474. flexmock(module.environment).should_receive('make_environment')
  475. flexmock(module).should_receive('execute_command').with_args(
  476. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  477. output_log_level=None,
  478. output_file=None,
  479. borg_local_path='borg',
  480. working_directory=None,
  481. extra_environment=None,
  482. )
  483. insert_logging_mock(logging.DEBUG)
  484. module.create_archive(
  485. dry_run=False,
  486. repository='repo',
  487. location_config={
  488. 'source_directories': ['foo', 'bar'],
  489. 'repositories': ['repo'],
  490. 'exclude_patterns': None,
  491. },
  492. storage_config={},
  493. local_borg_version='1.2.3',
  494. json=True,
  495. )
  496. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  497. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  498. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  499. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  500. flexmock(module).should_receive('expand_directories').and_return(())
  501. flexmock(module).should_receive('pattern_root_directories').and_return([])
  502. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  503. flexmock(module).should_receive('expand_home_directories').and_return(())
  504. flexmock(module).should_receive('write_pattern_file').and_return(None)
  505. flexmock(module.feature).should_receive('available').and_return(True)
  506. flexmock(module).should_receive('ensure_files_readable')
  507. flexmock(module).should_receive('make_pattern_flags').and_return(())
  508. flexmock(module).should_receive('make_exclude_flags').and_return(())
  509. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  510. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  511. )
  512. flexmock(module.environment).should_receive('make_environment')
  513. flexmock(module).should_receive('execute_command').with_args(
  514. ('borg', 'create', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  515. output_log_level=logging.INFO,
  516. output_file=None,
  517. borg_local_path='borg',
  518. working_directory=None,
  519. extra_environment=None,
  520. )
  521. module.create_archive(
  522. dry_run=True,
  523. repository='repo',
  524. location_config={
  525. 'source_directories': ['foo', 'bar'],
  526. 'repositories': ['repo'],
  527. 'exclude_patterns': None,
  528. },
  529. storage_config={},
  530. local_borg_version='1.2.3',
  531. )
  532. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  533. # --dry-run and --stats are mutually exclusive, see:
  534. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  535. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  536. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  537. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  538. flexmock(module).should_receive('expand_directories').and_return(())
  539. flexmock(module).should_receive('pattern_root_directories').and_return([])
  540. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  541. flexmock(module).should_receive('expand_home_directories').and_return(())
  542. flexmock(module).should_receive('write_pattern_file').and_return(None)
  543. flexmock(module.feature).should_receive('available').and_return(True)
  544. flexmock(module).should_receive('ensure_files_readable')
  545. flexmock(module).should_receive('make_pattern_flags').and_return(())
  546. flexmock(module).should_receive('make_exclude_flags').and_return(())
  547. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  548. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  549. )
  550. flexmock(module.environment).should_receive('make_environment')
  551. flexmock(module).should_receive('execute_command').with_args(
  552. ('borg', 'create', '--info', '--dry-run') + REPO_ARCHIVE_WITH_PATHS,
  553. output_log_level=logging.INFO,
  554. output_file=None,
  555. borg_local_path='borg',
  556. working_directory=None,
  557. extra_environment=None,
  558. )
  559. insert_logging_mock(logging.INFO)
  560. module.create_archive(
  561. dry_run=True,
  562. repository='repo',
  563. location_config={
  564. 'source_directories': ['foo', 'bar'],
  565. 'repositories': ['repo'],
  566. 'exclude_patterns': None,
  567. },
  568. storage_config={},
  569. local_borg_version='1.2.3',
  570. stats=True,
  571. )
  572. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  573. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  574. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  575. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  576. flexmock(module).should_receive('expand_directories').and_return(())
  577. flexmock(module).should_receive('pattern_root_directories').and_return([])
  578. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  579. flexmock(module).should_receive('expand_home_directories').and_return(())
  580. flexmock(module).should_receive('write_pattern_file').and_return(None)
  581. flexmock(module.feature).should_receive('available').and_return(True)
  582. flexmock(module).should_receive('ensure_files_readable')
  583. flexmock(module).should_receive('make_pattern_flags').and_return(())
  584. flexmock(module).should_receive('make_exclude_flags').and_return(())
  585. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  586. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  587. )
  588. flexmock(module.environment).should_receive('make_environment')
  589. flexmock(module).should_receive('execute_command').with_args(
  590. ('borg', 'create', '--checkpoint-interval', '600') + REPO_ARCHIVE_WITH_PATHS,
  591. output_log_level=logging.INFO,
  592. output_file=None,
  593. borg_local_path='borg',
  594. working_directory=None,
  595. extra_environment=None,
  596. )
  597. module.create_archive(
  598. dry_run=False,
  599. repository='repo',
  600. location_config={
  601. 'source_directories': ['foo', 'bar'],
  602. 'repositories': ['repo'],
  603. 'exclude_patterns': None,
  604. },
  605. storage_config={'checkpoint_interval': 600},
  606. local_borg_version='1.2.3',
  607. )
  608. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  609. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  610. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  611. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  612. flexmock(module).should_receive('expand_directories').and_return(())
  613. flexmock(module).should_receive('pattern_root_directories').and_return([])
  614. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  615. flexmock(module).should_receive('expand_home_directories').and_return(())
  616. flexmock(module).should_receive('write_pattern_file').and_return(None)
  617. flexmock(module.feature).should_receive('available').and_return(True)
  618. flexmock(module).should_receive('ensure_files_readable')
  619. flexmock(module).should_receive('make_pattern_flags').and_return(())
  620. flexmock(module).should_receive('make_exclude_flags').and_return(())
  621. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  622. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  623. )
  624. flexmock(module.environment).should_receive('make_environment')
  625. flexmock(module).should_receive('execute_command').with_args(
  626. ('borg', 'create', '--chunker-params', '1,2,3,4') + REPO_ARCHIVE_WITH_PATHS,
  627. output_log_level=logging.INFO,
  628. output_file=None,
  629. borg_local_path='borg',
  630. working_directory=None,
  631. extra_environment=None,
  632. )
  633. module.create_archive(
  634. dry_run=False,
  635. repository='repo',
  636. location_config={
  637. 'source_directories': ['foo', 'bar'],
  638. 'repositories': ['repo'],
  639. 'exclude_patterns': None,
  640. },
  641. storage_config={'chunker_params': '1,2,3,4'},
  642. local_borg_version='1.2.3',
  643. )
  644. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  645. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  646. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  647. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  648. flexmock(module).should_receive('expand_directories').and_return(())
  649. flexmock(module).should_receive('pattern_root_directories').and_return([])
  650. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  651. flexmock(module).should_receive('expand_home_directories').and_return(())
  652. flexmock(module).should_receive('write_pattern_file').and_return(None)
  653. flexmock(module.feature).should_receive('available').and_return(True)
  654. flexmock(module).should_receive('ensure_files_readable')
  655. flexmock(module).should_receive('make_pattern_flags').and_return(())
  656. flexmock(module).should_receive('make_exclude_flags').and_return(())
  657. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  658. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  659. )
  660. flexmock(module.environment).should_receive('make_environment')
  661. flexmock(module).should_receive('execute_command').with_args(
  662. ('borg', 'create', '--compression', 'rle') + REPO_ARCHIVE_WITH_PATHS,
  663. output_log_level=logging.INFO,
  664. output_file=None,
  665. borg_local_path='borg',
  666. working_directory=None,
  667. extra_environment=None,
  668. )
  669. module.create_archive(
  670. dry_run=False,
  671. repository='repo',
  672. location_config={
  673. 'source_directories': ['foo', 'bar'],
  674. 'repositories': ['repo'],
  675. 'exclude_patterns': None,
  676. },
  677. storage_config={'compression': 'rle'},
  678. local_borg_version='1.2.3',
  679. )
  680. @pytest.mark.parametrize(
  681. 'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  682. )
  683. def test_create_archive_with_upload_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  684. feature_available, option_flag
  685. ):
  686. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  687. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  688. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  689. flexmock(module).should_receive('expand_directories').and_return(())
  690. flexmock(module).should_receive('pattern_root_directories').and_return([])
  691. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  692. flexmock(module).should_receive('expand_home_directories').and_return(())
  693. flexmock(module).should_receive('write_pattern_file').and_return(None)
  694. flexmock(module.feature).should_receive('available').and_return(feature_available)
  695. flexmock(module).should_receive('ensure_files_readable')
  696. flexmock(module).should_receive('make_pattern_flags').and_return(())
  697. flexmock(module).should_receive('make_exclude_flags').and_return(())
  698. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  699. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  700. )
  701. flexmock(module.environment).should_receive('make_environment')
  702. flexmock(module).should_receive('execute_command').with_args(
  703. ('borg', 'create', option_flag, '100') + REPO_ARCHIVE_WITH_PATHS,
  704. output_log_level=logging.INFO,
  705. output_file=None,
  706. borg_local_path='borg',
  707. working_directory=None,
  708. extra_environment=None,
  709. )
  710. module.create_archive(
  711. dry_run=False,
  712. repository='repo',
  713. location_config={
  714. 'source_directories': ['foo', 'bar'],
  715. 'repositories': ['repo'],
  716. 'exclude_patterns': None,
  717. },
  718. storage_config={'upload_rate_limit': 100},
  719. local_borg_version='1.2.3',
  720. )
  721. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  722. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  723. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  724. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  725. flexmock(module).should_receive('expand_directories').and_return(())
  726. flexmock(module).should_receive('pattern_root_directories').and_return([])
  727. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  728. '/working/dir'
  729. )
  730. flexmock(module).should_receive('expand_home_directories').and_return(())
  731. flexmock(module).should_receive('write_pattern_file').and_return(None)
  732. flexmock(module.feature).should_receive('available').and_return(True)
  733. flexmock(module).should_receive('ensure_files_readable')
  734. flexmock(module).should_receive('make_pattern_flags').and_return(())
  735. flexmock(module).should_receive('make_exclude_flags').and_return(())
  736. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  737. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  738. )
  739. flexmock(module.environment).should_receive('make_environment')
  740. flexmock(module).should_receive('execute_command').with_args(
  741. ('borg', 'create') + REPO_ARCHIVE_WITH_PATHS,
  742. output_log_level=logging.INFO,
  743. output_file=None,
  744. borg_local_path='borg',
  745. working_directory='/working/dir',
  746. extra_environment=None,
  747. )
  748. module.create_archive(
  749. dry_run=False,
  750. repository='repo',
  751. location_config={
  752. 'source_directories': ['foo', 'bar'],
  753. 'repositories': ['repo'],
  754. 'working_directory': '/working/dir',
  755. 'exclude_patterns': None,
  756. },
  757. storage_config={},
  758. local_borg_version='1.2.3',
  759. )
  760. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  761. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  762. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  763. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  764. flexmock(module).should_receive('expand_directories').and_return(())
  765. flexmock(module).should_receive('pattern_root_directories').and_return([])
  766. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  767. flexmock(module).should_receive('expand_home_directories').and_return(())
  768. flexmock(module).should_receive('write_pattern_file').and_return(None)
  769. flexmock(module.feature).should_receive('available').and_return(True)
  770. flexmock(module).should_receive('ensure_files_readable')
  771. flexmock(module).should_receive('make_pattern_flags').and_return(())
  772. flexmock(module).should_receive('make_exclude_flags').and_return(())
  773. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  774. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  775. )
  776. flexmock(module.environment).should_receive('make_environment')
  777. flexmock(module).should_receive('execute_command').with_args(
  778. ('borg', 'create', '--one-file-system') + REPO_ARCHIVE_WITH_PATHS,
  779. output_log_level=logging.INFO,
  780. output_file=None,
  781. borg_local_path='borg',
  782. working_directory=None,
  783. extra_environment=None,
  784. )
  785. module.create_archive(
  786. dry_run=False,
  787. repository='repo',
  788. location_config={
  789. 'source_directories': ['foo', 'bar'],
  790. 'repositories': ['repo'],
  791. 'one_file_system': True,
  792. 'exclude_patterns': None,
  793. },
  794. storage_config={},
  795. local_borg_version='1.2.3',
  796. )
  797. @pytest.mark.parametrize(
  798. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')),
  799. )
  800. def test_create_archive_with_numeric_ids_calls_borg_with_numeric_ids_parameter(
  801. feature_available, option_flag
  802. ):
  803. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  804. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  805. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  806. flexmock(module).should_receive('expand_directories').and_return(())
  807. flexmock(module).should_receive('pattern_root_directories').and_return([])
  808. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  809. flexmock(module).should_receive('expand_home_directories').and_return(())
  810. flexmock(module).should_receive('write_pattern_file').and_return(None)
  811. flexmock(module.feature).should_receive('available').and_return(feature_available)
  812. flexmock(module).should_receive('ensure_files_readable')
  813. flexmock(module).should_receive('make_pattern_flags').and_return(())
  814. flexmock(module).should_receive('make_exclude_flags').and_return(())
  815. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  816. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  817. )
  818. flexmock(module.environment).should_receive('make_environment')
  819. flexmock(module).should_receive('execute_command').with_args(
  820. ('borg', 'create', option_flag) + REPO_ARCHIVE_WITH_PATHS,
  821. output_log_level=logging.INFO,
  822. output_file=None,
  823. borg_local_path='borg',
  824. working_directory=None,
  825. extra_environment=None,
  826. )
  827. module.create_archive(
  828. dry_run=False,
  829. repository='repo',
  830. location_config={
  831. 'source_directories': ['foo', 'bar'],
  832. 'repositories': ['repo'],
  833. 'numeric_ids': True,
  834. 'exclude_patterns': None,
  835. },
  836. storage_config={},
  837. local_borg_version='1.2.3',
  838. )
  839. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  840. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  841. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  842. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  843. flexmock(module).should_receive('expand_directories').and_return(())
  844. flexmock(module).should_receive('pattern_root_directories').and_return([])
  845. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  846. flexmock(module).should_receive('expand_home_directories').and_return(())
  847. flexmock(module).should_receive('write_pattern_file').and_return(None)
  848. flexmock(module.feature).should_receive('available').and_return(True)
  849. flexmock(module).should_receive('ensure_files_readable')
  850. flexmock(module).should_receive('make_pattern_flags').and_return(())
  851. flexmock(module).should_receive('make_exclude_flags').and_return(())
  852. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  853. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  854. )
  855. flexmock(module.environment).should_receive('make_environment')
  856. flexmock(module).should_receive('execute_command').with_args(
  857. ('borg', 'create', '--read-special') + REPO_ARCHIVE_WITH_PATHS,
  858. output_log_level=logging.INFO,
  859. output_file=None,
  860. borg_local_path='borg',
  861. working_directory=None,
  862. extra_environment=None,
  863. )
  864. module.create_archive(
  865. dry_run=False,
  866. repository='repo',
  867. location_config={
  868. 'source_directories': ['foo', 'bar'],
  869. 'repositories': ['repo'],
  870. 'read_special': True,
  871. 'exclude_patterns': None,
  872. },
  873. storage_config={},
  874. local_borg_version='1.2.3',
  875. )
  876. @pytest.mark.parametrize(
  877. 'option_name,option_value',
  878. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  879. )
  880. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  881. option_name, option_value
  882. ):
  883. option_flag = '--no' + option_name.replace('', '') if option_value is False else None
  884. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  885. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  886. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  887. flexmock(module).should_receive('expand_directories').and_return(())
  888. flexmock(module).should_receive('pattern_root_directories').and_return([])
  889. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  890. flexmock(module).should_receive('expand_home_directories').and_return(())
  891. flexmock(module).should_receive('write_pattern_file').and_return(None)
  892. flexmock(module.feature).should_receive('available').and_return(True)
  893. flexmock(module).should_receive('ensure_files_readable')
  894. flexmock(module).should_receive('make_pattern_flags').and_return(())
  895. flexmock(module).should_receive('make_exclude_flags').and_return(())
  896. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  897. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  898. )
  899. flexmock(module.environment).should_receive('make_environment')
  900. flexmock(module).should_receive('execute_command').with_args(
  901. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  902. output_log_level=logging.INFO,
  903. output_file=None,
  904. borg_local_path='borg',
  905. working_directory=None,
  906. extra_environment=None,
  907. )
  908. module.create_archive(
  909. dry_run=False,
  910. repository='repo',
  911. location_config={
  912. 'source_directories': ['foo', 'bar'],
  913. 'repositories': ['repo'],
  914. option_name: option_value,
  915. 'exclude_patterns': None,
  916. },
  917. storage_config={},
  918. local_borg_version='1.2.3',
  919. )
  920. @pytest.mark.parametrize(
  921. 'option_value,feature_available,option_flag',
  922. (
  923. (True, True, '--atime'),
  924. (True, False, None),
  925. (False, True, None),
  926. (False, False, '--noatime'),
  927. ),
  928. )
  929. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  930. option_value, feature_available, option_flag
  931. ):
  932. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  933. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  934. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  935. flexmock(module).should_receive('expand_directories').and_return(())
  936. flexmock(module).should_receive('pattern_root_directories').and_return([])
  937. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  938. flexmock(module).should_receive('expand_home_directories').and_return(())
  939. flexmock(module).should_receive('write_pattern_file').and_return(None)
  940. flexmock(module.feature).should_receive('available').and_return(feature_available)
  941. flexmock(module).should_receive('ensure_files_readable')
  942. flexmock(module).should_receive('make_pattern_flags').and_return(())
  943. flexmock(module).should_receive('make_exclude_flags').and_return(())
  944. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  945. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  946. )
  947. flexmock(module.environment).should_receive('make_environment')
  948. flexmock(module).should_receive('execute_command').with_args(
  949. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  950. output_log_level=logging.INFO,
  951. output_file=None,
  952. borg_local_path='borg',
  953. working_directory=None,
  954. extra_environment=None,
  955. )
  956. module.create_archive(
  957. dry_run=False,
  958. repository='repo',
  959. location_config={
  960. 'source_directories': ['foo', 'bar'],
  961. 'repositories': ['repo'],
  962. 'atime': option_value,
  963. 'exclude_patterns': None,
  964. },
  965. storage_config={},
  966. local_borg_version='1.2.3',
  967. )
  968. @pytest.mark.parametrize(
  969. 'option_value,feature_available,option_flag',
  970. (
  971. (True, True, None),
  972. (True, False, None),
  973. (False, True, '--noflags'),
  974. (False, False, '--nobsdflags'),
  975. ),
  976. )
  977. def test_create_archive_with_flags_option_calls_borg_with_corresponding_parameter(
  978. option_value, feature_available, option_flag
  979. ):
  980. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  981. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  982. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  983. flexmock(module).should_receive('expand_directories').and_return(())
  984. flexmock(module).should_receive('pattern_root_directories').and_return([])
  985. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  986. flexmock(module).should_receive('expand_home_directories').and_return(())
  987. flexmock(module).should_receive('write_pattern_file').and_return(None)
  988. flexmock(module.feature).should_receive('available').and_return(feature_available)
  989. flexmock(module).should_receive('ensure_files_readable')
  990. flexmock(module).should_receive('make_pattern_flags').and_return(())
  991. flexmock(module).should_receive('make_exclude_flags').and_return(())
  992. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  993. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  994. )
  995. flexmock(module.environment).should_receive('make_environment')
  996. flexmock(module).should_receive('execute_command').with_args(
  997. ('borg', 'create') + ((option_flag,) if option_flag else ()) + REPO_ARCHIVE_WITH_PATHS,
  998. output_log_level=logging.INFO,
  999. output_file=None,
  1000. borg_local_path='borg',
  1001. working_directory=None,
  1002. extra_environment=None,
  1003. )
  1004. module.create_archive(
  1005. dry_run=False,
  1006. repository='repo',
  1007. location_config={
  1008. 'source_directories': ['foo', 'bar'],
  1009. 'repositories': ['repo'],
  1010. 'flags': option_value,
  1011. 'exclude_patterns': None,
  1012. },
  1013. storage_config={},
  1014. local_borg_version='1.2.3',
  1015. )
  1016. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  1017. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1018. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1019. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1020. flexmock(module).should_receive('expand_directories').and_return(())
  1021. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1022. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1023. flexmock(module).should_receive('expand_home_directories').and_return(())
  1024. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1025. flexmock(module.feature).should_receive('available').and_return(True)
  1026. flexmock(module).should_receive('ensure_files_readable')
  1027. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1028. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1029. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1030. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1031. )
  1032. flexmock(module.environment).should_receive('make_environment')
  1033. flexmock(module).should_receive('execute_command').with_args(
  1034. ('borg', 'create', '--files-cache', 'ctime,size') + REPO_ARCHIVE_WITH_PATHS,
  1035. output_log_level=logging.INFO,
  1036. output_file=None,
  1037. borg_local_path='borg',
  1038. working_directory=None,
  1039. extra_environment=None,
  1040. )
  1041. module.create_archive(
  1042. dry_run=False,
  1043. repository='repo',
  1044. location_config={
  1045. 'source_directories': ['foo', 'bar'],
  1046. 'repositories': ['repo'],
  1047. 'files_cache': 'ctime,size',
  1048. 'exclude_patterns': None,
  1049. },
  1050. storage_config={},
  1051. local_borg_version='1.2.3',
  1052. )
  1053. def test_create_archive_with_local_path_calls_borg_via_local_path():
  1054. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1055. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1056. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1057. flexmock(module).should_receive('expand_directories').and_return(())
  1058. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1059. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1060. flexmock(module).should_receive('expand_home_directories').and_return(())
  1061. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1062. flexmock(module.feature).should_receive('available').and_return(True)
  1063. flexmock(module).should_receive('ensure_files_readable')
  1064. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1065. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1066. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1067. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1068. )
  1069. flexmock(module.environment).should_receive('make_environment')
  1070. flexmock(module).should_receive('execute_command').with_args(
  1071. ('borg1', 'create') + REPO_ARCHIVE_WITH_PATHS,
  1072. output_log_level=logging.INFO,
  1073. output_file=None,
  1074. borg_local_path='borg1',
  1075. working_directory=None,
  1076. extra_environment=None,
  1077. )
  1078. module.create_archive(
  1079. dry_run=False,
  1080. repository='repo',
  1081. location_config={
  1082. 'source_directories': ['foo', 'bar'],
  1083. 'repositories': ['repo'],
  1084. 'exclude_patterns': None,
  1085. },
  1086. storage_config={},
  1087. local_borg_version='1.2.3',
  1088. local_path='borg1',
  1089. )
  1090. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  1091. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1092. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1093. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1094. flexmock(module).should_receive('expand_directories').and_return(())
  1095. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1096. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1097. flexmock(module).should_receive('expand_home_directories').and_return(())
  1098. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1099. flexmock(module.feature).should_receive('available').and_return(True)
  1100. flexmock(module).should_receive('ensure_files_readable')
  1101. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1102. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1103. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1104. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1105. )
  1106. flexmock(module.environment).should_receive('make_environment')
  1107. flexmock(module).should_receive('execute_command').with_args(
  1108. ('borg', 'create', '--remote-path', 'borg1') + REPO_ARCHIVE_WITH_PATHS,
  1109. output_log_level=logging.INFO,
  1110. output_file=None,
  1111. borg_local_path='borg',
  1112. working_directory=None,
  1113. extra_environment=None,
  1114. )
  1115. module.create_archive(
  1116. dry_run=False,
  1117. repository='repo',
  1118. location_config={
  1119. 'source_directories': ['foo', 'bar'],
  1120. 'repositories': ['repo'],
  1121. 'exclude_patterns': None,
  1122. },
  1123. storage_config={},
  1124. local_borg_version='1.2.3',
  1125. remote_path='borg1',
  1126. )
  1127. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  1128. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1129. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1130. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1131. flexmock(module).should_receive('expand_directories').and_return(())
  1132. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1133. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1134. flexmock(module).should_receive('expand_home_directories').and_return(())
  1135. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1136. flexmock(module.feature).should_receive('available').and_return(True)
  1137. flexmock(module).should_receive('ensure_files_readable')
  1138. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1139. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1140. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1141. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1142. )
  1143. flexmock(module.environment).should_receive('make_environment')
  1144. flexmock(module).should_receive('execute_command').with_args(
  1145. ('borg', 'create', '--umask', '740') + REPO_ARCHIVE_WITH_PATHS,
  1146. output_log_level=logging.INFO,
  1147. output_file=None,
  1148. borg_local_path='borg',
  1149. working_directory=None,
  1150. extra_environment=None,
  1151. )
  1152. module.create_archive(
  1153. dry_run=False,
  1154. repository='repo',
  1155. location_config={
  1156. 'source_directories': ['foo', 'bar'],
  1157. 'repositories': ['repo'],
  1158. 'exclude_patterns': None,
  1159. },
  1160. storage_config={'umask': 740},
  1161. local_borg_version='1.2.3',
  1162. )
  1163. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  1164. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1165. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1166. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1167. flexmock(module).should_receive('expand_directories').and_return(())
  1168. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1169. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1170. flexmock(module).should_receive('expand_home_directories').and_return(())
  1171. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1172. flexmock(module.feature).should_receive('available').and_return(True)
  1173. flexmock(module).should_receive('ensure_files_readable')
  1174. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1175. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1176. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1177. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1178. )
  1179. flexmock(module.environment).should_receive('make_environment')
  1180. flexmock(module).should_receive('execute_command').with_args(
  1181. ('borg', 'create', '--lock-wait', '5') + REPO_ARCHIVE_WITH_PATHS,
  1182. output_log_level=logging.INFO,
  1183. output_file=None,
  1184. borg_local_path='borg',
  1185. working_directory=None,
  1186. extra_environment=None,
  1187. )
  1188. module.create_archive(
  1189. dry_run=False,
  1190. repository='repo',
  1191. location_config={
  1192. 'source_directories': ['foo', 'bar'],
  1193. 'repositories': ['repo'],
  1194. 'exclude_patterns': None,
  1195. },
  1196. storage_config={'lock_wait': 5},
  1197. local_borg_version='1.2.3',
  1198. )
  1199. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  1200. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1201. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1202. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1203. flexmock(module).should_receive('expand_directories').and_return(())
  1204. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1205. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1206. flexmock(module).should_receive('expand_home_directories').and_return(())
  1207. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1208. flexmock(module.feature).should_receive('available').and_return(True)
  1209. flexmock(module).should_receive('ensure_files_readable')
  1210. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1211. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1212. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1213. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1214. )
  1215. flexmock(module.environment).should_receive('make_environment')
  1216. flexmock(module).should_receive('execute_command').with_args(
  1217. ('borg', 'create', '--stats') + REPO_ARCHIVE_WITH_PATHS,
  1218. output_log_level=logging.WARNING,
  1219. output_file=None,
  1220. borg_local_path='borg',
  1221. working_directory=None,
  1222. extra_environment=None,
  1223. )
  1224. module.create_archive(
  1225. dry_run=False,
  1226. repository='repo',
  1227. location_config={
  1228. 'source_directories': ['foo', 'bar'],
  1229. 'repositories': ['repo'],
  1230. 'exclude_patterns': None,
  1231. },
  1232. storage_config={},
  1233. local_borg_version='1.2.3',
  1234. stats=True,
  1235. )
  1236. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  1237. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1238. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1239. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1240. flexmock(module).should_receive('expand_directories').and_return(())
  1241. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1242. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1243. flexmock(module).should_receive('expand_home_directories').and_return(())
  1244. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1245. flexmock(module.feature).should_receive('available').and_return(True)
  1246. flexmock(module).should_receive('ensure_files_readable')
  1247. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1248. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1249. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1250. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1251. )
  1252. flexmock(module.environment).should_receive('make_environment')
  1253. flexmock(module).should_receive('execute_command').with_args(
  1254. ('borg', 'create', '--info', '--stats') + REPO_ARCHIVE_WITH_PATHS,
  1255. output_log_level=logging.INFO,
  1256. output_file=None,
  1257. borg_local_path='borg',
  1258. working_directory=None,
  1259. extra_environment=None,
  1260. )
  1261. insert_logging_mock(logging.INFO)
  1262. module.create_archive(
  1263. dry_run=False,
  1264. repository='repo',
  1265. location_config={
  1266. 'source_directories': ['foo', 'bar'],
  1267. 'repositories': ['repo'],
  1268. 'exclude_patterns': None,
  1269. },
  1270. storage_config={},
  1271. local_borg_version='1.2.3',
  1272. stats=True,
  1273. )
  1274. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  1275. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1276. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1277. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1278. flexmock(module).should_receive('expand_directories').and_return(())
  1279. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1280. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1281. flexmock(module).should_receive('expand_home_directories').and_return(())
  1282. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1283. flexmock(module.feature).should_receive('available').and_return(True)
  1284. flexmock(module).should_receive('ensure_files_readable')
  1285. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1286. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1287. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1288. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1289. )
  1290. flexmock(module.environment).should_receive('make_environment')
  1291. flexmock(module).should_receive('execute_command').with_args(
  1292. ('borg', 'create', '--list', '--filter', 'AME-') + REPO_ARCHIVE_WITH_PATHS,
  1293. output_log_level=logging.WARNING,
  1294. output_file=None,
  1295. borg_local_path='borg',
  1296. working_directory=None,
  1297. extra_environment=None,
  1298. )
  1299. module.create_archive(
  1300. dry_run=False,
  1301. repository='repo',
  1302. location_config={
  1303. 'source_directories': ['foo', 'bar'],
  1304. 'repositories': ['repo'],
  1305. 'exclude_patterns': None,
  1306. },
  1307. storage_config={},
  1308. local_borg_version='1.2.3',
  1309. list_files=True,
  1310. )
  1311. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1312. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1313. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1314. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1315. flexmock(module).should_receive('expand_directories').and_return(())
  1316. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1317. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1318. flexmock(module).should_receive('expand_home_directories').and_return(())
  1319. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1320. flexmock(module.feature).should_receive('available').and_return(True)
  1321. flexmock(module).should_receive('ensure_files_readable')
  1322. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1323. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1324. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1325. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1326. )
  1327. flexmock(module.environment).should_receive('make_environment')
  1328. flexmock(module).should_receive('execute_command').with_args(
  1329. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + REPO_ARCHIVE_WITH_PATHS,
  1330. output_log_level=logging.INFO,
  1331. output_file=None,
  1332. borg_local_path='borg',
  1333. working_directory=None,
  1334. extra_environment=None,
  1335. )
  1336. insert_logging_mock(logging.INFO)
  1337. module.create_archive(
  1338. dry_run=False,
  1339. repository='repo',
  1340. location_config={
  1341. 'source_directories': ['foo', 'bar'],
  1342. 'repositories': ['repo'],
  1343. 'exclude_patterns': None,
  1344. },
  1345. storage_config={},
  1346. local_borg_version='1.2.3',
  1347. list_files=True,
  1348. )
  1349. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1350. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1351. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1352. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1353. flexmock(module).should_receive('expand_directories').and_return(())
  1354. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1355. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1356. flexmock(module).should_receive('expand_home_directories').and_return(())
  1357. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1358. flexmock(module.feature).should_receive('available').and_return(True)
  1359. flexmock(module).should_receive('ensure_files_readable')
  1360. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1361. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1362. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1363. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1364. )
  1365. flexmock(module.environment).should_receive('make_environment')
  1366. flexmock(module).should_receive('execute_command').with_args(
  1367. ('borg', 'create', '--info', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1368. output_log_level=logging.INFO,
  1369. output_file=module.DO_NOT_CAPTURE,
  1370. borg_local_path='borg',
  1371. working_directory=None,
  1372. extra_environment=None,
  1373. )
  1374. insert_logging_mock(logging.INFO)
  1375. module.create_archive(
  1376. dry_run=False,
  1377. repository='repo',
  1378. location_config={
  1379. 'source_directories': ['foo', 'bar'],
  1380. 'repositories': ['repo'],
  1381. 'exclude_patterns': None,
  1382. },
  1383. storage_config={},
  1384. local_borg_version='1.2.3',
  1385. progress=True,
  1386. )
  1387. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1388. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1389. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1390. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1391. flexmock(module).should_receive('expand_directories').and_return(())
  1392. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1393. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1394. flexmock(module).should_receive('expand_home_directories').and_return(())
  1395. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1396. flexmock(module.feature).should_receive('available').and_return(True)
  1397. flexmock(module).should_receive('ensure_files_readable')
  1398. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1399. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1400. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1401. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1402. )
  1403. flexmock(module.environment).should_receive('make_environment')
  1404. flexmock(module).should_receive('execute_command').with_args(
  1405. ('borg', 'create', '--progress') + REPO_ARCHIVE_WITH_PATHS,
  1406. output_log_level=logging.INFO,
  1407. output_file=module.DO_NOT_CAPTURE,
  1408. borg_local_path='borg',
  1409. working_directory=None,
  1410. extra_environment=None,
  1411. )
  1412. module.create_archive(
  1413. dry_run=False,
  1414. repository='repo',
  1415. location_config={
  1416. 'source_directories': ['foo', 'bar'],
  1417. 'repositories': ['repo'],
  1418. 'exclude_patterns': None,
  1419. },
  1420. storage_config={},
  1421. local_borg_version='1.2.3',
  1422. progress=True,
  1423. )
  1424. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1425. processes = flexmock()
  1426. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1427. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1428. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1429. flexmock(module).should_receive('expand_directories').and_return(())
  1430. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1431. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1432. flexmock(module).should_receive('expand_home_directories').and_return(())
  1433. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1434. flexmock(module.feature).should_receive('available').and_return(True)
  1435. flexmock(module).should_receive('ensure_files_readable')
  1436. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1437. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1438. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1439. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1440. )
  1441. flexmock(module.environment).should_receive('make_environment')
  1442. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1443. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  1444. + REPO_ARCHIVE_WITH_PATHS,
  1445. processes=processes,
  1446. output_log_level=logging.INFO,
  1447. output_file=module.DO_NOT_CAPTURE,
  1448. borg_local_path='borg',
  1449. working_directory=None,
  1450. extra_environment=None,
  1451. )
  1452. module.create_archive(
  1453. dry_run=False,
  1454. repository='repo',
  1455. location_config={
  1456. 'source_directories': ['foo', 'bar'],
  1457. 'repositories': ['repo'],
  1458. 'exclude_patterns': None,
  1459. },
  1460. storage_config={},
  1461. local_borg_version='1.2.3',
  1462. progress=True,
  1463. stream_processes=processes,
  1464. )
  1465. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1466. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1467. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1468. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1469. flexmock(module).should_receive('expand_directories').and_return(())
  1470. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1471. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1472. flexmock(module).should_receive('expand_home_directories').and_return(())
  1473. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1474. flexmock(module.feature).should_receive('available').and_return(True)
  1475. flexmock(module).should_receive('ensure_files_readable')
  1476. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1477. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1478. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1479. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1480. )
  1481. flexmock(module.environment).should_receive('make_environment')
  1482. flexmock(module).should_receive('execute_command').with_args(
  1483. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1484. output_log_level=None,
  1485. output_file=None,
  1486. borg_local_path='borg',
  1487. working_directory=None,
  1488. extra_environment=None,
  1489. ).and_return('[]')
  1490. json_output = module.create_archive(
  1491. dry_run=False,
  1492. repository='repo',
  1493. location_config={
  1494. 'source_directories': ['foo', 'bar'],
  1495. 'repositories': ['repo'],
  1496. 'exclude_patterns': None,
  1497. },
  1498. storage_config={},
  1499. local_borg_version='1.2.3',
  1500. json=True,
  1501. )
  1502. assert json_output == '[]'
  1503. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1504. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1505. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1506. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1507. flexmock(module).should_receive('expand_directories').and_return(())
  1508. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1509. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1510. flexmock(module).should_receive('expand_home_directories').and_return(())
  1511. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1512. flexmock(module.feature).should_receive('available').and_return(True)
  1513. flexmock(module).should_receive('ensure_files_readable')
  1514. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1515. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1516. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1517. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1518. )
  1519. flexmock(module.environment).should_receive('make_environment')
  1520. flexmock(module).should_receive('execute_command').with_args(
  1521. ('borg', 'create', '--json') + REPO_ARCHIVE_WITH_PATHS,
  1522. output_log_level=None,
  1523. output_file=None,
  1524. borg_local_path='borg',
  1525. working_directory=None,
  1526. extra_environment=None,
  1527. ).and_return('[]')
  1528. json_output = module.create_archive(
  1529. dry_run=False,
  1530. repository='repo',
  1531. location_config={
  1532. 'source_directories': ['foo', 'bar'],
  1533. 'repositories': ['repo'],
  1534. 'exclude_patterns': None,
  1535. },
  1536. storage_config={},
  1537. local_borg_version='1.2.3',
  1538. json=True,
  1539. stats=True,
  1540. )
  1541. assert json_output == '[]'
  1542. def test_create_archive_with_source_directories_glob_expands():
  1543. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1544. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1545. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1546. flexmock(module).should_receive('expand_directories').and_return(())
  1547. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1548. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1549. flexmock(module).should_receive('expand_home_directories').and_return(())
  1550. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1551. flexmock(module.feature).should_receive('available').and_return(True)
  1552. flexmock(module).should_receive('ensure_files_readable')
  1553. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1554. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1555. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1556. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1557. )
  1558. flexmock(module.environment).should_receive('make_environment')
  1559. flexmock(module).should_receive('execute_command').with_args(
  1560. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1561. output_log_level=logging.INFO,
  1562. output_file=None,
  1563. borg_local_path='borg',
  1564. working_directory=None,
  1565. extra_environment=None,
  1566. )
  1567. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1568. module.create_archive(
  1569. dry_run=False,
  1570. repository='repo',
  1571. location_config={
  1572. 'source_directories': ['foo*'],
  1573. 'repositories': ['repo'],
  1574. 'exclude_patterns': None,
  1575. },
  1576. storage_config={},
  1577. local_borg_version='1.2.3',
  1578. )
  1579. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1580. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1581. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1582. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1583. flexmock(module).should_receive('expand_directories').and_return(())
  1584. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1585. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1586. flexmock(module).should_receive('expand_home_directories').and_return(())
  1587. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1588. flexmock(module.feature).should_receive('available').and_return(True)
  1589. flexmock(module).should_receive('ensure_files_readable')
  1590. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1591. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1592. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1593. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1594. )
  1595. flexmock(module.environment).should_receive('make_environment')
  1596. flexmock(module).should_receive('execute_command').with_args(
  1597. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1598. output_log_level=logging.INFO,
  1599. output_file=None,
  1600. borg_local_path='borg',
  1601. working_directory=None,
  1602. extra_environment=None,
  1603. )
  1604. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1605. module.create_archive(
  1606. dry_run=False,
  1607. repository='repo',
  1608. location_config={
  1609. 'source_directories': ['foo*'],
  1610. 'repositories': ['repo'],
  1611. 'exclude_patterns': None,
  1612. },
  1613. storage_config={},
  1614. local_borg_version='1.2.3',
  1615. )
  1616. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1617. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1618. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1619. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1620. flexmock(module).should_receive('expand_directories').and_return(())
  1621. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1622. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1623. flexmock(module).should_receive('expand_home_directories').and_return(())
  1624. flexmock(module).should_receive('write_pattern_file').and_return(None)
  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(())
  1629. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1630. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1631. )
  1632. flexmock(module.environment).should_receive('make_environment')
  1633. flexmock(module).should_receive('execute_command').with_args(
  1634. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1635. output_log_level=logging.INFO,
  1636. output_file=None,
  1637. borg_local_path='borg',
  1638. working_directory=None,
  1639. extra_environment=None,
  1640. )
  1641. module.create_archive(
  1642. dry_run=False,
  1643. repository='repo',
  1644. location_config={
  1645. 'source_directories': ['foo*'],
  1646. 'repositories': ['repo'],
  1647. 'exclude_patterns': None,
  1648. },
  1649. storage_config={},
  1650. local_borg_version='1.2.3',
  1651. )
  1652. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1653. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1654. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1655. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1656. flexmock(module).should_receive('expand_directories').and_return(())
  1657. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1658. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1659. flexmock(module).should_receive('expand_home_directories').and_return(())
  1660. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1661. flexmock(module.feature).should_receive('available').and_return(True)
  1662. flexmock(module).should_receive('ensure_files_readable')
  1663. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1664. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1665. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1666. ('repo::ARCHIVE_NAME',)
  1667. )
  1668. flexmock(module.environment).should_receive('make_environment')
  1669. flexmock(module).should_receive('execute_command').with_args(
  1670. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1671. output_log_level=logging.INFO,
  1672. output_file=None,
  1673. borg_local_path='borg',
  1674. working_directory=None,
  1675. extra_environment=None,
  1676. )
  1677. module.create_archive(
  1678. dry_run=False,
  1679. repository='repo',
  1680. location_config={
  1681. 'source_directories': ['foo', 'bar'],
  1682. 'repositories': ['repo'],
  1683. 'exclude_patterns': None,
  1684. },
  1685. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1686. local_borg_version='1.2.3',
  1687. )
  1688. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1689. repository_archive_pattern = 'repo::Documents_{hostname}-{now}'
  1690. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1691. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1692. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1693. flexmock(module).should_receive('expand_directories').and_return(())
  1694. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1695. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1696. flexmock(module).should_receive('expand_home_directories').and_return(())
  1697. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1698. flexmock(module.feature).should_receive('available').and_return(True)
  1699. flexmock(module).should_receive('ensure_files_readable')
  1700. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1701. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1702. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1703. (repository_archive_pattern,)
  1704. )
  1705. flexmock(module.environment).should_receive('make_environment')
  1706. flexmock(module).should_receive('execute_command').with_args(
  1707. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1708. output_log_level=logging.INFO,
  1709. output_file=None,
  1710. borg_local_path='borg',
  1711. working_directory=None,
  1712. extra_environment=None,
  1713. )
  1714. module.create_archive(
  1715. dry_run=False,
  1716. repository='repo',
  1717. location_config={
  1718. 'source_directories': ['foo', 'bar'],
  1719. 'repositories': ['repo'],
  1720. 'exclude_patterns': None,
  1721. },
  1722. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1723. local_borg_version='1.2.3',
  1724. )
  1725. def test_create_archive_with_repository_accepts_borg_placeholders():
  1726. repository_archive_pattern = '{fqdn}::Documents_{hostname}-{now}'
  1727. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1728. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1729. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1730. flexmock(module).should_receive('expand_directories').and_return(())
  1731. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1732. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1733. flexmock(module).should_receive('expand_home_directories').and_return(())
  1734. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1735. flexmock(module.feature).should_receive('available').and_return(True)
  1736. flexmock(module).should_receive('ensure_files_readable')
  1737. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1738. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1739. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1740. (repository_archive_pattern,)
  1741. )
  1742. flexmock(module.environment).should_receive('make_environment')
  1743. flexmock(module).should_receive('execute_command').with_args(
  1744. ('borg', 'create', repository_archive_pattern, 'foo', 'bar'),
  1745. output_log_level=logging.INFO,
  1746. output_file=None,
  1747. borg_local_path='borg',
  1748. working_directory=None,
  1749. extra_environment=None,
  1750. )
  1751. module.create_archive(
  1752. dry_run=False,
  1753. repository='{fqdn}',
  1754. location_config={
  1755. 'source_directories': ['foo', 'bar'],
  1756. 'repositories': ['{fqdn}'],
  1757. 'exclude_patterns': None,
  1758. },
  1759. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1760. local_borg_version='1.2.3',
  1761. )
  1762. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1763. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1764. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1765. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1766. flexmock(module).should_receive('expand_directories').and_return(())
  1767. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1768. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1769. flexmock(module).should_receive('expand_home_directories').and_return(())
  1770. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1771. flexmock(module.feature).should_receive('available').and_return(True)
  1772. flexmock(module).should_receive('ensure_files_readable')
  1773. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1774. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1775. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1776. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1777. )
  1778. flexmock(module.environment).should_receive('make_environment')
  1779. flexmock(module).should_receive('execute_command').with_args(
  1780. ('borg', 'create', '--extra', '--options') + REPO_ARCHIVE_WITH_PATHS,
  1781. output_log_level=logging.INFO,
  1782. output_file=None,
  1783. borg_local_path='borg',
  1784. working_directory=None,
  1785. extra_environment=None,
  1786. )
  1787. module.create_archive(
  1788. dry_run=False,
  1789. repository='repo',
  1790. location_config={
  1791. 'source_directories': ['foo', 'bar'],
  1792. 'repositories': ['repo'],
  1793. 'exclude_patterns': None,
  1794. },
  1795. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1796. local_borg_version='1.2.3',
  1797. )
  1798. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1799. processes = flexmock()
  1800. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1801. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1802. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1803. flexmock(module).should_receive('expand_directories').and_return(())
  1804. flexmock(module).should_receive('pattern_root_directories').and_return([])
  1805. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1806. flexmock(module).should_receive('expand_home_directories').and_return(())
  1807. flexmock(module).should_receive('write_pattern_file').and_return(None)
  1808. flexmock(module.feature).should_receive('available').and_return(True)
  1809. flexmock(module).should_receive('ensure_files_readable')
  1810. flexmock(module).should_receive('make_pattern_flags').and_return(())
  1811. flexmock(module).should_receive('make_exclude_flags').and_return(())
  1812. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  1813. (f'repo::{DEFAULT_ARCHIVE_NAME}',)
  1814. )
  1815. flexmock(module.environment).should_receive('make_environment')
  1816. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1817. ('borg', 'create', '--one-file-system', '--read-special') + REPO_ARCHIVE_WITH_PATHS,
  1818. processes=processes,
  1819. output_log_level=logging.INFO,
  1820. output_file=None,
  1821. borg_local_path='borg',
  1822. working_directory=None,
  1823. extra_environment=None,
  1824. )
  1825. module.create_archive(
  1826. dry_run=False,
  1827. repository='repo',
  1828. location_config={
  1829. 'source_directories': ['foo', 'bar'],
  1830. 'repositories': ['repo'],
  1831. 'exclude_patterns': None,
  1832. },
  1833. storage_config={},
  1834. local_borg_version='1.2.3',
  1835. stream_processes=processes,
  1836. )