test_create.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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, capture_output=False, output_as_warning=False
  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, capture_output=False, output_as_warning=False
  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, capture_output=False, output_as_warning=False
  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. capture_output=False,
  164. output_as_warning=False,
  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. CREATE_COMMAND + ('--json',), capture_output=True, output_as_warning=False
  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. CREATE_COMMAND + ('--list', '--filter', 'AME-', '--stats', '--debug', '--show-rc'),
  207. capture_output=False,
  208. output_as_warning=False,
  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. CREATE_COMMAND + ('--json',), capture_output=True, output_as_warning=False
  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. CREATE_COMMAND + ('--dry-run',), capture_output=False, output_as_warning=False
  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. CREATE_COMMAND + ('--list', '--filter', 'AME-', '--info', '--dry-run'),
  273. capture_output=False,
  274. output_as_warning=False,
  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. CREATE_COMMAND + ('--list', '--filter', 'AME-', '--debug', '--show-rc', '--dry-run'),
  298. capture_output=False,
  299. output_as_warning=False,
  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. CREATE_COMMAND + ('--checkpoint-interval', '600'),
  320. capture_output=False,
  321. output_as_warning=False,
  322. )
  323. module.create_archive(
  324. dry_run=False,
  325. repository='repo',
  326. location_config={
  327. 'source_directories': ['foo', 'bar'],
  328. 'repositories': ['repo'],
  329. 'exclude_patterns': None,
  330. },
  331. storage_config={'checkpoint_interval': 600},
  332. )
  333. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  334. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  335. flexmock(module).should_receive('_expand_home_directories').and_return(())
  336. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  337. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  338. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  339. flexmock(module).should_receive('execute_command').with_args(
  340. CREATE_COMMAND + ('--chunker-params', '1,2,3,4'),
  341. capture_output=False,
  342. output_as_warning=False,
  343. )
  344. module.create_archive(
  345. dry_run=False,
  346. repository='repo',
  347. location_config={
  348. 'source_directories': ['foo', 'bar'],
  349. 'repositories': ['repo'],
  350. 'exclude_patterns': None,
  351. },
  352. storage_config={'chunker_params': '1,2,3,4'},
  353. )
  354. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  355. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  356. flexmock(module).should_receive('_expand_home_directories').and_return(())
  357. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  358. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  359. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  360. flexmock(module).should_receive('execute_command').with_args(
  361. CREATE_COMMAND + ('--compression', 'rle'), capture_output=False, output_as_warning=False
  362. )
  363. module.create_archive(
  364. dry_run=False,
  365. repository='repo',
  366. location_config={
  367. 'source_directories': ['foo', 'bar'],
  368. 'repositories': ['repo'],
  369. 'exclude_patterns': None,
  370. },
  371. storage_config={'compression': 'rle'},
  372. )
  373. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  374. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  375. flexmock(module).should_receive('_expand_home_directories').and_return(())
  376. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  377. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  378. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  379. flexmock(module).should_receive('execute_command').with_args(
  380. CREATE_COMMAND + ('--remote-ratelimit', '100'),
  381. capture_output=False,
  382. output_as_warning=False,
  383. )
  384. module.create_archive(
  385. dry_run=False,
  386. repository='repo',
  387. location_config={
  388. 'source_directories': ['foo', 'bar'],
  389. 'repositories': ['repo'],
  390. 'exclude_patterns': None,
  391. },
  392. storage_config={'remote_rate_limit': 100},
  393. )
  394. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  395. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  396. flexmock(module).should_receive('_expand_home_directories').and_return(())
  397. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  398. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  399. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  400. flexmock(module).should_receive('execute_command').with_args(
  401. CREATE_COMMAND + ('--one-file-system',), capture_output=False, output_as_warning=False
  402. )
  403. module.create_archive(
  404. dry_run=False,
  405. repository='repo',
  406. location_config={
  407. 'source_directories': ['foo', 'bar'],
  408. 'repositories': ['repo'],
  409. 'one_file_system': True,
  410. 'exclude_patterns': None,
  411. },
  412. storage_config={},
  413. )
  414. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  415. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  416. flexmock(module).should_receive('_expand_home_directories').and_return(())
  417. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  418. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  419. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  420. flexmock(module).should_receive('execute_command').with_args(
  421. CREATE_COMMAND + ('--numeric-owner',), capture_output=False, output_as_warning=False
  422. )
  423. module.create_archive(
  424. dry_run=False,
  425. repository='repo',
  426. location_config={
  427. 'source_directories': ['foo', 'bar'],
  428. 'repositories': ['repo'],
  429. 'numeric_owner': True,
  430. 'exclude_patterns': None,
  431. },
  432. storage_config={},
  433. )
  434. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  435. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  436. flexmock(module).should_receive('_expand_home_directories').and_return(())
  437. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  438. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  439. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  440. flexmock(module).should_receive('execute_command').with_args(
  441. CREATE_COMMAND + ('--read-special',), capture_output=False, output_as_warning=False
  442. )
  443. module.create_archive(
  444. dry_run=False,
  445. repository='repo',
  446. location_config={
  447. 'source_directories': ['foo', 'bar'],
  448. 'repositories': ['repo'],
  449. 'read_special': True,
  450. 'exclude_patterns': None,
  451. },
  452. storage_config={},
  453. )
  454. def test_create_archive_with_bsd_flags_true_calls_borg_without_nobsdflags_parameter():
  455. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  456. flexmock(module).should_receive('_expand_home_directories').and_return(())
  457. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  458. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  459. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  460. flexmock(module).should_receive('execute_command').with_args(
  461. CREATE_COMMAND, capture_output=False, output_as_warning=False
  462. )
  463. module.create_archive(
  464. dry_run=False,
  465. repository='repo',
  466. location_config={
  467. 'source_directories': ['foo', 'bar'],
  468. 'repositories': ['repo'],
  469. 'bsd_flags': True,
  470. 'exclude_patterns': None,
  471. },
  472. storage_config={},
  473. )
  474. def test_create_archive_with_bsd_flags_false_calls_borg_with_nobsdflags_parameter():
  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. CREATE_COMMAND + ('--nobsdflags',), capture_output=False, output_as_warning=False
  482. )
  483. module.create_archive(
  484. dry_run=False,
  485. repository='repo',
  486. location_config={
  487. 'source_directories': ['foo', 'bar'],
  488. 'repositories': ['repo'],
  489. 'bsd_flags': False,
  490. 'exclude_patterns': None,
  491. },
  492. storage_config={},
  493. )
  494. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  495. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  496. flexmock(module).should_receive('_expand_home_directories').and_return(())
  497. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  498. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  499. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  500. flexmock(module).should_receive('execute_command').with_args(
  501. CREATE_COMMAND + ('--files-cache', 'ctime,size'),
  502. capture_output=False,
  503. output_as_warning=False,
  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_COMMAND[1:], capture_output=False, output_as_warning=False
  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. CREATE_COMMAND + ('--remote-path', 'borg1'), capture_output=False, output_as_warning=False
  544. )
  545. module.create_archive(
  546. dry_run=False,
  547. repository='repo',
  548. location_config={
  549. 'source_directories': ['foo', 'bar'],
  550. 'repositories': ['repo'],
  551. 'exclude_patterns': None,
  552. },
  553. storage_config={},
  554. remote_path='borg1',
  555. )
  556. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  557. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  558. flexmock(module).should_receive('_expand_home_directories').and_return(())
  559. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  560. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  561. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  562. flexmock(module).should_receive('execute_command').with_args(
  563. CREATE_COMMAND + ('--umask', '740'), capture_output=False, output_as_warning=False
  564. )
  565. module.create_archive(
  566. dry_run=False,
  567. repository='repo',
  568. location_config={
  569. 'source_directories': ['foo', 'bar'],
  570. 'repositories': ['repo'],
  571. 'exclude_patterns': None,
  572. },
  573. storage_config={'umask': 740},
  574. )
  575. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  576. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  577. flexmock(module).should_receive('_expand_home_directories').and_return(())
  578. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  579. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  580. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  581. flexmock(module).should_receive('execute_command').with_args(
  582. CREATE_COMMAND + ('--lock-wait', '5'), capture_output=False, output_as_warning=False
  583. )
  584. module.create_archive(
  585. dry_run=False,
  586. repository='repo',
  587. location_config={
  588. 'source_directories': ['foo', 'bar'],
  589. 'repositories': ['repo'],
  590. 'exclude_patterns': None,
  591. },
  592. storage_config={'lock_wait': 5},
  593. )
  594. def test_create_archive_with_stats_calls_borg_with_stats_parameter():
  595. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  596. flexmock(module).should_receive('_expand_home_directories').and_return(())
  597. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  598. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  599. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  600. flexmock(module).should_receive('execute_command').with_args(
  601. CREATE_COMMAND + ('--stats',), capture_output=False, output_as_warning=True
  602. )
  603. module.create_archive(
  604. dry_run=False,
  605. repository='repo',
  606. location_config={
  607. 'source_directories': ['foo', 'bar'],
  608. 'repositories': ['repo'],
  609. 'exclude_patterns': None,
  610. },
  611. storage_config={},
  612. stats=True,
  613. )
  614. def test_create_archive_with_json_calls_borg_with_json_parameter():
  615. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  616. flexmock(module).should_receive('_expand_home_directories').and_return(())
  617. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  618. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  619. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  620. flexmock(module).should_receive('execute_command').with_args(
  621. CREATE_COMMAND + ('--json',), capture_output=True, output_as_warning=False
  622. ).and_return('[]')
  623. json_output = module.create_archive(
  624. dry_run=False,
  625. repository='repo',
  626. location_config={
  627. 'source_directories': ['foo', 'bar'],
  628. 'repositories': ['repo'],
  629. 'exclude_patterns': None,
  630. },
  631. storage_config={},
  632. json=True,
  633. )
  634. assert json_output == '[]'
  635. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  636. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  637. flexmock(module).should_receive('_expand_home_directories').and_return(())
  638. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  639. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  640. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  641. flexmock(module).should_receive('execute_command').with_args(
  642. CREATE_COMMAND + ('--json',), capture_output=True, output_as_warning=False
  643. ).and_return('[]')
  644. json_output = module.create_archive(
  645. dry_run=False,
  646. repository='repo',
  647. location_config={
  648. 'source_directories': ['foo', 'bar'],
  649. 'repositories': ['repo'],
  650. 'exclude_patterns': None,
  651. },
  652. storage_config={},
  653. json=True,
  654. stats=True,
  655. )
  656. assert json_output == '[]'
  657. def test_create_archive_with_source_directories_glob_expands():
  658. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  659. flexmock(module).should_receive('_expand_home_directories').and_return(())
  660. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  661. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  662. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  663. flexmock(module).should_receive('execute_command').with_args(
  664. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  665. capture_output=False,
  666. output_as_warning=False,
  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. capture_output=False,
  688. output_as_warning=False,
  689. )
  690. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  691. module.create_archive(
  692. dry_run=False,
  693. repository='repo',
  694. location_config={
  695. 'source_directories': ['foo*'],
  696. 'repositories': ['repo'],
  697. 'exclude_patterns': None,
  698. },
  699. storage_config={},
  700. )
  701. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  702. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  703. flexmock(module).should_receive('_expand_home_directories').and_return(())
  704. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  705. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  706. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  707. flexmock(module).should_receive('execute_command').with_args(
  708. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  709. capture_output=False,
  710. output_as_warning=False,
  711. )
  712. module.create_archive(
  713. dry_run=False,
  714. repository='repo',
  715. location_config={
  716. 'source_directories': ['foo*'],
  717. 'repositories': ['repo'],
  718. 'exclude_patterns': None,
  719. },
  720. storage_config={},
  721. )
  722. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  723. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  724. flexmock(module).should_receive('_expand_home_directories').and_return(())
  725. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  726. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  727. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  728. flexmock(module).should_receive('execute_command').with_args(
  729. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  730. capture_output=False,
  731. output_as_warning=False,
  732. )
  733. module.create_archive(
  734. dry_run=False,
  735. repository='repo',
  736. location_config={
  737. 'source_directories': ['foo', 'bar'],
  738. 'repositories': ['repo'],
  739. 'exclude_patterns': None,
  740. },
  741. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  742. )
  743. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  744. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  745. flexmock(module).should_receive('_expand_home_directories').and_return(())
  746. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  747. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  748. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  749. flexmock(module).should_receive('execute_command').with_args(
  750. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  751. capture_output=False,
  752. output_as_warning=False,
  753. )
  754. module.create_archive(
  755. dry_run=False,
  756. repository='repo',
  757. location_config={
  758. 'source_directories': ['foo', 'bar'],
  759. 'repositories': ['repo'],
  760. 'exclude_patterns': None,
  761. },
  762. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  763. )