test_create.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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).should_receive('_expand_home_directories').and_return(())
  169. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  170. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  171. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  172. flexmock(module).should_receive('execute_command').with_args(
  173. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  174. output_log_level=logging.INFO,
  175. output_file=None,
  176. borg_local_path='borg',
  177. )
  178. module.create_archive(
  179. dry_run=False,
  180. repository='repo',
  181. location_config={
  182. 'source_directories': ['foo', 'bar'],
  183. 'repositories': ['repo'],
  184. 'exclude_patterns': None,
  185. },
  186. storage_config={},
  187. )
  188. def test_create_archive_with_patterns_calls_borg_with_patterns():
  189. pattern_flags = ('--patterns-from', 'patterns')
  190. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  191. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  192. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  193. flexmock(module).should_receive('_expand_directories').and_return(())
  194. flexmock(module).should_receive('_expand_home_directories').and_return(())
  195. flexmock(module).should_receive('_write_pattern_file').and_return(
  196. flexmock(name='/tmp/patterns')
  197. ).and_return(None)
  198. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  199. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  200. flexmock(module).should_receive('execute_command').with_args(
  201. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  202. output_log_level=logging.INFO,
  203. output_file=None,
  204. borg_local_path='borg',
  205. )
  206. module.create_archive(
  207. dry_run=False,
  208. repository='repo',
  209. location_config={
  210. 'source_directories': ['foo', 'bar'],
  211. 'repositories': ['repo'],
  212. 'patterns': ['pattern'],
  213. },
  214. storage_config={},
  215. )
  216. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  217. exclude_flags = ('--exclude-from', 'excludes')
  218. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  219. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  220. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  221. flexmock(module).should_receive('_expand_directories').and_return(())
  222. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  223. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  224. flexmock(name='/tmp/excludes')
  225. )
  226. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  227. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  228. flexmock(module).should_receive('execute_command').with_args(
  229. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  230. output_log_level=logging.INFO,
  231. output_file=None,
  232. borg_local_path='borg',
  233. )
  234. module.create_archive(
  235. dry_run=False,
  236. repository='repo',
  237. location_config={
  238. 'source_directories': ['foo', 'bar'],
  239. 'repositories': ['repo'],
  240. 'exclude_patterns': ['exclude'],
  241. },
  242. storage_config={},
  243. )
  244. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  245. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  246. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  247. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  248. flexmock(module).should_receive('_expand_directories').and_return(())
  249. flexmock(module).should_receive('_expand_home_directories').and_return(())
  250. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  251. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  252. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  253. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  254. flexmock(module).should_receive('execute_command').with_args(
  255. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  256. output_log_level=logging.INFO,
  257. output_file=None,
  258. borg_local_path='borg',
  259. )
  260. insert_logging_mock(logging.INFO)
  261. module.create_archive(
  262. dry_run=False,
  263. repository='repo',
  264. location_config={
  265. 'source_directories': ['foo', 'bar'],
  266. 'repositories': ['repo'],
  267. 'exclude_patterns': None,
  268. },
  269. storage_config={},
  270. )
  271. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  272. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  273. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  274. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  275. flexmock(module).should_receive('_expand_directories').and_return(())
  276. flexmock(module).should_receive('_expand_home_directories').and_return(())
  277. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  278. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  279. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  280. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  281. flexmock(module).should_receive('execute_command').with_args(
  282. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  283. output_log_level=None,
  284. output_file=None,
  285. borg_local_path='borg',
  286. )
  287. insert_logging_mock(logging.INFO)
  288. module.create_archive(
  289. dry_run=False,
  290. repository='repo',
  291. location_config={
  292. 'source_directories': ['foo', 'bar'],
  293. 'repositories': ['repo'],
  294. 'exclude_patterns': None,
  295. },
  296. storage_config={},
  297. json=True,
  298. )
  299. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  300. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  301. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  302. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  303. flexmock(module).should_receive('_expand_directories').and_return(())
  304. flexmock(module).should_receive('_expand_home_directories').and_return(())
  305. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  306. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  307. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  308. flexmock(module).should_receive('execute_command').with_args(
  309. ('borg', 'create', '--debug', '--show-rc') + ARCHIVE_WITH_PATHS,
  310. output_log_level=logging.INFO,
  311. output_file=None,
  312. borg_local_path='borg',
  313. )
  314. insert_logging_mock(logging.DEBUG)
  315. module.create_archive(
  316. dry_run=False,
  317. repository='repo',
  318. location_config={
  319. 'source_directories': ['foo', 'bar'],
  320. 'repositories': ['repo'],
  321. 'exclude_patterns': None,
  322. },
  323. storage_config={},
  324. )
  325. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  326. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  327. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  328. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  329. flexmock(module).should_receive('_expand_directories').and_return(())
  330. flexmock(module).should_receive('_expand_home_directories').and_return(())
  331. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  332. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  333. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  334. flexmock(module).should_receive('execute_command').with_args(
  335. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  336. output_log_level=None,
  337. output_file=None,
  338. borg_local_path='borg',
  339. )
  340. insert_logging_mock(logging.DEBUG)
  341. module.create_archive(
  342. dry_run=False,
  343. repository='repo',
  344. location_config={
  345. 'source_directories': ['foo', 'bar'],
  346. 'repositories': ['repo'],
  347. 'exclude_patterns': None,
  348. },
  349. storage_config={},
  350. json=True,
  351. )
  352. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  353. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  354. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  355. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  356. flexmock(module).should_receive('_expand_directories').and_return(())
  357. flexmock(module).should_receive('_expand_home_directories').and_return(())
  358. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  359. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  360. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  361. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  362. flexmock(module).should_receive('execute_command').with_args(
  363. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  364. output_log_level=logging.INFO,
  365. output_file=None,
  366. borg_local_path='borg',
  367. )
  368. module.create_archive(
  369. dry_run=True,
  370. repository='repo',
  371. location_config={
  372. 'source_directories': ['foo', 'bar'],
  373. 'repositories': ['repo'],
  374. 'exclude_patterns': None,
  375. },
  376. storage_config={},
  377. )
  378. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  379. # --dry-run and --stats are mutually exclusive, see:
  380. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  381. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  382. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  383. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  384. flexmock(module).should_receive('_expand_directories').and_return(())
  385. flexmock(module).should_receive('_expand_home_directories').and_return(())
  386. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  387. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  388. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  389. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  390. flexmock(module).should_receive('execute_command').with_args(
  391. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  392. output_log_level=logging.INFO,
  393. output_file=None,
  394. borg_local_path='borg',
  395. )
  396. insert_logging_mock(logging.INFO)
  397. module.create_archive(
  398. dry_run=True,
  399. repository='repo',
  400. location_config={
  401. 'source_directories': ['foo', 'bar'],
  402. 'repositories': ['repo'],
  403. 'exclude_patterns': None,
  404. },
  405. storage_config={},
  406. stats=True,
  407. )
  408. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  409. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  410. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  411. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  412. flexmock(module).should_receive('_expand_directories').and_return(())
  413. flexmock(module).should_receive('_expand_home_directories').and_return(())
  414. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  415. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  416. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  417. flexmock(module).should_receive('execute_command').with_args(
  418. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  419. output_log_level=logging.INFO,
  420. output_file=None,
  421. borg_local_path='borg',
  422. )
  423. module.create_archive(
  424. dry_run=False,
  425. repository='repo',
  426. location_config={
  427. 'source_directories': ['foo', 'bar'],
  428. 'repositories': ['repo'],
  429. 'exclude_patterns': None,
  430. },
  431. storage_config={'checkpoint_interval': 600},
  432. )
  433. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  434. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  435. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  436. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  437. flexmock(module).should_receive('_expand_directories').and_return(())
  438. flexmock(module).should_receive('_expand_home_directories').and_return(())
  439. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  440. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  441. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  442. flexmock(module).should_receive('execute_command').with_args(
  443. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  444. output_log_level=logging.INFO,
  445. output_file=None,
  446. borg_local_path='borg',
  447. )
  448. module.create_archive(
  449. dry_run=False,
  450. repository='repo',
  451. location_config={
  452. 'source_directories': ['foo', 'bar'],
  453. 'repositories': ['repo'],
  454. 'exclude_patterns': None,
  455. },
  456. storage_config={'chunker_params': '1,2,3,4'},
  457. )
  458. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  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('_expand_home_directories').and_return(())
  464. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  465. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  466. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  467. flexmock(module).should_receive('execute_command').with_args(
  468. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  469. output_log_level=logging.INFO,
  470. output_file=None,
  471. borg_local_path='borg',
  472. )
  473. module.create_archive(
  474. dry_run=False,
  475. repository='repo',
  476. location_config={
  477. 'source_directories': ['foo', 'bar'],
  478. 'repositories': ['repo'],
  479. 'exclude_patterns': None,
  480. },
  481. storage_config={'compression': 'rle'},
  482. )
  483. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  484. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  485. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  486. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  487. flexmock(module).should_receive('_expand_directories').and_return(())
  488. flexmock(module).should_receive('_expand_home_directories').and_return(())
  489. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  490. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  491. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  492. flexmock(module).should_receive('execute_command').with_args(
  493. ('borg', 'create', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
  494. output_log_level=logging.INFO,
  495. output_file=None,
  496. borg_local_path='borg',
  497. )
  498. module.create_archive(
  499. dry_run=False,
  500. repository='repo',
  501. location_config={
  502. 'source_directories': ['foo', 'bar'],
  503. 'repositories': ['repo'],
  504. 'exclude_patterns': None,
  505. },
  506. storage_config={'remote_rate_limit': 100},
  507. )
  508. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  509. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  510. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  511. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  512. flexmock(module).should_receive('_expand_directories').and_return(())
  513. flexmock(module).should_receive('_expand_home_directories').and_return(())
  514. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  515. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  516. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  517. flexmock(module).should_receive('execute_command').with_args(
  518. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  519. output_log_level=logging.INFO,
  520. output_file=None,
  521. borg_local_path='borg',
  522. )
  523. module.create_archive(
  524. dry_run=False,
  525. repository='repo',
  526. location_config={
  527. 'source_directories': ['foo', 'bar'],
  528. 'repositories': ['repo'],
  529. 'one_file_system': True,
  530. 'exclude_patterns': None,
  531. },
  532. storage_config={},
  533. )
  534. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  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('_expand_home_directories').and_return(())
  540. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  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', '--numeric-owner') + ARCHIVE_WITH_PATHS,
  545. output_log_level=logging.INFO,
  546. output_file=None,
  547. borg_local_path='borg',
  548. )
  549. module.create_archive(
  550. dry_run=False,
  551. repository='repo',
  552. location_config={
  553. 'source_directories': ['foo', 'bar'],
  554. 'repositories': ['repo'],
  555. 'numeric_owner': True,
  556. 'exclude_patterns': None,
  557. },
  558. storage_config={},
  559. )
  560. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  561. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  562. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  563. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  564. flexmock(module).should_receive('_expand_directories').and_return(())
  565. flexmock(module).should_receive('_expand_home_directories').and_return(())
  566. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  567. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  568. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  569. flexmock(module).should_receive('execute_command').with_args(
  570. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  571. output_log_level=logging.INFO,
  572. output_file=None,
  573. borg_local_path='borg',
  574. )
  575. module.create_archive(
  576. dry_run=False,
  577. repository='repo',
  578. location_config={
  579. 'source_directories': ['foo', 'bar'],
  580. 'repositories': ['repo'],
  581. 'read_special': True,
  582. 'exclude_patterns': None,
  583. },
  584. storage_config={},
  585. )
  586. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  587. def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
  588. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  589. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  590. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  591. flexmock(module).should_receive('_expand_directories').and_return(())
  592. flexmock(module).should_receive('_expand_home_directories').and_return(())
  593. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  594. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  595. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  596. flexmock(module).should_receive('execute_command').with_args(
  597. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  598. output_log_level=logging.INFO,
  599. output_file=None,
  600. borg_local_path='borg',
  601. )
  602. module.create_archive(
  603. dry_run=False,
  604. repository='repo',
  605. location_config={
  606. 'source_directories': ['foo', 'bar'],
  607. 'repositories': ['repo'],
  608. option_name: True,
  609. 'exclude_patterns': None,
  610. },
  611. storage_config={},
  612. )
  613. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  614. def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
  615. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  616. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  617. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  618. flexmock(module).should_receive('_expand_directories').and_return(())
  619. flexmock(module).should_receive('_expand_home_directories').and_return(())
  620. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  621. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  622. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  623. flexmock(module).should_receive('execute_command').with_args(
  624. ('borg', 'create', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
  625. output_log_level=logging.INFO,
  626. output_file=None,
  627. borg_local_path='borg',
  628. )
  629. module.create_archive(
  630. dry_run=False,
  631. repository='repo',
  632. location_config={
  633. 'source_directories': ['foo', 'bar'],
  634. 'repositories': ['repo'],
  635. option_name: False,
  636. 'exclude_patterns': None,
  637. },
  638. storage_config={},
  639. )
  640. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  641. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  642. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  643. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  644. flexmock(module).should_receive('_expand_directories').and_return(())
  645. flexmock(module).should_receive('_expand_home_directories').and_return(())
  646. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  647. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  648. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  649. flexmock(module).should_receive('execute_command').with_args(
  650. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  651. output_log_level=logging.INFO,
  652. output_file=None,
  653. borg_local_path='borg',
  654. )
  655. module.create_archive(
  656. dry_run=False,
  657. repository='repo',
  658. location_config={
  659. 'source_directories': ['foo', 'bar'],
  660. 'repositories': ['repo'],
  661. 'files_cache': 'ctime,size',
  662. 'exclude_patterns': None,
  663. },
  664. storage_config={},
  665. )
  666. def test_create_archive_with_local_path_calls_borg_via_local_path():
  667. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  668. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  669. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  670. flexmock(module).should_receive('_expand_directories').and_return(())
  671. flexmock(module).should_receive('_expand_home_directories').and_return(())
  672. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  673. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  674. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  675. flexmock(module).should_receive('execute_command').with_args(
  676. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  677. output_log_level=logging.INFO,
  678. output_file=None,
  679. borg_local_path='borg1',
  680. )
  681. module.create_archive(
  682. dry_run=False,
  683. repository='repo',
  684. location_config={
  685. 'source_directories': ['foo', 'bar'],
  686. 'repositories': ['repo'],
  687. 'exclude_patterns': None,
  688. },
  689. storage_config={},
  690. local_path='borg1',
  691. )
  692. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  693. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  694. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  695. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  696. flexmock(module).should_receive('_expand_directories').and_return(())
  697. flexmock(module).should_receive('_expand_home_directories').and_return(())
  698. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  699. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  700. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  701. flexmock(module).should_receive('execute_command').with_args(
  702. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  703. output_log_level=logging.INFO,
  704. output_file=None,
  705. borg_local_path='borg',
  706. )
  707. module.create_archive(
  708. dry_run=False,
  709. repository='repo',
  710. location_config={
  711. 'source_directories': ['foo', 'bar'],
  712. 'repositories': ['repo'],
  713. 'exclude_patterns': None,
  714. },
  715. storage_config={},
  716. remote_path='borg1',
  717. )
  718. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  719. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  720. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  721. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  722. flexmock(module).should_receive('_expand_directories').and_return(())
  723. flexmock(module).should_receive('_expand_home_directories').and_return(())
  724. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  725. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  726. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  727. flexmock(module).should_receive('execute_command').with_args(
  728. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS,
  729. output_log_level=logging.INFO,
  730. output_file=None,
  731. borg_local_path='borg',
  732. )
  733. module.create_archive(
  734. dry_run=False,
  735. repository='repo',
  736. location_config={
  737. 'source_directories': ['foo', 'bar'],
  738. 'repositories': ['repo'],
  739. 'exclude_patterns': None,
  740. },
  741. storage_config={'umask': 740},
  742. )
  743. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  744. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  745. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  746. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  747. flexmock(module).should_receive('_expand_directories').and_return(())
  748. flexmock(module).should_receive('_expand_home_directories').and_return(())
  749. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  750. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  751. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  752. flexmock(module).should_receive('execute_command').with_args(
  753. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  754. output_log_level=logging.INFO,
  755. output_file=None,
  756. borg_local_path='borg',
  757. )
  758. module.create_archive(
  759. dry_run=False,
  760. repository='repo',
  761. location_config={
  762. 'source_directories': ['foo', 'bar'],
  763. 'repositories': ['repo'],
  764. 'exclude_patterns': None,
  765. },
  766. storage_config={'lock_wait': 5},
  767. )
  768. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  769. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  770. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  771. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  772. flexmock(module).should_receive('_expand_directories').and_return(())
  773. flexmock(module).should_receive('_expand_home_directories').and_return(())
  774. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  775. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  776. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  777. flexmock(module).should_receive('execute_command').with_args(
  778. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  779. output_log_level=logging.WARNING,
  780. output_file=None,
  781. borg_local_path='borg',
  782. )
  783. module.create_archive(
  784. dry_run=False,
  785. repository='repo',
  786. location_config={
  787. 'source_directories': ['foo', 'bar'],
  788. 'repositories': ['repo'],
  789. 'exclude_patterns': None,
  790. },
  791. storage_config={},
  792. stats=True,
  793. )
  794. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  795. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  796. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  797. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  798. flexmock(module).should_receive('_expand_directories').and_return(())
  799. flexmock(module).should_receive('_expand_home_directories').and_return(())
  800. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  801. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  802. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  803. flexmock(module).should_receive('execute_command').with_args(
  804. ('borg', 'create', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  805. output_log_level=logging.INFO,
  806. output_file=None,
  807. borg_local_path='borg',
  808. )
  809. insert_logging_mock(logging.INFO)
  810. module.create_archive(
  811. dry_run=False,
  812. repository='repo',
  813. location_config={
  814. 'source_directories': ['foo', 'bar'],
  815. 'repositories': ['repo'],
  816. 'exclude_patterns': None,
  817. },
  818. storage_config={},
  819. stats=True,
  820. )
  821. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  822. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  823. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  824. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  825. flexmock(module).should_receive('_expand_directories').and_return(())
  826. flexmock(module).should_receive('_expand_home_directories').and_return(())
  827. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  828. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  829. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  830. flexmock(module).should_receive('execute_command').with_args(
  831. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  832. output_log_level=logging.WARNING,
  833. output_file=None,
  834. borg_local_path='borg',
  835. )
  836. module.create_archive(
  837. dry_run=False,
  838. repository='repo',
  839. location_config={
  840. 'source_directories': ['foo', 'bar'],
  841. 'repositories': ['repo'],
  842. 'exclude_patterns': None,
  843. },
  844. storage_config={},
  845. files=True,
  846. )
  847. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  848. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  849. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  850. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  851. flexmock(module).should_receive('_expand_directories').and_return(())
  852. flexmock(module).should_receive('_expand_home_directories').and_return(())
  853. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  854. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  855. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  856. flexmock(module).should_receive('execute_command').with_args(
  857. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  858. output_log_level=logging.INFO,
  859. output_file=None,
  860. borg_local_path='borg',
  861. )
  862. insert_logging_mock(logging.INFO)
  863. module.create_archive(
  864. dry_run=False,
  865. repository='repo',
  866. location_config={
  867. 'source_directories': ['foo', 'bar'],
  868. 'repositories': ['repo'],
  869. 'exclude_patterns': None,
  870. },
  871. storage_config={},
  872. files=True,
  873. )
  874. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  875. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  876. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  877. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  878. flexmock(module).should_receive('_expand_directories').and_return(())
  879. flexmock(module).should_receive('_expand_home_directories').and_return(())
  880. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  881. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  882. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  883. flexmock(module).should_receive('execute_command').with_args(
  884. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS,
  885. output_log_level=logging.INFO,
  886. output_file=module.DO_NOT_CAPTURE,
  887. borg_local_path='borg',
  888. )
  889. insert_logging_mock(logging.INFO)
  890. module.create_archive(
  891. dry_run=False,
  892. repository='repo',
  893. location_config={
  894. 'source_directories': ['foo', 'bar'],
  895. 'repositories': ['repo'],
  896. 'exclude_patterns': None,
  897. },
  898. storage_config={},
  899. progress=True,
  900. )
  901. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  902. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  903. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  904. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  905. flexmock(module).should_receive('_expand_directories').and_return(())
  906. flexmock(module).should_receive('_expand_home_directories').and_return(())
  907. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  908. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  909. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  910. flexmock(module).should_receive('execute_command').with_args(
  911. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS,
  912. output_log_level=logging.INFO,
  913. output_file=module.DO_NOT_CAPTURE,
  914. borg_local_path='borg',
  915. )
  916. module.create_archive(
  917. dry_run=False,
  918. repository='repo',
  919. location_config={
  920. 'source_directories': ['foo', 'bar'],
  921. 'repositories': ['repo'],
  922. 'exclude_patterns': None,
  923. },
  924. storage_config={},
  925. progress=True,
  926. )
  927. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  928. processes = flexmock()
  929. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  930. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  931. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  932. flexmock(module).should_receive('_expand_directories').and_return(())
  933. flexmock(module).should_receive('_expand_home_directories').and_return(())
  934. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  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_processes').with_args(
  938. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  939. + ARCHIVE_WITH_PATHS,
  940. processes=processes,
  941. output_log_level=logging.INFO,
  942. output_file=module.DO_NOT_CAPTURE,
  943. borg_local_path='borg',
  944. )
  945. module.create_archive(
  946. dry_run=False,
  947. repository='repo',
  948. location_config={
  949. 'source_directories': ['foo', 'bar'],
  950. 'repositories': ['repo'],
  951. 'exclude_patterns': None,
  952. },
  953. storage_config={},
  954. progress=True,
  955. stream_processes=processes,
  956. )
  957. def test_create_archive_with_json_calls_borg_with_json_parameter():
  958. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  959. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  960. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  961. flexmock(module).should_receive('_expand_directories').and_return(())
  962. flexmock(module).should_receive('_expand_home_directories').and_return(())
  963. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  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', '--json') + ARCHIVE_WITH_PATHS,
  968. output_log_level=None,
  969. output_file=None,
  970. borg_local_path='borg',
  971. ).and_return('[]')
  972. json_output = module.create_archive(
  973. dry_run=False,
  974. repository='repo',
  975. location_config={
  976. 'source_directories': ['foo', 'bar'],
  977. 'repositories': ['repo'],
  978. 'exclude_patterns': None,
  979. },
  980. storage_config={},
  981. json=True,
  982. )
  983. assert json_output == '[]'
  984. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  985. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  986. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  987. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  988. flexmock(module).should_receive('_expand_directories').and_return(())
  989. flexmock(module).should_receive('_expand_home_directories').and_return(())
  990. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  991. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  992. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  993. flexmock(module).should_receive('execute_command').with_args(
  994. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  995. output_log_level=None,
  996. output_file=None,
  997. borg_local_path='borg',
  998. ).and_return('[]')
  999. json_output = module.create_archive(
  1000. dry_run=False,
  1001. repository='repo',
  1002. location_config={
  1003. 'source_directories': ['foo', 'bar'],
  1004. 'repositories': ['repo'],
  1005. 'exclude_patterns': None,
  1006. },
  1007. storage_config={},
  1008. json=True,
  1009. stats=True,
  1010. )
  1011. assert json_output == '[]'
  1012. def test_create_archive_with_source_directories_glob_expands():
  1013. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1014. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1015. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1016. flexmock(module).should_receive('_expand_directories').and_return(())
  1017. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1018. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1019. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1020. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1021. flexmock(module).should_receive('execute_command').with_args(
  1022. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1023. output_log_level=logging.INFO,
  1024. output_file=None,
  1025. borg_local_path='borg',
  1026. )
  1027. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1028. module.create_archive(
  1029. dry_run=False,
  1030. repository='repo',
  1031. location_config={
  1032. 'source_directories': ['foo*'],
  1033. 'repositories': ['repo'],
  1034. 'exclude_patterns': None,
  1035. },
  1036. storage_config={},
  1037. )
  1038. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1039. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1040. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1041. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1042. flexmock(module).should_receive('_expand_directories').and_return(())
  1043. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1044. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1045. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1046. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1047. flexmock(module).should_receive('execute_command').with_args(
  1048. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1049. output_log_level=logging.INFO,
  1050. output_file=None,
  1051. borg_local_path='borg',
  1052. )
  1053. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1054. module.create_archive(
  1055. dry_run=False,
  1056. repository='repo',
  1057. location_config={
  1058. 'source_directories': ['foo*'],
  1059. 'repositories': ['repo'],
  1060. 'exclude_patterns': None,
  1061. },
  1062. storage_config={},
  1063. )
  1064. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1065. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1066. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1067. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1068. flexmock(module).should_receive('_expand_directories').and_return(())
  1069. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1070. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1071. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1072. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1073. flexmock(module).should_receive('execute_command').with_args(
  1074. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1075. output_log_level=logging.INFO,
  1076. output_file=None,
  1077. borg_local_path='borg',
  1078. )
  1079. module.create_archive(
  1080. dry_run=False,
  1081. repository='repo',
  1082. location_config={
  1083. 'source_directories': ['foo*'],
  1084. 'repositories': ['repo'],
  1085. 'exclude_patterns': None,
  1086. },
  1087. storage_config={},
  1088. )
  1089. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1090. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1091. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1092. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1093. flexmock(module).should_receive('_expand_directories').and_return(())
  1094. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1095. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1096. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1097. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1098. flexmock(module).should_receive('execute_command').with_args(
  1099. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1100. output_log_level=logging.INFO,
  1101. output_file=None,
  1102. borg_local_path='borg',
  1103. )
  1104. module.create_archive(
  1105. dry_run=False,
  1106. repository='repo',
  1107. location_config={
  1108. 'source_directories': ['foo', 'bar'],
  1109. 'repositories': ['repo'],
  1110. 'exclude_patterns': None,
  1111. },
  1112. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1113. )
  1114. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1115. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1116. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1117. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1118. flexmock(module).should_receive('_expand_directories').and_return(())
  1119. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1120. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1121. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1122. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1123. flexmock(module).should_receive('execute_command').with_args(
  1124. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  1125. output_log_level=logging.INFO,
  1126. output_file=None,
  1127. borg_local_path='borg',
  1128. )
  1129. module.create_archive(
  1130. dry_run=False,
  1131. repository='repo',
  1132. location_config={
  1133. 'source_directories': ['foo', 'bar'],
  1134. 'repositories': ['repo'],
  1135. 'exclude_patterns': None,
  1136. },
  1137. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1138. )
  1139. def test_create_archive_with_repository_accepts_borg_placeholders():
  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).should_receive('_expand_home_directories').and_return(())
  1145. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1146. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1147. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1148. flexmock(module).should_receive('execute_command').with_args(
  1149. ('borg', 'create', '{fqdn}::Documents_{hostname}-{now}', 'foo', 'bar'),
  1150. output_log_level=logging.INFO,
  1151. output_file=None,
  1152. borg_local_path='borg',
  1153. )
  1154. module.create_archive(
  1155. dry_run=False,
  1156. repository='{fqdn}',
  1157. location_config={
  1158. 'source_directories': ['foo', 'bar'],
  1159. 'repositories': ['{fqdn}'],
  1160. 'exclude_patterns': None,
  1161. },
  1162. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1163. )
  1164. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1165. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1166. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1167. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1168. flexmock(module).should_receive('_expand_directories').and_return(())
  1169. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1170. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1171. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1172. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1173. flexmock(module).should_receive('execute_command').with_args(
  1174. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  1175. output_log_level=logging.INFO,
  1176. output_file=None,
  1177. borg_local_path='borg',
  1178. )
  1179. module.create_archive(
  1180. dry_run=False,
  1181. repository='repo',
  1182. location_config={
  1183. 'source_directories': ['foo', 'bar'],
  1184. 'repositories': ['repo'],
  1185. 'exclude_patterns': None,
  1186. },
  1187. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1188. )
  1189. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1190. processes = flexmock()
  1191. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1192. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1193. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1194. flexmock(module).should_receive('_expand_directories').and_return(())
  1195. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1196. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1197. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1198. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1199. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1200. ('borg', 'create', '--one-file-system', '--read-special') + ARCHIVE_WITH_PATHS,
  1201. processes=processes,
  1202. output_log_level=logging.INFO,
  1203. output_file=None,
  1204. borg_local_path='borg',
  1205. )
  1206. module.create_archive(
  1207. dry_run=False,
  1208. repository='repo',
  1209. location_config={
  1210. 'source_directories': ['foo', 'bar'],
  1211. 'repositories': ['repo'],
  1212. 'exclude_patterns': None,
  1213. },
  1214. storage_config={},
  1215. stream_processes=processes,
  1216. )