test_restore.py 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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. borgmatic_runtime_directory = flexmock()
  1072. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1073. borgmatic_runtime_directory,
  1074. )
  1075. flexmock(module.borgmatic.config.paths).should_receive(
  1076. 'make_runtime_directory_glob',
  1077. ).replace_with(lambda path: path)
  1078. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1079. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  1080. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  1081. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1082. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1083. flexmock(),
  1084. )
  1085. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1086. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1087. flexmock(module).should_receive('get_configured_data_source').and_return(
  1088. {'name': 'foo'},
  1089. ).and_return({'name': 'bar'})
  1090. flexmock(module).should_receive('restore_single_dump').with_args(
  1091. repository=object,
  1092. config=object,
  1093. local_borg_version=object,
  1094. global_arguments=object,
  1095. local_path=object,
  1096. remote_path=object,
  1097. archive_name=object,
  1098. hook_name='postgresql_databases',
  1099. data_source={'name': 'foo', 'schemas': None},
  1100. connection_params=object,
  1101. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1102. ).once()
  1103. flexmock(module).should_receive('restore_single_dump').with_args(
  1104. repository=object,
  1105. config=object,
  1106. local_borg_version=object,
  1107. global_arguments=object,
  1108. local_path=object,
  1109. remote_path=object,
  1110. archive_name=object,
  1111. hook_name='postgresql_databases',
  1112. data_source={'name': 'bar', 'schemas': None},
  1113. connection_params=object,
  1114. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1115. ).once()
  1116. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1117. module.run_restore(
  1118. repository={'path': 'repo'},
  1119. config=flexmock(),
  1120. local_borg_version=flexmock(),
  1121. restore_arguments=flexmock(
  1122. repository='repo',
  1123. archive='archive',
  1124. data_sources=flexmock(),
  1125. schemas=None,
  1126. hostname=None,
  1127. port=None,
  1128. username=None,
  1129. password=None,
  1130. restore_path=None,
  1131. container=None,
  1132. ),
  1133. global_arguments=flexmock(dry_run=False),
  1134. local_path=flexmock(),
  1135. remote_path=flexmock(),
  1136. )
  1137. def test_run_restore_restores_data_source_by_falling_back_to_all_name():
  1138. dumps_to_restore = {
  1139. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1140. }
  1141. borgmatic_runtime_directory = flexmock()
  1142. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1143. borgmatic_runtime_directory,
  1144. )
  1145. flexmock(module.borgmatic.config.paths).should_receive(
  1146. 'make_runtime_directory_glob',
  1147. ).replace_with(lambda path: path)
  1148. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1149. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  1150. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  1151. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1152. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1153. flexmock(),
  1154. )
  1155. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1156. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1157. flexmock(module).should_receive('get_configured_data_source').and_return(
  1158. {'name': 'foo'},
  1159. ).and_return({'name': 'all'})
  1160. flexmock(module).should_receive('restore_single_dump').with_args(
  1161. repository=object,
  1162. config=object,
  1163. local_borg_version=object,
  1164. global_arguments=object,
  1165. local_path=object,
  1166. remote_path=object,
  1167. archive_name=object,
  1168. hook_name='postgresql_databases',
  1169. data_source={'name': 'foo', 'schemas': None},
  1170. connection_params=object,
  1171. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1172. ).once()
  1173. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1174. module.run_restore(
  1175. repository={'path': 'repo'},
  1176. config=flexmock(),
  1177. local_borg_version=flexmock(),
  1178. restore_arguments=flexmock(
  1179. repository='repo',
  1180. archive='archive',
  1181. data_sources=flexmock(),
  1182. schemas=None,
  1183. hostname=None,
  1184. port=None,
  1185. username=None,
  1186. password=None,
  1187. restore_path=None,
  1188. container=None,
  1189. ),
  1190. global_arguments=flexmock(dry_run=False),
  1191. local_path=flexmock(),
  1192. remote_path=flexmock(),
  1193. )
  1194. def test_run_restore_restores_data_source_configured_with_all_name():
  1195. dumps_to_restore = {
  1196. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1197. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1198. }
  1199. borgmatic_runtime_directory = flexmock()
  1200. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1201. borgmatic_runtime_directory,
  1202. )
  1203. flexmock(module.borgmatic.config.paths).should_receive(
  1204. 'make_runtime_directory_glob',
  1205. ).replace_with(lambda path: path)
  1206. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1207. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  1208. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  1209. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1210. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1211. flexmock(),
  1212. )
  1213. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1214. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1215. flexmock(module).should_receive('get_configured_data_source').with_args(
  1216. config=object,
  1217. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1218. ).and_return({'name': 'foo'})
  1219. flexmock(module).should_receive('get_configured_data_source').with_args(
  1220. config=object,
  1221. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1222. ).and_return(None)
  1223. flexmock(module).should_receive('get_configured_data_source').with_args(
  1224. config=object,
  1225. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='all'),
  1226. ).and_return({'name': 'bar'})
  1227. flexmock(module).should_receive('restore_single_dump').with_args(
  1228. repository=object,
  1229. config=object,
  1230. local_borg_version=object,
  1231. global_arguments=object,
  1232. local_path=object,
  1233. remote_path=object,
  1234. archive_name=object,
  1235. hook_name='postgresql_databases',
  1236. data_source={'name': 'foo', 'schemas': None},
  1237. connection_params=object,
  1238. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1239. ).once()
  1240. flexmock(module).should_receive('restore_single_dump').with_args(
  1241. repository=object,
  1242. config=object,
  1243. local_borg_version=object,
  1244. global_arguments=object,
  1245. local_path=object,
  1246. remote_path=object,
  1247. archive_name=object,
  1248. hook_name='postgresql_databases',
  1249. data_source={'name': 'bar', 'schemas': None},
  1250. connection_params=object,
  1251. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1252. ).once()
  1253. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1254. module.run_restore(
  1255. repository={'path': 'repo'},
  1256. config=flexmock(),
  1257. local_borg_version=flexmock(),
  1258. restore_arguments=flexmock(
  1259. repository='repo',
  1260. archive='archive',
  1261. data_sources=flexmock(),
  1262. schemas=None,
  1263. hostname=None,
  1264. port=None,
  1265. username=None,
  1266. password=None,
  1267. restore_path=None,
  1268. container=None,
  1269. ),
  1270. global_arguments=flexmock(dry_run=False),
  1271. local_path=flexmock(),
  1272. remote_path=flexmock(),
  1273. )
  1274. def test_run_restore_skips_missing_data_source():
  1275. dumps_to_restore = {
  1276. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1277. module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1278. }
  1279. borgmatic_runtime_directory = flexmock()
  1280. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1281. borgmatic_runtime_directory,
  1282. )
  1283. flexmock(module.borgmatic.config.paths).should_receive(
  1284. 'make_runtime_directory_glob',
  1285. ).replace_with(lambda path: path)
  1286. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1287. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  1288. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  1289. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1290. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1291. flexmock(),
  1292. )
  1293. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1294. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1295. flexmock(module).should_receive('get_configured_data_source').with_args(
  1296. config=object,
  1297. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1298. ).and_return({'name': 'foo'})
  1299. flexmock(module).should_receive('get_configured_data_source').with_args(
  1300. config=object,
  1301. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
  1302. ).and_return(None)
  1303. flexmock(module).should_receive('get_configured_data_source').with_args(
  1304. config=object,
  1305. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='all'),
  1306. ).and_return(None)
  1307. flexmock(module).should_receive('restore_single_dump').with_args(
  1308. repository=object,
  1309. config=object,
  1310. local_borg_version=object,
  1311. global_arguments=object,
  1312. local_path=object,
  1313. remote_path=object,
  1314. archive_name=object,
  1315. hook_name='postgresql_databases',
  1316. data_source={'name': 'foo', 'schemas': None},
  1317. connection_params=object,
  1318. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1319. ).once()
  1320. flexmock(module).should_receive('restore_single_dump').with_args(
  1321. repository=object,
  1322. config=object,
  1323. local_borg_version=object,
  1324. global_arguments=object,
  1325. local_path=object,
  1326. remote_path=object,
  1327. archive_name=object,
  1328. hook_name='postgresql_databases',
  1329. data_source={'name': 'bar', 'schemas': None},
  1330. connection_params=object,
  1331. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1332. ).never()
  1333. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1334. module.run_restore(
  1335. repository={'path': 'repo'},
  1336. config=flexmock(),
  1337. local_borg_version=flexmock(),
  1338. restore_arguments=flexmock(
  1339. repository='repo',
  1340. archive='archive',
  1341. data_sources=flexmock(),
  1342. schemas=None,
  1343. hostname=None,
  1344. port=None,
  1345. username=None,
  1346. password=None,
  1347. restore_path=None,
  1348. container=None,
  1349. ),
  1350. global_arguments=flexmock(dry_run=False),
  1351. local_path=flexmock(),
  1352. remote_path=flexmock(),
  1353. )
  1354. def test_run_restore_restores_data_sources_from_different_hooks():
  1355. dumps_to_restore = {
  1356. module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1357. module.Dump(hook_name='mysql_databases', data_source_name='foo'),
  1358. }
  1359. borgmatic_runtime_directory = flexmock()
  1360. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1361. borgmatic_runtime_directory,
  1362. )
  1363. flexmock(module.borgmatic.config.paths).should_receive(
  1364. 'make_runtime_directory_glob',
  1365. ).replace_with(lambda path: path)
  1366. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  1367. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  1368. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  1369. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  1370. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1371. flexmock(),
  1372. )
  1373. flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
  1374. flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
  1375. flexmock(module).should_receive('get_configured_data_source').with_args(
  1376. config=object,
  1377. restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
  1378. ).and_return({'name': 'foo'})
  1379. flexmock(module).should_receive('get_configured_data_source').with_args(
  1380. config=object,
  1381. restore_dump=module.Dump(hook_name='mysql_databases', data_source_name='foo'),
  1382. ).and_return({'name': 'bar'})
  1383. flexmock(module).should_receive('restore_single_dump').with_args(
  1384. repository=object,
  1385. config=object,
  1386. local_borg_version=object,
  1387. global_arguments=object,
  1388. local_path=object,
  1389. remote_path=object,
  1390. archive_name=object,
  1391. hook_name='postgresql_databases',
  1392. data_source={'name': 'foo', 'schemas': None},
  1393. connection_params=object,
  1394. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1395. ).once()
  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='mysql_databases',
  1405. data_source={'name': 'bar', 'schemas': None},
  1406. connection_params=object,
  1407. borgmatic_runtime_directory=borgmatic_runtime_directory,
  1408. ).once()
  1409. flexmock(module).should_receive('ensure_requested_dumps_restored')
  1410. module.run_restore(
  1411. repository={'path': 'repo'},
  1412. config=flexmock(),
  1413. local_borg_version=flexmock(),
  1414. restore_arguments=flexmock(
  1415. repository='repo',
  1416. archive='archive',
  1417. data_sources=flexmock(),
  1418. schemas=None,
  1419. hostname=None,
  1420. port=None,
  1421. username=None,
  1422. password=None,
  1423. restore_path=None,
  1424. container=None,
  1425. ),
  1426. global_arguments=flexmock(dry_run=False),
  1427. local_path=flexmock(),
  1428. remote_path=flexmock(),
  1429. )