2
0

test_create.py 48 KB

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