2
0

test_create.py 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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. @pytest.mark.parametrize(
  37. 'directories,expected_directories',
  38. (
  39. ({'/': 1, '/root': 1}, ('/',)),
  40. ({'/': 1, '/root/': 1}, ('/',)),
  41. ({'/': 1, '/root': 2}, ('/', '/root')),
  42. ({'/root': 1, '/': 1}, ('/',)),
  43. ({'/root': 1, '/root/foo': 1}, ('/root',)),
  44. ({'/root/': 1, '/root/foo': 1}, ('/root/',)),
  45. ({'/root': 1, '/root/foo/': 1}, ('/root',)),
  46. ({'/root': 1, '/root/foo': 2}, ('/root', '/root/foo')),
  47. ({'/root/foo': 1, '/root': 1}, ('/root',)),
  48. ({'/root': 1, '/etc': 1, '/root/foo/bar': 1}, ('/etc', '/root')),
  49. ({'/root': 1, '/root/foo': 1, '/root/foo/bar': 1}, ('/root',)),
  50. ({'/dup': 1, '/dup': 1}, ('/dup',)),
  51. ({'/foo': 1, '/bar': 1}, ('/bar', '/foo')),
  52. ({'/foo': 1, '/bar': 2}, ('/bar', '/foo')),
  53. ),
  54. )
  55. def test_deduplicate_directories_removes_child_paths_on_the_same_filesystem(
  56. directories, expected_directories
  57. ):
  58. assert module.deduplicate_directories(directories) == expected_directories
  59. def test_write_pattern_file_does_not_raise():
  60. temporary_file = flexmock(name='filename', write=lambda mode: None, flush=lambda: None)
  61. flexmock(module.tempfile).should_receive('NamedTemporaryFile').and_return(temporary_file)
  62. module._write_pattern_file(['exclude'])
  63. def test_write_pattern_file_with_empty_exclude_patterns_does_not_raise():
  64. module._write_pattern_file([])
  65. def test_make_pattern_flags_includes_pattern_filename_when_given():
  66. pattern_flags = module._make_pattern_flags(
  67. location_config={'patterns': ['R /', '- /var']}, pattern_filename='/tmp/patterns'
  68. )
  69. assert pattern_flags == ('--patterns-from', '/tmp/patterns')
  70. def test_make_pattern_flags_includes_patterns_from_filenames_when_in_config():
  71. pattern_flags = module._make_pattern_flags(
  72. location_config={'patterns_from': ['patterns', 'other']}
  73. )
  74. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', 'other')
  75. def test_make_pattern_flags_includes_both_filenames_when_patterns_given_and_patterns_from_in_config():
  76. pattern_flags = module._make_pattern_flags(
  77. location_config={'patterns_from': ['patterns']}, pattern_filename='/tmp/patterns'
  78. )
  79. assert pattern_flags == ('--patterns-from', 'patterns', '--patterns-from', '/tmp/patterns')
  80. def test_make_pattern_flags_considers_none_patterns_from_filenames_as_empty():
  81. pattern_flags = module._make_pattern_flags(location_config={'patterns_from': None})
  82. assert pattern_flags == ()
  83. def test_make_exclude_flags_includes_exclude_patterns_filename_when_given():
  84. exclude_flags = module._make_exclude_flags(
  85. location_config={'exclude_patterns': ['*.pyc', '/var']}, exclude_filename='/tmp/excludes'
  86. )
  87. assert exclude_flags == ('--exclude-from', '/tmp/excludes')
  88. def test_make_exclude_flags_includes_exclude_from_filenames_when_in_config():
  89. exclude_flags = module._make_exclude_flags(
  90. location_config={'exclude_from': ['excludes', 'other']}
  91. )
  92. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', 'other')
  93. def test_make_exclude_flags_includes_both_filenames_when_patterns_given_and_exclude_from_in_config():
  94. exclude_flags = module._make_exclude_flags(
  95. location_config={'exclude_from': ['excludes']}, exclude_filename='/tmp/excludes'
  96. )
  97. assert exclude_flags == ('--exclude-from', 'excludes', '--exclude-from', '/tmp/excludes')
  98. def test_make_exclude_flags_considers_none_exclude_from_filenames_as_empty():
  99. exclude_flags = module._make_exclude_flags(location_config={'exclude_from': None})
  100. assert exclude_flags == ()
  101. def test_make_exclude_flags_includes_exclude_caches_when_true_in_config():
  102. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': True})
  103. assert exclude_flags == ('--exclude-caches',)
  104. def test_make_exclude_flags_does_not_include_exclude_caches_when_false_in_config():
  105. exclude_flags = module._make_exclude_flags(location_config={'exclude_caches': False})
  106. assert exclude_flags == ()
  107. def test_make_exclude_flags_includes_exclude_if_present_when_in_config():
  108. exclude_flags = module._make_exclude_flags(
  109. location_config={'exclude_if_present': ['exclude_me', 'also_me']}
  110. )
  111. assert exclude_flags == (
  112. '--exclude-if-present',
  113. 'exclude_me',
  114. '--exclude-if-present',
  115. 'also_me',
  116. )
  117. def test_make_exclude_flags_includes_keep_exclude_tags_when_true_in_config():
  118. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': True})
  119. assert exclude_flags == ('--keep-exclude-tags',)
  120. def test_make_exclude_flags_does_not_include_keep_exclude_tags_when_false_in_config():
  121. exclude_flags = module._make_exclude_flags(location_config={'keep_exclude_tags': False})
  122. assert exclude_flags == ()
  123. def test_make_exclude_flags_includes_exclude_nodump_when_true_in_config():
  124. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': True})
  125. assert exclude_flags == ('--exclude-nodump',)
  126. def test_make_exclude_flags_does_not_include_exclude_nodump_when_false_in_config():
  127. exclude_flags = module._make_exclude_flags(location_config={'exclude_nodump': False})
  128. assert exclude_flags == ()
  129. def test_make_exclude_flags_is_empty_when_config_has_no_excludes():
  130. exclude_flags = module._make_exclude_flags(location_config={})
  131. assert exclude_flags == ()
  132. def test_borgmatic_source_directories_set_when_directory_exists():
  133. flexmock(module.os.path).should_receive('exists').and_return(True)
  134. flexmock(module.os.path).should_receive('expanduser')
  135. assert module.borgmatic_source_directories('/tmp') == ['/tmp']
  136. def test_borgmatic_source_directories_empty_when_directory_does_not_exist():
  137. flexmock(module.os.path).should_receive('exists').and_return(False)
  138. flexmock(module.os.path).should_receive('expanduser')
  139. assert module.borgmatic_source_directories('/tmp') == []
  140. def test_borgmatic_source_directories_defaults_when_directory_not_given():
  141. flexmock(module.os.path).should_receive('exists').and_return(True)
  142. flexmock(module.os.path).should_receive('expanduser')
  143. assert module.borgmatic_source_directories(None) == [module.DEFAULT_BORGMATIC_SOURCE_DIRECTORY]
  144. DEFAULT_ARCHIVE_NAME = '{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f}'
  145. ARCHIVE_WITH_PATHS = ('repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'bar')
  146. def test_create_archive_calls_borg_with_parameters():
  147. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  148. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  149. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  150. flexmock(module).should_receive('_expand_directories').and_return(())
  151. flexmock(module).should_receive('_expand_home_directories').and_return(())
  152. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  153. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  154. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  155. flexmock(module).should_receive('execute_command').with_args(
  156. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  157. output_log_level=logging.INFO,
  158. output_file=None,
  159. borg_local_path='borg',
  160. )
  161. module.create_archive(
  162. dry_run=False,
  163. repository='repo',
  164. location_config={
  165. 'source_directories': ['foo', 'bar'],
  166. 'repositories': ['repo'],
  167. 'exclude_patterns': None,
  168. },
  169. storage_config={},
  170. )
  171. def test_create_archive_with_patterns_calls_borg_with_patterns():
  172. pattern_flags = ('--patterns-from', 'patterns')
  173. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  174. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  175. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  176. flexmock(module).should_receive('_expand_directories').and_return(())
  177. flexmock(module).should_receive('_expand_home_directories').and_return(())
  178. flexmock(module).should_receive('_write_pattern_file').and_return(
  179. flexmock(name='/tmp/patterns')
  180. ).and_return(None)
  181. flexmock(module).should_receive('_make_pattern_flags').and_return(pattern_flags)
  182. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  183. flexmock(module).should_receive('execute_command').with_args(
  184. ('borg', 'create') + pattern_flags + ARCHIVE_WITH_PATHS,
  185. output_log_level=logging.INFO,
  186. output_file=None,
  187. borg_local_path='borg',
  188. )
  189. module.create_archive(
  190. dry_run=False,
  191. repository='repo',
  192. location_config={
  193. 'source_directories': ['foo', 'bar'],
  194. 'repositories': ['repo'],
  195. 'patterns': ['pattern'],
  196. },
  197. storage_config={},
  198. )
  199. def test_create_archive_with_exclude_patterns_calls_borg_with_excludes():
  200. exclude_flags = ('--exclude-from', 'excludes')
  201. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  202. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  203. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  204. flexmock(module).should_receive('_expand_directories').and_return(())
  205. flexmock(module).should_receive('_expand_home_directories').and_return(('exclude',))
  206. flexmock(module).should_receive('_write_pattern_file').and_return(None).and_return(
  207. flexmock(name='/tmp/excludes')
  208. )
  209. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  210. flexmock(module).should_receive('_make_exclude_flags').and_return(exclude_flags)
  211. flexmock(module).should_receive('execute_command').with_args(
  212. ('borg', 'create') + exclude_flags + ARCHIVE_WITH_PATHS,
  213. output_log_level=logging.INFO,
  214. output_file=None,
  215. borg_local_path='borg',
  216. )
  217. module.create_archive(
  218. dry_run=False,
  219. repository='repo',
  220. location_config={
  221. 'source_directories': ['foo', 'bar'],
  222. 'repositories': ['repo'],
  223. 'exclude_patterns': ['exclude'],
  224. },
  225. storage_config={},
  226. )
  227. def test_create_archive_with_log_info_calls_borg_with_info_parameter():
  228. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  229. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  230. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  231. flexmock(module).should_receive('_expand_directories').and_return(())
  232. flexmock(module).should_receive('_expand_home_directories').and_return(())
  233. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  234. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  235. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  236. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  237. flexmock(module).should_receive('execute_command').with_args(
  238. ('borg', 'create', '--info') + ARCHIVE_WITH_PATHS,
  239. output_log_level=logging.INFO,
  240. output_file=None,
  241. borg_local_path='borg',
  242. )
  243. insert_logging_mock(logging.INFO)
  244. module.create_archive(
  245. dry_run=False,
  246. repository='repo',
  247. location_config={
  248. 'source_directories': ['foo', 'bar'],
  249. 'repositories': ['repo'],
  250. 'exclude_patterns': None,
  251. },
  252. storage_config={},
  253. )
  254. def test_create_archive_with_log_info_and_json_suppresses_most_borg_output():
  255. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  256. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  257. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  258. flexmock(module).should_receive('_expand_directories').and_return(())
  259. flexmock(module).should_receive('_expand_home_directories').and_return(())
  260. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  261. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  262. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  263. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  264. flexmock(module).should_receive('execute_command').with_args(
  265. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  266. output_log_level=None,
  267. output_file=None,
  268. borg_local_path='borg',
  269. )
  270. insert_logging_mock(logging.INFO)
  271. module.create_archive(
  272. dry_run=False,
  273. repository='repo',
  274. location_config={
  275. 'source_directories': ['foo', 'bar'],
  276. 'repositories': ['repo'],
  277. 'exclude_patterns': None,
  278. },
  279. storage_config={},
  280. json=True,
  281. )
  282. def test_create_archive_with_log_debug_calls_borg_with_debug_parameter():
  283. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  284. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  285. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  286. flexmock(module).should_receive('_expand_directories').and_return(())
  287. flexmock(module).should_receive('_expand_home_directories').and_return(())
  288. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  289. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  290. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  291. flexmock(module).should_receive('execute_command').with_args(
  292. ('borg', 'create', '--debug', '--show-rc') + ARCHIVE_WITH_PATHS,
  293. output_log_level=logging.INFO,
  294. output_file=None,
  295. borg_local_path='borg',
  296. )
  297. insert_logging_mock(logging.DEBUG)
  298. module.create_archive(
  299. dry_run=False,
  300. repository='repo',
  301. location_config={
  302. 'source_directories': ['foo', 'bar'],
  303. 'repositories': ['repo'],
  304. 'exclude_patterns': None,
  305. },
  306. storage_config={},
  307. )
  308. def test_create_archive_with_log_debug_and_json_suppresses_most_borg_output():
  309. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  310. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  311. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  312. flexmock(module).should_receive('_expand_directories').and_return(())
  313. flexmock(module).should_receive('_expand_home_directories').and_return(())
  314. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  315. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  316. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  317. flexmock(module).should_receive('execute_command').with_args(
  318. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  319. output_log_level=None,
  320. output_file=None,
  321. borg_local_path='borg',
  322. )
  323. insert_logging_mock(logging.DEBUG)
  324. module.create_archive(
  325. dry_run=False,
  326. repository='repo',
  327. location_config={
  328. 'source_directories': ['foo', 'bar'],
  329. 'repositories': ['repo'],
  330. 'exclude_patterns': None,
  331. },
  332. storage_config={},
  333. json=True,
  334. )
  335. def test_create_archive_with_dry_run_calls_borg_with_dry_run_parameter():
  336. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  337. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  338. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  339. flexmock(module).should_receive('_expand_directories').and_return(())
  340. flexmock(module).should_receive('_expand_home_directories').and_return(())
  341. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  342. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  343. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  344. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  345. flexmock(module).should_receive('execute_command').with_args(
  346. ('borg', 'create', '--dry-run') + ARCHIVE_WITH_PATHS,
  347. output_log_level=logging.INFO,
  348. output_file=None,
  349. borg_local_path='borg',
  350. )
  351. module.create_archive(
  352. dry_run=True,
  353. repository='repo',
  354. location_config={
  355. 'source_directories': ['foo', 'bar'],
  356. 'repositories': ['repo'],
  357. 'exclude_patterns': None,
  358. },
  359. storage_config={},
  360. )
  361. def test_create_archive_with_stats_and_dry_run_calls_borg_without_stats_parameter():
  362. # --dry-run and --stats are mutually exclusive, see:
  363. # https://borgbackup.readthedocs.io/en/stable/usage/create.html#description
  364. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  365. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  366. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  367. flexmock(module).should_receive('_expand_directories').and_return(())
  368. flexmock(module).should_receive('_expand_home_directories').and_return(())
  369. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  370. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  371. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  372. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  373. flexmock(module).should_receive('execute_command').with_args(
  374. ('borg', 'create', '--info', '--dry-run') + ARCHIVE_WITH_PATHS,
  375. output_log_level=logging.INFO,
  376. output_file=None,
  377. borg_local_path='borg',
  378. )
  379. insert_logging_mock(logging.INFO)
  380. module.create_archive(
  381. dry_run=True,
  382. repository='repo',
  383. location_config={
  384. 'source_directories': ['foo', 'bar'],
  385. 'repositories': ['repo'],
  386. 'exclude_patterns': None,
  387. },
  388. storage_config={},
  389. stats=True,
  390. )
  391. def test_create_archive_with_checkpoint_interval_calls_borg_with_checkpoint_interval_parameters():
  392. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  393. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  394. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  395. flexmock(module).should_receive('_expand_directories').and_return(())
  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', '--checkpoint-interval', '600') + ARCHIVE_WITH_PATHS,
  402. output_log_level=logging.INFO,
  403. output_file=None,
  404. borg_local_path='borg',
  405. )
  406. module.create_archive(
  407. dry_run=False,
  408. repository='repo',
  409. location_config={
  410. 'source_directories': ['foo', 'bar'],
  411. 'repositories': ['repo'],
  412. 'exclude_patterns': None,
  413. },
  414. storage_config={'checkpoint_interval': 600},
  415. )
  416. def test_create_archive_with_chunker_params_calls_borg_with_chunker_params_parameters():
  417. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  418. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  419. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  420. flexmock(module).should_receive('_expand_directories').and_return(())
  421. flexmock(module).should_receive('_expand_home_directories').and_return(())
  422. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  423. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  424. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  425. flexmock(module).should_receive('execute_command').with_args(
  426. ('borg', 'create', '--chunker-params', '1,2,3,4') + ARCHIVE_WITH_PATHS,
  427. output_log_level=logging.INFO,
  428. output_file=None,
  429. borg_local_path='borg',
  430. )
  431. module.create_archive(
  432. dry_run=False,
  433. repository='repo',
  434. location_config={
  435. 'source_directories': ['foo', 'bar'],
  436. 'repositories': ['repo'],
  437. 'exclude_patterns': None,
  438. },
  439. storage_config={'chunker_params': '1,2,3,4'},
  440. )
  441. def test_create_archive_with_compression_calls_borg_with_compression_parameters():
  442. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  443. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  444. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  445. flexmock(module).should_receive('_expand_directories').and_return(())
  446. flexmock(module).should_receive('_expand_home_directories').and_return(())
  447. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  448. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  449. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  450. flexmock(module).should_receive('execute_command').with_args(
  451. ('borg', 'create', '--compression', 'rle') + ARCHIVE_WITH_PATHS,
  452. output_log_level=logging.INFO,
  453. output_file=None,
  454. borg_local_path='borg',
  455. )
  456. module.create_archive(
  457. dry_run=False,
  458. repository='repo',
  459. location_config={
  460. 'source_directories': ['foo', 'bar'],
  461. 'repositories': ['repo'],
  462. 'exclude_patterns': None,
  463. },
  464. storage_config={'compression': 'rle'},
  465. )
  466. def test_create_archive_with_remote_rate_limit_calls_borg_with_remote_ratelimit_parameters():
  467. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  468. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  469. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  470. flexmock(module).should_receive('_expand_directories').and_return(())
  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', '--remote-ratelimit', '100') + ARCHIVE_WITH_PATHS,
  477. output_log_level=logging.INFO,
  478. output_file=None,
  479. borg_local_path='borg',
  480. )
  481. module.create_archive(
  482. dry_run=False,
  483. repository='repo',
  484. location_config={
  485. 'source_directories': ['foo', 'bar'],
  486. 'repositories': ['repo'],
  487. 'exclude_patterns': None,
  488. },
  489. storage_config={'remote_rate_limit': 100},
  490. )
  491. def test_create_archive_with_one_file_system_calls_borg_with_one_file_system_parameter():
  492. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  493. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  494. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  495. flexmock(module).should_receive('_expand_directories').and_return(())
  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. ('borg', 'create', '--one-file-system') + ARCHIVE_WITH_PATHS,
  502. output_log_level=logging.INFO,
  503. output_file=None,
  504. borg_local_path='borg',
  505. )
  506. module.create_archive(
  507. dry_run=False,
  508. repository='repo',
  509. location_config={
  510. 'source_directories': ['foo', 'bar'],
  511. 'repositories': ['repo'],
  512. 'one_file_system': True,
  513. 'exclude_patterns': None,
  514. },
  515. storage_config={},
  516. )
  517. def test_create_archive_with_numeric_owner_calls_borg_with_numeric_owner_parameter():
  518. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  519. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  520. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  521. flexmock(module).should_receive('_expand_directories').and_return(())
  522. flexmock(module).should_receive('_expand_home_directories').and_return(())
  523. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  524. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  525. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  526. flexmock(module).should_receive('execute_command').with_args(
  527. ('borg', 'create', '--numeric-owner') + ARCHIVE_WITH_PATHS,
  528. output_log_level=logging.INFO,
  529. output_file=None,
  530. borg_local_path='borg',
  531. )
  532. module.create_archive(
  533. dry_run=False,
  534. repository='repo',
  535. location_config={
  536. 'source_directories': ['foo', 'bar'],
  537. 'repositories': ['repo'],
  538. 'numeric_owner': True,
  539. 'exclude_patterns': None,
  540. },
  541. storage_config={},
  542. )
  543. def test_create_archive_with_read_special_calls_borg_with_read_special_parameter():
  544. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  545. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  546. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  547. flexmock(module).should_receive('_expand_directories').and_return(())
  548. flexmock(module).should_receive('_expand_home_directories').and_return(())
  549. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  550. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  551. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  552. flexmock(module).should_receive('execute_command').with_args(
  553. ('borg', 'create', '--read-special') + ARCHIVE_WITH_PATHS,
  554. output_log_level=logging.INFO,
  555. output_file=None,
  556. borg_local_path='borg',
  557. )
  558. module.create_archive(
  559. dry_run=False,
  560. repository='repo',
  561. location_config={
  562. 'source_directories': ['foo', 'bar'],
  563. 'repositories': ['repo'],
  564. 'read_special': True,
  565. 'exclude_patterns': None,
  566. },
  567. storage_config={},
  568. )
  569. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  570. def test_create_archive_with_option_true_calls_borg_without_corresponding_parameter(option_name):
  571. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  572. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  573. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  574. flexmock(module).should_receive('_expand_directories').and_return(())
  575. flexmock(module).should_receive('_expand_home_directories').and_return(())
  576. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  577. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  578. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  579. flexmock(module).should_receive('execute_command').with_args(
  580. ('borg', 'create') + ARCHIVE_WITH_PATHS,
  581. output_log_level=logging.INFO,
  582. output_file=None,
  583. borg_local_path='borg',
  584. )
  585. module.create_archive(
  586. dry_run=False,
  587. repository='repo',
  588. location_config={
  589. 'source_directories': ['foo', 'bar'],
  590. 'repositories': ['repo'],
  591. option_name: True,
  592. 'exclude_patterns': None,
  593. },
  594. storage_config={},
  595. )
  596. @pytest.mark.parametrize('option_name', ('atime', 'ctime', 'birthtime', 'bsd_flags'))
  597. def test_create_archive_with_option_false_calls_borg_with_corresponding_parameter(option_name):
  598. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  599. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  600. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  601. flexmock(module).should_receive('_expand_directories').and_return(())
  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', '--no' + option_name.replace('_', '')) + ARCHIVE_WITH_PATHS,
  608. output_log_level=logging.INFO,
  609. output_file=None,
  610. borg_local_path='borg',
  611. )
  612. module.create_archive(
  613. dry_run=False,
  614. repository='repo',
  615. location_config={
  616. 'source_directories': ['foo', 'bar'],
  617. 'repositories': ['repo'],
  618. option_name: False,
  619. 'exclude_patterns': None,
  620. },
  621. storage_config={},
  622. )
  623. def test_create_archive_with_files_cache_calls_borg_with_files_cache_parameters():
  624. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  625. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  626. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  627. flexmock(module).should_receive('_expand_directories').and_return(())
  628. flexmock(module).should_receive('_expand_home_directories').and_return(())
  629. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  630. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  631. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  632. flexmock(module).should_receive('execute_command').with_args(
  633. ('borg', 'create', '--files-cache', 'ctime,size') + ARCHIVE_WITH_PATHS,
  634. output_log_level=logging.INFO,
  635. output_file=None,
  636. borg_local_path='borg',
  637. )
  638. module.create_archive(
  639. dry_run=False,
  640. repository='repo',
  641. location_config={
  642. 'source_directories': ['foo', 'bar'],
  643. 'repositories': ['repo'],
  644. 'files_cache': 'ctime,size',
  645. 'exclude_patterns': None,
  646. },
  647. storage_config={},
  648. )
  649. def test_create_archive_with_local_path_calls_borg_via_local_path():
  650. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  651. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  652. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  653. flexmock(module).should_receive('_expand_directories').and_return(())
  654. flexmock(module).should_receive('_expand_home_directories').and_return(())
  655. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  656. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  657. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  658. flexmock(module).should_receive('execute_command').with_args(
  659. ('borg1', 'create') + ARCHIVE_WITH_PATHS,
  660. output_log_level=logging.INFO,
  661. output_file=None,
  662. borg_local_path='borg1',
  663. )
  664. module.create_archive(
  665. dry_run=False,
  666. repository='repo',
  667. location_config={
  668. 'source_directories': ['foo', 'bar'],
  669. 'repositories': ['repo'],
  670. 'exclude_patterns': None,
  671. },
  672. storage_config={},
  673. local_path='borg1',
  674. )
  675. def test_create_archive_with_remote_path_calls_borg_with_remote_path_parameters():
  676. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  677. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  678. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  679. flexmock(module).should_receive('_expand_directories').and_return(())
  680. flexmock(module).should_receive('_expand_home_directories').and_return(())
  681. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  682. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  683. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  684. flexmock(module).should_receive('execute_command').with_args(
  685. ('borg', 'create', '--remote-path', 'borg1') + ARCHIVE_WITH_PATHS,
  686. output_log_level=logging.INFO,
  687. output_file=None,
  688. borg_local_path='borg',
  689. )
  690. module.create_archive(
  691. dry_run=False,
  692. repository='repo',
  693. location_config={
  694. 'source_directories': ['foo', 'bar'],
  695. 'repositories': ['repo'],
  696. 'exclude_patterns': None,
  697. },
  698. storage_config={},
  699. remote_path='borg1',
  700. )
  701. def test_create_archive_with_umask_calls_borg_with_umask_parameters():
  702. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  703. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  704. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  705. flexmock(module).should_receive('_expand_directories').and_return(())
  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', '--umask', '740') + ARCHIVE_WITH_PATHS,
  712. output_log_level=logging.INFO,
  713. output_file=None,
  714. borg_local_path='borg',
  715. )
  716. module.create_archive(
  717. dry_run=False,
  718. repository='repo',
  719. location_config={
  720. 'source_directories': ['foo', 'bar'],
  721. 'repositories': ['repo'],
  722. 'exclude_patterns': None,
  723. },
  724. storage_config={'umask': 740},
  725. )
  726. def test_create_archive_with_lock_wait_calls_borg_with_lock_wait_parameters():
  727. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  728. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  729. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  730. flexmock(module).should_receive('_expand_directories').and_return(())
  731. flexmock(module).should_receive('_expand_home_directories').and_return(())
  732. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  733. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  734. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  735. flexmock(module).should_receive('execute_command').with_args(
  736. ('borg', 'create', '--lock-wait', '5') + ARCHIVE_WITH_PATHS,
  737. output_log_level=logging.INFO,
  738. output_file=None,
  739. borg_local_path='borg',
  740. )
  741. module.create_archive(
  742. dry_run=False,
  743. repository='repo',
  744. location_config={
  745. 'source_directories': ['foo', 'bar'],
  746. 'repositories': ['repo'],
  747. 'exclude_patterns': None,
  748. },
  749. storage_config={'lock_wait': 5},
  750. )
  751. def test_create_archive_with_stats_calls_borg_with_stats_parameter_and_warning_output_log_level():
  752. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  753. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  754. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  755. flexmock(module).should_receive('_expand_directories').and_return(())
  756. flexmock(module).should_receive('_expand_home_directories').and_return(())
  757. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  758. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  759. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  760. flexmock(module).should_receive('execute_command').with_args(
  761. ('borg', 'create', '--stats') + ARCHIVE_WITH_PATHS,
  762. output_log_level=logging.WARNING,
  763. output_file=None,
  764. borg_local_path='borg',
  765. )
  766. module.create_archive(
  767. dry_run=False,
  768. repository='repo',
  769. location_config={
  770. 'source_directories': ['foo', 'bar'],
  771. 'repositories': ['repo'],
  772. 'exclude_patterns': None,
  773. },
  774. storage_config={},
  775. stats=True,
  776. )
  777. def test_create_archive_with_stats_and_log_info_calls_borg_with_stats_parameter_and_info_output_log_level():
  778. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  779. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  780. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  781. flexmock(module).should_receive('_expand_directories').and_return(())
  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', '--info', '--stats') + ARCHIVE_WITH_PATHS,
  788. output_log_level=logging.INFO,
  789. output_file=None,
  790. borg_local_path='borg',
  791. )
  792. insert_logging_mock(logging.INFO)
  793. module.create_archive(
  794. dry_run=False,
  795. repository='repo',
  796. location_config={
  797. 'source_directories': ['foo', 'bar'],
  798. 'repositories': ['repo'],
  799. 'exclude_patterns': None,
  800. },
  801. storage_config={},
  802. stats=True,
  803. )
  804. def test_create_archive_with_files_calls_borg_with_list_parameter_and_warning_output_log_level():
  805. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  806. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  807. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  808. flexmock(module).should_receive('_expand_directories').and_return(())
  809. flexmock(module).should_receive('_expand_home_directories').and_return(())
  810. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  811. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  812. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  813. flexmock(module).should_receive('execute_command').with_args(
  814. ('borg', 'create', '--list', '--filter', 'AME-') + ARCHIVE_WITH_PATHS,
  815. output_log_level=logging.WARNING,
  816. output_file=None,
  817. borg_local_path='borg',
  818. )
  819. module.create_archive(
  820. dry_run=False,
  821. repository='repo',
  822. location_config={
  823. 'source_directories': ['foo', 'bar'],
  824. 'repositories': ['repo'],
  825. 'exclude_patterns': None,
  826. },
  827. storage_config={},
  828. files=True,
  829. )
  830. def test_create_archive_with_files_and_log_info_calls_borg_with_list_parameter_and_info_output_log_level():
  831. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  832. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  833. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  834. flexmock(module).should_receive('_expand_directories').and_return(())
  835. flexmock(module).should_receive('_expand_home_directories').and_return(())
  836. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  837. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  838. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  839. flexmock(module).should_receive('execute_command').with_args(
  840. ('borg', 'create', '--list', '--filter', 'AME-', '--info') + ARCHIVE_WITH_PATHS,
  841. output_log_level=logging.INFO,
  842. output_file=None,
  843. borg_local_path='borg',
  844. )
  845. insert_logging_mock(logging.INFO)
  846. module.create_archive(
  847. dry_run=False,
  848. repository='repo',
  849. location_config={
  850. 'source_directories': ['foo', 'bar'],
  851. 'repositories': ['repo'],
  852. 'exclude_patterns': None,
  853. },
  854. storage_config={},
  855. files=True,
  856. )
  857. def test_create_archive_with_progress_and_log_info_calls_borg_with_progress_parameter_and_no_list():
  858. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  859. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  860. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  861. flexmock(module).should_receive('_expand_directories').and_return(())
  862. flexmock(module).should_receive('_expand_home_directories').and_return(())
  863. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  864. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  865. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  866. flexmock(module).should_receive('execute_command').with_args(
  867. ('borg', 'create', '--info', '--progress') + ARCHIVE_WITH_PATHS,
  868. output_log_level=logging.INFO,
  869. output_file=module.DO_NOT_CAPTURE,
  870. borg_local_path='borg',
  871. )
  872. insert_logging_mock(logging.INFO)
  873. module.create_archive(
  874. dry_run=False,
  875. repository='repo',
  876. location_config={
  877. 'source_directories': ['foo', 'bar'],
  878. 'repositories': ['repo'],
  879. 'exclude_patterns': None,
  880. },
  881. storage_config={},
  882. progress=True,
  883. )
  884. def test_create_archive_with_progress_calls_borg_with_progress_parameter():
  885. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  886. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  887. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  888. flexmock(module).should_receive('_expand_directories').and_return(())
  889. flexmock(module).should_receive('_expand_home_directories').and_return(())
  890. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  891. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  892. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  893. flexmock(module).should_receive('execute_command').with_args(
  894. ('borg', 'create', '--progress') + ARCHIVE_WITH_PATHS,
  895. output_log_level=logging.INFO,
  896. output_file=module.DO_NOT_CAPTURE,
  897. borg_local_path='borg',
  898. )
  899. module.create_archive(
  900. dry_run=False,
  901. repository='repo',
  902. location_config={
  903. 'source_directories': ['foo', 'bar'],
  904. 'repositories': ['repo'],
  905. 'exclude_patterns': None,
  906. },
  907. storage_config={},
  908. progress=True,
  909. )
  910. def test_create_archive_with_progress_and_stream_processes_calls_borg_with_progress_parameter():
  911. processes = flexmock()
  912. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  913. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  914. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  915. flexmock(module).should_receive('_expand_directories').and_return(())
  916. flexmock(module).should_receive('_expand_home_directories').and_return(())
  917. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  918. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  919. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  920. flexmock(module).should_receive('execute_command_with_processes').with_args(
  921. ('borg', 'create', '--one-file-system', '--read-special', '--progress')
  922. + ARCHIVE_WITH_PATHS,
  923. processes=processes,
  924. output_log_level=logging.INFO,
  925. output_file=module.DO_NOT_CAPTURE,
  926. borg_local_path='borg',
  927. )
  928. module.create_archive(
  929. dry_run=False,
  930. repository='repo',
  931. location_config={
  932. 'source_directories': ['foo', 'bar'],
  933. 'repositories': ['repo'],
  934. 'exclude_patterns': None,
  935. },
  936. storage_config={},
  937. progress=True,
  938. stream_processes=processes,
  939. )
  940. def test_create_archive_with_json_calls_borg_with_json_parameter():
  941. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  942. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  943. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  944. flexmock(module).should_receive('_expand_directories').and_return(())
  945. flexmock(module).should_receive('_expand_home_directories').and_return(())
  946. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  947. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  948. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  949. flexmock(module).should_receive('execute_command').with_args(
  950. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  951. output_log_level=None,
  952. output_file=None,
  953. borg_local_path='borg',
  954. ).and_return('[]')
  955. json_output = module.create_archive(
  956. dry_run=False,
  957. repository='repo',
  958. location_config={
  959. 'source_directories': ['foo', 'bar'],
  960. 'repositories': ['repo'],
  961. 'exclude_patterns': None,
  962. },
  963. storage_config={},
  964. json=True,
  965. )
  966. assert json_output == '[]'
  967. def test_create_archive_with_stats_and_json_calls_borg_without_stats_parameter():
  968. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  969. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  970. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  971. flexmock(module).should_receive('_expand_directories').and_return(())
  972. flexmock(module).should_receive('_expand_home_directories').and_return(())
  973. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  974. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  975. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  976. flexmock(module).should_receive('execute_command').with_args(
  977. ('borg', 'create', '--json') + ARCHIVE_WITH_PATHS,
  978. output_log_level=None,
  979. output_file=None,
  980. borg_local_path='borg',
  981. ).and_return('[]')
  982. json_output = module.create_archive(
  983. dry_run=False,
  984. repository='repo',
  985. location_config={
  986. 'source_directories': ['foo', 'bar'],
  987. 'repositories': ['repo'],
  988. 'exclude_patterns': None,
  989. },
  990. storage_config={},
  991. json=True,
  992. stats=True,
  993. )
  994. assert json_output == '[]'
  995. def test_create_archive_with_source_directories_glob_expands():
  996. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  997. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  998. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  999. flexmock(module).should_receive('_expand_directories').and_return(())
  1000. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1001. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1002. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1003. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1004. flexmock(module).should_receive('execute_command').with_args(
  1005. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1006. output_log_level=logging.INFO,
  1007. output_file=None,
  1008. borg_local_path='borg',
  1009. )
  1010. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return(['foo', 'food'])
  1011. module.create_archive(
  1012. dry_run=False,
  1013. repository='repo',
  1014. location_config={
  1015. 'source_directories': ['foo*'],
  1016. 'repositories': ['repo'],
  1017. 'exclude_patterns': None,
  1018. },
  1019. storage_config={},
  1020. )
  1021. def test_create_archive_with_non_matching_source_directories_glob_passes_through():
  1022. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1023. flexmock(module).should_receive('deduplicate_directories').and_return(('foo*',))
  1024. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1025. flexmock(module).should_receive('_expand_directories').and_return(())
  1026. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1027. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1028. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1029. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1030. flexmock(module).should_receive('execute_command').with_args(
  1031. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo*'),
  1032. output_log_level=logging.INFO,
  1033. output_file=None,
  1034. borg_local_path='borg',
  1035. )
  1036. flexmock(module.glob).should_receive('glob').with_args('foo*').and_return([])
  1037. module.create_archive(
  1038. dry_run=False,
  1039. repository='repo',
  1040. location_config={
  1041. 'source_directories': ['foo*'],
  1042. 'repositories': ['repo'],
  1043. 'exclude_patterns': None,
  1044. },
  1045. storage_config={},
  1046. )
  1047. def test_create_archive_with_glob_calls_borg_with_expanded_directories():
  1048. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1049. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'food'))
  1050. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1051. flexmock(module).should_receive('_expand_directories').and_return(())
  1052. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1053. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1054. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1055. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1056. flexmock(module).should_receive('execute_command').with_args(
  1057. ('borg', 'create', 'repo::{}'.format(DEFAULT_ARCHIVE_NAME), 'foo', 'food'),
  1058. output_log_level=logging.INFO,
  1059. output_file=None,
  1060. borg_local_path='borg',
  1061. )
  1062. module.create_archive(
  1063. dry_run=False,
  1064. repository='repo',
  1065. location_config={
  1066. 'source_directories': ['foo*'],
  1067. 'repositories': ['repo'],
  1068. 'exclude_patterns': None,
  1069. },
  1070. storage_config={},
  1071. )
  1072. def test_create_archive_with_archive_name_format_calls_borg_with_archive_name():
  1073. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1074. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1075. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1076. flexmock(module).should_receive('_expand_directories').and_return(())
  1077. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1078. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1079. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1080. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1081. flexmock(module).should_receive('execute_command').with_args(
  1082. ('borg', 'create', 'repo::ARCHIVE_NAME', 'foo', 'bar'),
  1083. output_log_level=logging.INFO,
  1084. output_file=None,
  1085. borg_local_path='borg',
  1086. )
  1087. module.create_archive(
  1088. dry_run=False,
  1089. repository='repo',
  1090. location_config={
  1091. 'source_directories': ['foo', 'bar'],
  1092. 'repositories': ['repo'],
  1093. 'exclude_patterns': None,
  1094. },
  1095. storage_config={'archive_name_format': 'ARCHIVE_NAME'},
  1096. )
  1097. def test_create_archive_with_archive_name_format_accepts_borg_placeholders():
  1098. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1099. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1100. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1101. flexmock(module).should_receive('_expand_directories').and_return(())
  1102. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1103. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1104. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1105. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1106. flexmock(module).should_receive('execute_command').with_args(
  1107. ('borg', 'create', 'repo::Documents_{hostname}-{now}', 'foo', 'bar'),
  1108. output_log_level=logging.INFO,
  1109. output_file=None,
  1110. borg_local_path='borg',
  1111. )
  1112. module.create_archive(
  1113. dry_run=False,
  1114. repository='repo',
  1115. location_config={
  1116. 'source_directories': ['foo', 'bar'],
  1117. 'repositories': ['repo'],
  1118. 'exclude_patterns': None,
  1119. },
  1120. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1121. )
  1122. def test_create_archive_with_repository_accepts_borg_placeholders():
  1123. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1124. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1125. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1126. flexmock(module).should_receive('_expand_directories').and_return(())
  1127. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1128. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1129. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1130. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1131. flexmock(module).should_receive('execute_command').with_args(
  1132. ('borg', 'create', '{fqdn}::Documents_{hostname}-{now}', 'foo', 'bar'),
  1133. output_log_level=logging.INFO,
  1134. output_file=None,
  1135. borg_local_path='borg',
  1136. )
  1137. module.create_archive(
  1138. dry_run=False,
  1139. repository='{fqdn}',
  1140. location_config={
  1141. 'source_directories': ['foo', 'bar'],
  1142. 'repositories': ['{fqdn}'],
  1143. 'exclude_patterns': None,
  1144. },
  1145. storage_config={'archive_name_format': 'Documents_{hostname}-{now}'},
  1146. )
  1147. def test_create_archive_with_extra_borg_options_calls_borg_with_extra_options():
  1148. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1149. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1150. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1151. flexmock(module).should_receive('_expand_directories').and_return(())
  1152. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1153. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1154. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1155. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1156. flexmock(module).should_receive('execute_command').with_args(
  1157. ('borg', 'create', '--extra', '--options') + ARCHIVE_WITH_PATHS,
  1158. output_log_level=logging.INFO,
  1159. output_file=None,
  1160. borg_local_path='borg',
  1161. )
  1162. module.create_archive(
  1163. dry_run=False,
  1164. repository='repo',
  1165. location_config={
  1166. 'source_directories': ['foo', 'bar'],
  1167. 'repositories': ['repo'],
  1168. 'exclude_patterns': None,
  1169. },
  1170. storage_config={'extra_borg_options': {'create': '--extra --options'}},
  1171. )
  1172. def test_create_archive_with_stream_processes_calls_borg_with_processes():
  1173. processes = flexmock()
  1174. flexmock(module).should_receive('borgmatic_source_directories').and_return([])
  1175. flexmock(module).should_receive('deduplicate_directories').and_return(('foo', 'bar'))
  1176. flexmock(module).should_receive('map_directories_to_devices').and_return({})
  1177. flexmock(module).should_receive('_expand_directories').and_return(())
  1178. flexmock(module).should_receive('_expand_home_directories').and_return(())
  1179. flexmock(module).should_receive('_write_pattern_file').and_return(None)
  1180. flexmock(module).should_receive('_make_pattern_flags').and_return(())
  1181. flexmock(module).should_receive('_make_exclude_flags').and_return(())
  1182. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1183. ('borg', 'create', '--one-file-system', '--read-special') + ARCHIVE_WITH_PATHS,
  1184. processes=processes,
  1185. output_log_level=logging.INFO,
  1186. output_file=None,
  1187. borg_local_path='borg',
  1188. )
  1189. module.create_archive(
  1190. dry_run=False,
  1191. repository='repo',
  1192. location_config={
  1193. 'source_directories': ['foo', 'bar'],
  1194. 'repositories': ['repo'],
  1195. 'exclude_patterns': None,
  1196. },
  1197. storage_config={},
  1198. stream_processes=processes,
  1199. )