2
0

test_create.py 43 KB

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