test_create.py 41 KB

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