test_restore.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. import pytest
  2. from flexmock import flexmock
  3. import borgmatic.actions.restore as module
  4. def test_get_configured_data_source_matches_data_source_by_name():
  5. assert module.get_configured_data_source(
  6. config={
  7. 'other_databases': [{'name': 'other'}],
  8. 'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
  9. },
  10. archive_data_source_names={'postgresql_databases': ['other', 'foo', 'bar']},
  11. hook_name='postgresql_databases',
  12. data_source_name='bar',
  13. ) == ('postgresql_databases', {'name': 'bar'})
  14. def test_get_configured_data_source_matches_nothing_when_nothing_configured():
  15. assert module.get_configured_data_source(
  16. config={},
  17. archive_data_source_names={'postgresql_databases': ['foo']},
  18. hook_name='postgresql_databases',
  19. data_source_name='quux',
  20. ) == (None, None)
  21. def test_get_configured_data_source_matches_nothing_when_data_source_name_not_configured():
  22. assert module.get_configured_data_source(
  23. config={'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]},
  24. archive_data_source_names={'postgresql_databases': ['foo']},
  25. hook_name='postgresql_databases',
  26. data_source_name='quux',
  27. ) == (None, None)
  28. def test_get_configured_data_source_matches_nothing_when_data_source_name_not_in_archive():
  29. assert module.get_configured_data_source(
  30. config={'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]},
  31. archive_data_source_names={'postgresql_databases': ['bar']},
  32. hook_name='postgresql_databases',
  33. data_source_name='foo',
  34. ) == (None, None)
  35. def test_get_configured_data_source_matches_data_source_by_configuration_data_source_name():
  36. assert module.get_configured_data_source(
  37. config={'postgresql_databases': [{'name': 'all'}, {'name': 'bar'}]},
  38. archive_data_source_names={'postgresql_databases': ['foo']},
  39. hook_name='postgresql_databases',
  40. data_source_name='foo',
  41. configuration_data_source_name='all',
  42. ) == ('postgresql_databases', {'name': 'all'})
  43. def test_get_configured_data_source_with_unspecified_hook_matches_data_source_by_name():
  44. assert module.get_configured_data_source(
  45. config={
  46. 'other_databases': [{'name': 'other'}],
  47. 'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
  48. },
  49. archive_data_source_names={'postgresql_databases': ['other', 'foo', 'bar']},
  50. hook_name=module.UNSPECIFIED_HOOK,
  51. data_source_name='bar',
  52. ) == ('postgresql_databases', {'name': 'bar'})
  53. def test_strip_path_prefix_from_extracted_dump_destination_renames_first_matching_databases_subdirectory():
  54. flexmock(module.os).should_receive('walk').and_return(
  55. [
  56. ('/foo', flexmock(), flexmock()),
  57. ('/foo/bar', flexmock(), flexmock()),
  58. ('/foo/bar/postgresql_databases', flexmock(), flexmock()),
  59. ('/foo/bar/mariadb_databases', flexmock(), flexmock()),
  60. ]
  61. )
  62. flexmock(module.shutil).should_receive('move').with_args(
  63. '/foo/bar/postgresql_databases', '/run/user/0/borgmatic/postgresql_databases'
  64. ).once()
  65. flexmock(module.shutil).should_receive('move').with_args(
  66. '/foo/bar/mariadb_databases', '/run/user/0/borgmatic/mariadb_databases'
  67. ).never()
  68. module.strip_path_prefix_from_extracted_dump_destination('/foo', '/run/user/0/borgmatic')
  69. def test_restore_single_data_source_extracts_and_restores_single_file_dump():
  70. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  71. 'make_data_source_dump_patterns', object, object, object, object
  72. ).and_return({'postgresql': flexmock()})
  73. flexmock(module.borgmatic.config.paths).should_receive(
  74. 'get_borgmatic_runtime_directory'
  75. ).and_return('/run/user/0/borgmatic')
  76. flexmock(module.tempfile).should_receive('mkdtemp').never()
  77. flexmock(module.borgmatic.hooks.dump).should_receive(
  78. 'convert_glob_patterns_to_borg_pattern'
  79. ).and_return(flexmock())
  80. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  81. flexmock()
  82. ).once()
  83. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
  84. flexmock(module.shutil).should_receive('rmtree').never()
  85. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  86. function_name='restore_data_source_dump',
  87. config=object,
  88. log_prefix=object,
  89. hook_names=object,
  90. data_source=object,
  91. dry_run=object,
  92. extract_process=object,
  93. connection_params=object,
  94. ).once()
  95. module.restore_single_data_source(
  96. repository={'path': 'test.borg'},
  97. config=flexmock(),
  98. local_borg_version=flexmock(),
  99. global_arguments=flexmock(dry_run=False),
  100. local_path=None,
  101. remote_path=None,
  102. archive_name=flexmock(),
  103. hook_name='postgresql',
  104. data_source={'name': 'test', 'format': 'plain'},
  105. connection_params=flexmock(),
  106. )
  107. def test_restore_single_data_source_extracts_and_restores_directory_dump():
  108. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  109. 'make_data_source_dump_patterns', object, object, object, object
  110. ).and_return({'postgresql': flexmock()})
  111. flexmock(module.borgmatic.config.paths).should_receive(
  112. 'get_borgmatic_runtime_directory'
  113. ).and_return('/run/user/0/borgmatic')
  114. flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
  115. '/run/user/0/borgmatic/tmp1234'
  116. )
  117. flexmock(module.borgmatic.hooks.dump).should_receive(
  118. 'convert_glob_patterns_to_borg_pattern'
  119. ).and_return(flexmock())
  120. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  121. flexmock()
  122. ).once()
  123. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').once()
  124. flexmock(module.shutil).should_receive('rmtree').once()
  125. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  126. function_name='restore_data_source_dump',
  127. config=object,
  128. log_prefix=object,
  129. hook_names=object,
  130. data_source=object,
  131. dry_run=object,
  132. extract_process=object,
  133. connection_params=object,
  134. ).once()
  135. module.restore_single_data_source(
  136. repository={'path': 'test.borg'},
  137. config=flexmock(),
  138. local_borg_version=flexmock(),
  139. global_arguments=flexmock(dry_run=False),
  140. local_path=None,
  141. remote_path=None,
  142. archive_name=flexmock(),
  143. hook_name='postgresql',
  144. data_source={'name': 'test', 'format': 'directory'},
  145. connection_params=flexmock(),
  146. )
  147. def test_restore_single_data_source_with_directory_dump_error_cleans_up_temporary_directory():
  148. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  149. 'make_data_source_dump_patterns', object, object, object, object
  150. ).and_return({'postgresql': flexmock()})
  151. flexmock(module.borgmatic.config.paths).should_receive(
  152. 'get_borgmatic_runtime_directory'
  153. ).and_return('/run/user/0/borgmatic')
  154. flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
  155. '/run/user/0/borgmatic/tmp1234'
  156. )
  157. flexmock(module.borgmatic.hooks.dump).should_receive(
  158. 'convert_glob_patterns_to_borg_pattern'
  159. ).and_return(flexmock())
  160. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_raise(
  161. ValueError
  162. ).once()
  163. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
  164. flexmock(module.shutil).should_receive('rmtree').once()
  165. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  166. function_name='restore_data_source_dump',
  167. config=object,
  168. log_prefix=object,
  169. hook_names=object,
  170. data_source=object,
  171. dry_run=object,
  172. extract_process=object,
  173. connection_params=object,
  174. ).never()
  175. with pytest.raises(ValueError):
  176. module.restore_single_data_source(
  177. repository={'path': 'test.borg'},
  178. config=flexmock(),
  179. local_borg_version=flexmock(),
  180. global_arguments=flexmock(dry_run=False),
  181. local_path=None,
  182. remote_path=None,
  183. archive_name=flexmock(),
  184. hook_name='postgresql',
  185. data_source={'name': 'test', 'format': 'directory'},
  186. connection_params=flexmock(),
  187. )
  188. def test_restore_single_data_source_with_directory_dump_and_dry_run_skips_directory_move_and_cleanup():
  189. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  190. 'make_data_source_dump_patterns', object, object, object, object
  191. ).and_return({'postgresql': flexmock()})
  192. flexmock(module.borgmatic.config.paths).should_receive(
  193. 'get_borgmatic_runtime_directory'
  194. ).and_return('/run/user/0/borgmatic')
  195. flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
  196. '/run/user/0/borgmatic/tmp1234'
  197. )
  198. flexmock(module.borgmatic.hooks.dump).should_receive(
  199. 'convert_glob_patterns_to_borg_pattern'
  200. ).and_return(flexmock())
  201. flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
  202. flexmock()
  203. ).once()
  204. flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
  205. flexmock(module.shutil).should_receive('rmtree').never()
  206. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
  207. function_name='restore_data_source_dump',
  208. config=object,
  209. log_prefix=object,
  210. hook_names=object,
  211. data_source=object,
  212. dry_run=object,
  213. extract_process=object,
  214. connection_params=object,
  215. ).once()
  216. module.restore_single_data_source(
  217. repository={'path': 'test.borg'},
  218. config=flexmock(),
  219. local_borg_version=flexmock(),
  220. global_arguments=flexmock(dry_run=True),
  221. local_path=None,
  222. remote_path=None,
  223. archive_name=flexmock(),
  224. hook_name='postgresql',
  225. data_source={'name': 'test', 'format': 'directory'},
  226. connection_params=flexmock(),
  227. )
  228. def test_collect_archive_data_source_names_parses_archive_paths():
  229. flexmock(module.borgmatic.config.paths).should_receive(
  230. 'get_borgmatic_source_directory'
  231. ).and_return('/root/.borgmatic')
  232. flexmock(module.borgmatic.config.paths).should_receive(
  233. 'get_borgmatic_runtime_directory'
  234. ).and_return('/run/user/0/borgmatic')
  235. flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
  236. ''
  237. )
  238. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  239. [
  240. 'borgmatic/postgresql_databases/localhost/foo',
  241. 'borgmatic/postgresql_databases/localhost/bar',
  242. 'borgmatic/mysql_databases/localhost/quux',
  243. ]
  244. )
  245. archive_data_source_names = module.collect_archive_data_source_names(
  246. repository={'path': 'repo'},
  247. archive='archive',
  248. config={},
  249. local_borg_version=flexmock(),
  250. global_arguments=flexmock(log_json=False),
  251. local_path=flexmock(),
  252. remote_path=flexmock(),
  253. )
  254. assert archive_data_source_names == {
  255. 'postgresql_databases': ['foo', 'bar'],
  256. 'mysql_databases': ['quux'],
  257. }
  258. def test_collect_archive_data_source_names_parses_archive_paths_with_different_base_directories():
  259. flexmock(module.borgmatic.config.paths).should_receive(
  260. 'get_borgmatic_source_directory'
  261. ).and_return('/root/.borgmatic')
  262. flexmock(module.borgmatic.config.paths).should_receive(
  263. 'get_borgmatic_runtime_directory'
  264. ).and_return('/run/user/0/borgmatic')
  265. flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
  266. ''
  267. )
  268. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  269. [
  270. 'borgmatic/postgresql_databases/localhost/foo',
  271. '.borgmatic/postgresql_databases/localhost/bar',
  272. '/root/.borgmatic/postgresql_databases/localhost/baz',
  273. '/var/run/0/borgmatic/mysql_databases/localhost/quux',
  274. ]
  275. )
  276. archive_data_source_names = module.collect_archive_data_source_names(
  277. repository={'path': 'repo'},
  278. archive='archive',
  279. config={},
  280. local_borg_version=flexmock(),
  281. global_arguments=flexmock(log_json=False),
  282. local_path=flexmock(),
  283. remote_path=flexmock(),
  284. )
  285. assert archive_data_source_names == {
  286. 'postgresql_databases': ['foo', 'bar', 'baz'],
  287. 'mysql_databases': ['quux'],
  288. }
  289. def test_collect_archive_data_source_names_parses_directory_format_archive_paths():
  290. flexmock(module.borgmatic.config.paths).should_receive(
  291. 'get_borgmatic_source_directory'
  292. ).and_return('/root/.borgmatic')
  293. flexmock(module.borgmatic.config.paths).should_receive(
  294. 'get_borgmatic_runtime_directory'
  295. ).and_return('/run/user/0/borgmatic')
  296. flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
  297. ''
  298. )
  299. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  300. [
  301. 'borgmatic/postgresql_databases/localhost/foo/table1',
  302. 'borgmatic/postgresql_databases/localhost/foo/table2',
  303. ]
  304. )
  305. archive_data_source_names = module.collect_archive_data_source_names(
  306. repository={'path': 'repo'},
  307. archive='archive',
  308. config={},
  309. local_borg_version=flexmock(),
  310. global_arguments=flexmock(log_json=False),
  311. local_path=flexmock(),
  312. remote_path=flexmock(),
  313. )
  314. assert archive_data_source_names == {
  315. 'postgresql_databases': ['foo'],
  316. }
  317. def test_collect_archive_data_source_names_skips_bad_archive_paths():
  318. flexmock(module.borgmatic.config.paths).should_receive(
  319. 'get_borgmatic_source_directory'
  320. ).and_return('/root/.borgmatic')
  321. flexmock(module.borgmatic.config.paths).should_receive(
  322. 'get_borgmatic_runtime_directory'
  323. ).and_return('/run/user/0/borgmatic')
  324. flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
  325. ''
  326. )
  327. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  328. [
  329. 'borgmatic/postgresql_databases/localhost/foo',
  330. 'borgmatic/invalid',
  331. 'invalid/as/well',
  332. '',
  333. ]
  334. )
  335. archive_data_source_names = module.collect_archive_data_source_names(
  336. repository={'path': 'repo'},
  337. archive='archive',
  338. config={},
  339. local_borg_version=flexmock(),
  340. global_arguments=flexmock(log_json=False),
  341. local_path=flexmock(),
  342. remote_path=flexmock(),
  343. )
  344. assert archive_data_source_names == {
  345. 'postgresql_databases': ['foo'],
  346. }
  347. def test_find_data_sources_to_restore_passes_through_requested_names_found_in_archive():
  348. restore_names = module.find_data_sources_to_restore(
  349. requested_data_source_names=['foo', 'bar'],
  350. archive_data_source_names={'postresql_databases': ['foo', 'bar', 'baz']},
  351. )
  352. assert restore_names == {module.UNSPECIFIED_HOOK: ['foo', 'bar']}
  353. def test_find_data_sources_to_restore_raises_for_requested_names_missing_from_archive():
  354. with pytest.raises(ValueError):
  355. module.find_data_sources_to_restore(
  356. requested_data_source_names=['foo', 'bar'],
  357. archive_data_source_names={'postresql_databases': ['foo']},
  358. )
  359. def test_find_data_sources_to_restore_without_requested_names_finds_all_archive_data_sources():
  360. archive_data_source_names = {'postresql_databases': ['foo', 'bar']}
  361. restore_names = module.find_data_sources_to_restore(
  362. requested_data_source_names=[],
  363. archive_data_source_names=archive_data_source_names,
  364. )
  365. assert restore_names == archive_data_source_names
  366. def test_find_data_sources_to_restore_with_all_in_requested_names_finds_all_archive_data_sources():
  367. archive_data_source_names = {'postresql_databases': ['foo', 'bar']}
  368. restore_names = module.find_data_sources_to_restore(
  369. requested_data_source_names=['all'],
  370. archive_data_source_names=archive_data_source_names,
  371. )
  372. assert restore_names == archive_data_source_names
  373. def test_find_data_sources_to_restore_with_all_in_requested_names_plus_additional_requested_names_omits_duplicates():
  374. archive_data_source_names = {'postresql_databases': ['foo', 'bar']}
  375. restore_names = module.find_data_sources_to_restore(
  376. requested_data_source_names=['all', 'foo', 'bar'],
  377. archive_data_source_names=archive_data_source_names,
  378. )
  379. assert restore_names == archive_data_source_names
  380. def test_find_data_sources_to_restore_raises_for_all_in_requested_names_and_requested_named_missing_from_archives():
  381. with pytest.raises(ValueError):
  382. module.find_data_sources_to_restore(
  383. requested_data_source_names=['all', 'foo', 'bar'],
  384. archive_data_source_names={'postresql_databases': ['foo']},
  385. )
  386. def test_ensure_data_sources_found_with_all_data_sources_found_does_not_raise():
  387. module.ensure_data_sources_found(
  388. restore_names={'postgresql_databases': ['foo']},
  389. remaining_restore_names={'postgresql_databases': ['bar']},
  390. found_names=['foo', 'bar'],
  391. )
  392. def test_ensure_data_sources_found_with_no_data_sources_raises():
  393. with pytest.raises(ValueError):
  394. module.ensure_data_sources_found(
  395. restore_names={'postgresql_databases': []},
  396. remaining_restore_names={},
  397. found_names=[],
  398. )
  399. def test_ensure_data_sources_found_with_missing_data_sources_raises():
  400. with pytest.raises(ValueError):
  401. module.ensure_data_sources_found(
  402. restore_names={'postgresql_databases': ['foo']},
  403. remaining_restore_names={'postgresql_databases': ['bar']},
  404. found_names=['foo'],
  405. )
  406. def test_run_restore_restores_each_data_source():
  407. restore_names = {
  408. 'postgresql_databases': ['foo', 'bar'],
  409. }
  410. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  411. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  412. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  413. flexmock()
  414. )
  415. flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
  416. flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
  417. flexmock(module).should_receive('get_configured_data_source').and_return(
  418. ('postgresql_databases', {'name': 'foo'})
  419. ).and_return(('postgresql_databases', {'name': 'bar'}))
  420. flexmock(module).should_receive('restore_single_data_source').with_args(
  421. repository=object,
  422. config=object,
  423. local_borg_version=object,
  424. global_arguments=object,
  425. local_path=object,
  426. remote_path=object,
  427. archive_name=object,
  428. hook_name='postgresql_databases',
  429. data_source={'name': 'foo', 'schemas': None},
  430. connection_params=object,
  431. ).once()
  432. flexmock(module).should_receive('restore_single_data_source').with_args(
  433. repository=object,
  434. config=object,
  435. local_borg_version=object,
  436. global_arguments=object,
  437. local_path=object,
  438. remote_path=object,
  439. archive_name=object,
  440. hook_name='postgresql_databases',
  441. data_source={'name': 'bar', 'schemas': None},
  442. connection_params=object,
  443. ).once()
  444. flexmock(module).should_receive('ensure_data_sources_found')
  445. module.run_restore(
  446. repository={'path': 'repo'},
  447. config=flexmock(),
  448. local_borg_version=flexmock(),
  449. restore_arguments=flexmock(
  450. repository='repo',
  451. archive='archive',
  452. data_sources=flexmock(),
  453. schemas=None,
  454. hostname=None,
  455. port=None,
  456. username=None,
  457. password=None,
  458. restore_path=None,
  459. ),
  460. global_arguments=flexmock(dry_run=False),
  461. local_path=flexmock(),
  462. remote_path=flexmock(),
  463. )
  464. def test_run_restore_bails_for_non_matching_repository():
  465. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(
  466. False
  467. )
  468. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  469. 'call_hooks_even_if_unconfigured'
  470. ).never()
  471. flexmock(module).should_receive('restore_single_data_source').never()
  472. module.run_restore(
  473. repository={'path': 'repo'},
  474. config=flexmock(),
  475. local_borg_version=flexmock(),
  476. restore_arguments=flexmock(repository='repo', archive='archive', data_sources=flexmock()),
  477. global_arguments=flexmock(dry_run=False),
  478. local_path=flexmock(),
  479. remote_path=flexmock(),
  480. )
  481. def test_run_restore_restores_data_source_configured_with_all_name():
  482. restore_names = {
  483. 'postgresql_databases': ['foo', 'bar'],
  484. }
  485. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  486. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  487. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  488. flexmock()
  489. )
  490. flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
  491. flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
  492. flexmock(module).should_receive('get_configured_data_source').with_args(
  493. config=object,
  494. archive_data_source_names=object,
  495. hook_name='postgresql_databases',
  496. data_source_name='foo',
  497. ).and_return(('postgresql_databases', {'name': 'foo'}))
  498. flexmock(module).should_receive('get_configured_data_source').with_args(
  499. config=object,
  500. archive_data_source_names=object,
  501. hook_name='postgresql_databases',
  502. data_source_name='bar',
  503. ).and_return((None, None))
  504. flexmock(module).should_receive('get_configured_data_source').with_args(
  505. config=object,
  506. archive_data_source_names=object,
  507. hook_name='postgresql_databases',
  508. data_source_name='bar',
  509. configuration_data_source_name='all',
  510. ).and_return(('postgresql_databases', {'name': 'bar'}))
  511. flexmock(module).should_receive('restore_single_data_source').with_args(
  512. repository=object,
  513. config=object,
  514. local_borg_version=object,
  515. global_arguments=object,
  516. local_path=object,
  517. remote_path=object,
  518. archive_name=object,
  519. hook_name='postgresql_databases',
  520. data_source={'name': 'foo', 'schemas': None},
  521. connection_params=object,
  522. ).once()
  523. flexmock(module).should_receive('restore_single_data_source').with_args(
  524. repository=object,
  525. config=object,
  526. local_borg_version=object,
  527. global_arguments=object,
  528. local_path=object,
  529. remote_path=object,
  530. archive_name=object,
  531. hook_name='postgresql_databases',
  532. data_source={'name': 'bar', 'schemas': None},
  533. connection_params=object,
  534. ).once()
  535. flexmock(module).should_receive('ensure_data_sources_found')
  536. module.run_restore(
  537. repository={'path': 'repo'},
  538. config=flexmock(),
  539. local_borg_version=flexmock(),
  540. restore_arguments=flexmock(
  541. repository='repo',
  542. archive='archive',
  543. data_sources=flexmock(),
  544. schemas=None,
  545. hostname=None,
  546. port=None,
  547. username=None,
  548. password=None,
  549. restore_path=None,
  550. ),
  551. global_arguments=flexmock(dry_run=False),
  552. local_path=flexmock(),
  553. remote_path=flexmock(),
  554. )
  555. def test_run_restore_skips_missing_data_source():
  556. restore_names = {
  557. 'postgresql_databases': ['foo', 'bar'],
  558. }
  559. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  560. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  561. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  562. flexmock()
  563. )
  564. flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
  565. flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
  566. flexmock(module).should_receive('get_configured_data_source').with_args(
  567. config=object,
  568. archive_data_source_names=object,
  569. hook_name='postgresql_databases',
  570. data_source_name='foo',
  571. ).and_return(('postgresql_databases', {'name': 'foo'}))
  572. flexmock(module).should_receive('get_configured_data_source').with_args(
  573. config=object,
  574. archive_data_source_names=object,
  575. hook_name='postgresql_databases',
  576. data_source_name='bar',
  577. ).and_return((None, None))
  578. flexmock(module).should_receive('get_configured_data_source').with_args(
  579. config=object,
  580. archive_data_source_names=object,
  581. hook_name='postgresql_databases',
  582. data_source_name='bar',
  583. configuration_data_source_name='all',
  584. ).and_return((None, None))
  585. flexmock(module).should_receive('restore_single_data_source').with_args(
  586. repository=object,
  587. config=object,
  588. local_borg_version=object,
  589. global_arguments=object,
  590. local_path=object,
  591. remote_path=object,
  592. archive_name=object,
  593. hook_name='postgresql_databases',
  594. data_source={'name': 'foo', 'schemas': None},
  595. connection_params=object,
  596. ).once()
  597. flexmock(module).should_receive('restore_single_data_source').with_args(
  598. repository=object,
  599. config=object,
  600. local_borg_version=object,
  601. global_arguments=object,
  602. local_path=object,
  603. remote_path=object,
  604. archive_name=object,
  605. hook_name='postgresql_databases',
  606. data_source={'name': 'bar', 'schemas': None},
  607. connection_params=object,
  608. ).never()
  609. flexmock(module).should_receive('ensure_data_sources_found')
  610. module.run_restore(
  611. repository={'path': 'repo'},
  612. config=flexmock(),
  613. local_borg_version=flexmock(),
  614. restore_arguments=flexmock(
  615. repository='repo',
  616. archive='archive',
  617. data_sources=flexmock(),
  618. schemas=None,
  619. hostname=None,
  620. port=None,
  621. username=None,
  622. password=None,
  623. restore_path=None,
  624. ),
  625. global_arguments=flexmock(dry_run=False),
  626. local_path=flexmock(),
  627. remote_path=flexmock(),
  628. )
  629. def test_run_restore_restores_data_sources_from_different_hooks():
  630. restore_names = {
  631. 'postgresql_databases': ['foo'],
  632. 'mysql_databases': ['bar'],
  633. }
  634. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  635. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  636. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  637. flexmock()
  638. )
  639. flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
  640. flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
  641. flexmock(module).should_receive('get_configured_data_source').with_args(
  642. config=object,
  643. archive_data_source_names=object,
  644. hook_name='postgresql_databases',
  645. data_source_name='foo',
  646. ).and_return(('postgresql_databases', {'name': 'foo'}))
  647. flexmock(module).should_receive('get_configured_data_source').with_args(
  648. config=object,
  649. archive_data_source_names=object,
  650. hook_name='mysql_databases',
  651. data_source_name='bar',
  652. ).and_return(('mysql_databases', {'name': 'bar'}))
  653. flexmock(module).should_receive('restore_single_data_source').with_args(
  654. repository=object,
  655. config=object,
  656. local_borg_version=object,
  657. global_arguments=object,
  658. local_path=object,
  659. remote_path=object,
  660. archive_name=object,
  661. hook_name='postgresql_databases',
  662. data_source={'name': 'foo', 'schemas': None},
  663. connection_params=object,
  664. ).once()
  665. flexmock(module).should_receive('restore_single_data_source').with_args(
  666. repository=object,
  667. config=object,
  668. local_borg_version=object,
  669. global_arguments=object,
  670. local_path=object,
  671. remote_path=object,
  672. archive_name=object,
  673. hook_name='mysql_databases',
  674. data_source={'name': 'bar', 'schemas': None},
  675. connection_params=object,
  676. ).once()
  677. flexmock(module).should_receive('ensure_data_sources_found')
  678. module.run_restore(
  679. repository={'path': 'repo'},
  680. config=flexmock(),
  681. local_borg_version=flexmock(),
  682. restore_arguments=flexmock(
  683. repository='repo',
  684. archive='archive',
  685. data_sources=flexmock(),
  686. schemas=None,
  687. hostname=None,
  688. port=None,
  689. username=None,
  690. password=None,
  691. restore_path=None,
  692. ),
  693. global_arguments=flexmock(dry_run=False),
  694. local_path=flexmock(),
  695. remote_path=flexmock(),
  696. )