test_create.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import create as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_expand_directory_with_basic_path_passes_it_through():
  7. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  8. flexmock(module.glob).should_receive('glob').and_return([])
  9. paths = module._expand_directory('foo')
  10. assert paths == ['foo']
  11. def test_expand_directory_with_glob_expands():
  12. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  13. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  14. paths = module._expand_directory('foo*')
  15. assert paths == ['foo', 'food']
  16. def test_expand_directories_flattens_expanded_directories():
  17. flexmock(module).should_receive('_expand_directory').with_args('~/foo').and_return(
  18. ['/root/foo']
  19. )
  20. flexmock(module).should_receive('_expand_directory').with_args('bar*').and_return(
  21. ['bar', 'barf']
  22. )
  23. paths = module._expand_directories(('~/foo', 'bar*'))
  24. assert paths == ('/root/foo', 'bar', 'barf')
  25. def test_expand_directories_considers_none_as_no_directories():
  26. paths = module._expand_directories(None)
  27. assert paths == ()
  28. def test_expand_home_directories_expands_tildes():
  29. flexmock(module.os.path).should_receive('expanduser').with_args('~/bar').and_return('/foo/bar')
  30. flexmock(module.os.path).should_receive('expanduser').with_args('baz').and_return('baz')
  31. paths = module._expand_home_directories(('~/bar', 'baz'))
  32. assert paths == ('/foo/bar', 'baz')
  33. def test_expand_home_directories_considers_none_as_no_directories():
  34. paths = module._expand_home_directories(None)
  35. assert paths == ()
  36. def test_write_pattern_file_does_not_raise():
  37. temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
  38. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  39. module._write_pattern_file(['exclude'])
  40. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  41. module._write_pattern_file([])
  42. def test_make_pattern_flags_includes_pattern_filename_when_given():
  43. pattern_flags = module._make_pattern_flags(
  44. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  45. )
  46. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  47. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  48. pattern_flags = module._make_pattern_flags(
  49. location_config={'patterns_from': ['patterns', 'other']}
  50. )
  51. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  52. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  53. pattern_flags = module._make_pattern_flags(
  54. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  55. )
  56. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  57. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  58. pattern_flags = module._make_pattern_flags(location_config={'patterns_from': None})
  59. assert pattern_flags == ()
  60. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  61. exclude_flags = module._make_exclude_flags(
  62. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  63. )
  64. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  65. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  66. exclude_flags = module._make_exclude_flags(
  67. location_config={'exclude_from': ['excludes', 'other']}
  68. )
  69. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  70. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  71. exclude_flags = module._make_exclude_flags(
  72. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  73. )
  74. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  75. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  76. exclude_flags = module._make_exclude_flags(location_config={'exclude_from': None})
  77. assert exclude_flags == ()
  78. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  79. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': True})
  80. assert exclude_flags == ('--exclude-caches',)
  81. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  82. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': False})
  83. assert exclude_flags == ()
  84. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  85. exclude_flags = module._make_exclude_flags(location_config={'exclude_if_present': 'exclude_me'})
  86. assert exclude_flags == ('--exclude-if-present', 'exclude_me')
  87. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  88. exclude_flags = module._make_exclude_flags(location_config={})
  89. assert exclude_flags == ()
  90. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  91. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  92. def test_create_archive_calls_borg_with_parameters():
  93. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  94. flexmock(module).should_receive('_expand_home_directories').and_return(())
  95. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  96. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  97. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  98. flexmock(module).should_receive('execute_command').with_args(
  99. ('borg', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  100. )
  101. module.create_archive(
  102. dry_run=False,
  103. repository='repo',
  104. location_config={
  105. 'source_directories': ['foo', 'bar'],
  106. 'repositories': ['repo'],
  107. 'exclude_patterns': None,
  108. },
  109. storage_config={},
  110. )
  111. def test_create_archive_with_patterns_calls_borg_with_patterns():
  112. pattern_flags = ('--patterns-from', 'patterns')
  113. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  114. flexmock(module).should_receive('_expand_home_directories').and_return(())
  115. flexmock(module).should_receive('_write_pattern_file').and_return(
  116. flexmock(name='/tmp/patterns')
  117. ).and_return(None)
  118. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  119. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  120. flexmock(module).should_receive('execute_command').with_args(
  121. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  122. )
  123. module.create_archive(
  124. dry_run=False,
  125. repository='repo',
  126. location_config={
  127. 'source_directories': ['foo', 'bar'],
  128. 'repositories': ['repo'],
  129. 'patterns': ['pattern'],
  130. },
  131. storage_config={},
  132. )
  133. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  134. exclude_flags = ('--exclude-from', 'excludes')
  135. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  136. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  137. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  138. flexmock(name='/tmp/excludes')
  139. )
  140. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  141. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  142. flexmock(module).should_receive('execute_command').with_args(
  143. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  144. )
  145. module.create_archive(
  146. dry_run=False,
  147. repository='repo',
  148. location_config={
  149. 'source_directories': ['foo', 'bar'],
  150. 'repositories': ['repo'],
  151. 'exclude_patterns': ['exclude'],
  152. },
  153. storage_config={},
  154. )
  155. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  156. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  157. flexmock(module).should_receive('_expand_home_directories').and_return(())
  158. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  159. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  160. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  161. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  162. flexmock(module).should_receive('execute_command').with_args(
  163. ('borg', 'create', '--list', '--filter', 'AME-', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  164. output_log_level=logging.INFO,
  165. )
  166. insert_logging_mock(logging.INFO)
  167. module.create_archive(
  168. dry_run=False,
  169. repository='repo',
  170. location_config={
  171. 'source_directories': ['foo', 'bar'],
  172. 'repositories': ['repo'],
  173. 'exclude_patterns': None,
  174. },
  175. storage_config={},
  176. )
  177. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  178. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  179. flexmock(module).should_receive('_expand_home_directories').and_return(())
  180. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  181. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  182. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  183. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  184. flexmock(module).should_receive('execute_command').with_args(
  185. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  186. )
  187. insert_logging_mock(logging.INFO)
  188. module.create_archive(
  189. dry_run=False,
  190. repository='repo',
  191. location_config={
  192. 'source_directories': ['foo', 'bar'],
  193. 'repositories': ['repo'],
  194. 'exclude_patterns': None,
  195. },
  196. storage_config={},
  197. json=True,
  198. )
  199. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  200. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  201. flexmock(module).should_receive('_expand_home_directories').and_return(())
  202. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  203. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  204. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  205. flexmock(module).should_receive('execute_command').with_args(
  206. ('borg', 'create', '--list', '--filter', 'AME-', '--stats', '--debug', '--show-rc')
  207. + ARCHIVE_WITH_PATHS,
  208. output_log_level=logging.INFO,
  209. )
  210. insert_logging_mock(logging.DEBUG)
  211. module.create_archive(
  212. dry_run=False,
  213. repository='repo',
  214. location_config={
  215. 'source_directories': ['foo', 'bar'],
  216. 'repositories': ['repo'],
  217. 'exclude_patterns': None,
  218. },
  219. storage_config={},
  220. )
  221. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  222. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  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_exclude_flags').and_return(())
  227. flexmock(module).should_receive('execute_command').with_args(
  228. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  229. )
  230. insert_logging_mock(logging.DEBUG)
  231. module.create_archive(
  232. dry_run=False,
  233. repository='repo',
  234. location_config={
  235. 'source_directories': ['foo', 'bar'],
  236. 'repositories': ['repo'],
  237. 'exclude_patterns': None,
  238. },
  239. storage_config={},
  240. json=True,
  241. )
  242. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  243. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  244. flexmock(module).should_receive('_expand_home_directories').and_return(())
  245. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  246. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  247. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  248. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  249. flexmock(module).should_receive('execute_command').with_args(
  250. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  251. )
  252. module.create_archive(
  253. dry_run=True,
  254. repository='repo',
  255. location_config={
  256. 'source_directories': ['foo', 'bar'],
  257. 'repositories': ['repo'],
  258. 'exclude_patterns': None,
  259. },
  260. storage_config={},
  261. )
  262. def test_create_archive_with_dry_run_and_log_info_calls_borg_without_stats_parameter():
  263. # --dry-run and --stats are mutually exclusive, see:
  264. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  265. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  266. flexmock(module).should_receive('_expand_home_directories').and_return(())
  267. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  268. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  269. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  270. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  271. flexmock(module).should_receive('execute_command').with_args(
  272. ('borg', 'create', '--list', '--filter', 'AME-', '--info', '--dry-run')
  273. + ARCHIVE_WITH_PATHS,
  274. output_log_level=logging.INFO,
  275. )
  276. insert_logging_mock(logging.INFO)
  277. module.create_archive(
  278. dry_run=True,
  279. repository='repo',
  280. location_config={
  281. 'source_directories': ['foo', 'bar'],
  282. 'repositories': ['repo'],
  283. 'exclude_patterns': None,
  284. },
  285. storage_config={},
  286. )
  287. def test_create_archive_with_dry_run_and_log_debug_calls_borg_without_stats_parameter():
  288. # --dry-run and --stats are mutually exclusive, see:
  289. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  290. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  291. flexmock(module).should_receive('_expand_home_directories').and_return(())
  292. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  293. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  294. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  295. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  296. flexmock(module).should_receive('execute_command').with_args(
  297. ('borg', 'create', '--list', '--filter', 'AME-', '--debug', '--show-rc', '--dry-run')
  298. + ARCHIVE_WITH_PATHS,
  299. output_log_level=logging.INFO,
  300. )
  301. insert_logging_mock(logging.DEBUG)
  302. module.create_archive(
  303. dry_run=True,
  304. repository='repo',
  305. location_config={
  306. 'source_directories': ['foo', 'bar'],
  307. 'repositories': ['repo'],
  308. 'exclude_patterns': None,
  309. },
  310. storage_config={},
  311. )
  312. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  313. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  314. flexmock(module).should_receive('_expand_home_directories').and_return(())
  315. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  316. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  317. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  318. flexmock(module).should_receive('execute_command').with_args(
  319. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  320. output_log_level=logging.INFO,
  321. )
  322. module.create_archive(
  323. dry_run=False,
  324. repository='repo',
  325. location_config={
  326. 'source_directories': ['foo', 'bar'],
  327. 'repositories': ['repo'],
  328. 'exclude_patterns': None,
  329. },
  330. storage_config={'checkpoint_interval': 600},
  331. )
  332. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  333. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  334. flexmock(module).should_receive('_expand_home_directories').and_return(())
  335. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  336. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  337. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  338. flexmock(module).should_receive('execute_command').with_args(
  339. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  340. output_log_level=logging.INFO,
  341. )
  342. module.create_archive(
  343. dry_run=False,
  344. repository='repo',
  345. location_config={
  346. 'source_directories': ['foo', 'bar'],
  347. 'repositories': ['repo'],
  348. 'exclude_patterns': None,
  349. },
  350. storage_config={'chunker_params': '1,2,3,4'},
  351. )
  352. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  353. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  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_exclude_flags').and_return(())
  358. flexmock(module).should_receive('execute_command').with_args(
  359. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  360. output_log_level=logging.INFO,
  361. )
  362. module.create_archive(
  363. dry_run=False,
  364. repository='repo',
  365. location_config={
  366. 'source_directories': ['foo', 'bar'],
  367. 'repositories': ['repo'],
  368. 'exclude_patterns': None,
  369. },
  370. storage_config={'compression': 'rle'},
  371. )
  372. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  373. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  374. flexmock(module).should_receive('_expand_home_directories').and_return(())
  375. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  376. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  377. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  378. flexmock(module).should_receive('execute_command').with_args(
  379. ('borg', 'create', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
  380. output_log_level=logging.INFO,
  381. )
  382. module.create_archive(
  383. dry_run=False,
  384. repository='repo',
  385. location_config={
  386. 'source_directories': ['foo', 'bar'],
  387. 'repositories': ['repo'],
  388. 'exclude_patterns': None,
  389. },
  390. storage_config={'remote_rate_limit': 100},
  391. )
  392. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  393. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  394. flexmock(module).should_receive('_expand_home_directories').and_return(())
  395. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  396. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  397. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  398. flexmock(module).should_receive('execute_command').with_args(
  399. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  400. )
  401. module.create_archive(
  402. dry_run=False,
  403. repository='repo',
  404. location_config={
  405. 'source_directories': ['foo', 'bar'],
  406. 'repositories': ['repo'],
  407. 'one_file_system': True,
  408. 'exclude_patterns': None,
  409. },
  410. storage_config={},
  411. )
  412. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  413. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  414. flexmock(module).should_receive('_expand_home_directories').and_return(())
  415. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  416. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  417. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  418. flexmock(module).should_receive('execute_command').with_args(
  419. ('borg', 'create', '--numeric-owner') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  420. )
  421. module.create_archive(
  422. dry_run=False,
  423. repository='repo',
  424. location_config={
  425. 'source_directories': ['foo', 'bar'],
  426. 'repositories': ['repo'],
  427. 'numeric_owner': True,
  428. 'exclude_patterns': None,
  429. },
  430. storage_config={},
  431. )
  432. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  433. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  434. flexmock(module).should_receive('_expand_home_directories').and_return(())
  435. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  436. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  437. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  438. flexmock(module).should_receive('execute_command').with_args(
  439. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  440. )
  441. module.create_archive(
  442. dry_run=False,
  443. repository='repo',
  444. location_config={
  445. 'source_directories': ['foo', 'bar'],
  446. 'repositories': ['repo'],
  447. 'read_special': True,
  448. 'exclude_patterns': None,
  449. },
  450. storage_config={},
  451. )
  452. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  453. def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
  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') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  461. )
  462. module.create_archive(
  463. dry_run=False,
  464. repository='repo',
  465. location_config={
  466. 'source_directories': ['foo', 'bar'],
  467. 'repositories': ['repo'],
  468. option_name: True,
  469. 'exclude_patterns': None,
  470. },
  471. storage_config={},
  472. )
  473. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  474. def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
  475. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  476. flexmock(module).should_receive('_expand_home_directories').and_return(())
  477. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  478. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  479. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  480. flexmock(module).should_receive('execute_command').with_args(
  481. ('borg', 'create', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
  482. output_log_level=logging.INFO,
  483. )
  484. module.create_archive(
  485. dry_run=False,
  486. repository='repo',
  487. location_config={
  488. 'source_directories': ['foo', 'bar'],
  489. 'repositories': ['repo'],
  490. option_name: False,
  491. 'exclude_patterns': None,
  492. },
  493. storage_config={},
  494. )
  495. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  496. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  497. flexmock(module).should_receive('_expand_home_directories').and_return(())
  498. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  499. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  500. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  501. flexmock(module).should_receive('execute_command').with_args(
  502. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  503. output_log_level=logging.INFO,
  504. )
  505. module.create_archive(
  506. dry_run=False,
  507. repository='repo',
  508. location_config={
  509. 'source_directories': ['foo', 'bar'],
  510. 'repositories': ['repo'],
  511. 'files_cache': 'ctime,size',
  512. 'exclude_patterns': None,
  513. },
  514. storage_config={},
  515. )
  516. def test_create_archive_with_local_path_calls_borg_via_local_path():
  517. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  518. flexmock(module).should_receive('_expand_home_directories').and_return(())
  519. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  520. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  521. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  522. flexmock(module).should_receive('execute_command').with_args(
  523. ('borg1', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  524. )
  525. module.create_archive(
  526. dry_run=False,
  527. repository='repo',
  528. location_config={
  529. 'source_directories': ['foo', 'bar'],
  530. 'repositories': ['repo'],
  531. 'exclude_patterns': None,
  532. },
  533. storage_config={},
  534. local_path='borg1',
  535. )
  536. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  537. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  538. flexmock(module).should_receive('_expand_home_directories').and_return(())
  539. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  540. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  541. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  542. flexmock(module).should_receive('execute_command').with_args(
  543. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  544. output_log_level=logging.INFO,
  545. )
  546. module.create_archive(
  547. dry_run=False,
  548. repository='repo',
  549. location_config={
  550. 'source_directories': ['foo', 'bar'],
  551. 'repositories': ['repo'],
  552. 'exclude_patterns': None,
  553. },
  554. storage_config={},
  555. remote_path='borg1',
  556. )
  557. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  558. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  559. flexmock(module).should_receive('_expand_home_directories').and_return(())
  560. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  561. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  562. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  563. flexmock(module).should_receive('execute_command').with_args(
  564. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  565. )
  566. module.create_archive(
  567. dry_run=False,
  568. repository='repo',
  569. location_config={
  570. 'source_directories': ['foo', 'bar'],
  571. 'repositories': ['repo'],
  572. 'exclude_patterns': None,
  573. },
  574. storage_config={'umask': 740},
  575. )
  576. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  577. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  578. flexmock(module).should_receive('_expand_home_directories').and_return(())
  579. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  580. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  581. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  582. flexmock(module).should_receive('execute_command').with_args(
  583. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  584. )
  585. module.create_archive(
  586. dry_run=False,
  587. repository='repo',
  588. location_config={
  589. 'source_directories': ['foo', 'bar'],
  590. 'repositories': ['repo'],
  591. 'exclude_patterns': None,
  592. },
  593. storage_config={'lock_wait': 5},
  594. )
  595. def test_create_archive_with_stats_calls_borg_with_stats_parameter():
  596. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  597. flexmock(module).should_receive('_expand_home_directories').and_return(())
  598. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  599. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  600. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  601. flexmock(module).should_receive('execute_command').with_args(
  602. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS, output_log_level=logging.WARNING
  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. stats=True,
  614. )
  615. def test_create_archive_with_json_calls_borg_with_json_parameter():
  616. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  617. flexmock(module).should_receive('_expand_home_directories').and_return(())
  618. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  619. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  620. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  621. flexmock(module).should_receive('execute_command').with_args(
  622. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  623. ).and_return('[]')
  624. json_output = module.create_archive(
  625. dry_run=False,
  626. repository='repo',
  627. location_config={
  628. 'source_directories': ['foo', 'bar'],
  629. 'repositories': ['repo'],
  630. 'exclude_patterns': None,
  631. },
  632. storage_config={},
  633. json=True,
  634. )
  635. assert json_output == '[]'
  636. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  637. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  638. flexmock(module).should_receive('_expand_home_directories').and_return(())
  639. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  640. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  641. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  642. flexmock(module).should_receive('execute_command').with_args(
  643. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  644. ).and_return('[]')
  645. json_output = module.create_archive(
  646. dry_run=False,
  647. repository='repo',
  648. location_config={
  649. 'source_directories': ['foo', 'bar'],
  650. 'repositories': ['repo'],
  651. 'exclude_patterns': None,
  652. },
  653. storage_config={},
  654. json=True,
  655. stats=True,
  656. )
  657. assert json_output == '[]'
  658. def test_create_archive_with_source_directories_glob_expands():
  659. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  660. flexmock(module).should_receive('_expand_home_directories').and_return(())
  661. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  662. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  663. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  664. flexmock(module).should_receive('execute_command').with_args(
  665. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  666. output_log_level=logging.INFO,
  667. )
  668. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  669. module.create_archive(
  670. dry_run=False,
  671. repository='repo',
  672. location_config={
  673. 'source_directories': ['foo*'],
  674. 'repositories': ['repo'],
  675. 'exclude_patterns': None,
  676. },
  677. storage_config={},
  678. )
  679. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  680. flexmock(module).should_receive('_expand_directories').and_return(('foo*',))
  681. flexmock(module).should_receive('_expand_home_directories').and_return(())
  682. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  683. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  684. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  685. flexmock(module).should_receive('execute_command').with_args(
  686. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  687. output_log_level=logging.INFO,
  688. )
  689. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  690. module.create_archive(
  691. dry_run=False,
  692. repository='repo',
  693. location_config={
  694. 'source_directories': ['foo*'],
  695. 'repositories': ['repo'],
  696. 'exclude_patterns': None,
  697. },
  698. storage_config={},
  699. )
  700. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  701. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  702. flexmock(module).should_receive('_expand_home_directories').and_return(())
  703. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  704. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  705. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  706. flexmock(module).should_receive('execute_command').with_args(
  707. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  708. output_log_level=logging.INFO,
  709. )
  710. module.create_archive(
  711. dry_run=False,
  712. repository='repo',
  713. location_config={
  714. 'source_directories': ['foo*'],
  715. 'repositories': ['repo'],
  716. 'exclude_patterns': None,
  717. },
  718. storage_config={},
  719. )
  720. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  721. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  722. flexmock(module).should_receive('_expand_home_directories').and_return(())
  723. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  724. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  725. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  726. flexmock(module).should_receive('execute_command').with_args(
  727. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'), output_log_level=logging.INFO
  728. )
  729. module.create_archive(
  730. dry_run=False,
  731. repository='repo',
  732. location_config={
  733. 'source_directories': ['foo', 'bar'],
  734. 'repositories': ['repo'],
  735. 'exclude_patterns': None,
  736. },
  737. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  738. )
  739. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  740. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  741. flexmock(module).should_receive('_expand_home_directories').and_return(())
  742. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  743. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  744. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  745. flexmock(module).should_receive('execute_command').with_args(
  746. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  747. output_log_level=logging.INFO,
  748. )
  749. module.create_archive(
  750. dry_run=False,
  751. repository='repo',
  752. location_config={
  753. 'source_directories': ['foo', 'bar'],
  754. 'repositories': ['repo'],
  755. 'exclude_patterns': None,
  756. },
  757. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  758. )