test_restore.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. import pytest
  2. from flexmock import flexmock
  3. import borgmatic.actions.restore as module
  4. @pytest.mark.parametrize(
  5. 'first_dump,second_dump,default_port,expected_result',
  6. (
  7. (
  8. module.Dump('postgresql_databases', 'foo'),
  9. module.Dump('postgresql_databases', 'foo'),
  10. None,
  11. True,
  12. ),
  13. (
  14. module.Dump('postgresql_databases', 'foo'),
  15. module.Dump('postgresql_databases', 'bar'),
  16. None,
  17. False,
  18. ),
  19. (
  20. module.Dump('postgresql_databases', 'foo'),
  21. module.Dump('mariadb_databases', 'foo'),
  22. None,
  23. False,
  24. ),
  25. (
  26. module.Dump('postgresql_databases', 'foo'),
  27. module.Dump(module.UNSPECIFIED, 'foo'),
  28. None,
  29. True,
  30. ),
  31. (
  32. module.Dump('postgresql_databases', 'foo'),
  33. module.Dump(module.UNSPECIFIED, 'bar'),
  34. None,
  35. False,
  36. ),
  37. (
  38. module.Dump('postgresql_databases', module.UNSPECIFIED),
  39. module.Dump('postgresql_databases', 'foo'),
  40. None,
  41. True,
  42. ),
  43. (
  44. module.Dump('postgresql_databases', module.UNSPECIFIED),
  45. module.Dump('mariadb_databases', 'foo'),
  46. None,
  47. False,
  48. ),
  49. (
  50. module.Dump('postgresql_databases', 'foo', 'myhost'),
  51. module.Dump('postgresql_databases', 'foo', 'myhost'),
  52. None,
  53. True,
  54. ),
  55. (
  56. module.Dump('postgresql_databases', 'foo', 'myhost'),
  57. module.Dump('postgresql_databases', 'foo', 'otherhost'),
  58. None,
  59. False,
  60. ),
  61. (
  62. module.Dump('postgresql_databases', 'foo', 'myhost'),
  63. module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED),
  64. None,
  65. True,
  66. ),
  67. (
  68. module.Dump('postgresql_databases', 'foo', 'myhost'),
  69. module.Dump('postgresql_databases', 'bar', module.UNSPECIFIED),
  70. None,
  71. False,
  72. ),
  73. (
  74. module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
  75. module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
  76. None,
  77. True,
  78. ),
  79. (
  80. module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
  81. module.Dump('postgresql_databases', 'foo', 'myhost', 4321),
  82. None,
  83. False,
  84. ),
  85. (
  86. module.Dump('postgresql_databases', 'foo', 'myhost', module.UNSPECIFIED),
  87. module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
  88. None,
  89. True,
  90. ),
  91. (
  92. module.Dump('postgresql_databases', 'foo', 'myhost', module.UNSPECIFIED),
  93. module.Dump('postgresql_databases', 'foo', 'otherhost', 1234),
  94. None,
  95. False,
  96. ),
  97. (
  98. module.Dump(
  99. module.UNSPECIFIED,
  100. module.UNSPECIFIED,
  101. module.UNSPECIFIED,
  102. module.UNSPECIFIED,
  103. ),
  104. module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
  105. None,
  106. True,
  107. ),
  108. (
  109. module.Dump('postgresql_databases', 'foo', 'myhost', 5432),
  110. module.Dump('postgresql_databases', 'foo', 'myhost', None),
  111. 5432,
  112. True,
  113. ),
  114. (
  115. module.Dump('postgresql_databases', 'foo', 'myhost', None),
  116. module.Dump('postgresql_databases', 'foo', 'myhost', 5432),
  117. 5432,
  118. True,
  119. ),
  120. (
  121. module.Dump('postgresql_databases', 'foo', 'myhost', 5433),
  122. module.Dump('postgresql_databases', 'foo', 'myhost', None),
  123. 5432,
  124. False,
  125. ),
  126. (
  127. module.Dump('postgresql_databases', 'foo', 'some_host1', 5433, 'unique'),
  128. module.Dump('postgresql_databases', 'foo', 'some_host2', None, 'unique'),
  129. 5432,
  130. True,
  131. ),
  132. (
  133. module.Dump('postgresql_databases', 'foo', 'some_host1', 5433, 'unique'),
  134. module.Dump(module.UNSPECIFIED, 'foo', 'some_host2', None, 'unique'),
  135. 5432,
  136. True,
  137. ),
  138. (
  139. module.Dump('postgresql_databases', 'foo', container='container'),
  140. module.Dump('postgresql_databases', 'foo', container='container'),
  141. 5432,
  142. True,
  143. ),
  144. ),
  145. )
  146. def test_dumps_match_compares_two_dumps_while_respecting_unspecified_values(
  147. first_dump,
  148. second_dump,
  149. default_port,
  150. expected_result,
  151. ):
  152. assert module.dumps_match(first_dump, second_dump, default_port) == expected_result
  153. @pytest.mark.parametrize(
  154. 'dump,expected_result',
  155. (
  156. (
  157. module.Dump('postgresql_databases', 'foo'),
  158. 'foo (postgresql_databases)',
  159. ),
  160. (
  161. module.Dump(module.UNSPECIFIED, 'foo'),
  162. 'foo',
  163. ),
  164. (
  165. module.Dump('postgresql_databases', module.UNSPECIFIED),
  166. 'unspecified (postgresql_databases)',
  167. ),
  168. (
  169. module.Dump('postgresql_databases', 'foo', 'host'),
  170. 'foo@host (postgresql_databases)',
  171. ),
  172. (
  173. module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED),
  174. 'foo (postgresql_databases)',
  175. ),
  176. (
  177. module.Dump('postgresql_databases', 'foo', 'host', 1234),
  178. 'foo@host:1234 (postgresql_databases)',
  179. ),
  180. (
  181. module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED, 1234),
  182. 'foo@:1234 (postgresql_databases)',
  183. ),
  184. (
  185. module.Dump('postgresql_databases', 'foo', 'host', module.UNSPECIFIED),
  186. 'foo@host (postgresql_databases)',
  187. ),
  188. (
  189. module.Dump('postgresql_databases', 'foo', 'host', 1234, 'label'),
  190. 'foo@label (postgresql_databases)',
  191. ),
  192. (
  193. module.Dump('postgresql_databases', 'foo', container='container'),
  194. 'foo@container (postgresql_databases)',
  195. ),
  196. (
  197. module.Dump(
  198. module.UNSPECIFIED,
  199. module.UNSPECIFIED,
  200. module.UNSPECIFIED,
  201. module.UNSPECIFIED,
  202. ),
  203. 'unspecified',
  204. ),
  205. ),
  206. )
  207. def test_render_dump_metadata_renders_dump_values_into_string(dump, expected_result):
  208. assert module.render_dump_metadata(dump) == expected_result
  209. def test_get_configured_data_source_matches_data_source_with_restore_dump():
  210. default_port = flexmock()
  211. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(default_port)
  212. flexmock(module).should_receive('dumps_match').and_return(False)
  213. flexmock(module).should_receive('dumps_match').with_args(
  214. module.Dump('postgresql_databases', 'bar', label=module.UNSPECIFIED),
  215. module.Dump('postgresql_databases', 'bar'),
  216. default_port=default_port,
  217. ).and_return(True)
  218. assert module.get_configured_data_source(
  219. config={
  220. 'other_databases': [{'name': 'other'}],
  221. 'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
  222. },
  223. restore_dump=module.Dump('postgresql_databases', 'bar'),
  224. ) == {'name': 'bar'}
  225. def test_get_configured_data_source_matches_nothing_when_nothing_configured():
  226. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(flexmock())
  227. flexmock(module).should_receive('dumps_match').and_return(False)
  228. assert (
  229. module.get_configured_data_source(
  230. config={},
  231. restore_dump=module.Dump('postgresql_databases', 'quux'),
  232. )
  233. is None
  234. )
  235. def test_get_configured_data_source_matches_nothing_when_restore_dump_does_not_match_configuration():
  236. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(flexmock())
  237. flexmock(module).should_receive('dumps_match').and_return(False)
  238. assert (
  239. module.get_configured_data_source(
  240. config={
  241. 'postgresql_databases': [{'name': 'foo'}],
  242. },
  243. restore_dump=module.Dump('postgresql_databases', 'quux'),
  244. )
  245. is None
  246. )
  247. def test_get_configured_data_source_with_multiple_matching_data_sources_errors():
  248. default_port = flexmock()
  249. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(default_port)
  250. flexmock(module).should_receive('dumps_match').and_return(False)
  251. flexmock(module).should_receive('dumps_match').with_args(
  252. module.Dump('postgresql_databases', 'bar', label=module.UNSPECIFIED),
  253. module.Dump('postgresql_databases', 'bar'),
  254. default_port=default_port,
  255. ).and_return(True)
  256. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  257. with pytest.raises(ValueError):
  258. module.get_configured_data_source(
  259. config={
  260. 'other_databases': [{'name': 'other'}],
  261. 'postgresql_databases': [
  262. {'name': 'foo'},
  263. {'name': 'bar'},
  264. {'name': 'bar', 'format': 'directory'},
  265. ],
  266. },
  267. restore_dump=module.Dump('postgresql_databases', 'bar'),
  268. )
  269. def test_strip_path_prefix_from_extracted_dump_destination_renames_first_matching_databases_subdirectory():
  270. flexmock(module.os).should_receive('walk').and_return(
  271. [
  272. ('/foo', flexmock(), flexmock()),
  273. ('/foo/bar', flexmock(), flexmock()),
  274. ('/foo/bar/postgresql_databases', flexmock(), flexmock()),
  275. ('/foo/bar/mariadb_databases', flexmock(), flexmock()),
  276. ],
  277. )
  278. flexmock(module.shutil).should_receive('move').with_args(
  279. '/foo/bar/postgresql_databases',
  280. '/run/user/0/borgmatic/postgresql_databases',
  281. ).once()
  282. flexmock(module.shutil).should_receive('move').with_args(
  283. '/foo/bar/mariadb_databases',
  284. '/run/user/0/borgmatic/mariadb_databases',
  285. ).never()
  286. module.strip_path_prefix_from_extracted_dump_destination('/foo', '/run/user/0/borgmatic')
  287. def test_restore_single_dump_extracts_and_restores_single_file_dump():
  288. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  289. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  290. 'make_data_source_dump_patterns',
  291. object,
  292. object,
  293. object,
  294. object,
  295. ).and_return({'postgresql': flexmock()})
  296. flexmock(module.tempfile).should_receive('mkdtemp').never()
  297. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  298. 'convert_glob_patterns_to_borg_pattern',
  299. ).and_return(flexmock())
  300. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  301. flexmock(),
  302. ).once()
  303. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
  304. flexmock(module.shutil).should_receive('rmtree').never()
  305. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  306. function_name='restore_data_source_dump',
  307. config=object,
  308. hook_name=object,
  309. data_source=object,
  310. dry_run=object,
  311. extract_process=object,
  312. connection_params=object,
  313. borgmatic_runtime_directory=object,
  314. ).once()
  315. module.restore_single_dump(
  316. repository={'path': 'test.borg'},
  317. config=flexmock(),
  318. local_borg_version=flexmock(),
  319. global_arguments=flexmock(dry_run=False),
  320. local_path=None,
  321. remote_path=None,
  322. archive_name=flexmock(),
  323. hook_name='postgresql',
  324. data_source={'name': 'test', 'format': 'plain'},
  325. connection_params=flexmock(),
  326. borgmatic_runtime_directory='/run/borgmatic',
  327. )
  328. def test_restore_single_dump_extracts_and_restores_directory_dump():
  329. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  330. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  331. 'make_data_source_dump_patterns',
  332. object,
  333. object,
  334. object,
  335. object,
  336. ).and_return({'postgresql': flexmock()})
  337. flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
  338. '/run/user/0/borgmatic/tmp1234',
  339. )
  340. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  341. 'convert_glob_patterns_to_borg_pattern',
  342. ).and_return(flexmock())
  343. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  344. flexmock(),
  345. ).once()
  346. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').once()
  347. flexmock(module.shutil).should_receive('rmtree').once()
  348. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  349. function_name='restore_data_source_dump',
  350. config=object,
  351. hook_name=object,
  352. data_source=object,
  353. dry_run=object,
  354. extract_process=object,
  355. connection_params=object,
  356. borgmatic_runtime_directory='/run/borgmatic',
  357. ).once()
  358. module.restore_single_dump(
  359. repository={'path': 'test.borg'},
  360. config=flexmock(),
  361. local_borg_version=flexmock(),
  362. global_arguments=flexmock(dry_run=False),
  363. local_path=None,
  364. remote_path=None,
  365. archive_name=flexmock(),
  366. hook_name='postgresql',
  367. data_source={'name': 'test', 'format': 'directory'},
  368. connection_params=flexmock(),
  369. borgmatic_runtime_directory='/run/borgmatic',
  370. )
  371. def test_restore_single_dump_with_directory_dump_error_cleans_up_temporary_directory():
  372. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  373. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  374. 'make_data_source_dump_patterns',
  375. object,
  376. object,
  377. object,
  378. object,
  379. ).and_return({'postgresql': flexmock()})
  380. flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
  381. '/run/user/0/borgmatic/tmp1234',
  382. )
  383. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  384. 'convert_glob_patterns_to_borg_pattern',
  385. ).and_return(flexmock())
  386. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_raise(
  387. ValueError,
  388. ).once()
  389. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
  390. flexmock(module.shutil).should_receive('rmtree').once()
  391. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  392. function_name='restore_data_source_dump',
  393. config=object,
  394. hook_name=object,
  395. data_source=object,
  396. dry_run=object,
  397. extract_process=object,
  398. connection_params=object,
  399. borgmatic_runtime_directory='/run/user/0/borgmatic/tmp1234',
  400. ).never()
  401. with pytest.raises(ValueError):
  402. module.restore_single_dump(
  403. repository={'path': 'test.borg'},
  404. config=flexmock(),
  405. local_borg_version=flexmock(),
  406. global_arguments=flexmock(dry_run=False),
  407. local_path=None,
  408. remote_path=None,
  409. archive_name=flexmock(),
  410. hook_name='postgresql',
  411. data_source={'name': 'test', 'format': 'directory'},
  412. connection_params=flexmock(),
  413. borgmatic_runtime_directory='/run/borgmatic',
  414. )
  415. def test_restore_single_dump_with_directory_dump_and_dry_run_skips_directory_move_and_cleanup():
  416. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  417. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  418. 'make_data_source_dump_patterns',
  419. object,
  420. object,
  421. object,
  422. object,
  423. ).and_return({'postgresql': flexmock()})
  424. flexmock(module.tempfile).should_receive('mkdtemp').once().and_return('/run/borgmatic/tmp1234')
  425. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  426. 'convert_glob_patterns_to_borg_pattern',
  427. ).and_return(flexmock())
  428. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  429. flexmock(),
  430. ).once()
  431. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
  432. flexmock(module.shutil).should_receive('rmtree').never()
  433. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
  434. function_name='restore_data_source_dump',
  435. config=object,
  436. hook_name=object,
  437. data_source=object,
  438. dry_run=object,
  439. extract_process=object,
  440. connection_params=object,
  441. borgmatic_runtime_directory='/run/borgmatic',
  442. ).once()
  443. module.restore_single_dump(
  444. repository={'path': 'test.borg'},
  445. config=flexmock(),
  446. local_borg_version=flexmock(),
  447. global_arguments=flexmock(dry_run=True),
  448. local_path=None,
  449. remote_path=None,
  450. archive_name=flexmock(),
  451. hook_name='postgresql',
  452. data_source={'name': 'test', 'format': 'directory'},
  453. connection_params=flexmock(),
  454. borgmatic_runtime_directory='/run/borgmatic',
  455. )
  456. def test_collect_dumps_from_archive_with_dumps_metadata_parses_it():
  457. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  458. 'make_data_source_dump_path',
  459. ).and_return('')
  460. flexmock(module.borgmatic.config.paths).should_receive(
  461. 'make_runtime_directory_glob'
  462. ).and_return('')
  463. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  464. [
  465. 'borgmatic/postgresql_databases/dumps.json',
  466. 'borgmatic/mysql_databases/dumps.json',
  467. ],
  468. )
  469. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  470. flexmock(stdout=flexmock(read=lambda: b''))
  471. )
  472. dumps_metadata = [
  473. module.Dump('postgresql_databases', 'foo'),
  474. module.Dump('postgresql_databases', 'bar', 'host', 1234),
  475. module.Dump('mysql_databases', 'quux'),
  476. ]
  477. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  478. 'parse_data_source_dumps_metadata'
  479. ).and_return(dumps_metadata)
  480. flexmock(module.borgmatic.config.paths).should_receive('get_borgmatic_source_directory').never()
  481. archive_dumps = module.collect_dumps_from_archive(
  482. repository={'path': 'repo'},
  483. archive='archive',
  484. config={},
  485. local_borg_version=flexmock(),
  486. global_arguments=flexmock(dry_run=False, log_json=False),
  487. local_path=flexmock(),
  488. remote_path=flexmock(),
  489. borgmatic_runtime_directory='/run/borgmatic',
  490. )
  491. assert archive_dumps == set(dumps_metadata)
  492. def test_collect_dumps_from_archive_with_empty_dumps_metadata_path_falls_back_to_parsing_archive_paths():
  493. flexmock(module.borgmatic.config.paths).should_receive(
  494. 'make_runtime_directory_glob'
  495. ).and_return('')
  496. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  497. ['']
  498. ).and_return(
  499. [
  500. 'borgmatic/postgresql_databases/localhost/foo',
  501. 'borgmatic/postgresql_databases/host:1234/bar',
  502. 'borgmatic/mysql_databases/localhost/quux',
  503. ],
  504. )
  505. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').never()
  506. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  507. 'parse_data_source_dumps_metadata'
  508. ).never()
  509. flexmock(module.borgmatic.config.paths).should_receive(
  510. 'get_borgmatic_source_directory',
  511. ).and_return('/root/.borgmatic')
  512. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  513. 'make_data_source_dump_path',
  514. ).and_return('')
  515. archive_dumps = module.collect_dumps_from_archive(
  516. repository={'path': 'repo'},
  517. archive='archive',
  518. config={},
  519. local_borg_version=flexmock(),
  520. global_arguments=flexmock(dry_run=False, log_json=False),
  521. local_path=flexmock(),
  522. remote_path=flexmock(),
  523. borgmatic_runtime_directory='/run/borgmatic',
  524. )
  525. assert archive_dumps == {
  526. module.Dump('postgresql_databases', 'foo'),
  527. module.Dump('postgresql_databases', 'bar', 'host', 1234),
  528. module.Dump('mysql_databases', 'quux'),
  529. }
  530. def test_collect_dumps_from_archive_without_dumps_metadata_falls_back_to_parsing_archive_paths():
  531. flexmock(module.borgmatic.config.paths).should_receive(
  532. 'make_runtime_directory_glob'
  533. ).and_return('')
  534. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  535. []
  536. ).and_return(
  537. [
  538. 'borgmatic/postgresql_databases/localhost/foo',
  539. 'borgmatic/postgresql_databases/host:1234/bar',
  540. 'borgmatic/mysql_databases/localhost/quux',
  541. ],
  542. )
  543. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').never()
  544. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  545. 'parse_data_source_dumps_metadata'
  546. ).never()
  547. flexmock(module.borgmatic.config.paths).should_receive(
  548. 'get_borgmatic_source_directory',
  549. ).and_return('/root/.borgmatic')
  550. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  551. 'make_data_source_dump_path',
  552. ).and_return('')
  553. archive_dumps = module.collect_dumps_from_archive(
  554. repository={'path': 'repo'},
  555. archive='archive',
  556. config={},
  557. local_borg_version=flexmock(),
  558. global_arguments=flexmock(dry_run=False, log_json=False),
  559. local_path=flexmock(),
  560. remote_path=flexmock(),
  561. borgmatic_runtime_directory='/run/borgmatic',
  562. )
  563. assert archive_dumps == {
  564. module.Dump('postgresql_databases', 'foo'),
  565. module.Dump('postgresql_databases', 'bar', 'host', 1234),
  566. module.Dump('mysql_databases', 'quux'),
  567. }
  568. def test_collect_dumps_from_archive_parses_archive_paths_with_different_base_directories():
  569. flexmock(module.borgmatic.config.paths).should_receive(
  570. 'make_runtime_directory_glob'
  571. ).and_return('')
  572. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  573. []
  574. ).and_return(
  575. [
  576. 'borgmatic/postgresql_databases/localhost/foo',
  577. '.borgmatic/postgresql_databases/localhost/bar',
  578. '/root/.borgmatic/postgresql_databases/localhost/baz',
  579. '/var/run/0/borgmatic/mysql_databases/localhost/quux',
  580. ],
  581. )
  582. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').never()
  583. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  584. 'parse_data_source_dumps_metadata'
  585. ).never()
  586. flexmock(module.borgmatic.config.paths).should_receive(
  587. 'get_borgmatic_source_directory',
  588. ).and_return('/root/.borgmatic')
  589. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  590. 'make_data_source_dump_path',
  591. ).and_return('')
  592. archive_dumps = module.collect_dumps_from_archive(
  593. repository={'path': 'repo'},
  594. archive='archive',
  595. config={},
  596. local_borg_version=flexmock(),
  597. global_arguments=flexmock(log_json=False),
  598. local_path=flexmock(),
  599. remote_path=flexmock(),
  600. borgmatic_runtime_directory='/run/borgmatic',
  601. )
  602. assert archive_dumps == {
  603. module.Dump('postgresql_databases', 'foo'),
  604. module.Dump('postgresql_databases', 'bar'),
  605. module.Dump('postgresql_databases', 'baz'),
  606. module.Dump('mysql_databases', 'quux'),
  607. }
  608. def test_collect_dumps_from_archive_parses_directory_format_archive_paths():
  609. flexmock(module.borgmatic.config.paths).should_receive(
  610. 'make_runtime_directory_glob'
  611. ).and_return('')
  612. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  613. []
  614. ).and_return(
  615. [
  616. 'borgmatic/postgresql_databases/localhost/foo/table1',
  617. 'borgmatic/postgresql_databases/localhost/foo/table2',
  618. ],
  619. )
  620. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').never()
  621. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  622. 'parse_data_source_dumps_metadata'
  623. ).never()
  624. flexmock(module.borgmatic.config.paths).should_receive(
  625. 'get_borgmatic_source_directory',
  626. ).and_return('/root/.borgmatic')
  627. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  628. 'make_data_source_dump_path',
  629. ).and_return('')
  630. archive_dumps = module.collect_dumps_from_archive(
  631. repository={'path': 'repo'},
  632. archive='archive',
  633. config={},
  634. local_borg_version=flexmock(),
  635. global_arguments=flexmock(log_json=False),
  636. local_path=flexmock(),
  637. remote_path=flexmock(),
  638. borgmatic_runtime_directory='/run/borgmatic',
  639. )
  640. assert archive_dumps == {
  641. module.Dump('postgresql_databases', 'foo'),
  642. }
  643. def test_collect_dumps_from_archive_skips_bad_archive_paths_or_bad_path_components():
  644. flexmock(module.borgmatic.config.paths).should_receive(
  645. 'make_runtime_directory_glob'
  646. ).and_return('')
  647. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  648. []
  649. ).and_return(
  650. [
  651. 'borgmatic/postgresql_databases/localhost/foo',
  652. 'borgmatic/postgresql_databases/localhost:abcd/bar',
  653. 'borgmatic/invalid',
  654. 'invalid/as/well',
  655. '',
  656. ],
  657. )
  658. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').never()
  659. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  660. 'parse_data_source_dumps_metadata'
  661. ).never()
  662. flexmock(module.borgmatic.config.paths).should_receive(
  663. 'get_borgmatic_source_directory',
  664. ).and_return('/root/.borgmatic')
  665. flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
  666. 'make_data_source_dump_path',
  667. ).and_return('')
  668. archive_dumps = module.collect_dumps_from_archive(
  669. repository={'path': 'repo'},
  670. archive='archive',
  671. config={},
  672. local_borg_version=flexmock(),
  673. global_arguments=flexmock(log_json=False),
  674. local_path=flexmock(),
  675. remote_path=flexmock(),
  676. borgmatic_runtime_directory='/run/borgmatic',
  677. )
  678. assert archive_dumps == {
  679. module.Dump('postgresql_databases', 'foo'),
  680. module.Dump('postgresql_databases', 'bar'),
  681. }
  682. def test_get_dumps_to_restore_gets_requested_dumps_found_in_archive():
  683. dumps_from_archive = {
  684. module.Dump('postgresql_databases', 'foo'),
  685. module.Dump('postgresql_databases', 'bar'),
  686. module.Dump('postgresql_databases', 'baz'),
  687. }
  688. flexmock(module).should_receive('dumps_match').and_return(False)
  689. flexmock(module).should_receive('dumps_match').with_args(
  690. module.Dump(
  691. module.UNSPECIFIED,
  692. 'foo',
  693. hostname=module.UNSPECIFIED,
  694. label=module.UNSPECIFIED,
  695. container=module.UNSPECIFIED,
  696. ),
  697. module.Dump('postgresql_databases', 'foo'),
  698. ).and_return(True)
  699. flexmock(module).should_receive('dumps_match').with_args(
  700. module.Dump(
  701. module.UNSPECIFIED,
  702. 'bar',
  703. hostname=module.UNSPECIFIED,
  704. label=module.UNSPECIFIED,
  705. container=module.UNSPECIFIED,
  706. ),
  707. module.Dump('postgresql_databases', 'bar'),
  708. ).and_return(True)
  709. assert module.get_dumps_to_restore(
  710. restore_arguments=flexmock(
  711. hook=None,
  712. data_sources=['foo', 'bar'],
  713. original_hostname=None,
  714. original_port=None,
  715. original_label=None,
  716. original_container=None,
  717. ),
  718. dumps_from_archive=dumps_from_archive,
  719. ) == {
  720. module.Dump('postgresql_databases', 'foo'),
  721. module.Dump('postgresql_databases', 'bar'),
  722. }
  723. def test_get_dumps_to_restore_raises_for_requested_dumps_missing_from_archive():
  724. dumps_from_archive = {
  725. module.Dump('postgresql_databases', 'foo'),
  726. }
  727. flexmock(module).should_receive('dumps_match').and_return(False)
  728. flexmock(module).should_receive('dumps_match').with_args(
  729. module.Dump(
  730. module.UNSPECIFIED,
  731. 'foo',
  732. hostname=module.UNSPECIFIED,
  733. label=module.UNSPECIFIED,
  734. container=module.UNSPECIFIED,
  735. ),
  736. module.Dump('postgresql_databases', 'foo'),
  737. ).and_return(True)
  738. flexmock(module).should_receive('render_dump_metadata').and_return('test').once()
  739. with pytest.raises(ValueError) as exc_info:
  740. module.get_dumps_to_restore(
  741. restore_arguments=flexmock(
  742. hook=None,
  743. data_sources=['foo', 'bar'],
  744. original_hostname=None,
  745. original_port=None,
  746. original_label=None,
  747. original_container=None,
  748. ),
  749. dumps_from_archive=dumps_from_archive,
  750. )
  751. assert 'dump test missing from archive' in str(exc_info.value)
  752. def test_get_dumps_to_restore_without_requested_dumps_finds_all_archive_dumps():
  753. dumps_from_archive = {
  754. module.Dump('postgresql_databases', 'foo'),
  755. module.Dump('postgresql_databases', 'bar'),
  756. }
  757. flexmock(module).should_receive('dumps_match').and_return(False)
  758. assert (
  759. module.get_dumps_to_restore(
  760. restore_arguments=flexmock(
  761. hook=None,
  762. data_sources=[],
  763. original_hostname=None,
  764. original_port=None,
  765. original_label=None,
  766. original_container=None,
  767. ),
  768. dumps_from_archive=dumps_from_archive,
  769. )
  770. == dumps_from_archive
  771. )
  772. def test_get_dumps_to_restore_with_all_in_requested_dumps_finds_all_archive_dumps():
  773. dumps_from_archive = {
  774. module.Dump('postgresql_databases', 'foo'),
  775. module.Dump('postgresql_databases', 'bar'),
  776. }
  777. flexmock(module).should_receive('dumps_match').and_return(False)
  778. flexmock(module).should_receive('dumps_match').with_args(
  779. module.Dump(
  780. module.UNSPECIFIED,
  781. 'foo',
  782. hostname=module.UNSPECIFIED,
  783. label=module.UNSPECIFIED,
  784. container=module.UNSPECIFIED,
  785. ),
  786. module.Dump('postgresql_databases', 'foo'),
  787. ).and_return(True)
  788. flexmock(module).should_receive('dumps_match').with_args(
  789. module.Dump(
  790. module.UNSPECIFIED,
  791. 'bar',
  792. hostname=module.UNSPECIFIED,
  793. label=module.UNSPECIFIED,
  794. container=module.UNSPECIFIED,
  795. ),
  796. module.Dump('postgresql_databases', 'bar'),
  797. ).and_return(True)
  798. assert (
  799. module.get_dumps_to_restore(
  800. restore_arguments=flexmock(
  801. hook=None,
  802. data_sources=['all'],
  803. original_hostname=None,
  804. original_port=None,
  805. original_label=None,
  806. original_container=None,
  807. ),
  808. dumps_from_archive=dumps_from_archive,
  809. )
  810. == dumps_from_archive
  811. )
  812. def test_get_dumps_to_restore_with_all_in_requested_dumps_plus_additional_requested_dumps_omits_duplicates():
  813. dumps_from_archive = {
  814. module.Dump('postgresql_databases', 'foo'),
  815. module.Dump('postgresql_databases', 'bar'),
  816. }
  817. flexmock(module).should_receive('dumps_match').and_return(False)
  818. flexmock(module).should_receive('dumps_match').with_args(
  819. module.Dump(
  820. module.UNSPECIFIED,
  821. 'foo',
  822. hostname=module.UNSPECIFIED,
  823. label=module.UNSPECIFIED,
  824. container=module.UNSPECIFIED,
  825. ),
  826. module.Dump('postgresql_databases', 'foo'),
  827. ).and_return(True)
  828. flexmock(module).should_receive('dumps_match').with_args(
  829. module.Dump(
  830. module.UNSPECIFIED,
  831. 'bar',
  832. hostname=module.UNSPECIFIED,
  833. label=module.UNSPECIFIED,
  834. container=module.UNSPECIFIED,
  835. ),
  836. module.Dump('postgresql_databases', 'bar'),
  837. ).and_return(True)
  838. assert (
  839. module.get_dumps_to_restore(
  840. restore_arguments=flexmock(
  841. hook=None,
  842. data_sources=['all', 'foo', 'bar'],
  843. original_hostname=None,
  844. original_port=None,
  845. original_label=None,
  846. original_container=None,
  847. ),
  848. dumps_from_archive=dumps_from_archive,
  849. )
  850. == dumps_from_archive
  851. )
  852. def test_get_dumps_to_restore_raises_for_multiple_matching_dumps_in_archive():
  853. flexmock(module).should_receive('dumps_match').and_return(False)
  854. flexmock(module).should_receive('dumps_match').with_args(
  855. module.Dump(
  856. module.UNSPECIFIED,
  857. 'foo',
  858. hostname=module.UNSPECIFIED,
  859. label=module.UNSPECIFIED,
  860. container=module.UNSPECIFIED,
  861. ),
  862. module.Dump('postgresql_databases', 'foo'),
  863. ).and_return(True)
  864. flexmock(module).should_receive('dumps_match').with_args(
  865. module.Dump(
  866. module.UNSPECIFIED,
  867. 'foo',
  868. hostname=module.UNSPECIFIED,
  869. label=module.UNSPECIFIED,
  870. container=module.UNSPECIFIED,
  871. ),
  872. module.Dump('mariadb_databases', 'foo'),
  873. ).and_return(True)
  874. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  875. with pytest.raises(ValueError) as exc_info:
  876. module.get_dumps_to_restore(
  877. restore_arguments=flexmock(
  878. hook=None,
  879. data_sources=['foo'],
  880. original_hostname=None,
  881. original_port=None,
  882. original_label=None,
  883. original_container=None,
  884. ),
  885. dumps_from_archive={
  886. module.Dump('postgresql_databases', 'foo'),
  887. module.Dump('mariadb_databases', 'foo'),
  888. },
  889. )
  890. assert 'Try adding flags to disambiguate.' in str(exc_info.value)
  891. def test_get_dumps_to_restore_raises_for_all_in_requested_dumps_and_requested_dumps_missing_from_archive():
  892. flexmock(module).should_receive('dumps_match').and_return(False)
  893. flexmock(module).should_receive('dumps_match').with_args(
  894. module.Dump(
  895. module.UNSPECIFIED,
  896. 'foo',
  897. hostname=module.UNSPECIFIED,
  898. label=module.UNSPECIFIED,
  899. container=module.UNSPECIFIED,
  900. ),
  901. module.Dump('postgresql_databases', 'foo'),
  902. ).and_return(True)
  903. flexmock(module).should_receive('render_dump_metadata').and_return('test').once()
  904. with pytest.raises(ValueError) as exc_info:
  905. module.get_dumps_to_restore(
  906. restore_arguments=flexmock(
  907. hook=None,
  908. data_sources=['all', 'foo', 'bar'],
  909. original_hostname=None,
  910. original_port=None,
  911. original_label=None,
  912. original_container=None,
  913. ),
  914. dumps_from_archive={module.Dump('postgresql_databases', 'foo')},
  915. )
  916. assert 'dump test missing from archive' in str(exc_info.value)
  917. def test_get_dumps_to_restore_with_requested_hook_name_filters_dumps_found_in_archive():
  918. dumps_from_archive = {
  919. module.Dump('mariadb_databases', 'foo'),
  920. module.Dump('postgresql_databases', 'foo'),
  921. module.Dump('sqlite_databases', 'bar'),
  922. }
  923. flexmock(module).should_receive('dumps_match').and_return(False)
  924. flexmock(module).should_receive('dumps_match').with_args(
  925. module.Dump(
  926. 'postgresql_databases',
  927. 'foo',
  928. hostname=module.UNSPECIFIED,
  929. label=module.UNSPECIFIED,
  930. container=module.UNSPECIFIED,
  931. ),
  932. module.Dump('postgresql_databases', 'foo'),
  933. ).and_return(True)
  934. assert module.get_dumps_to_restore(
  935. restore_arguments=flexmock(
  936. hook='postgresql_databases',
  937. data_sources=['foo'],
  938. original_hostname=None,
  939. original_port=None,
  940. original_label=None,
  941. original_container=None,
  942. ),
  943. dumps_from_archive=dumps_from_archive,
  944. ) == {
  945. module.Dump('postgresql_databases', 'foo'),
  946. }
  947. def test_get_dumps_to_restore_with_requested_shortened_hook_name_filters_dumps_found_in_archive():
  948. dumps_from_archive = {
  949. module.Dump('mariadb_databases', 'foo'),
  950. module.Dump('postgresql_databases', 'foo'),
  951. module.Dump('sqlite_databases', 'bar'),
  952. }
  953. flexmock(module).should_receive('dumps_match').and_return(False)
  954. flexmock(module).should_receive('dumps_match').with_args(
  955. module.Dump(
  956. 'postgresql_databases',
  957. 'foo',
  958. hostname=module.UNSPECIFIED,
  959. label=module.UNSPECIFIED,
  960. container=module.UNSPECIFIED,
  961. ),
  962. module.Dump('postgresql_databases', 'foo'),
  963. ).and_return(True)
  964. assert module.get_dumps_to_restore(
  965. restore_arguments=flexmock(
  966. hook='postgresql',
  967. data_sources=['foo'],
  968. original_hostname=None,
  969. original_port=None,
  970. original_label=None,
  971. original_container=None,
  972. ),
  973. dumps_from_archive=dumps_from_archive,
  974. ) == {
  975. module.Dump('postgresql_databases', 'foo'),
  976. }
  977. def test_get_dumps_to_restore_with_requested_hostname_filters_dumps_found_in_archive():
  978. dumps_from_archive = {
  979. module.Dump('postgresql_databases', 'foo'),
  980. module.Dump('postgresql_databases', 'foo', 'host'),
  981. module.Dump('postgresql_databases', 'bar'),
  982. }
  983. flexmock(module).should_receive('dumps_match').and_return(False)
  984. flexmock(module).should_receive('dumps_match').with_args(
  985. module.Dump(
  986. 'postgresql_databases',
  987. 'foo',
  988. 'host',
  989. label=module.UNSPECIFIED,
  990. container=module.UNSPECIFIED,
  991. ),
  992. module.Dump('postgresql_databases', 'foo', 'host'),
  993. ).and_return(True)
  994. assert module.get_dumps_to_restore(
  995. restore_arguments=flexmock(
  996. hook='postgresql_databases',
  997. data_sources=['foo'],
  998. original_hostname='host',
  999. original_port=None,
  1000. original_label=None,
  1001. original_container=None,
  1002. ),
  1003. dumps_from_archive=dumps_from_archive,
  1004. ) == {
  1005. module.Dump('postgresql_databases', 'foo', 'host'),
  1006. }
  1007. def test_get_dumps_to_restore_with_requested_port_filters_dumps_found_in_archive():
  1008. dumps_from_archive = {
  1009. module.Dump('postgresql_databases', 'foo', 'host'),
  1010. module.Dump('postgresql_databases', 'foo', 'host', 1234),
  1011. module.Dump('postgresql_databases', 'bar'),
  1012. }
  1013. flexmock(module).should_receive('dumps_match').and_return(False)
  1014. flexmock(module).should_receive('dumps_match').with_args(
  1015. module.Dump(
  1016. 'postgresql_databases',
  1017. 'foo',
  1018. 'host',
  1019. 1234,
  1020. label=module.UNSPECIFIED,
  1021. container=module.UNSPECIFIED,
  1022. ),
  1023. module.Dump('postgresql_databases', 'foo', 'host', 1234),
  1024. ).and_return(True)
  1025. assert module.get_dumps_to_restore(
  1026. restore_arguments=flexmock(
  1027. hook='postgresql_databases',
  1028. data_sources=['foo'],
  1029. original_hostname='host',
  1030. original_port=1234,
  1031. original_label=None,
  1032. original_container=None,
  1033. ),
  1034. dumps_from_archive=dumps_from_archive,
  1035. ) == {
  1036. module.Dump('postgresql_databases', 'foo', 'host', 1234),
  1037. }
  1038. def test_ensure_requested_dumps_restored_with_all_dumps_restored_does_not_raise():
  1039. module.ensure_requested_dumps_restored(
  1040. dumps_to_restore={
  1041. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1042. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1043. },
  1044. dumps_actually_restored={
  1045. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1046. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1047. },
  1048. )
  1049. def test_ensure_requested_dumps_restored_with_no_dumps_raises():
  1050. with pytest.raises(ValueError):
  1051. module.ensure_requested_dumps_restored(
  1052. dumps_to_restore={},
  1053. dumps_actually_restored={},
  1054. )
  1055. def test_ensure_requested_dumps_restored_with_missing_dumps_raises():
  1056. flexmock(module).should_receive('render_dump_metadata').and_return('test')
  1057. with pytest.raises(ValueError):
  1058. module.ensure_requested_dumps_restored(
  1059. dumps_to_restore={
  1060. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1061. },
  1062. dumps_actually_restored={
  1063. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1064. },
  1065. )
  1066. def test_run_restore_restores_each_data_source():
  1067. dumps_to_restore = {
  1068. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1069. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1070. }
  1071. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  1072. borgmatic_runtime_directory = flexmock()
  1073. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1074. borgmatic_runtime_directory,
  1075. )
  1076. flexmock(module.borgmatic.config.paths).should_receive(
  1077. 'make_runtime_directory_glob',
  1078. ).replace_with(lambda path: path)
  1079. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1080. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1081. flexmock(),
  1082. )
  1083. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1084. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1085. flexmock(module).should_receive('get_configured_data_source').and_return(
  1086. {'name': 'foo'},
  1087. ).and_return({'name': 'bar'})
  1088. flexmock(module).should_receive('restore_single_dump').with_args(
  1089. repository=object,
  1090. config=object,
  1091. local_borg_version=object,
  1092. global_arguments=object,
  1093. local_path=object,
  1094. remote_path=object,
  1095. archive_name=object,
  1096. hook_name='postgresql_databases',
  1097. data_source={'name': 'foo', 'schemas': None},
  1098. connection_params=object,
  1099. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1100. ).once()
  1101. flexmock(module).should_receive('restore_single_dump').with_args(
  1102. repository=object,
  1103. config=object,
  1104. local_borg_version=object,
  1105. global_arguments=object,
  1106. local_path=object,
  1107. remote_path=object,
  1108. archive_name=object,
  1109. hook_name='postgresql_databases',
  1110. data_source={'name': 'bar', 'schemas': None},
  1111. connection_params=object,
  1112. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1113. ).once()
  1114. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1115. module.run_restore(
  1116. repository={'path': 'repo'},
  1117. config=flexmock(),
  1118. local_borg_version=flexmock(),
  1119. restore_arguments=flexmock(
  1120. repository='repo',
  1121. archive='archive',
  1122. data_sources=flexmock(),
  1123. schemas=None,
  1124. hostname=None,
  1125. port=None,
  1126. username=None,
  1127. password=None,
  1128. restore_path=None,
  1129. container=None,
  1130. ),
  1131. global_arguments=flexmock(dry_run=False),
  1132. local_path=flexmock(),
  1133. remote_path=flexmock(),
  1134. )
  1135. def test_run_restore_bails_for_non_matching_repository():
  1136. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(
  1137. False,
  1138. )
  1139. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1140. flexmock(),
  1141. )
  1142. flexmock(module.borgmatic.config.paths).should_receive(
  1143. 'make_runtime_directory_glob',
  1144. ).replace_with(lambda path: path)
  1145. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  1146. 'call_hooks_even_if_unconfigured',
  1147. ).never()
  1148. flexmock(module).should_receive('restore_single_dump').never()
  1149. module.run_restore(
  1150. repository={'path': 'repo'},
  1151. config=flexmock(),
  1152. local_borg_version=flexmock(),
  1153. restore_arguments=flexmock(repository='repo', archive='archive', data_sources=flexmock()),
  1154. global_arguments=flexmock(dry_run=False),
  1155. local_path=flexmock(),
  1156. remote_path=flexmock(),
  1157. )
  1158. def test_run_restore_restores_data_source_by_falling_back_to_all_name():
  1159. dumps_to_restore = {
  1160. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1161. }
  1162. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  1163. borgmatic_runtime_directory = flexmock()
  1164. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1165. borgmatic_runtime_directory,
  1166. )
  1167. flexmock(module.borgmatic.config.paths).should_receive(
  1168. 'make_runtime_directory_glob',
  1169. ).replace_with(lambda path: path)
  1170. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1171. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1172. flexmock(),
  1173. )
  1174. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1175. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1176. flexmock(module).should_receive('get_configured_data_source').and_return(
  1177. {'name': 'foo'},
  1178. ).and_return({'name': 'all'})
  1179. flexmock(module).should_receive('restore_single_dump').with_args(
  1180. repository=object,
  1181. config=object,
  1182. local_borg_version=object,
  1183. global_arguments=object,
  1184. local_path=object,
  1185. remote_path=object,
  1186. archive_name=object,
  1187. hook_name='postgresql_databases',
  1188. data_source={'name': 'foo', 'schemas': None},
  1189. connection_params=object,
  1190. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1191. ).once()
  1192. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1193. module.run_restore(
  1194. repository={'path': 'repo'},
  1195. config=flexmock(),
  1196. local_borg_version=flexmock(),
  1197. restore_arguments=flexmock(
  1198. repository='repo',
  1199. archive='archive',
  1200. data_sources=flexmock(),
  1201. schemas=None,
  1202. hostname=None,
  1203. port=None,
  1204. username=None,
  1205. password=None,
  1206. restore_path=None,
  1207. container=None,
  1208. ),
  1209. global_arguments=flexmock(dry_run=False),
  1210. local_path=flexmock(),
  1211. remote_path=flexmock(),
  1212. )
  1213. def test_run_restore_restores_data_source_configured_with_all_name():
  1214. dumps_to_restore = {
  1215. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1216. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1217. }
  1218. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  1219. borgmatic_runtime_directory = flexmock()
  1220. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1221. borgmatic_runtime_directory,
  1222. )
  1223. flexmock(module.borgmatic.config.paths).should_receive(
  1224. 'make_runtime_directory_glob',
  1225. ).replace_with(lambda path: path)
  1226. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1227. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1228. flexmock(),
  1229. )
  1230. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1231. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1232. flexmock(module).should_receive('get_configured_data_source').with_args(
  1233. config=object,
  1234. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1235. ).and_return({'name': 'foo'})
  1236. flexmock(module).should_receive('get_configured_data_source').with_args(
  1237. config=object,
  1238. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1239. ).and_return(None)
  1240. flexmock(module).should_receive('get_configured_data_source').with_args(
  1241. config=object,
  1242. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='all'),
  1243. ).and_return({'name': 'bar'})
  1244. flexmock(module).should_receive('restore_single_dump').with_args(
  1245. repository=object,
  1246. config=object,
  1247. local_borg_version=object,
  1248. global_arguments=object,
  1249. local_path=object,
  1250. remote_path=object,
  1251. archive_name=object,
  1252. hook_name='postgresql_databases',
  1253. data_source={'name': 'foo', 'schemas': None},
  1254. connection_params=object,
  1255. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1256. ).once()
  1257. flexmock(module).should_receive('restore_single_dump').with_args(
  1258. repository=object,
  1259. config=object,
  1260. local_borg_version=object,
  1261. global_arguments=object,
  1262. local_path=object,
  1263. remote_path=object,
  1264. archive_name=object,
  1265. hook_name='postgresql_databases',
  1266. data_source={'name': 'bar', 'schemas': None},
  1267. connection_params=object,
  1268. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1269. ).once()
  1270. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1271. module.run_restore(
  1272. repository={'path': 'repo'},
  1273. config=flexmock(),
  1274. local_borg_version=flexmock(),
  1275. restore_arguments=flexmock(
  1276. repository='repo',
  1277. archive='archive',
  1278. data_sources=flexmock(),
  1279. schemas=None,
  1280. hostname=None,
  1281. port=None,
  1282. username=None,
  1283. password=None,
  1284. restore_path=None,
  1285. container=None,
  1286. ),
  1287. global_arguments=flexmock(dry_run=False),
  1288. local_path=flexmock(),
  1289. remote_path=flexmock(),
  1290. )
  1291. def test_run_restore_skips_missing_data_source():
  1292. dumps_to_restore = {
  1293. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1294. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1295. }
  1296. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  1297. borgmatic_runtime_directory = flexmock()
  1298. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1299. borgmatic_runtime_directory,
  1300. )
  1301. flexmock(module.borgmatic.config.paths).should_receive(
  1302. 'make_runtime_directory_glob',
  1303. ).replace_with(lambda path: path)
  1304. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1305. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1306. flexmock(),
  1307. )
  1308. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1309. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1310. flexmock(module).should_receive('get_configured_data_source').with_args(
  1311. config=object,
  1312. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1313. ).and_return({'name': 'foo'})
  1314. flexmock(module).should_receive('get_configured_data_source').with_args(
  1315. config=object,
  1316. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1317. ).and_return(None)
  1318. flexmock(module).should_receive('get_configured_data_source').with_args(
  1319. config=object,
  1320. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='all'),
  1321. ).and_return(None)
  1322. flexmock(module).should_receive('restore_single_dump').with_args(
  1323. repository=object,
  1324. config=object,
  1325. local_borg_version=object,
  1326. global_arguments=object,
  1327. local_path=object,
  1328. remote_path=object,
  1329. archive_name=object,
  1330. hook_name='postgresql_databases',
  1331. data_source={'name': 'foo', 'schemas': None},
  1332. connection_params=object,
  1333. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1334. ).once()
  1335. flexmock(module).should_receive('restore_single_dump').with_args(
  1336. repository=object,
  1337. config=object,
  1338. local_borg_version=object,
  1339. global_arguments=object,
  1340. local_path=object,
  1341. remote_path=object,
  1342. archive_name=object,
  1343. hook_name='postgresql_databases',
  1344. data_source={'name': 'bar', 'schemas': None},
  1345. connection_params=object,
  1346. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1347. ).never()
  1348. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1349. module.run_restore(
  1350. repository={'path': 'repo'},
  1351. config=flexmock(),
  1352. local_borg_version=flexmock(),
  1353. restore_arguments=flexmock(
  1354. repository='repo',
  1355. archive='archive',
  1356. data_sources=flexmock(),
  1357. schemas=None,
  1358. hostname=None,
  1359. port=None,
  1360. username=None,
  1361. password=None,
  1362. restore_path=None,
  1363. container=None,
  1364. ),
  1365. global_arguments=flexmock(dry_run=False),
  1366. local_path=flexmock(),
  1367. remote_path=flexmock(),
  1368. )
  1369. def test_run_restore_restores_data_sources_from_different_hooks():
  1370. dumps_to_restore = {
  1371. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1372. module.Dump(hook_name='mysql_databases', data_source_name='foo'),
  1373. }
  1374. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  1375. borgmatic_runtime_directory = flexmock()
  1376. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1377. borgmatic_runtime_directory,
  1378. )
  1379. flexmock(module.borgmatic.config.paths).should_receive(
  1380. 'make_runtime_directory_glob',
  1381. ).replace_with(lambda path: path)
  1382. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1383. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1384. flexmock(),
  1385. )
  1386. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1387. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1388. flexmock(module).should_receive('get_configured_data_source').with_args(
  1389. config=object,
  1390. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1391. ).and_return({'name': 'foo'})
  1392. flexmock(module).should_receive('get_configured_data_source').with_args(
  1393. config=object,
  1394. restore_dump=module.Dump(hook_name='mysql_databases', data_source_name='foo'),
  1395. ).and_return({'name': 'bar'})
  1396. flexmock(module).should_receive('restore_single_dump').with_args(
  1397. repository=object,
  1398. config=object,
  1399. local_borg_version=object,
  1400. global_arguments=object,
  1401. local_path=object,
  1402. remote_path=object,
  1403. archive_name=object,
  1404. hook_name='postgresql_databases',
  1405. data_source={'name': 'foo', 'schemas': None},
  1406. connection_params=object,
  1407. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1408. ).once()
  1409. flexmock(module).should_receive('restore_single_dump').with_args(
  1410. repository=object,
  1411. config=object,
  1412. local_borg_version=object,
  1413. global_arguments=object,
  1414. local_path=object,
  1415. remote_path=object,
  1416. archive_name=object,
  1417. hook_name='mysql_databases',
  1418. data_source={'name': 'bar', 'schemas': None},
  1419. connection_params=object,
  1420. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1421. ).once()
  1422. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1423. module.run_restore(
  1424. repository={'path': 'repo'},
  1425. config=flexmock(),
  1426. local_borg_version=flexmock(),
  1427. restore_arguments=flexmock(
  1428. repository='repo',
  1429. archive='archive',
  1430. data_sources=flexmock(),
  1431. schemas=None,
  1432. hostname=None,
  1433. port=None,
  1434. username=None,
  1435. password=None,
  1436. restore_path=None,
  1437. container=None,
  1438. ),
  1439. global_arguments=flexmock(dry_run=False),
  1440. local_path=flexmock(),
  1441. remote_path=flexmock(),
  1442. )