test_create.py 42 KB

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