test_create.py 54 KB

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