test_restore.py 19 KB

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