test_create.py 35 KB

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