test_create.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. def test_borgmatic_source_directories_set_when_directory_exists():
  91. flexmock(module.os.path).should_receive('exists').and_return(True)
  92. flexmock(module.os.path).should_receive('expanduser')
  93. assert module.borgmatic_source_directories() == [module.BORGMATIC_SOURCE_DIRECTORY]
  94. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  95. flexmock(module.os.path).should_receive('exists').and_return(False)
  96. flexmock(module.os.path).should_receive('expanduser')
  97. assert module.borgmatic_source_directories() == []
  98. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  99. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  100. def test_create_archive_calls_borg_with_parameters():
  101. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  102. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  103. flexmock(module).should_receive('_expand_home_directories').and_return(())
  104. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  105. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  106. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  107. flexmock(module).should_receive('execute_command').with_args(
  108. ('borg', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  109. )
  110. module.create_archive(
  111. dry_run=False,
  112. repository='repo',
  113. location_config={
  114. 'source_directories': ['foo', 'bar'],
  115. 'repositories': ['repo'],
  116. 'exclude_patterns': None,
  117. },
  118. storage_config={},
  119. )
  120. def test_create_archive_with_patterns_calls_borg_with_patterns():
  121. pattern_flags = ('--patterns-from', 'patterns')
  122. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  123. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  124. flexmock(module).should_receive('_expand_home_directories').and_return(())
  125. flexmock(module).should_receive('_write_pattern_file').and_return(
  126. flexmock(name='/tmp/patterns')
  127. ).and_return(None)
  128. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  129. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  130. flexmock(module).should_receive('execute_command').with_args(
  131. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  132. )
  133. module.create_archive(
  134. dry_run=False,
  135. repository='repo',
  136. location_config={
  137. 'source_directories': ['foo', 'bar'],
  138. 'repositories': ['repo'],
  139. 'patterns': ['pattern'],
  140. },
  141. storage_config={},
  142. )
  143. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  144. exclude_flags = ('--exclude-from', 'excludes')
  145. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  146. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  147. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  148. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  149. flexmock(name='/tmp/excludes')
  150. )
  151. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  152. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  153. flexmock(module).should_receive('execute_command').with_args(
  154. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  155. )
  156. module.create_archive(
  157. dry_run=False,
  158. repository='repo',
  159. location_config={
  160. 'source_directories': ['foo', 'bar'],
  161. 'repositories': ['repo'],
  162. 'exclude_patterns': ['exclude'],
  163. },
  164. storage_config={},
  165. )
  166. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  167. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  168. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  169. flexmock(module).should_receive('_expand_home_directories').and_return(())
  170. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  171. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  172. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  173. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  174. flexmock(module).should_receive('execute_command').with_args(
  175. ('borg', 'create', '--list', '--filter', 'AME-', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  176. output_log_level=logging.INFO,
  177. )
  178. insert_logging_mock(logging.INFO)
  179. module.create_archive(
  180. dry_run=False,
  181. repository='repo',
  182. location_config={
  183. 'source_directories': ['foo', 'bar'],
  184. 'repositories': ['repo'],
  185. 'exclude_patterns': None,
  186. },
  187. storage_config={},
  188. )
  189. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  190. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  191. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  192. flexmock(module).should_receive('_expand_home_directories').and_return(())
  193. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  194. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  195. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  196. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  197. flexmock(module).should_receive('execute_command').with_args(
  198. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  199. )
  200. insert_logging_mock(logging.INFO)
  201. module.create_archive(
  202. dry_run=False,
  203. repository='repo',
  204. location_config={
  205. 'source_directories': ['foo', 'bar'],
  206. 'repositories': ['repo'],
  207. 'exclude_patterns': None,
  208. },
  209. storage_config={},
  210. json=True,
  211. )
  212. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  213. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  214. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  215. flexmock(module).should_receive('_expand_home_directories').and_return(())
  216. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  217. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  218. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  219. flexmock(module).should_receive('execute_command').with_args(
  220. ('borg', 'create', '--list', '--filter', 'AME-', '--stats', '--debug', '--show-rc')
  221. + ARCHIVE_WITH_PATHS,
  222. output_log_level=logging.INFO,
  223. )
  224. insert_logging_mock(logging.DEBUG)
  225. module.create_archive(
  226. dry_run=False,
  227. repository='repo',
  228. location_config={
  229. 'source_directories': ['foo', 'bar'],
  230. 'repositories': ['repo'],
  231. 'exclude_patterns': None,
  232. },
  233. storage_config={},
  234. )
  235. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  236. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  237. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  238. flexmock(module).should_receive('_expand_home_directories').and_return(())
  239. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  240. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  241. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  242. flexmock(module).should_receive('execute_command').with_args(
  243. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  244. )
  245. insert_logging_mock(logging.DEBUG)
  246. module.create_archive(
  247. dry_run=False,
  248. repository='repo',
  249. location_config={
  250. 'source_directories': ['foo', 'bar'],
  251. 'repositories': ['repo'],
  252. 'exclude_patterns': None,
  253. },
  254. storage_config={},
  255. json=True,
  256. )
  257. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  258. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  259. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  260. flexmock(module).should_receive('_expand_home_directories').and_return(())
  261. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  262. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  263. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  264. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  265. flexmock(module).should_receive('execute_command').with_args(
  266. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  267. )
  268. module.create_archive(
  269. dry_run=True,
  270. repository='repo',
  271. location_config={
  272. 'source_directories': ['foo', 'bar'],
  273. 'repositories': ['repo'],
  274. 'exclude_patterns': None,
  275. },
  276. storage_config={},
  277. )
  278. def test_create_archive_with_dry_run_and_log_info_calls_borg_without_stats_parameter():
  279. # --dry-run and --stats are mutually exclusive, see:
  280. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  281. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  282. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  283. flexmock(module).should_receive('_expand_home_directories').and_return(())
  284. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  285. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  286. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  287. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  288. flexmock(module).should_receive('execute_command').with_args(
  289. ('borg', 'create', '--list', '--filter', 'AME-', '--info', '--dry-run')
  290. + ARCHIVE_WITH_PATHS,
  291. output_log_level=logging.INFO,
  292. )
  293. insert_logging_mock(logging.INFO)
  294. module.create_archive(
  295. dry_run=True,
  296. repository='repo',
  297. location_config={
  298. 'source_directories': ['foo', 'bar'],
  299. 'repositories': ['repo'],
  300. 'exclude_patterns': None,
  301. },
  302. storage_config={},
  303. )
  304. def test_create_archive_with_dry_run_and_log_debug_calls_borg_without_stats_parameter():
  305. # --dry-run and --stats are mutually exclusive, see:
  306. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  307. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  308. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  309. flexmock(module).should_receive('_expand_home_directories').and_return(())
  310. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  311. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  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. ('borg', 'create', '--list', '--filter', 'AME-', '--debug', '--show-rc', '--dry-run')
  316. + ARCHIVE_WITH_PATHS,
  317. output_log_level=logging.INFO,
  318. )
  319. insert_logging_mock(logging.DEBUG)
  320. module.create_archive(
  321. dry_run=True,
  322. repository='repo',
  323. location_config={
  324. 'source_directories': ['foo', 'bar'],
  325. 'repositories': ['repo'],
  326. 'exclude_patterns': None,
  327. },
  328. storage_config={},
  329. )
  330. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  331. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  332. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  333. flexmock(module).should_receive('_expand_home_directories').and_return(())
  334. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  335. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  336. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  337. flexmock(module).should_receive('execute_command').with_args(
  338. ('borg', 'create', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  339. output_log_level=logging.INFO,
  340. )
  341. module.create_archive(
  342. dry_run=False,
  343. repository='repo',
  344. location_config={
  345. 'source_directories': ['foo', 'bar'],
  346. 'repositories': ['repo'],
  347. 'exclude_patterns': None,
  348. },
  349. storage_config={'checkpoint_interval': 600},
  350. )
  351. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  352. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  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', '--chunker-params', '1,2,3,4') + 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={'chunker_params': '1,2,3,4'},
  371. )
  372. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  373. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  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. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  381. output_log_level=logging.INFO,
  382. )
  383. module.create_archive(
  384. dry_run=False,
  385. repository='repo',
  386. location_config={
  387. 'source_directories': ['foo', 'bar'],
  388. 'repositories': ['repo'],
  389. 'exclude_patterns': None,
  390. },
  391. storage_config={'compression': 'rle'},
  392. )
  393. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  394. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  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. ('borg', 'create', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
  402. output_log_level=logging.INFO,
  403. )
  404. module.create_archive(
  405. dry_run=False,
  406. repository='repo',
  407. location_config={
  408. 'source_directories': ['foo', 'bar'],
  409. 'repositories': ['repo'],
  410. 'exclude_patterns': None,
  411. },
  412. storage_config={'remote_rate_limit': 100},
  413. )
  414. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  415. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  416. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  417. flexmock(module).should_receive('_expand_home_directories').and_return(())
  418. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  419. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  420. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  421. flexmock(module).should_receive('execute_command').with_args(
  422. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  423. )
  424. module.create_archive(
  425. dry_run=False,
  426. repository='repo',
  427. location_config={
  428. 'source_directories': ['foo', 'bar'],
  429. 'repositories': ['repo'],
  430. 'one_file_system': True,
  431. 'exclude_patterns': None,
  432. },
  433. storage_config={},
  434. )
  435. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  436. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  437. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  438. flexmock(module).should_receive('_expand_home_directories').and_return(())
  439. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  440. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  441. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  442. flexmock(module).should_receive('execute_command').with_args(
  443. ('borg', 'create', '--numeric-owner') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  444. )
  445. module.create_archive(
  446. dry_run=False,
  447. repository='repo',
  448. location_config={
  449. 'source_directories': ['foo', 'bar'],
  450. 'repositories': ['repo'],
  451. 'numeric_owner': True,
  452. 'exclude_patterns': None,
  453. },
  454. storage_config={},
  455. )
  456. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  457. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  458. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  459. flexmock(module).should_receive('_expand_home_directories').and_return(())
  460. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  461. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  462. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  463. flexmock(module).should_receive('execute_command').with_args(
  464. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  465. )
  466. module.create_archive(
  467. dry_run=False,
  468. repository='repo',
  469. location_config={
  470. 'source_directories': ['foo', 'bar'],
  471. 'repositories': ['repo'],
  472. 'read_special': True,
  473. 'exclude_patterns': None,
  474. },
  475. storage_config={},
  476. )
  477. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  478. def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
  479. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  480. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  481. flexmock(module).should_receive('_expand_home_directories').and_return(())
  482. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  483. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  484. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  485. flexmock(module).should_receive('execute_command').with_args(
  486. ('borg', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  487. )
  488. module.create_archive(
  489. dry_run=False,
  490. repository='repo',
  491. location_config={
  492. 'source_directories': ['foo', 'bar'],
  493. 'repositories': ['repo'],
  494. option_name: True,
  495. 'exclude_patterns': None,
  496. },
  497. storage_config={},
  498. )
  499. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  500. def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
  501. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  502. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  503. flexmock(module).should_receive('_expand_home_directories').and_return(())
  504. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  505. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  506. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  507. flexmock(module).should_receive('execute_command').with_args(
  508. ('borg', 'create', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
  509. output_log_level=logging.INFO,
  510. )
  511. module.create_archive(
  512. dry_run=False,
  513. repository='repo',
  514. location_config={
  515. 'source_directories': ['foo', 'bar'],
  516. 'repositories': ['repo'],
  517. option_name: False,
  518. 'exclude_patterns': None,
  519. },
  520. storage_config={},
  521. )
  522. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  523. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  524. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  525. flexmock(module).should_receive('_expand_home_directories').and_return(())
  526. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  527. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  528. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  529. flexmock(module).should_receive('execute_command').with_args(
  530. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  531. 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. 'files_cache': 'ctime,size',
  540. 'exclude_patterns': None,
  541. },
  542. storage_config={},
  543. )
  544. def test_create_archive_with_local_path_calls_borg_via_local_path():
  545. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  546. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  547. flexmock(module).should_receive('_expand_home_directories').and_return(())
  548. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  549. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  550. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  551. flexmock(module).should_receive('execute_command').with_args(
  552. ('borg1', 'create') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  553. )
  554. module.create_archive(
  555. dry_run=False,
  556. repository='repo',
  557. location_config={
  558. 'source_directories': ['foo', 'bar'],
  559. 'repositories': ['repo'],
  560. 'exclude_patterns': None,
  561. },
  562. storage_config={},
  563. local_path='borg1',
  564. )
  565. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  566. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  567. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  568. flexmock(module).should_receive('_expand_home_directories').and_return(())
  569. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  570. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  571. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  572. flexmock(module).should_receive('execute_command').with_args(
  573. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  574. output_log_level=logging.INFO,
  575. )
  576. module.create_archive(
  577. dry_run=False,
  578. repository='repo',
  579. location_config={
  580. 'source_directories': ['foo', 'bar'],
  581. 'repositories': ['repo'],
  582. 'exclude_patterns': None,
  583. },
  584. storage_config={},
  585. remote_path='borg1',
  586. )
  587. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  588. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  589. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  590. flexmock(module).should_receive('_expand_home_directories').and_return(())
  591. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  592. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  593. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  594. flexmock(module).should_receive('execute_command').with_args(
  595. ('borg', 'create', '--umask', '740') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  596. )
  597. module.create_archive(
  598. dry_run=False,
  599. repository='repo',
  600. location_config={
  601. 'source_directories': ['foo', 'bar'],
  602. 'repositories': ['repo'],
  603. 'exclude_patterns': None,
  604. },
  605. storage_config={'umask': 740},
  606. )
  607. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  608. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  609. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  610. flexmock(module).should_receive('_expand_home_directories').and_return(())
  611. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  612. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  613. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  614. flexmock(module).should_receive('execute_command').with_args(
  615. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS, output_log_level=logging.INFO
  616. )
  617. module.create_archive(
  618. dry_run=False,
  619. repository='repo',
  620. location_config={
  621. 'source_directories': ['foo', 'bar'],
  622. 'repositories': ['repo'],
  623. 'exclude_patterns': None,
  624. },
  625. storage_config={'lock_wait': 5},
  626. )
  627. def test_create_archive_with_stats_calls_borg_with_stats_parameter():
  628. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  629. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  630. flexmock(module).should_receive('_expand_home_directories').and_return(())
  631. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  632. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  633. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  634. flexmock(module).should_receive('execute_command').with_args(
  635. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS, output_log_level=logging.WARNING
  636. )
  637. module.create_archive(
  638. dry_run=False,
  639. repository='repo',
  640. location_config={
  641. 'source_directories': ['foo', 'bar'],
  642. 'repositories': ['repo'],
  643. 'exclude_patterns': None,
  644. },
  645. storage_config={},
  646. stats=True,
  647. )
  648. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  649. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  650. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  651. flexmock(module).should_receive('_expand_home_directories').and_return(())
  652. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  653. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  654. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  655. flexmock(module).should_receive('execute_command_without_capture').with_args(
  656. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS
  657. )
  658. module.create_archive(
  659. dry_run=False,
  660. repository='repo',
  661. location_config={
  662. 'source_directories': ['foo', 'bar'],
  663. 'repositories': ['repo'],
  664. 'exclude_patterns': None,
  665. },
  666. storage_config={},
  667. progress=True,
  668. )
  669. def test_create_archive_with_json_calls_borg_with_json_parameter():
  670. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  671. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  672. flexmock(module).should_receive('_expand_home_directories').and_return(())
  673. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  674. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  675. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  676. flexmock(module).should_receive('execute_command').with_args(
  677. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  678. ).and_return('[]')
  679. json_output = module.create_archive(
  680. dry_run=False,
  681. repository='repo',
  682. location_config={
  683. 'source_directories': ['foo', 'bar'],
  684. 'repositories': ['repo'],
  685. 'exclude_patterns': None,
  686. },
  687. storage_config={},
  688. json=True,
  689. )
  690. assert json_output == '[]'
  691. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  692. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  693. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  694. flexmock(module).should_receive('_expand_home_directories').and_return(())
  695. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  696. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  697. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  698. flexmock(module).should_receive('execute_command').with_args(
  699. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS, output_log_level=None
  700. ).and_return('[]')
  701. json_output = module.create_archive(
  702. dry_run=False,
  703. repository='repo',
  704. location_config={
  705. 'source_directories': ['foo', 'bar'],
  706. 'repositories': ['repo'],
  707. 'exclude_patterns': None,
  708. },
  709. storage_config={},
  710. json=True,
  711. stats=True,
  712. )
  713. assert json_output == '[]'
  714. def test_create_archive_with_source_directories_glob_expands():
  715. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  716. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  717. flexmock(module).should_receive('_expand_home_directories').and_return(())
  718. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  719. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  720. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  721. flexmock(module).should_receive('execute_command').with_args(
  722. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  723. output_log_level=logging.INFO,
  724. )
  725. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  726. module.create_archive(
  727. dry_run=False,
  728. repository='repo',
  729. location_config={
  730. 'source_directories': ['foo*'],
  731. 'repositories': ['repo'],
  732. 'exclude_patterns': None,
  733. },
  734. storage_config={},
  735. )
  736. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  737. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  738. flexmock(module).should_receive('_expand_directories').and_return(('foo*',))
  739. flexmock(module).should_receive('_expand_home_directories').and_return(())
  740. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  741. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  742. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  743. flexmock(module).should_receive('execute_command').with_args(
  744. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  745. output_log_level=logging.INFO,
  746. )
  747. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  748. module.create_archive(
  749. dry_run=False,
  750. repository='repo',
  751. location_config={
  752. 'source_directories': ['foo*'],
  753. 'repositories': ['repo'],
  754. 'exclude_patterns': None,
  755. },
  756. storage_config={},
  757. )
  758. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  759. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  760. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'food'))
  761. flexmock(module).should_receive('_expand_home_directories').and_return(())
  762. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  763. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  764. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  765. flexmock(module).should_receive('execute_command').with_args(
  766. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  767. output_log_level=logging.INFO,
  768. )
  769. module.create_archive(
  770. dry_run=False,
  771. repository='repo',
  772. location_config={
  773. 'source_directories': ['foo*'],
  774. 'repositories': ['repo'],
  775. 'exclude_patterns': None,
  776. },
  777. storage_config={},
  778. )
  779. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  780. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  781. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  782. flexmock(module).should_receive('_expand_home_directories').and_return(())
  783. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  784. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  785. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  786. flexmock(module).should_receive('execute_command').with_args(
  787. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'), output_log_level=logging.INFO
  788. )
  789. module.create_archive(
  790. dry_run=False,
  791. repository='repo',
  792. location_config={
  793. 'source_directories': ['foo', 'bar'],
  794. 'repositories': ['repo'],
  795. 'exclude_patterns': None,
  796. },
  797. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  798. )
  799. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  800. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  801. flexmock(module).should_receive('_expand_directories').and_return(('foo', 'bar'))
  802. flexmock(module).should_receive('_expand_home_directories').and_return(())
  803. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  804. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  805. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  806. flexmock(module).should_receive('execute_command').with_args(
  807. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  808. output_log_level=logging.INFO,
  809. )
  810. module.create_archive(
  811. dry_run=False,
  812. repository='repo',
  813. location_config={
  814. 'source_directories': ['foo', 'bar'],
  815. 'repositories': ['repo'],
  816. 'exclude_patterns': None,
  817. },
  818. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  819. )