test_restore.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. 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. location={'borgmatic_source_directory': '.borgmatic'},
  80. storage=flexmock(),
  81. local_borg_version=flexmock(),
  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. location={'borgmatic_source_directory': '.borgmatic'},
  97. storage=flexmock(),
  98. local_borg_version=flexmock(),
  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=[], archive_database_names=archive_database_names,
  121. )
  122. assert restore_names == archive_database_names
  123. def test_find_databases_to_restore_with_all_in_requested_names_finds_all_archive_databases():
  124. archive_database_names = {'postresql_databases': ['foo', 'bar']}
  125. restore_names = module.find_databases_to_restore(
  126. requested_database_names=['all'], archive_database_names=archive_database_names,
  127. )
  128. assert restore_names == archive_database_names
  129. def test_find_databases_to_restore_with_all_in_requested_names_plus_additional_requested_names_omits_duplicates():
  130. archive_database_names = {'postresql_databases': ['foo', 'bar']}
  131. restore_names = module.find_databases_to_restore(
  132. requested_database_names=['all', 'foo', 'bar'],
  133. archive_database_names=archive_database_names,
  134. )
  135. assert restore_names == archive_database_names
  136. def test_find_databases_to_restore_raises_for_all_in_requested_names_and_requested_named_missing_from_archives():
  137. with pytest.raises(ValueError):
  138. module.find_databases_to_restore(
  139. requested_database_names=['all', 'foo', 'bar'],
  140. archive_database_names={'postresql_databases': ['foo']},
  141. )
  142. def test_ensure_databases_found_with_all_databases_found_does_not_raise():
  143. module.ensure_databases_found(
  144. restore_names={'postgresql_databases': ['foo']},
  145. remaining_restore_names={'postgresql_databases': ['bar']},
  146. found_names=['foo', 'bar'],
  147. )
  148. def test_ensure_databases_found_with_no_databases_raises():
  149. with pytest.raises(ValueError):
  150. module.ensure_databases_found(
  151. restore_names={'postgresql_databases': []}, remaining_restore_names={}, found_names=[],
  152. )
  153. def test_ensure_databases_found_with_missing_databases_raises():
  154. with pytest.raises(ValueError):
  155. module.ensure_databases_found(
  156. restore_names={'postgresql_databases': ['foo']},
  157. remaining_restore_names={'postgresql_databases': ['bar']},
  158. found_names=['foo'],
  159. )
  160. def test_run_restore_restores_each_database():
  161. restore_names = {
  162. 'postgresql_databases': ['foo', 'bar'],
  163. }
  164. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  165. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  166. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  167. flexmock()
  168. )
  169. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  170. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  171. flexmock(module).should_receive('get_configured_database').and_return(
  172. ('postgresql_databases', {'name': 'foo'})
  173. ).and_return(('postgresql_databases', {'name': 'bar'}))
  174. flexmock(module).should_receive('restore_single_database').with_args(
  175. repository=object,
  176. location=object,
  177. storage=object,
  178. hooks=object,
  179. local_borg_version=object,
  180. global_arguments=object,
  181. local_path=object,
  182. remote_path=object,
  183. archive_name=object,
  184. hook_name='postgresql_databases',
  185. database={'name': 'foo'},
  186. ).once()
  187. flexmock(module).should_receive('restore_single_database').with_args(
  188. repository=object,
  189. location=object,
  190. storage=object,
  191. hooks=object,
  192. local_borg_version=object,
  193. global_arguments=object,
  194. local_path=object,
  195. remote_path=object,
  196. archive_name=object,
  197. hook_name='postgresql_databases',
  198. database={'name': 'bar'},
  199. ).once()
  200. flexmock(module).should_receive('ensure_databases_found')
  201. module.run_restore(
  202. repository={'path': 'repo'},
  203. location=flexmock(),
  204. storage=flexmock(),
  205. hooks=flexmock(),
  206. local_borg_version=flexmock(),
  207. restore_arguments=flexmock(repository='repo', archive='archive', databases=flexmock()),
  208. global_arguments=flexmock(dry_run=False),
  209. local_path=flexmock(),
  210. remote_path=flexmock(),
  211. )
  212. def test_run_restore_bails_for_non_matching_repository():
  213. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(
  214. False
  215. )
  216. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  217. 'call_hooks_even_if_unconfigured'
  218. ).never()
  219. flexmock(module).should_receive('restore_single_database').never()
  220. module.run_restore(
  221. repository={'path': 'repo'},
  222. location=flexmock(),
  223. storage=flexmock(),
  224. hooks=flexmock(),
  225. local_borg_version=flexmock(),
  226. restore_arguments=flexmock(repository='repo', archive='archive', databases=flexmock()),
  227. global_arguments=flexmock(dry_run=False),
  228. local_path=flexmock(),
  229. remote_path=flexmock(),
  230. )
  231. def test_run_restore_restores_database_configured_with_all_name():
  232. restore_names = {
  233. 'postgresql_databases': ['foo', 'bar'],
  234. }
  235. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  236. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  237. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  238. flexmock()
  239. )
  240. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  241. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  242. flexmock(module).should_receive('get_configured_database').with_args(
  243. hooks=object,
  244. archive_database_names=object,
  245. hook_name='postgresql_databases',
  246. database_name='foo',
  247. ).and_return(('postgresql_databases', {'name': 'foo'}))
  248. flexmock(module).should_receive('get_configured_database').with_args(
  249. hooks=object,
  250. archive_database_names=object,
  251. hook_name='postgresql_databases',
  252. database_name='bar',
  253. ).and_return((None, None))
  254. flexmock(module).should_receive('get_configured_database').with_args(
  255. hooks=object,
  256. archive_database_names=object,
  257. hook_name='postgresql_databases',
  258. database_name='bar',
  259. configuration_database_name='all',
  260. ).and_return(('postgresql_databases', {'name': 'bar'}))
  261. flexmock(module).should_receive('restore_single_database').with_args(
  262. repository=object,
  263. location=object,
  264. storage=object,
  265. hooks=object,
  266. local_borg_version=object,
  267. global_arguments=object,
  268. local_path=object,
  269. remote_path=object,
  270. archive_name=object,
  271. hook_name='postgresql_databases',
  272. database={'name': 'foo'},
  273. ).once()
  274. flexmock(module).should_receive('restore_single_database').with_args(
  275. repository=object,
  276. location=object,
  277. storage=object,
  278. hooks=object,
  279. local_borg_version=object,
  280. global_arguments=object,
  281. local_path=object,
  282. remote_path=object,
  283. archive_name=object,
  284. hook_name='postgresql_databases',
  285. database={'name': 'bar'},
  286. ).once()
  287. flexmock(module).should_receive('ensure_databases_found')
  288. module.run_restore(
  289. repository={'path': 'repo'},
  290. location=flexmock(),
  291. storage=flexmock(),
  292. hooks=flexmock(),
  293. local_borg_version=flexmock(),
  294. restore_arguments=flexmock(repository='repo', archive='archive', databases=flexmock()),
  295. global_arguments=flexmock(dry_run=False),
  296. local_path=flexmock(),
  297. remote_path=flexmock(),
  298. )
  299. def test_run_restore_skips_missing_database():
  300. restore_names = {
  301. 'postgresql_databases': ['foo', 'bar'],
  302. }
  303. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  304. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  305. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  306. flexmock()
  307. )
  308. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  309. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  310. flexmock(module).should_receive('get_configured_database').with_args(
  311. hooks=object,
  312. archive_database_names=object,
  313. hook_name='postgresql_databases',
  314. database_name='foo',
  315. ).and_return(('postgresql_databases', {'name': 'foo'}))
  316. flexmock(module).should_receive('get_configured_database').with_args(
  317. hooks=object,
  318. archive_database_names=object,
  319. hook_name='postgresql_databases',
  320. database_name='bar',
  321. ).and_return((None, None))
  322. flexmock(module).should_receive('get_configured_database').with_args(
  323. hooks=object,
  324. archive_database_names=object,
  325. hook_name='postgresql_databases',
  326. database_name='bar',
  327. configuration_database_name='all',
  328. ).and_return((None, None))
  329. flexmock(module).should_receive('restore_single_database').with_args(
  330. repository=object,
  331. location=object,
  332. storage=object,
  333. hooks=object,
  334. local_borg_version=object,
  335. global_arguments=object,
  336. local_path=object,
  337. remote_path=object,
  338. archive_name=object,
  339. hook_name='postgresql_databases',
  340. database={'name': 'foo'},
  341. ).once()
  342. flexmock(module).should_receive('restore_single_database').with_args(
  343. repository=object,
  344. location=object,
  345. storage=object,
  346. hooks=object,
  347. local_borg_version=object,
  348. global_arguments=object,
  349. local_path=object,
  350. remote_path=object,
  351. archive_name=object,
  352. hook_name='postgresql_databases',
  353. database={'name': 'bar'},
  354. ).never()
  355. flexmock(module).should_receive('ensure_databases_found')
  356. module.run_restore(
  357. repository={'path': 'repo'},
  358. location=flexmock(),
  359. storage=flexmock(),
  360. hooks=flexmock(),
  361. local_borg_version=flexmock(),
  362. restore_arguments=flexmock(repository='repo', archive='archive', databases=flexmock()),
  363. global_arguments=flexmock(dry_run=False),
  364. local_path=flexmock(),
  365. remote_path=flexmock(),
  366. )
  367. def test_run_restore_restores_databases_from_different_hooks():
  368. restore_names = {
  369. 'postgresql_databases': ['foo'],
  370. 'mysql_databases': ['bar'],
  371. }
  372. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  373. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
  374. flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
  375. flexmock()
  376. )
  377. flexmock(module).should_receive('collect_archive_database_names').and_return(flexmock())
  378. flexmock(module).should_receive('find_databases_to_restore').and_return(restore_names)
  379. flexmock(module).should_receive('get_configured_database').with_args(
  380. hooks=object,
  381. archive_database_names=object,
  382. hook_name='postgresql_databases',
  383. database_name='foo',
  384. ).and_return(('postgresql_databases', {'name': 'foo'}))
  385. flexmock(module).should_receive('get_configured_database').with_args(
  386. hooks=object,
  387. archive_database_names=object,
  388. hook_name='mysql_databases',
  389. database_name='bar',
  390. ).and_return(('mysql_databases', {'name': 'bar'}))
  391. flexmock(module).should_receive('restore_single_database').with_args(
  392. repository=object,
  393. location=object,
  394. storage=object,
  395. hooks=object,
  396. local_borg_version=object,
  397. global_arguments=object,
  398. local_path=object,
  399. remote_path=object,
  400. archive_name=object,
  401. hook_name='postgresql_databases',
  402. database={'name': 'foo'},
  403. ).once()
  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='mysql_databases',
  415. database={'name': 'bar'},
  416. ).once()
  417. flexmock(module).should_receive('ensure_databases_found')
  418. module.run_restore(
  419. repository={'path': 'repo'},
  420. location=flexmock(),
  421. storage=flexmock(),
  422. hooks=flexmock(),
  423. local_borg_version=flexmock(),
  424. restore_arguments=flexmock(repository='repo', archive='archive', databases=flexmock()),
  425. global_arguments=flexmock(dry_run=False),
  426. local_path=flexmock(),
  427. remote_path=flexmock(),
  428. )