2
0

test_create.py 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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_write_pattern_file_does_not_raise():
  37. temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
  38. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  39. module._write_pattern_file(['exclude'])
  40. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  41. module._write_pattern_file([])
  42. def test_make_pattern_flags_includes_pattern_filename_when_given():
  43. pattern_flags = module._make_pattern_flags(
  44. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  45. )
  46. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  47. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  48. pattern_flags = module._make_pattern_flags(
  49. location_config={'patterns_from': ['patterns', 'other']}
  50. )
  51. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  52. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  53. pattern_flags = module._make_pattern_flags(
  54. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  55. )
  56. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  57. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  58. pattern_flags = module._make_pattern_flags(location_config={'patterns_from': None})
  59. assert pattern_flags == ()
  60. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  61. exclude_flags = module._make_exclude_flags(
  62. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  63. )
  64. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  65. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  66. exclude_flags = module._make_exclude_flags(
  67. location_config={'exclude_from': ['excludes', 'other']}
  68. )
  69. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  70. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  71. exclude_flags = module._make_exclude_flags(
  72. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  73. )
  74. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  75. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  76. exclude_flags = module._make_exclude_flags(location_config={'exclude_from': None})
  77. assert exclude_flags == ()
  78. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  79. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': True})
  80. assert exclude_flags == ('--exclude-caches',)
  81. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  82. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': False})
  83. assert exclude_flags == ()
  84. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  85. exclude_flags = module._make_exclude_flags(location_config={'exclude_if_present': 'exclude_me'})
  86. assert exclude_flags == ('--exclude-if-present', 'exclude_me')
  87. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  88. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': True})
  89. assert exclude_flags == ('--keep-exclude-tags',)
  90. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  91. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': False})
  92. assert exclude_flags == ()
  93. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  94. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': True})
  95. assert exclude_flags == ('--exclude-nodump',)
  96. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  97. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': False})
  98. assert exclude_flags == ()
  99. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  100. exclude_flags = module._make_exclude_flags(location_config={})
  101. assert exclude_flags == ()
  102. def test_borgmatic_source_directories_set_when_directory_exists():
  103. flexmock(module.os.path).should_receive('exists').and_return(True)
  104. flexmock(module.os.path).should_receive('expanduser')
  105. assert module.borgmatic_source_directories('/tmp') == ['/tmp']
  106. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  107. flexmock(module.os.path).should_receive('exists').and_return(False)
  108. flexmock(module.os.path).should_receive('expanduser')
  109. assert module.borgmatic_source_directories('/tmp') == []
  110. def test_borgmatic_source_directories_defaults_when_directory_not_given():
  111. flexmock(module.os.path).should_receive('exists').and_return(True)
  112. flexmock(module.os.path).should_receive('expanduser')
  113. assert module.borgmatic_source_directories(None) == [module.DEFAULT_BORGMATIC_SOURCE_DIRECTORY]
  114. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  115. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  116. def test_create_archive_calls_borg_with_parameters():
  117. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  118. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  119. flexmock(module).should_receive('_expand_home_directories').and_return(())
  120. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  121. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  122. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  123. flexmock(module).should_receive('execute_command').with_args(
  124. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  125. output_log_level=logging.INFO,
  126. error_on_warnings=False,
  127. )
  128. module.create_archive(
  129. dry_run=False,
  130. repository='repo',
  131. location_config={
  132. 'source_directories': ['foo', 'bar'],
  133. 'repositories': ['repo'],
  134. 'exclude_patterns': None,
  135. },
  136. storage_config={},
  137. )
  138. def test_create_archive_with_patterns_calls_borg_with_patterns():
  139. pattern_flags = ('--patterns-from', 'patterns')
  140. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  141. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  142. flexmock(module).should_receive('_expand_home_directories').and_return(())
  143. flexmock(module).should_receive('_write_pattern_file').and_return(
  144. flexmock(name='/tmp/patterns')
  145. ).and_return(None)
  146. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  147. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  148. flexmock(module).should_receive('execute_command').with_args(
  149. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  150. output_log_level=logging.INFO,
  151. error_on_warnings=False,
  152. )
  153. module.create_archive(
  154. dry_run=False,
  155. repository='repo',
  156. location_config={
  157. 'source_directories': ['foo', 'bar'],
  158. 'repositories': ['repo'],
  159. 'patterns': ['pattern'],
  160. },
  161. storage_config={},
  162. )
  163. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  164. exclude_flags = ('--exclude-from', 'excludes')
  165. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  166. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  167. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  168. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  169. flexmock(name='/tmp/excludes')
  170. )
  171. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  172. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  173. flexmock(module).should_receive('execute_command').with_args(
  174. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  175. output_log_level=logging.INFO,
  176. error_on_warnings=False,
  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': ['exclude'],
  185. },
  186. storage_config={},
  187. )
  188. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  189. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  190. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  191. flexmock(module).should_receive('_expand_home_directories').and_return(())
  192. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  193. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  194. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  195. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  196. flexmock(module).should_receive('execute_command').with_args(
  197. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  198. output_log_level=logging.INFO,
  199. error_on_warnings=False,
  200. )
  201. insert_logging_mock(logging.INFO)
  202. module.create_archive(
  203. dry_run=False,
  204. repository='repo',
  205. location_config={
  206. 'source_directories': ['foo', 'bar'],
  207. 'repositories': ['repo'],
  208. 'exclude_patterns': None,
  209. },
  210. storage_config={},
  211. )
  212. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  213. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  214. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  215. flexmock(module).should_receive('_expand_home_directories').and_return(())
  216. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  217. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  218. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  219. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  220. flexmock(module).should_receive('execute_command').with_args(
  221. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  222. output_log_level=None,
  223. error_on_warnings=False,
  224. )
  225. insert_logging_mock(logging.INFO)
  226. module.create_archive(
  227. dry_run=False,
  228. repository='repo',
  229. location_config={
  230. 'source_directories': ['foo', 'bar'],
  231. 'repositories': ['repo'],
  232. 'exclude_patterns': None,
  233. },
  234. storage_config={},
  235. json=True,
  236. )
  237. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  238. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  239. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  240. flexmock(module).should_receive('_expand_home_directories').and_return(())
  241. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  242. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  243. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  244. flexmock(module).should_receive('execute_command').with_args(
  245. ('borg', 'create', '--list', '--filter', 'AME-', '--stats', '--debug', '--show-rc')
  246. + ARCHIVE_WITH_PATHS,
  247. output_log_level=logging.INFO,
  248. error_on_warnings=False,
  249. )
  250. insert_logging_mock(logging.DEBUG)
  251. module.create_archive(
  252. dry_run=False,
  253. repository='repo',
  254. location_config={
  255. 'source_directories': ['foo', 'bar'],
  256. 'repositories': ['repo'],
  257. 'exclude_patterns': None,
  258. },
  259. storage_config={},
  260. )
  261. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  262. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  263. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  264. flexmock(module).should_receive('_expand_home_directories').and_return(())
  265. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  266. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  267. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  268. flexmock(module).should_receive('execute_command').with_args(
  269. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  270. output_log_level=None,
  271. error_on_warnings=False,
  272. )
  273. insert_logging_mock(logging.DEBUG)
  274. module.create_archive(
  275. dry_run=False,
  276. repository='repo',
  277. location_config={
  278. 'source_directories': ['foo', 'bar'],
  279. 'repositories': ['repo'],
  280. 'exclude_patterns': None,
  281. },
  282. storage_config={},
  283. json=True,
  284. )
  285. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  286. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  287. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  288. flexmock(module).should_receive('_expand_home_directories').and_return(())
  289. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  290. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  291. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  292. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  293. flexmock(module).should_receive('execute_command').with_args(
  294. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  295. output_log_level=logging.INFO,
  296. error_on_warnings=False,
  297. )
  298. module.create_archive(
  299. dry_run=True,
  300. repository='repo',
  301. location_config={
  302. 'source_directories': ['foo', 'bar'],
  303. 'repositories': ['repo'],
  304. 'exclude_patterns': None,
  305. },
  306. storage_config={},
  307. )
  308. def test_create_archive_with_dry_run_and_log_info_calls_borg_without_stats_parameter():
  309. # --dry-run and --stats are mutually exclusive, see:
  310. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  311. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  312. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  313. flexmock(module).should_receive('_expand_home_directories').and_return(())
  314. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  315. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  316. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  317. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  318. flexmock(module).should_receive('execute_command').with_args(
  319. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  320. output_log_level=logging.INFO,
  321. error_on_warnings=False,
  322. )
  323. insert_logging_mock(logging.INFO)
  324. module.create_archive(
  325. dry_run=True,
  326. repository='repo',
  327. location_config={
  328. 'source_directories': ['foo', 'bar'],
  329. 'repositories': ['repo'],
  330. 'exclude_patterns': None,
  331. },
  332. storage_config={},
  333. )
  334. def test_create_archive_with_dry_run_and_log_debug_calls_borg_without_stats_parameter():
  335. # --dry-run and --stats are mutually exclusive, see:
  336. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  337. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  338. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  339. flexmock(module).should_receive('_expand_home_directories').and_return(())
  340. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  341. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  342. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  343. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  344. flexmock(module).should_receive('execute_command').with_args(
  345. ('borg', 'create', '--list', '--filter', 'AME-', '--debug', '--show-rc', '--dry-run')
  346. + ARCHIVE_WITH_PATHS,
  347. output_log_level=logging.INFO,
  348. error_on_warnings=False,
  349. )
  350. insert_logging_mock(logging.DEBUG)
  351. module.create_archive(
  352. dry_run=True,
  353. repository='repo',
  354. location_config={
  355. 'source_directories': ['foo', 'bar'],
  356. 'repositories': ['repo'],
  357. 'exclude_patterns': None,
  358. },
  359. storage_config={},
  360. )
  361. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  362. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  363. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  364. flexmock(module).should_receive('_expand_home_directories').and_return(())
  365. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  366. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  367. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  368. flexmock(module).should_receive('execute_command').with_args(
  369. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  370. output_log_level=logging.INFO,
  371. error_on_warnings=False,
  372. )
  373. module.create_archive(
  374. dry_run=False,
  375. repository='repo',
  376. location_config={
  377. 'source_directories': ['foo', 'bar'],
  378. 'repositories': ['repo'],
  379. 'exclude_patterns': None,
  380. },
  381. storage_config={'checkpoint_interval': 600},
  382. )
  383. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  384. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  385. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  386. flexmock(module).should_receive('_expand_home_directories').and_return(())
  387. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  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', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  392. output_log_level=logging.INFO,
  393. error_on_warnings=False,
  394. )
  395. module.create_archive(
  396. dry_run=False,
  397. repository='repo',
  398. location_config={
  399. 'source_directories': ['foo', 'bar'],
  400. 'repositories': ['repo'],
  401. 'exclude_patterns': None,
  402. },
  403. storage_config={'chunker_params': '1,2,3,4'},
  404. )
  405. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  406. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  407. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  408. flexmock(module).should_receive('_expand_home_directories').and_return(())
  409. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  410. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  411. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  412. flexmock(module).should_receive('execute_command').with_args(
  413. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  414. output_log_level=logging.INFO,
  415. error_on_warnings=False,
  416. )
  417. module.create_archive(
  418. dry_run=False,
  419. repository='repo',
  420. location_config={
  421. 'source_directories': ['foo', 'bar'],
  422. 'repositories': ['repo'],
  423. 'exclude_patterns': None,
  424. },
  425. storage_config={'compression': 'rle'},
  426. )
  427. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  428. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  429. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  430. flexmock(module).should_receive('_expand_home_directories').and_return(())
  431. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  432. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  433. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  434. flexmock(module).should_receive('execute_command').with_args(
  435. ('borg', 'create', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
  436. output_log_level=logging.INFO,
  437. error_on_warnings=False,
  438. )
  439. module.create_archive(
  440. dry_run=False,
  441. repository='repo',
  442. location_config={
  443. 'source_directories': ['foo', 'bar'],
  444. 'repositories': ['repo'],
  445. 'exclude_patterns': None,
  446. },
  447. storage_config={'remote_rate_limit': 100},
  448. )
  449. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  450. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  451. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  452. flexmock(module).should_receive('_expand_home_directories').and_return(())
  453. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  454. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  455. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  456. flexmock(module).should_receive('execute_command').with_args(
  457. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  458. output_log_level=logging.INFO,
  459. error_on_warnings=False,
  460. )
  461. module.create_archive(
  462. dry_run=False,
  463. repository='repo',
  464. location_config={
  465. 'source_directories': ['foo', 'bar'],
  466. 'repositories': ['repo'],
  467. 'one_file_system': True,
  468. 'exclude_patterns': None,
  469. },
  470. storage_config={},
  471. )
  472. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  473. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  474. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  475. flexmock(module).should_receive('_expand_home_directories').and_return(())
  476. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  477. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  478. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  479. flexmock(module).should_receive('execute_command').with_args(
  480. ('borg', 'create', '--numeric-owner') + ARCHIVE_WITH_PATHS,
  481. output_log_level=logging.INFO,
  482. error_on_warnings=False,
  483. )
  484. module.create_archive(
  485. dry_run=False,
  486. repository='repo',
  487. location_config={
  488. 'source_directories': ['foo', 'bar'],
  489. 'repositories': ['repo'],
  490. 'numeric_owner': True,
  491. 'exclude_patterns': None,
  492. },
  493. storage_config={},
  494. )
  495. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  496. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  497. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  498. flexmock(module).should_receive('_expand_home_directories').and_return(())
  499. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  500. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  501. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  502. flexmock(module).should_receive('execute_command').with_args(
  503. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  504. output_log_level=logging.INFO,
  505. error_on_warnings=False,
  506. )
  507. module.create_archive(
  508. dry_run=False,
  509. repository='repo',
  510. location_config={
  511. 'source_directories': ['foo', 'bar'],
  512. 'repositories': ['repo'],
  513. 'read_special': True,
  514. 'exclude_patterns': None,
  515. },
  516. storage_config={},
  517. )
  518. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  519. def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
  520. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  521. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  522. flexmock(module).should_receive('_expand_home_directories').and_return(())
  523. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  524. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  525. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  526. flexmock(module).should_receive('execute_command').with_args(
  527. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  528. output_log_level=logging.INFO,
  529. error_on_warnings=False,
  530. )
  531. module.create_archive(
  532. dry_run=False,
  533. repository='repo',
  534. location_config={
  535. 'source_directories': ['foo', 'bar'],
  536. 'repositories': ['repo'],
  537. option_name: True,
  538. 'exclude_patterns': None,
  539. },
  540. storage_config={},
  541. )
  542. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  543. def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
  544. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  545. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  546. flexmock(module).should_receive('_expand_home_directories').and_return(())
  547. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  548. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  549. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  550. flexmock(module).should_receive('execute_command').with_args(
  551. ('borg', 'create', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
  552. output_log_level=logging.INFO,
  553. error_on_warnings=False,
  554. )
  555. module.create_archive(
  556. dry_run=False,
  557. repository='repo',
  558. location_config={
  559. 'source_directories': ['foo', 'bar'],
  560. 'repositories': ['repo'],
  561. option_name: False,
  562. 'exclude_patterns': None,
  563. },
  564. storage_config={},
  565. )
  566. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  567. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  568. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  569. flexmock(module).should_receive('_expand_home_directories').and_return(())
  570. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  571. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  572. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  573. flexmock(module).should_receive('execute_command').with_args(
  574. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  575. output_log_level=logging.INFO,
  576. error_on_warnings=False,
  577. )
  578. module.create_archive(
  579. dry_run=False,
  580. repository='repo',
  581. location_config={
  582. 'source_directories': ['foo', 'bar'],
  583. 'repositories': ['repo'],
  584. 'files_cache': 'ctime,size',
  585. 'exclude_patterns': None,
  586. },
  587. storage_config={},
  588. )
  589. def test_create_archive_with_local_path_calls_borg_via_local_path():
  590. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  591. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  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. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  598. output_log_level=logging.INFO,
  599. error_on_warnings=False,
  600. )
  601. module.create_archive(
  602. dry_run=False,
  603. repository='repo',
  604. location_config={
  605. 'source_directories': ['foo', 'bar'],
  606. 'repositories': ['repo'],
  607. 'exclude_patterns': None,
  608. },
  609. storage_config={},
  610. local_path='borg1',
  611. )
  612. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  613. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  614. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  615. flexmock(module).should_receive('_expand_home_directories').and_return(())
  616. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  617. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  618. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  619. flexmock(module).should_receive('execute_command').with_args(
  620. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  621. output_log_level=logging.INFO,
  622. error_on_warnings=False,
  623. )
  624. module.create_archive(
  625. dry_run=False,
  626. repository='repo',
  627. location_config={
  628. 'source_directories': ['foo', 'bar'],
  629. 'repositories': ['repo'],
  630. 'exclude_patterns': None,
  631. },
  632. storage_config={},
  633. remote_path='borg1',
  634. )
  635. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  636. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  637. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  638. flexmock(module).should_receive('_expand_home_directories').and_return(())
  639. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  640. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  641. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  642. flexmock(module).should_receive('execute_command').with_args(
  643. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS,
  644. output_log_level=logging.INFO,
  645. error_on_warnings=False,
  646. )
  647. module.create_archive(
  648. dry_run=False,
  649. repository='repo',
  650. location_config={
  651. 'source_directories': ['foo', 'bar'],
  652. 'repositories': ['repo'],
  653. 'exclude_patterns': None,
  654. },
  655. storage_config={'umask': 740},
  656. )
  657. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  658. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  659. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  660. flexmock(module).should_receive('_expand_home_directories').and_return(())
  661. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  662. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  663. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  664. flexmock(module).should_receive('execute_command').with_args(
  665. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  666. output_log_level=logging.INFO,
  667. error_on_warnings=False,
  668. )
  669. module.create_archive(
  670. dry_run=False,
  671. repository='repo',
  672. location_config={
  673. 'source_directories': ['foo', 'bar'],
  674. 'repositories': ['repo'],
  675. 'exclude_patterns': None,
  676. },
  677. storage_config={'lock_wait': 5},
  678. )
  679. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  680. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  681. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  682. flexmock(module).should_receive('_expand_home_directories').and_return(())
  683. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  684. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  685. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  686. flexmock(module).should_receive('execute_command').with_args(
  687. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  688. output_log_level=logging.WARNING,
  689. error_on_warnings=False,
  690. )
  691. module.create_archive(
  692. dry_run=False,
  693. repository='repo',
  694. location_config={
  695. 'source_directories': ['foo', 'bar'],
  696. 'repositories': ['repo'],
  697. 'exclude_patterns': None,
  698. },
  699. storage_config={},
  700. stats=True,
  701. )
  702. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  703. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  704. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  705. flexmock(module).should_receive('_expand_home_directories').and_return(())
  706. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  707. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  708. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  709. flexmock(module).should_receive('execute_command').with_args(
  710. ('borg', 'create', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  711. output_log_level=logging.INFO,
  712. error_on_warnings=False,
  713. )
  714. insert_logging_mock(logging.INFO)
  715. module.create_archive(
  716. dry_run=False,
  717. repository='repo',
  718. location_config={
  719. 'source_directories': ['foo', 'bar'],
  720. 'repositories': ['repo'],
  721. 'exclude_patterns': None,
  722. },
  723. storage_config={},
  724. stats=True,
  725. )
  726. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  727. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  728. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  729. flexmock(module).should_receive('_expand_home_directories').and_return(())
  730. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  731. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  732. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  733. flexmock(module).should_receive('execute_command').with_args(
  734. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  735. output_log_level=logging.WARNING,
  736. error_on_warnings=False,
  737. )
  738. module.create_archive(
  739. dry_run=False,
  740. repository='repo',
  741. location_config={
  742. 'source_directories': ['foo', 'bar'],
  743. 'repositories': ['repo'],
  744. 'exclude_patterns': None,
  745. },
  746. storage_config={},
  747. files=True,
  748. )
  749. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  750. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  751. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  752. flexmock(module).should_receive('_expand_home_directories').and_return(())
  753. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  754. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  755. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  756. flexmock(module).should_receive('execute_command').with_args(
  757. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  758. output_log_level=logging.INFO,
  759. error_on_warnings=False,
  760. )
  761. insert_logging_mock(logging.INFO)
  762. module.create_archive(
  763. dry_run=False,
  764. repository='repo',
  765. location_config={
  766. 'source_directories': ['foo', 'bar'],
  767. 'repositories': ['repo'],
  768. 'exclude_patterns': None,
  769. },
  770. storage_config={},
  771. files=True,
  772. )
  773. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  774. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  775. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  776. flexmock(module).should_receive('_expand_home_directories').and_return(())
  777. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  778. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  779. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  780. flexmock(module).should_receive('execute_command_without_capture').with_args(
  781. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS, error_on_warnings=False
  782. )
  783. insert_logging_mock(logging.INFO)
  784. module.create_archive(
  785. dry_run=False,
  786. repository='repo',
  787. location_config={
  788. 'source_directories': ['foo', 'bar'],
  789. 'repositories': ['repo'],
  790. 'exclude_patterns': None,
  791. },
  792. storage_config={},
  793. progress=True,
  794. )
  795. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  796. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  797. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  798. flexmock(module).should_receive('_expand_home_directories').and_return(())
  799. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  800. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  801. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  802. flexmock(module).should_receive('execute_command_without_capture').with_args(
  803. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS, error_on_warnings=False
  804. )
  805. module.create_archive(
  806. dry_run=False,
  807. repository='repo',
  808. location_config={
  809. 'source_directories': ['foo', 'bar'],
  810. 'repositories': ['repo'],
  811. 'exclude_patterns': None,
  812. },
  813. storage_config={},
  814. progress=True,
  815. )
  816. def test_create_archive_with_json_calls_borg_with_json_parameter():
  817. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  818. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  819. flexmock(module).should_receive('_expand_home_directories').and_return(())
  820. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  821. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  822. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  823. flexmock(module).should_receive('execute_command').with_args(
  824. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  825. output_log_level=None,
  826. error_on_warnings=False,
  827. ).and_return('[]')
  828. json_output = module.create_archive(
  829. dry_run=False,
  830. repository='repo',
  831. location_config={
  832. 'source_directories': ['foo', 'bar'],
  833. 'repositories': ['repo'],
  834. 'exclude_patterns': None,
  835. },
  836. storage_config={},
  837. json=True,
  838. )
  839. assert json_output == '[]'
  840. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  841. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  842. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  843. flexmock(module).should_receive('_expand_home_directories').and_return(())
  844. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  845. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  846. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  847. flexmock(module).should_receive('execute_command').with_args(
  848. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  849. output_log_level=None,
  850. error_on_warnings=False,
  851. ).and_return('[]')
  852. json_output = module.create_archive(
  853. dry_run=False,
  854. repository='repo',
  855. location_config={
  856. 'source_directories': ['foo', 'bar'],
  857. 'repositories': ['repo'],
  858. 'exclude_patterns': None,
  859. },
  860. storage_config={},
  861. json=True,
  862. stats=True,
  863. )
  864. assert json_output == '[]'
  865. def test_create_archive_with_source_directories_glob_expands():
  866. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  867. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  868. flexmock(module).should_receive('_expand_home_directories').and_return(())
  869. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  870. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  871. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  872. flexmock(module).should_receive('execute_command').with_args(
  873. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  874. output_log_level=logging.INFO,
  875. error_on_warnings=False,
  876. )
  877. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  878. module.create_archive(
  879. dry_run=False,
  880. repository='repo',
  881. location_config={
  882. 'source_directories': ['foo*'],
  883. 'repositories': ['repo'],
  884. 'exclude_patterns': None,
  885. },
  886. storage_config={},
  887. )
  888. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  889. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  890. flexmock(module).should_receive('_expand_directories').and_return(('foo*',))
  891. flexmock(module).should_receive('_expand_home_directories').and_return(())
  892. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  893. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  894. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  895. flexmock(module).should_receive('execute_command').with_args(
  896. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  897. output_log_level=logging.INFO,
  898. error_on_warnings=False,
  899. )
  900. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  901. module.create_archive(
  902. dry_run=False,
  903. repository='repo',
  904. location_config={
  905. 'source_directories': ['foo*'],
  906. 'repositories': ['repo'],
  907. 'exclude_patterns': None,
  908. },
  909. storage_config={},
  910. )
  911. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  912. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  913. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  914. flexmock(module).should_receive('_expand_home_directories').and_return(())
  915. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  916. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  917. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  918. flexmock(module).should_receive('execute_command').with_args(
  919. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  920. output_log_level=logging.INFO,
  921. error_on_warnings=False,
  922. )
  923. module.create_archive(
  924. dry_run=False,
  925. repository='repo',
  926. location_config={
  927. 'source_directories': ['foo*'],
  928. 'repositories': ['repo'],
  929. 'exclude_patterns': None,
  930. },
  931. storage_config={},
  932. )
  933. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  934. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  935. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  936. flexmock(module).should_receive('_expand_home_directories').and_return(())
  937. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  938. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  939. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  940. flexmock(module).should_receive('execute_command').with_args(
  941. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  942. output_log_level=logging.INFO,
  943. error_on_warnings=False,
  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={'archive_name_format': 'ARCHIVE_NAME'},
  954. )
  955. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  956. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  957. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  958. flexmock(module).should_receive('_expand_home_directories').and_return(())
  959. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  960. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  961. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  962. flexmock(module).should_receive('execute_command').with_args(
  963. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  964. output_log_level=logging.INFO,
  965. error_on_warnings=False,
  966. )
  967. module.create_archive(
  968. dry_run=False,
  969. repository='repo',
  970. location_config={
  971. 'source_directories': ['foo', 'bar'],
  972. 'repositories': ['repo'],
  973. 'exclude_patterns': None,
  974. },
  975. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  976. )
  977. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  978. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  979. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  980. flexmock(module).should_receive('_expand_home_directories').and_return(())
  981. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  982. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  983. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  984. flexmock(module).should_receive('execute_command').with_args(
  985. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  986. output_log_level=logging.INFO,
  987. error_on_warnings=False,
  988. )
  989. module.create_archive(
  990. dry_run=False,
  991. repository='repo',
  992. location_config={
  993. 'source_directories': ['foo', 'bar'],
  994. 'repositories': ['repo'],
  995. 'exclude_patterns': None,
  996. },
  997. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  998. )