test_restore.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import pytest
  2. from flexmock import flexmock
  3. import borgmatic.actions.restore as module
  4. def test_get_configured_database_matches_database_by_name():
  5. assert module.get_configured_database(
  6. config={
  7. 'other_databases': [{'name': 'other'}],
  8. 'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
  9. },
  10. archive_database_names={'postgresql_databases': ['other', 'foo', 'bar']},
  11. hook_name='postgresql_databases',
  12. database_name='bar',
  13. ) == ('postgresql_databases', {'name': 'bar'})
  14. def test_get_configured_database_matches_nothing_when_database_name_not_configured():
  15. assert module.get_configured_database(
  16. config={'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]},
  17. archive_database_names={'postgresql_databases': ['foo']},
  18. hook_name='postgresql_databases',
  19. database_name='quux',
  20. ) == (None, None)
  21. def test_get_configured_database_matches_nothing_when_database_name_not_in_archive():
  22. assert module.get_configured_database(
  23. config={'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]},
  24. archive_database_names={'postgresql_databases': ['bar']},
  25. hook_name='postgresql_databases',
  26. database_name='foo',
  27. ) == (None, None)
  28. def test_get_configured_database_matches_database_by_configuration_database_name():
  29. assert module.get_configured_database(
  30. config={'postgresql_databases': [{'name': 'all'}, {'name': 'bar'}]},
  31. archive_database_names={'postgresql_databases': ['foo']},
  32. hook_name='postgresql_databases',
  33. database_name='foo',
  34. configuration_database_name='all',
  35. ) == ('postgresql_databases', {'name': 'all'})
  36. def test_get_configured_database_with_unspecified_hook_matches_database_by_name():
  37. assert module.get_configured_database(
  38. config={
  39. 'other_databases': [{'name': 'other'}],
  40. 'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
  41. },
  42. archive_database_names={'postgresql_databases': ['other', 'foo', 'bar']},
  43. hook_name=module.UNSPECIFIED_HOOK,
  44. database_name='bar',
  45. ) == ('postgresql_databases', {'name': 'bar'})
  46. def test_collect_archive_database_names_parses_archive_paths():
  47. flexmock(module.borgmatic.hooks.dump).should_receive('make_database_dump_path').and_return('')
  48. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  49. [
  50. '.borgmatic/postgresql_databases/localhost/foo',
  51. '.borgmatic/postgresql_databases/localhost/bar',
  52. '.borgmatic/mysql_databases/localhost/quux',
  53. ]
  54. )
  55. archive_database_names = module.collect_archive_database_names(
  56. repository={'path': 'repo'},
  57. archive='archive',
  58. config={'borgmatic_source_directory': '.borgmatic'},
  59. local_borg_version=flexmock(),
  60. global_arguments=flexmock(log_json=False),
  61. local_path=flexmock(),
  62. remote_path=flexmock(),
  63. )
  64. assert archive_database_names == {
  65. 'postgresql_databases': ['foo', 'bar'],
  66. 'mysql_databases': ['quux'],
  67. }
  68. def test_collect_archive_database_names_parses_directory_format_archive_paths():
  69. flexmock(module.borgmatic.hooks.dump).should_receive('make_database_dump_path').and_return('')
  70. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  71. [
  72. '.borgmatic/postgresql_databases/localhost/foo/table1',
  73. '.borgmatic/postgresql_databases/localhost/foo/table2',
  74. ]
  75. )
  76. archive_database_names = module.collect_archive_database_names(
  77. repository={'path': 'repo'},
  78. archive='archive',
  79. config={'borgmatic_source_directory': '.borgmatic'},
  80. local_borg_version=flexmock(),
  81. global_arguments=flexmock(log_json=False),
  82. local_path=flexmock(),
  83. remote_path=flexmock(),
  84. )
  85. assert archive_database_names == {
  86. 'postgresql_databases': ['foo'],
  87. }
  88. def test_collect_archive_database_names_skips_bad_archive_paths():
  89. flexmock(module.borgmatic.hooks.dump).should_receive('make_database_dump_path').and_return('')
  90. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  91. ['.borgmatic/postgresql_databases/localhost/foo', '.borgmatic/invalid', 'invalid/as/well']
  92. )
  93. archive_database_names = module.collect_archive_database_names(
  94. repository={'path': 'repo'},
  95. archive='archive',
  96. config={'borgmatic_source_directory': '.borgmatic'},
  97. local_borg_version=flexmock(),
  98. global_arguments=flexmock(log_json=False),
  99. local_path=flexmock(),
  100. remote_path=flexmock(),
  101. )
  102. assert archive_database_names == {
  103. 'postgresql_databases': ['foo'],
  104. }
  105. def test_find_databases_to_restore_passes_through_requested_names_found_in_archive():
  106. restore_names = module.find_databases_to_restore(
  107. requested_database_names=['foo', 'bar'],
  108. archive_database_names={'postresql_databases': ['foo', 'bar', 'baz']},
  109. )
  110. assert restore_names == {module.UNSPECIFIED_HOOK: ['foo', 'bar']}
  111. def test_find_databases_to_restore_raises_for_requested_names_missing_from_archive():
  112. with pytest.raises(ValueError):
  113. module.find_databases_to_restore(
  114. requested_database_names=['foo', 'bar'],
  115. archive_database_names={'postresql_databases': ['foo']},
  116. )
  117. def test_find_databases_to_restore_without_requested_names_finds_all_archive_databases():
  118. archive_database_names = {'postresql_databases': ['foo', 'bar']}
  119. restore_names = module.find_databases_to_restore(
  120. requested_database_names=[],
  121. archive_database_names=archive_database_names,
  122. )
  123. assert restore_names == archive_database_names
  124. def test_find_databases_to_restore_with_all_in_requested_names_finds_all_archive_databases():
  125. archive_database_names = {'postresql_databases': ['foo', 'bar']}
  126. restore_names = module.find_databases_to_restore(
  127. requested_database_names=['all'],
  128. archive_database_names=archive_database_names,
  129. )
  130. assert restore_names == archive_database_names
  131. def test_find_databases_to_restore_with_all_in_requested_names_plus_additional_requested_names_omits_duplicates():
  132. archive_database_names = {'postresql_databases': ['foo', 'bar']}
  133. restore_names = module.find_databases_to_restore(
  134. requested_database_names=['all', 'foo', 'bar'],
  135. archive_database_names=archive_database_names,
  136. )
  137. assert restore_names == archive_database_names
  138. def test_find_databases_to_restore_raises_for_all_in_requested_names_and_requested_named_missing_from_archives():
  139. with pytest.raises(ValueError):
  140. module.find_databases_to_restore(
  141. requested_database_names=['all', 'foo', 'bar'],
  142. archive_database_names={'postresql_databases': ['foo']},
  143. )
  144. def test_ensure_databases_found_with_all_databases_found_does_not_raise():
  145. module.ensure_databases_found(
  146. restore_names={'postgresql_databases': ['foo']},
  147. remaining_restore_names={'postgresql_databases': ['bar']},
  148. found_names=['foo', 'bar'],
  149. )
  150. def test_ensure_databases_found_with_no_databases_raises():
  151. with pytest.raises(ValueError):
  152. module.ensure_databases_found(
  153. restore_names={'postgresql_databases': []},
  154. remaining_restore_names={},
  155. found_names=[],
  156. )
  157. def test_ensure_databases_found_with_missing_databases_raises():
  158. with pytest.raises(ValueError):
  159. module.ensure_databases_found(
  160. restore_names={'postgresql_databases': ['foo']},
  161. remaining_restore_names={'postgresql_databases': ['bar']},
  162. found_names=['foo'],
  163. )
  164. def test_run_restore_restores_each_database():
  165. restore_names = {
  166. 'postgresql_databases': ['foo', 'bar'],
  167. }
  168. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  169. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  170. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  171. flexmock()
  172. )
  173. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  174. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  175. flexmock(module).should_receive('get_configured_database').and_return(
  176. ('postgresql_databases', {'name': 'foo'})
  177. ).and_return(('postgresql_databases', {'name': 'bar'}))
  178. flexmock(module).should_receive('restore_single_database').with_args(
  179. repository=object,
  180. config=object,
  181. local_borg_version=object,
  182. global_arguments=object,
  183. local_path=object,
  184. remote_path=object,
  185. archive_name=object,
  186. hook_name='postgresql_databases',
  187. database={'name': 'foo', 'schemas': None},
  188. connection_params=object,
  189. ).once()
  190. flexmock(module).should_receive('restore_single_database').with_args(
  191. repository=object,
  192. config=object,
  193. local_borg_version=object,
  194. global_arguments=object,
  195. local_path=object,
  196. remote_path=object,
  197. archive_name=object,
  198. hook_name='postgresql_databases',
  199. database={'name': 'bar', 'schemas': None},
  200. connection_params=object,
  201. ).once()
  202. flexmock(module).should_receive('ensure_databases_found')
  203. module.run_restore(
  204. repository={'path': 'repo'},
  205. config=flexmock(),
  206. local_borg_version=flexmock(),
  207. restore_arguments=flexmock(
  208. repository='repo',
  209. archive='archive',
  210. databases=flexmock(),
  211. schemas=None,
  212. hostname=None,
  213. port=None,
  214. username=None,
  215. password=None,
  216. restore_path=None,
  217. ),
  218. global_arguments=flexmock(dry_run=False),
  219. local_path=flexmock(),
  220. remote_path=flexmock(),
  221. )
  222. def test_run_restore_bails_for_non_matching_repository():
  223. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(
  224. False
  225. )
  226. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  227. 'call_hooks_even_if_unconfigured'
  228. ).never()
  229. flexmock(module).should_receive('restore_single_database').never()
  230. module.run_restore(
  231. repository={'path': 'repo'},
  232. config=flexmock(),
  233. local_borg_version=flexmock(),
  234. restore_arguments=flexmock(repository='repo', archive='archive', databases=flexmock()),
  235. global_arguments=flexmock(dry_run=False),
  236. local_path=flexmock(),
  237. remote_path=flexmock(),
  238. )
  239. def test_run_restore_restores_database_configured_with_all_name():
  240. restore_names = {
  241. 'postgresql_databases': ['foo', 'bar'],
  242. }
  243. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  244. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  245. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  246. flexmock()
  247. )
  248. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  249. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  250. flexmock(module).should_receive('get_configured_database').with_args(
  251. config=object,
  252. archive_database_names=object,
  253. hook_name='postgresql_databases',
  254. database_name='foo',
  255. ).and_return(('postgresql_databases', {'name': 'foo'}))
  256. flexmock(module).should_receive('get_configured_database').with_args(
  257. config=object,
  258. archive_database_names=object,
  259. hook_name='postgresql_databases',
  260. database_name='bar',
  261. ).and_return((None, None))
  262. flexmock(module).should_receive('get_configured_database').with_args(
  263. config=object,
  264. archive_database_names=object,
  265. hook_name='postgresql_databases',
  266. database_name='bar',
  267. configuration_database_name='all',
  268. ).and_return(('postgresql_databases', {'name': 'bar'}))
  269. flexmock(module).should_receive('restore_single_database').with_args(
  270. repository=object,
  271. config=object,
  272. local_borg_version=object,
  273. global_arguments=object,
  274. local_path=object,
  275. remote_path=object,
  276. archive_name=object,
  277. hook_name='postgresql_databases',
  278. database={'name': 'foo', 'schemas': None},
  279. connection_params=object,
  280. ).once()
  281. flexmock(module).should_receive('restore_single_database').with_args(
  282. repository=object,
  283. config=object,
  284. local_borg_version=object,
  285. global_arguments=object,
  286. local_path=object,
  287. remote_path=object,
  288. archive_name=object,
  289. hook_name='postgresql_databases',
  290. database={'name': 'bar', 'schemas': None},
  291. connection_params=object,
  292. ).once()
  293. flexmock(module).should_receive('ensure_databases_found')
  294. module.run_restore(
  295. repository={'path': 'repo'},
  296. config=flexmock(),
  297. local_borg_version=flexmock(),
  298. restore_arguments=flexmock(
  299. repository='repo',
  300. archive='archive',
  301. databases=flexmock(),
  302. schemas=None,
  303. hostname=None,
  304. port=None,
  305. username=None,
  306. password=None,
  307. restore_path=None,
  308. ),
  309. global_arguments=flexmock(dry_run=False),
  310. local_path=flexmock(),
  311. remote_path=flexmock(),
  312. )
  313. def test_run_restore_skips_missing_database():
  314. restore_names = {
  315. 'postgresql_databases': ['foo', 'bar'],
  316. }
  317. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  318. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  319. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  320. flexmock()
  321. )
  322. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  323. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  324. flexmock(module).should_receive('get_configured_database').with_args(
  325. config=object,
  326. archive_database_names=object,
  327. hook_name='postgresql_databases',
  328. database_name='foo',
  329. ).and_return(('postgresql_databases', {'name': 'foo'}))
  330. flexmock(module).should_receive('get_configured_database').with_args(
  331. config=object,
  332. archive_database_names=object,
  333. hook_name='postgresql_databases',
  334. database_name='bar',
  335. ).and_return((None, None))
  336. flexmock(module).should_receive('get_configured_database').with_args(
  337. config=object,
  338. archive_database_names=object,
  339. hook_name='postgresql_databases',
  340. database_name='bar',
  341. configuration_database_name='all',
  342. ).and_return((None, None))
  343. flexmock(module).should_receive('restore_single_database').with_args(
  344. repository=object,
  345. config=object,
  346. local_borg_version=object,
  347. global_arguments=object,
  348. local_path=object,
  349. remote_path=object,
  350. archive_name=object,
  351. hook_name='postgresql_databases',
  352. database={'name': 'foo', 'schemas': None},
  353. connection_params=object,
  354. ).once()
  355. flexmock(module).should_receive('restore_single_database').with_args(
  356. repository=object,
  357. config=object,
  358. local_borg_version=object,
  359. global_arguments=object,
  360. local_path=object,
  361. remote_path=object,
  362. archive_name=object,
  363. hook_name='postgresql_databases',
  364. database={'name': 'bar', 'schemas': None},
  365. connection_params=object,
  366. ).never()
  367. flexmock(module).should_receive('ensure_databases_found')
  368. module.run_restore(
  369. repository={'path': 'repo'},
  370. config=flexmock(),
  371. local_borg_version=flexmock(),
  372. restore_arguments=flexmock(
  373. repository='repo',
  374. archive='archive',
  375. databases=flexmock(),
  376. schemas=None,
  377. hostname=None,
  378. port=None,
  379. username=None,
  380. password=None,
  381. restore_path=None,
  382. ),
  383. global_arguments=flexmock(dry_run=False),
  384. local_path=flexmock(),
  385. remote_path=flexmock(),
  386. )
  387. def test_run_restore_restores_databases_from_different_hooks():
  388. restore_names = {
  389. 'postgresql_databases': ['foo'],
  390. 'mysql_databases': ['bar'],
  391. }
  392. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  393. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  394. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  395. flexmock()
  396. )
  397. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  398. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  399. flexmock(module).should_receive('get_configured_database').with_args(
  400. config=object,
  401. archive_database_names=object,
  402. hook_name='postgresql_databases',
  403. database_name='foo',
  404. ).and_return(('postgresql_databases', {'name': 'foo'}))
  405. flexmock(module).should_receive('get_configured_database').with_args(
  406. config=object,
  407. archive_database_names=object,
  408. hook_name='mysql_databases',
  409. database_name='bar',
  410. ).and_return(('mysql_databases', {'name': 'bar'}))
  411. flexmock(module).should_receive('restore_single_database').with_args(
  412. repository=object,
  413. config=object,
  414. local_borg_version=object,
  415. global_arguments=object,
  416. local_path=object,
  417. remote_path=object,
  418. archive_name=object,
  419. hook_name='postgresql_databases',
  420. database={'name': 'foo', 'schemas': None},
  421. connection_params=object,
  422. ).once()
  423. flexmock(module).should_receive('restore_single_database').with_args(
  424. repository=object,
  425. config=object,
  426. local_borg_version=object,
  427. global_arguments=object,
  428. local_path=object,
  429. remote_path=object,
  430. archive_name=object,
  431. hook_name='mysql_databases',
  432. database={'name': 'bar', 'schemas': None},
  433. connection_params=object,
  434. ).once()
  435. flexmock(module).should_receive('ensure_databases_found')
  436. module.run_restore(
  437. repository={'path': 'repo'},
  438. config=flexmock(),
  439. local_borg_version=flexmock(),
  440. restore_arguments=flexmock(
  441. repository='repo',
  442. archive='archive',
  443. databases=flexmock(),
  444. schemas=None,
  445. hostname=None,
  446. port=None,
  447. username=None,
  448. password=None,
  449. restore_path=None,
  450. ),
  451. global_arguments=flexmock(dry_run=False),
  452. local_path=flexmock(),
  453. remote_path=flexmock(),
  454. )