test_create.py 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import create as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_expand_directory_with_basic_path_passes_it_through():
  7. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  8. flexmock(module.glob).should_receive('glob').and_return([])
  9. paths = module._expand_directory('foo')
  10. assert paths == ['foo']
  11. def test_expand_directory_with_glob_expands():
  12. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  13. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  14. paths = module._expand_directory('foo*')
  15. assert paths == ['foo', 'food']
  16. def test_expand_directories_flattens_expanded_directories():
  17. flexmock(module).should_receive('_expand_directory').with_args('~/foo').and_return(
  18. ['/root/foo']
  19. )
  20. flexmock(module).should_receive('_expand_directory').with_args('bar*').and_return(
  21. ['bar', 'barf']
  22. )
  23. paths = module._expand_directories(('~/foo', 'bar*'))
  24. assert paths == ('/root/foo', 'bar', 'barf')
  25. def test_expand_directories_considers_none_as_no_directories():
  26. paths = module._expand_directories(None)
  27. assert paths == ()
  28. def test_expand_home_directories_expands_tildes():
  29. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  30. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  31. paths = module._expand_home_directories(('~/bar', 'baz'))
  32. assert paths == ('/foo/bar', 'baz')
  33. def test_expand_home_directories_considers_none_as_no_directories():
  34. paths = module._expand_home_directories(None)
  35. assert paths == ()
  36. def test_map_directories_to_devices_gives_device_id_per_path():
  37. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  38. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=66))
  39. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  40. assert device_map == {
  41. '/foo': 55,
  42. '/bar': 66,
  43. }
  44. def test_map_directories_to_devices_with_missing_path_does_not_error():
  45. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  46. flexmock(module.os).should_receive('stat').with_args('/bar').and_raise(FileNotFoundError)
  47. device_map = module.map_directories_to_devices(('/foo', '/bar'))
  48. assert device_map == {
  49. '/foo': 55,
  50. '/bar': None,
  51. }
  52. @pytest.mark.parametrize(
  53. 'directories,expected_directories',
  54. (
  55. ({'/': 1, '/root': 1}, ('/',)),
  56. ({'/': 1, '/root/': 1}, ('/',)),
  57. ({'/': 1, '/root': 2}, ('/', '/root')),
  58. ({'/root': 1, '/': 1}, ('/',)),
  59. ({'/root': 1, '/root/foo': 1}, ('/root',)),
  60. ({'/root/': 1, '/root/foo': 1}, ('/root/',)),
  61. ({'/root': 1, '/root/foo/': 1}, ('/root',)),
  62. ({'/root': 1, '/root/foo': 2}, ('/root', '/root/foo')),
  63. ({'/root/foo': 1, '/root': 1}, ('/root',)),
  64. ({'/root': None, '/root/foo': None}, ('/root', '/root/foo')),
  65. ({'/root': 1, '/etc': 1, '/root/foo/bar': 1}, ('/etc', '/root')),
  66. ({'/root': 1, '/root/foo': 1, '/root/foo/bar': 1}, ('/root',)),
  67. ({'/dup': 1, '/dup': 1}, ('/dup',)),
  68. ({'/foo': 1, '/bar': 1}, ('/bar', '/foo')),
  69. ({'/foo': 1, '/bar': 2}, ('/bar', '/foo')),
  70. ),
  71. )
  72. def test_deduplicate_directories_removes_child_paths_on_the_same_filesystem(
  73. directories, expected_directories
  74. ):
  75. assert module.deduplicate_directories(directories) == expected_directories
  76. def test_write_pattern_file_does_not_raise():
  77. temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
  78. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  79. module._write_pattern_file(['exclude'])
  80. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  81. module._write_pattern_file([])
  82. def test_make_pattern_flags_includes_pattern_filename_when_given():
  83. pattern_flags = module._make_pattern_flags(
  84. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  85. )
  86. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  87. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  88. pattern_flags = module._make_pattern_flags(
  89. location_config={'patterns_from': ['patterns', 'other']}
  90. )
  91. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  92. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  93. pattern_flags = module._make_pattern_flags(
  94. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  95. )
  96. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  97. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  98. pattern_flags = module._make_pattern_flags(location_config={'patterns_from': None})
  99. assert pattern_flags == ()
  100. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  101. exclude_flags = module._make_exclude_flags(
  102. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  103. )
  104. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  105. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  106. exclude_flags = module._make_exclude_flags(
  107. location_config={'exclude_from': ['excludes', 'other']}
  108. )
  109. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  110. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  111. exclude_flags = module._make_exclude_flags(
  112. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  113. )
  114. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  115. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  116. exclude_flags = module._make_exclude_flags(location_config={'exclude_from': None})
  117. assert exclude_flags == ()
  118. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  119. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': True})
  120. assert exclude_flags == ('--exclude-caches',)
  121. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  122. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': False})
  123. assert exclude_flags == ()
  124. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  125. exclude_flags = module._make_exclude_flags(
  126. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  127. )
  128. assert exclude_flags == (
  129. '--exclude-if-present',
  130. 'exclude_me',
  131. '--exclude-if-present',
  132. 'also_me',
  133. )
  134. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  135. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': True})
  136. assert exclude_flags == ('--keep-exclude-tags',)
  137. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  138. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': False})
  139. assert exclude_flags == ()
  140. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  141. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': True})
  142. assert exclude_flags == ('--exclude-nodump',)
  143. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  144. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': False})
  145. assert exclude_flags == ()
  146. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  147. exclude_flags = module._make_exclude_flags(location_config={})
  148. assert exclude_flags == ()
  149. def test_borgmatic_source_directories_set_when_directory_exists():
  150. flexmock(module.os.path).should_receive('exists').and_return(True)
  151. flexmock(module.os.path).should_receive('expanduser')
  152. assert module.borgmatic_source_directories('/tmp') == ['/tmp']
  153. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  154. flexmock(module.os.path).should_receive('exists').and_return(False)
  155. flexmock(module.os.path).should_receive('expanduser')
  156. assert module.borgmatic_source_directories('/tmp') == []
  157. def test_borgmatic_source_directories_defaults_when_directory_not_given():
  158. flexmock(module.os.path).should_receive('exists').and_return(True)
  159. flexmock(module.os.path).should_receive('expanduser')
  160. assert module.borgmatic_source_directories(None) == [module.DEFAULT_BORGMATIC_SOURCE_DIRECTORY]
  161. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  162. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  163. def test_create_archive_calls_borg_with_parameters():
  164. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  165. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  166. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  167. flexmock(module).should_receive('_expand_directories').and_return(())
  168. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  169. flexmock(module).should_receive('_expand_home_directories').and_return(())
  170. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  171. flexmock(module.feature).should_receive('available').and_return(True)
  172. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  173. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  174. flexmock(module).should_receive('execute_command').with_args(
  175. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  176. output_log_level=logging.INFO,
  177. output_file=None,
  178. borg_local_path='borg',
  179. working_directory=None,
  180. )
  181. module.create_archive(
  182. dry_run=False,
  183. repository='repo',
  184. location_config={
  185. 'source_directories': ['foo', 'bar'],
  186. 'repositories': ['repo'],
  187. 'exclude_patterns': None,
  188. },
  189. storage_config={},
  190. local_borg_version='1.2.3',
  191. )
  192. def test_create_archive_with_patterns_calls_borg_with_patterns():
  193. pattern_flags = ('--patterns-from', 'patterns')
  194. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  195. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  196. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  197. flexmock(module).should_receive('_expand_directories').and_return(())
  198. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  199. flexmock(module).should_receive('_expand_home_directories').and_return(())
  200. flexmock(module).should_receive('_write_pattern_file').and_return(
  201. flexmock(name='/tmp/patterns')
  202. ).and_return(None)
  203. flexmock(module.feature).should_receive('available').and_return(True)
  204. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  205. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  206. flexmock(module).should_receive('execute_command').with_args(
  207. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  208. output_log_level=logging.INFO,
  209. output_file=None,
  210. borg_local_path='borg',
  211. working_directory=None,
  212. )
  213. module.create_archive(
  214. dry_run=False,
  215. repository='repo',
  216. location_config={
  217. 'source_directories': ['foo', 'bar'],
  218. 'repositories': ['repo'],
  219. 'patterns': ['pattern'],
  220. },
  221. storage_config={},
  222. local_borg_version='1.2.3',
  223. )
  224. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  225. exclude_flags = ('--exclude-from', 'excludes')
  226. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  227. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  228. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  229. flexmock(module).should_receive('_expand_directories').and_return(())
  230. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  231. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  232. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  233. flexmock(name='/tmp/excludes')
  234. )
  235. flexmock(module.feature).should_receive('available').and_return(True)
  236. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  237. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  238. flexmock(module).should_receive('execute_command').with_args(
  239. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  240. output_log_level=logging.INFO,
  241. output_file=None,
  242. borg_local_path='borg',
  243. working_directory=None,
  244. )
  245. module.create_archive(
  246. dry_run=False,
  247. repository='repo',
  248. location_config={
  249. 'source_directories': ['foo', 'bar'],
  250. 'repositories': ['repo'],
  251. 'exclude_patterns': ['exclude'],
  252. },
  253. storage_config={},
  254. local_borg_version='1.2.3',
  255. )
  256. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  257. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  258. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  259. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  260. flexmock(module).should_receive('_expand_directories').and_return(())
  261. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  262. flexmock(module).should_receive('_expand_home_directories').and_return(())
  263. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  264. flexmock(module.feature).should_receive('available').and_return(True)
  265. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  266. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  267. flexmock(module).should_receive('execute_command').with_args(
  268. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  269. output_log_level=logging.INFO,
  270. output_file=None,
  271. borg_local_path='borg',
  272. working_directory=None,
  273. )
  274. insert_logging_mock(logging.INFO)
  275. module.create_archive(
  276. dry_run=False,
  277. repository='repo',
  278. location_config={
  279. 'source_directories': ['foo', 'bar'],
  280. 'repositories': ['repo'],
  281. 'exclude_patterns': None,
  282. },
  283. storage_config={},
  284. local_borg_version='1.2.3',
  285. )
  286. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  287. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  288. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  289. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  290. flexmock(module).should_receive('_expand_directories').and_return(())
  291. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  292. flexmock(module).should_receive('_expand_home_directories').and_return(())
  293. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  294. flexmock(module.feature).should_receive('available').and_return(True)
  295. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  296. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  297. flexmock(module).should_receive('execute_command').with_args(
  298. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  299. output_log_level=None,
  300. output_file=None,
  301. borg_local_path='borg',
  302. working_directory=None,
  303. )
  304. insert_logging_mock(logging.INFO)
  305. module.create_archive(
  306. dry_run=False,
  307. repository='repo',
  308. location_config={
  309. 'source_directories': ['foo', 'bar'],
  310. 'repositories': ['repo'],
  311. 'exclude_patterns': None,
  312. },
  313. storage_config={},
  314. local_borg_version='1.2.3',
  315. json=True,
  316. )
  317. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  318. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  319. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  320. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  321. flexmock(module).should_receive('_expand_directories').and_return(())
  322. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  323. flexmock(module).should_receive('_expand_home_directories').and_return(())
  324. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  325. flexmock(module.feature).should_receive('available').and_return(True)
  326. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  327. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  328. flexmock(module).should_receive('execute_command').with_args(
  329. ('borg', 'create', '--debug', '--show-rc') + ARCHIVE_WITH_PATHS,
  330. output_log_level=logging.INFO,
  331. output_file=None,
  332. borg_local_path='borg',
  333. working_directory=None,
  334. )
  335. insert_logging_mock(logging.DEBUG)
  336. module.create_archive(
  337. dry_run=False,
  338. repository='repo',
  339. location_config={
  340. 'source_directories': ['foo', 'bar'],
  341. 'repositories': ['repo'],
  342. 'exclude_patterns': None,
  343. },
  344. storage_config={},
  345. local_borg_version='1.2.3',
  346. )
  347. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  348. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  349. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  350. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  351. flexmock(module).should_receive('_expand_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('_make_pattern_flags').and_return(())
  357. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  358. flexmock(module).should_receive('execute_command').with_args(
  359. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  360. output_log_level=None,
  361. output_file=None,
  362. borg_local_path='borg',
  363. working_directory=None,
  364. )
  365. insert_logging_mock(logging.DEBUG)
  366. module.create_archive(
  367. dry_run=False,
  368. repository='repo',
  369. location_config={
  370. 'source_directories': ['foo', 'bar'],
  371. 'repositories': ['repo'],
  372. 'exclude_patterns': None,
  373. },
  374. storage_config={},
  375. local_borg_version='1.2.3',
  376. json=True,
  377. )
  378. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  379. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  380. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  381. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  382. flexmock(module).should_receive('_expand_directories').and_return(())
  383. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  384. flexmock(module).should_receive('_expand_home_directories').and_return(())
  385. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  386. flexmock(module.feature).should_receive('available').and_return(True)
  387. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  388. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  389. flexmock(module).should_receive('execute_command').with_args(
  390. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  391. output_log_level=logging.INFO,
  392. output_file=None,
  393. borg_local_path='borg',
  394. working_directory=None,
  395. )
  396. module.create_archive(
  397. dry_run=True,
  398. repository='repo',
  399. location_config={
  400. 'source_directories': ['foo', 'bar'],
  401. 'repositories': ['repo'],
  402. 'exclude_patterns': None,
  403. },
  404. storage_config={},
  405. local_borg_version='1.2.3',
  406. )
  407. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  408. # --dry-run and --stats are mutually exclusive, see:
  409. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  410. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  411. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  412. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  413. flexmock(module).should_receive('_expand_directories').and_return(())
  414. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  415. flexmock(module).should_receive('_expand_home_directories').and_return(())
  416. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  417. flexmock(module.feature).should_receive('available').and_return(True)
  418. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  419. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  420. flexmock(module).should_receive('execute_command').with_args(
  421. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  422. output_log_level=logging.INFO,
  423. output_file=None,
  424. borg_local_path='borg',
  425. working_directory=None,
  426. )
  427. insert_logging_mock(logging.INFO)
  428. module.create_archive(
  429. dry_run=True,
  430. repository='repo',
  431. location_config={
  432. 'source_directories': ['foo', 'bar'],
  433. 'repositories': ['repo'],
  434. 'exclude_patterns': None,
  435. },
  436. storage_config={},
  437. local_borg_version='1.2.3',
  438. stats=True,
  439. )
  440. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  441. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  442. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  443. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  444. flexmock(module).should_receive('_expand_directories').and_return(())
  445. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  446. flexmock(module).should_receive('_expand_home_directories').and_return(())
  447. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  448. flexmock(module.feature).should_receive('available').and_return(True)
  449. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  450. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  451. flexmock(module).should_receive('execute_command').with_args(
  452. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  453. output_log_level=logging.INFO,
  454. output_file=None,
  455. borg_local_path='borg',
  456. working_directory=None,
  457. )
  458. module.create_archive(
  459. dry_run=False,
  460. repository='repo',
  461. location_config={
  462. 'source_directories': ['foo', 'bar'],
  463. 'repositories': ['repo'],
  464. 'exclude_patterns': None,
  465. },
  466. storage_config={'checkpoint_interval': 600},
  467. local_borg_version='1.2.3',
  468. )
  469. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  470. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  471. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  472. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  473. flexmock(module).should_receive('_expand_directories').and_return(())
  474. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  475. flexmock(module).should_receive('_expand_home_directories').and_return(())
  476. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  477. flexmock(module.feature).should_receive('available').and_return(True)
  478. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  479. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  480. flexmock(module).should_receive('execute_command').with_args(
  481. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  482. output_log_level=logging.INFO,
  483. output_file=None,
  484. borg_local_path='borg',
  485. working_directory=None,
  486. )
  487. module.create_archive(
  488. dry_run=False,
  489. repository='repo',
  490. location_config={
  491. 'source_directories': ['foo', 'bar'],
  492. 'repositories': ['repo'],
  493. 'exclude_patterns': None,
  494. },
  495. storage_config={'chunker_params': '1,2,3,4'},
  496. local_borg_version='1.2.3',
  497. )
  498. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  499. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  500. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  501. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  502. flexmock(module).should_receive('_expand_directories').and_return(())
  503. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  504. flexmock(module).should_receive('_expand_home_directories').and_return(())
  505. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  506. flexmock(module.feature).should_receive('available').and_return(True)
  507. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  508. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  509. flexmock(module).should_receive('execute_command').with_args(
  510. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  511. output_log_level=logging.INFO,
  512. output_file=None,
  513. borg_local_path='borg',
  514. working_directory=None,
  515. )
  516. module.create_archive(
  517. dry_run=False,
  518. repository='repo',
  519. location_config={
  520. 'source_directories': ['foo', 'bar'],
  521. 'repositories': ['repo'],
  522. 'exclude_patterns': None,
  523. },
  524. storage_config={'compression': 'rle'},
  525. local_borg_version='1.2.3',
  526. )
  527. @pytest.mark.parametrize(
  528. 'feature_available,option_flag', ((True, '--upload-ratelimit'), (False, '--remote-ratelimit')),
  529. )
  530. def test_create_archive_with_remote_rate_limit_calls_borg_with_upload_ratelimit_parameters(
  531. feature_available, option_flag
  532. ):
  533. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  534. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  535. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  536. flexmock(module).should_receive('_expand_directories').and_return(())
  537. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  538. flexmock(module).should_receive('_expand_home_directories').and_return(())
  539. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  540. flexmock(module.feature).should_receive('available').and_return(feature_available)
  541. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  542. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  543. flexmock(module).should_receive('execute_command').with_args(
  544. ('borg', 'create', option_flag, '100') + ARCHIVE_WITH_PATHS,
  545. output_log_level=logging.INFO,
  546. output_file=None,
  547. borg_local_path='borg',
  548. working_directory=None,
  549. )
  550. module.create_archive(
  551. dry_run=False,
  552. repository='repo',
  553. location_config={
  554. 'source_directories': ['foo', 'bar'],
  555. 'repositories': ['repo'],
  556. 'exclude_patterns': None,
  557. },
  558. storage_config={'remote_rate_limit': 100},
  559. local_borg_version='1.2.3',
  560. )
  561. def test_create_archive_with_working_directory_calls_borg_with_working_directory():
  562. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  563. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  564. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  565. flexmock(module).should_receive('_expand_directories').and_return(())
  566. flexmock(module.os.path).should_receive('expanduser').with_args('/working/dir').and_return(
  567. '/working/dir'
  568. )
  569. flexmock(module).should_receive('_expand_home_directories').and_return(())
  570. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  571. flexmock(module.feature).should_receive('available').and_return(True)
  572. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  573. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  574. flexmock(module).should_receive('execute_command').with_args(
  575. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  576. output_log_level=logging.INFO,
  577. output_file=None,
  578. borg_local_path='borg',
  579. working_directory='/working/dir',
  580. )
  581. module.create_archive(
  582. dry_run=False,
  583. repository='repo',
  584. location_config={
  585. 'source_directories': ['foo', 'bar'],
  586. 'repositories': ['repo'],
  587. 'working_directory': '/working/dir',
  588. 'exclude_patterns': None,
  589. },
  590. storage_config={},
  591. local_borg_version='1.2.3',
  592. )
  593. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  594. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  595. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  596. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  597. flexmock(module).should_receive('_expand_directories').and_return(())
  598. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  599. flexmock(module).should_receive('_expand_home_directories').and_return(())
  600. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  601. flexmock(module.feature).should_receive('available').and_return(True)
  602. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  603. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  604. flexmock(module).should_receive('execute_command').with_args(
  605. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  606. output_log_level=logging.INFO,
  607. output_file=None,
  608. borg_local_path='borg',
  609. working_directory=None,
  610. )
  611. module.create_archive(
  612. dry_run=False,
  613. repository='repo',
  614. location_config={
  615. 'source_directories': ['foo', 'bar'],
  616. 'repositories': ['repo'],
  617. 'one_file_system': True,
  618. 'exclude_patterns': None,
  619. },
  620. storage_config={},
  621. local_borg_version='1.2.3',
  622. )
  623. @pytest.mark.parametrize(
  624. 'feature_available,option_flag', ((True, '--numeric-ids'), (False, '--numeric-owner')),
  625. )
  626. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_ids_parameter(
  627. feature_available, option_flag
  628. ):
  629. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  630. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  631. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  632. flexmock(module).should_receive('_expand_directories').and_return(())
  633. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  634. flexmock(module).should_receive('_expand_home_directories').and_return(())
  635. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  636. flexmock(module.feature).should_receive('available').and_return(feature_available)
  637. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  638. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  639. flexmock(module).should_receive('execute_command').with_args(
  640. ('borg', 'create', option_flag) + ARCHIVE_WITH_PATHS,
  641. output_log_level=logging.INFO,
  642. output_file=None,
  643. borg_local_path='borg',
  644. working_directory=None,
  645. )
  646. module.create_archive(
  647. dry_run=False,
  648. repository='repo',
  649. location_config={
  650. 'source_directories': ['foo', 'bar'],
  651. 'repositories': ['repo'],
  652. 'numeric_owner': True,
  653. 'exclude_patterns': None,
  654. },
  655. storage_config={},
  656. local_borg_version='1.2.3',
  657. )
  658. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  659. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  660. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  661. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  662. flexmock(module).should_receive('_expand_directories').and_return(())
  663. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  664. flexmock(module).should_receive('_expand_home_directories').and_return(())
  665. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  666. flexmock(module.feature).should_receive('available').and_return(True)
  667. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  668. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  669. flexmock(module).should_receive('execute_command').with_args(
  670. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  671. output_log_level=logging.INFO,
  672. output_file=None,
  673. borg_local_path='borg',
  674. working_directory=None,
  675. )
  676. module.create_archive(
  677. dry_run=False,
  678. repository='repo',
  679. location_config={
  680. 'source_directories': ['foo', 'bar'],
  681. 'repositories': ['repo'],
  682. 'read_special': True,
  683. 'exclude_patterns': None,
  684. },
  685. storage_config={},
  686. local_borg_version='1.2.3',
  687. )
  688. @pytest.mark.parametrize(
  689. 'option_name,option_value',
  690. (('ctime', True), ('ctime', False), ('birthtime', True), ('birthtime', False),),
  691. )
  692. def test_create_archive_with_basic_option_calls_borg_with_corresponding_parameter(
  693. option_name, option_value
  694. ):
  695. option_flag = '--no' + option_name.replace('_', '') if option_value is False else None
  696. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  697. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  698. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  699. flexmock(module).should_receive('_expand_directories').and_return(())
  700. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  701. flexmock(module).should_receive('_expand_home_directories').and_return(())
  702. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  703. flexmock(module.feature).should_receive('available').and_return(True)
  704. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  705. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  706. flexmock(module).should_receive('execute_command').with_args(
  707. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  708. output_log_level=logging.INFO,
  709. output_file=None,
  710. borg_local_path='borg',
  711. working_directory=None,
  712. )
  713. module.create_archive(
  714. dry_run=False,
  715. repository='repo',
  716. location_config={
  717. 'source_directories': ['foo', 'bar'],
  718. 'repositories': ['repo'],
  719. option_name: option_value,
  720. 'exclude_patterns': None,
  721. },
  722. storage_config={},
  723. local_borg_version='1.2.3',
  724. )
  725. @pytest.mark.parametrize(
  726. 'option_value,feature_available,option_flag',
  727. (
  728. (True, True, '--atime'),
  729. (True, False, None),
  730. (False, True, None),
  731. (False, False, '--noatime'),
  732. ),
  733. )
  734. def test_create_archive_with_atime_option_calls_borg_with_corresponding_parameter(
  735. option_value, feature_available, option_flag
  736. ):
  737. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  738. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  739. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  740. flexmock(module).should_receive('_expand_directories').and_return(())
  741. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  742. flexmock(module).should_receive('_expand_home_directories').and_return(())
  743. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  744. flexmock(module.feature).should_receive('available').and_return(feature_available)
  745. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  746. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  747. flexmock(module).should_receive('execute_command').with_args(
  748. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  749. output_log_level=logging.INFO,
  750. output_file=None,
  751. borg_local_path='borg',
  752. working_directory=None,
  753. )
  754. module.create_archive(
  755. dry_run=False,
  756. repository='repo',
  757. location_config={
  758. 'source_directories': ['foo', 'bar'],
  759. 'repositories': ['repo'],
  760. 'atime': option_value,
  761. 'exclude_patterns': None,
  762. },
  763. storage_config={},
  764. local_borg_version='1.2.3',
  765. )
  766. @pytest.mark.parametrize(
  767. 'option_value,feature_available,option_flag',
  768. (
  769. (True, True, None),
  770. (True, False, None),
  771. (False, True, '--noflags'),
  772. (False, False, '--nobsdflags'),
  773. ),
  774. )
  775. def test_create_archive_with_bsd_flags_option_calls_borg_with_corresponding_parameter(
  776. option_value, feature_available, option_flag
  777. ):
  778. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  779. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  780. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  781. flexmock(module).should_receive('_expand_directories').and_return(())
  782. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  783. flexmock(module).should_receive('_expand_home_directories').and_return(())
  784. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  785. flexmock(module.feature).should_receive('available').and_return(feature_available)
  786. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  787. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  788. flexmock(module).should_receive('execute_command').with_args(
  789. ('borg', 'create') + ((option_flag,) if option_flag else ()) + ARCHIVE_WITH_PATHS,
  790. output_log_level=logging.INFO,
  791. output_file=None,
  792. borg_local_path='borg',
  793. working_directory=None,
  794. )
  795. module.create_archive(
  796. dry_run=False,
  797. repository='repo',
  798. location_config={
  799. 'source_directories': ['foo', 'bar'],
  800. 'repositories': ['repo'],
  801. 'bsd_flags': option_value,
  802. 'exclude_patterns': None,
  803. },
  804. storage_config={},
  805. local_borg_version='1.2.3',
  806. )
  807. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  808. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  809. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  810. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  811. flexmock(module).should_receive('_expand_directories').and_return(())
  812. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  813. flexmock(module).should_receive('_expand_home_directories').and_return(())
  814. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  815. flexmock(module.feature).should_receive('available').and_return(True)
  816. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  817. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  818. flexmock(module).should_receive('execute_command').with_args(
  819. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  820. output_log_level=logging.INFO,
  821. output_file=None,
  822. borg_local_path='borg',
  823. working_directory=None,
  824. )
  825. module.create_archive(
  826. dry_run=False,
  827. repository='repo',
  828. location_config={
  829. 'source_directories': ['foo', 'bar'],
  830. 'repositories': ['repo'],
  831. 'files_cache': 'ctime,size',
  832. 'exclude_patterns': None,
  833. },
  834. storage_config={},
  835. local_borg_version='1.2.3',
  836. )
  837. def test_create_archive_with_local_path_calls_borg_via_local_path():
  838. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  839. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  840. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  841. flexmock(module).should_receive('_expand_directories').and_return(())
  842. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  843. flexmock(module).should_receive('_expand_home_directories').and_return(())
  844. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  845. flexmock(module.feature).should_receive('available').and_return(True)
  846. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  847. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  848. flexmock(module).should_receive('execute_command').with_args(
  849. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  850. output_log_level=logging.INFO,
  851. output_file=None,
  852. borg_local_path='borg1',
  853. working_directory=None,
  854. )
  855. module.create_archive(
  856. dry_run=False,
  857. repository='repo',
  858. location_config={
  859. 'source_directories': ['foo', 'bar'],
  860. 'repositories': ['repo'],
  861. 'exclude_patterns': None,
  862. },
  863. storage_config={},
  864. local_borg_version='1.2.3',
  865. local_path='borg1',
  866. )
  867. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  868. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  869. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  870. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  871. flexmock(module).should_receive('_expand_directories').and_return(())
  872. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  873. flexmock(module).should_receive('_expand_home_directories').and_return(())
  874. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  875. flexmock(module.feature).should_receive('available').and_return(True)
  876. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  877. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  878. flexmock(module).should_receive('execute_command').with_args(
  879. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  880. output_log_level=logging.INFO,
  881. output_file=None,
  882. borg_local_path='borg',
  883. working_directory=None,
  884. )
  885. module.create_archive(
  886. dry_run=False,
  887. repository='repo',
  888. location_config={
  889. 'source_directories': ['foo', 'bar'],
  890. 'repositories': ['repo'],
  891. 'exclude_patterns': None,
  892. },
  893. storage_config={},
  894. local_borg_version='1.2.3',
  895. remote_path='borg1',
  896. )
  897. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  898. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  899. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  900. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  901. flexmock(module).should_receive('_expand_directories').and_return(())
  902. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  903. flexmock(module).should_receive('_expand_home_directories').and_return(())
  904. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  905. flexmock(module.feature).should_receive('available').and_return(True)
  906. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  907. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  908. flexmock(module).should_receive('execute_command').with_args(
  909. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS,
  910. output_log_level=logging.INFO,
  911. output_file=None,
  912. borg_local_path='borg',
  913. working_directory=None,
  914. )
  915. module.create_archive(
  916. dry_run=False,
  917. repository='repo',
  918. location_config={
  919. 'source_directories': ['foo', 'bar'],
  920. 'repositories': ['repo'],
  921. 'exclude_patterns': None,
  922. },
  923. storage_config={'umask': 740},
  924. local_borg_version='1.2.3',
  925. )
  926. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  927. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  928. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  929. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  930. flexmock(module).should_receive('_expand_directories').and_return(())
  931. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  932. flexmock(module).should_receive('_expand_home_directories').and_return(())
  933. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  934. flexmock(module.feature).should_receive('available').and_return(True)
  935. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  936. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  937. flexmock(module).should_receive('execute_command').with_args(
  938. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  939. output_log_level=logging.INFO,
  940. output_file=None,
  941. borg_local_path='borg',
  942. working_directory=None,
  943. )
  944. module.create_archive(
  945. dry_run=False,
  946. repository='repo',
  947. location_config={
  948. 'source_directories': ['foo', 'bar'],
  949. 'repositories': ['repo'],
  950. 'exclude_patterns': None,
  951. },
  952. storage_config={'lock_wait': 5},
  953. local_borg_version='1.2.3',
  954. )
  955. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  956. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  957. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  958. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  959. flexmock(module).should_receive('_expand_directories').and_return(())
  960. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  961. flexmock(module).should_receive('_expand_home_directories').and_return(())
  962. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  963. flexmock(module.feature).should_receive('available').and_return(True)
  964. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  965. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  966. flexmock(module).should_receive('execute_command').with_args(
  967. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  968. output_log_level=logging.WARNING,
  969. output_file=None,
  970. borg_local_path='borg',
  971. working_directory=None,
  972. )
  973. module.create_archive(
  974. dry_run=False,
  975. repository='repo',
  976. location_config={
  977. 'source_directories': ['foo', 'bar'],
  978. 'repositories': ['repo'],
  979. 'exclude_patterns': None,
  980. },
  981. storage_config={},
  982. local_borg_version='1.2.3',
  983. stats=True,
  984. )
  985. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  986. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  987. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  988. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  989. flexmock(module).should_receive('_expand_directories').and_return(())
  990. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  991. flexmock(module).should_receive('_expand_home_directories').and_return(())
  992. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  993. flexmock(module.feature).should_receive('available').and_return(True)
  994. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  995. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  996. flexmock(module).should_receive('execute_command').with_args(
  997. ('borg', 'create', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  998. output_log_level=logging.INFO,
  999. output_file=None,
  1000. borg_local_path='borg',
  1001. working_directory=None,
  1002. )
  1003. insert_logging_mock(logging.INFO)
  1004. module.create_archive(
  1005. dry_run=False,
  1006. repository='repo',
  1007. location_config={
  1008. 'source_directories': ['foo', 'bar'],
  1009. 'repositories': ['repo'],
  1010. 'exclude_patterns': None,
  1011. },
  1012. storage_config={},
  1013. local_borg_version='1.2.3',
  1014. stats=True,
  1015. )
  1016. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  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.os.path).should_receive('expanduser').and_raise(TypeError)
  1022. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1023. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1024. flexmock(module.feature).should_receive('available').and_return(True)
  1025. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1026. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1027. flexmock(module).should_receive('execute_command').with_args(
  1028. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  1029. output_log_level=logging.WARNING,
  1030. output_file=None,
  1031. borg_local_path='borg',
  1032. working_directory=None,
  1033. )
  1034. module.create_archive(
  1035. dry_run=False,
  1036. repository='repo',
  1037. location_config={
  1038. 'source_directories': ['foo', 'bar'],
  1039. 'repositories': ['repo'],
  1040. 'exclude_patterns': None,
  1041. },
  1042. storage_config={},
  1043. local_borg_version='1.2.3',
  1044. files=True,
  1045. )
  1046. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  1047. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1048. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1049. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1050. flexmock(module).should_receive('_expand_directories').and_return(())
  1051. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1052. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1053. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1054. flexmock(module.feature).should_receive('available').and_return(True)
  1055. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1056. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1057. flexmock(module).should_receive('execute_command').with_args(
  1058. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  1059. output_log_level=logging.INFO,
  1060. output_file=None,
  1061. borg_local_path='borg',
  1062. working_directory=None,
  1063. )
  1064. insert_logging_mock(logging.INFO)
  1065. module.create_archive(
  1066. dry_run=False,
  1067. repository='repo',
  1068. location_config={
  1069. 'source_directories': ['foo', 'bar'],
  1070. 'repositories': ['repo'],
  1071. 'exclude_patterns': None,
  1072. },
  1073. storage_config={},
  1074. local_borg_version='1.2.3',
  1075. files=True,
  1076. )
  1077. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  1078. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1079. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1080. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1081. flexmock(module).should_receive('_expand_directories').and_return(())
  1082. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1083. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1084. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1085. flexmock(module.feature).should_receive('available').and_return(True)
  1086. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1087. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1088. flexmock(module).should_receive('execute_command').with_args(
  1089. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS,
  1090. output_log_level=logging.INFO,
  1091. output_file=module.DO_NOT_CAPTURE,
  1092. borg_local_path='borg',
  1093. working_directory=None,
  1094. )
  1095. insert_logging_mock(logging.INFO)
  1096. module.create_archive(
  1097. dry_run=False,
  1098. repository='repo',
  1099. location_config={
  1100. 'source_directories': ['foo', 'bar'],
  1101. 'repositories': ['repo'],
  1102. 'exclude_patterns': None,
  1103. },
  1104. storage_config={},
  1105. local_borg_version='1.2.3',
  1106. progress=True,
  1107. )
  1108. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  1109. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1110. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1111. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1112. flexmock(module).should_receive('_expand_directories').and_return(())
  1113. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1114. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1115. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1116. flexmock(module.feature).should_receive('available').and_return(True)
  1117. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1118. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1119. flexmock(module).should_receive('execute_command').with_args(
  1120. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS,
  1121. output_log_level=logging.INFO,
  1122. output_file=module.DO_NOT_CAPTURE,
  1123. borg_local_path='borg',
  1124. working_directory=None,
  1125. )
  1126. module.create_archive(
  1127. dry_run=False,
  1128. repository='repo',
  1129. location_config={
  1130. 'source_directories': ['foo', 'bar'],
  1131. 'repositories': ['repo'],
  1132. 'exclude_patterns': None,
  1133. },
  1134. storage_config={},
  1135. local_borg_version='1.2.3',
  1136. progress=True,
  1137. )
  1138. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  1139. processes = flexmock()
  1140. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1141. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1142. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1143. flexmock(module).should_receive('_expand_directories').and_return(())
  1144. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1145. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1146. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1147. flexmock(module.feature).should_receive('available').and_return(True)
  1148. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1149. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1150. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1151. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  1152. + ARCHIVE_WITH_PATHS,
  1153. processes=processes,
  1154. output_log_level=logging.INFO,
  1155. output_file=module.DO_NOT_CAPTURE,
  1156. borg_local_path='borg',
  1157. working_directory=None,
  1158. )
  1159. module.create_archive(
  1160. dry_run=False,
  1161. repository='repo',
  1162. location_config={
  1163. 'source_directories': ['foo', 'bar'],
  1164. 'repositories': ['repo'],
  1165. 'exclude_patterns': None,
  1166. },
  1167. storage_config={},
  1168. local_borg_version='1.2.3',
  1169. progress=True,
  1170. stream_processes=processes,
  1171. )
  1172. def test_create_archive_with_json_calls_borg_with_json_parameter():
  1173. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1174. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1175. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1176. flexmock(module).should_receive('_expand_directories').and_return(())
  1177. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1178. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1179. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1180. flexmock(module.feature).should_receive('available').and_return(True)
  1181. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1182. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1183. flexmock(module).should_receive('execute_command').with_args(
  1184. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  1185. output_log_level=None,
  1186. output_file=None,
  1187. borg_local_path='borg',
  1188. working_directory=None,
  1189. ).and_return('[]')
  1190. json_output = module.create_archive(
  1191. dry_run=False,
  1192. repository='repo',
  1193. location_config={
  1194. 'source_directories': ['foo', 'bar'],
  1195. 'repositories': ['repo'],
  1196. 'exclude_patterns': None,
  1197. },
  1198. storage_config={},
  1199. local_borg_version='1.2.3',
  1200. json=True,
  1201. )
  1202. assert json_output == '[]'
  1203. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  1204. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1205. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1206. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1207. flexmock(module).should_receive('_expand_directories').and_return(())
  1208. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1209. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1210. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1211. flexmock(module.feature).should_receive('available').and_return(True)
  1212. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1213. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1214. flexmock(module).should_receive('execute_command').with_args(
  1215. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  1216. output_log_level=None,
  1217. output_file=None,
  1218. borg_local_path='borg',
  1219. working_directory=None,
  1220. ).and_return('[]')
  1221. json_output = module.create_archive(
  1222. dry_run=False,
  1223. repository='repo',
  1224. location_config={
  1225. 'source_directories': ['foo', 'bar'],
  1226. 'repositories': ['repo'],
  1227. 'exclude_patterns': None,
  1228. },
  1229. storage_config={},
  1230. local_borg_version='1.2.3',
  1231. json=True,
  1232. stats=True,
  1233. )
  1234. assert json_output == '[]'
  1235. def test_create_archive_with_source_directories_glob_expands():
  1236. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1237. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1238. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1239. flexmock(module).should_receive('_expand_directories').and_return(())
  1240. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1241. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1242. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1243. flexmock(module.feature).should_receive('available').and_return(True)
  1244. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1245. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1246. flexmock(module).should_receive('execute_command').with_args(
  1247. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1248. output_log_level=logging.INFO,
  1249. output_file=None,
  1250. borg_local_path='borg',
  1251. working_directory=None,
  1252. )
  1253. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1254. module.create_archive(
  1255. dry_run=False,
  1256. repository='repo',
  1257. location_config={
  1258. 'source_directories': ['foo*'],
  1259. 'repositories': ['repo'],
  1260. 'exclude_patterns': None,
  1261. },
  1262. storage_config={},
  1263. local_borg_version='1.2.3',
  1264. )
  1265. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1266. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1267. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1268. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1269. flexmock(module).should_receive('_expand_directories').and_return(())
  1270. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1271. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1272. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1273. flexmock(module.feature).should_receive('available').and_return(True)
  1274. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1275. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1276. flexmock(module).should_receive('execute_command').with_args(
  1277. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1278. output_log_level=logging.INFO,
  1279. output_file=None,
  1280. borg_local_path='borg',
  1281. working_directory=None,
  1282. )
  1283. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1284. module.create_archive(
  1285. dry_run=False,
  1286. repository='repo',
  1287. location_config={
  1288. 'source_directories': ['foo*'],
  1289. 'repositories': ['repo'],
  1290. 'exclude_patterns': None,
  1291. },
  1292. storage_config={},
  1293. local_borg_version='1.2.3',
  1294. )
  1295. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1296. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1297. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1298. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1299. flexmock(module).should_receive('_expand_directories').and_return(())
  1300. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1301. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1302. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1303. flexmock(module.feature).should_receive('available').and_return(True)
  1304. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1305. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1306. flexmock(module).should_receive('execute_command').with_args(
  1307. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1308. output_log_level=logging.INFO,
  1309. output_file=None,
  1310. borg_local_path='borg',
  1311. working_directory=None,
  1312. )
  1313. module.create_archive(
  1314. dry_run=False,
  1315. repository='repo',
  1316. location_config={
  1317. 'source_directories': ['foo*'],
  1318. 'repositories': ['repo'],
  1319. 'exclude_patterns': None,
  1320. },
  1321. storage_config={},
  1322. local_borg_version='1.2.3',
  1323. )
  1324. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1325. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1326. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1327. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1328. flexmock(module).should_receive('_expand_directories').and_return(())
  1329. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1330. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1331. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1332. flexmock(module.feature).should_receive('available').and_return(True)
  1333. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1334. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1335. flexmock(module).should_receive('execute_command').with_args(
  1336. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1337. output_log_level=logging.INFO,
  1338. output_file=None,
  1339. borg_local_path='borg',
  1340. working_directory=None,
  1341. )
  1342. module.create_archive(
  1343. dry_run=False,
  1344. repository='repo',
  1345. location_config={
  1346. 'source_directories': ['foo', 'bar'],
  1347. 'repositories': ['repo'],
  1348. 'exclude_patterns': None,
  1349. },
  1350. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1351. local_borg_version='1.2.3',
  1352. )
  1353. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1354. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1355. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1356. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1357. flexmock(module).should_receive('_expand_directories').and_return(())
  1358. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1359. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1360. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1361. flexmock(module.feature).should_receive('available').and_return(True)
  1362. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1363. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1364. flexmock(module).should_receive('execute_command').with_args(
  1365. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  1366. output_log_level=logging.INFO,
  1367. output_file=None,
  1368. borg_local_path='borg',
  1369. working_directory=None,
  1370. )
  1371. module.create_archive(
  1372. dry_run=False,
  1373. repository='repo',
  1374. location_config={
  1375. 'source_directories': ['foo', 'bar'],
  1376. 'repositories': ['repo'],
  1377. 'exclude_patterns': None,
  1378. },
  1379. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1380. local_borg_version='1.2.3',
  1381. )
  1382. def test_create_archive_with_repository_accepts_borg_placeholders():
  1383. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1384. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1385. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1386. flexmock(module).should_receive('_expand_directories').and_return(())
  1387. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1388. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1389. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1390. flexmock(module.feature).should_receive('available').and_return(True)
  1391. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1392. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1393. flexmock(module).should_receive('execute_command').with_args(
  1394. ('borg', 'create', '{fqdn}::Documents_{hostname}-{now}', 'foo', 'bar'),
  1395. output_log_level=logging.INFO,
  1396. output_file=None,
  1397. borg_local_path='borg',
  1398. working_directory=None,
  1399. )
  1400. module.create_archive(
  1401. dry_run=False,
  1402. repository='{fqdn}',
  1403. location_config={
  1404. 'source_directories': ['foo', 'bar'],
  1405. 'repositories': ['{fqdn}'],
  1406. 'exclude_patterns': None,
  1407. },
  1408. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1409. local_borg_version='1.2.3',
  1410. )
  1411. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1412. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1413. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1414. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1415. flexmock(module).should_receive('_expand_directories').and_return(())
  1416. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1417. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1418. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1419. flexmock(module.feature).should_receive('available').and_return(True)
  1420. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1421. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1422. flexmock(module).should_receive('execute_command').with_args(
  1423. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  1424. output_log_level=logging.INFO,
  1425. output_file=None,
  1426. borg_local_path='borg',
  1427. working_directory=None,
  1428. )
  1429. module.create_archive(
  1430. dry_run=False,
  1431. repository='repo',
  1432. location_config={
  1433. 'source_directories': ['foo', 'bar'],
  1434. 'repositories': ['repo'],
  1435. 'exclude_patterns': None,
  1436. },
  1437. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1438. local_borg_version='1.2.3',
  1439. )
  1440. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1441. processes = flexmock()
  1442. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1443. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1444. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1445. flexmock(module).should_receive('_expand_directories').and_return(())
  1446. flexmock(module.os.path).should_receive('expanduser').and_raise(TypeError)
  1447. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1448. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1449. flexmock(module.feature).should_receive('available').and_return(True)
  1450. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1451. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1452. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1453. ('borg', 'create', '--one-file-system', '--read-special') + ARCHIVE_WITH_PATHS,
  1454. processes=processes,
  1455. output_log_level=logging.INFO,
  1456. output_file=None,
  1457. borg_local_path='borg',
  1458. working_directory=None,
  1459. )
  1460. module.create_archive(
  1461. dry_run=False,
  1462. repository='repo',
  1463. location_config={
  1464. 'source_directories': ['foo', 'bar'],
  1465. 'repositories': ['repo'],
  1466. 'exclude_patterns': None,
  1467. },
  1468. storage_config={},
  1469. local_borg_version='1.2.3',
  1470. stream_processes=processes,
  1471. )