2
0

test_postgresql.py 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import postgresql as module
  5. def test_make_extra_environment_maps_options_to_environment():
  6. database = {
  7. 'name': 'foo',
  8. 'password': 'pass',
  9. 'ssl_mode': 'require',
  10. 'ssl_cert': 'cert.crt',
  11. 'ssl_key': 'key.key',
  12. 'ssl_root_cert': 'root.crt',
  13. 'ssl_crl': 'crl.crl',
  14. }
  15. expected = {
  16. 'PGPASSWORD': 'pass',
  17. 'PGSSLMODE': 'require',
  18. 'PGSSLCERT': 'cert.crt',
  19. 'PGSSLKEY': 'key.key',
  20. 'PGSSLROOTCERT': 'root.crt',
  21. 'PGSSLCRL': 'crl.crl',
  22. }
  23. extra_env = module.make_extra_environment(database)
  24. assert extra_env == expected
  25. def test_make_extra_environment_with_cli_password_sets_correct_password():
  26. database = {'name': 'foo', 'restore_password': 'trustsome1', 'password': 'anotherpassword'}
  27. extra = module.make_extra_environment(
  28. database, restore_connection_params={'password': 'clipassword'}
  29. )
  30. assert extra['PGPASSWORD'] == 'clipassword'
  31. def test_make_extra_environment_without_cli_password_or_configured_password_does_not_set_password():
  32. database = {'name': 'foo'}
  33. extra = module.make_extra_environment(
  34. database, restore_connection_params={'username': 'someone'}
  35. )
  36. assert 'PGPASSWORD' not in extra
  37. def test_make_extra_environment_without_ssl_mode_does_not_set_ssl_mode():
  38. database = {'name': 'foo'}
  39. extra = module.make_extra_environment(database)
  40. assert 'PGSSLMODE' not in extra
  41. def test_database_names_to_dump_passes_through_individual_database_name():
  42. database = {'name': 'foo'}
  43. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  44. 'foo',
  45. )
  46. def test_database_names_to_dump_passes_through_individual_database_name_with_format():
  47. database = {'name': 'foo', 'format': 'custom'}
  48. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  49. 'foo',
  50. )
  51. def test_database_names_to_dump_passes_through_all_without_format():
  52. database = {'name': 'all'}
  53. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  54. 'all',
  55. )
  56. def test_database_names_to_dump_with_all_and_format_and_dry_run_bails():
  57. database = {'name': 'all', 'format': 'custom'}
  58. flexmock(module).should_receive('execute_command_and_capture_output').never()
  59. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=True) == ()
  60. def test_database_names_to_dump_with_all_and_format_lists_databases():
  61. database = {'name': 'all', 'format': 'custom'}
  62. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  63. 'foo,test,\nbar,test,"stuff and such"'
  64. )
  65. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  66. 'foo',
  67. 'bar',
  68. )
  69. def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostname_and_port():
  70. database = {'name': 'all', 'format': 'custom', 'hostname': 'localhost', 'port': 1234}
  71. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  72. (
  73. 'psql',
  74. '--list',
  75. '--no-password',
  76. '--no-psqlrc',
  77. '--csv',
  78. '--tuples-only',
  79. '--host',
  80. 'localhost',
  81. '--port',
  82. '1234',
  83. ),
  84. extra_environment=object,
  85. ).and_return('foo,test,\nbar,test,"stuff and such"')
  86. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  87. 'foo',
  88. 'bar',
  89. )
  90. def test_database_names_to_dump_with_all_and_format_lists_databases_with_username():
  91. database = {'name': 'all', 'format': 'custom', 'username': 'postgres'}
  92. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  93. (
  94. 'psql',
  95. '--list',
  96. '--no-password',
  97. '--no-psqlrc',
  98. '--csv',
  99. '--tuples-only',
  100. '--username',
  101. 'postgres',
  102. ),
  103. extra_environment=object,
  104. ).and_return('foo,test,\nbar,test,"stuff and such"')
  105. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  106. 'foo',
  107. 'bar',
  108. )
  109. def test_database_names_to_dump_with_all_and_format_lists_databases_with_options():
  110. database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'}
  111. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  112. ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'),
  113. extra_environment=object,
  114. ).and_return('foo,test,\nbar,test,"stuff and such"')
  115. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  116. 'foo',
  117. 'bar',
  118. )
  119. def test_database_names_to_dump_with_all_and_format_excludes_particular_databases():
  120. database = {'name': 'all', 'format': 'custom'}
  121. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  122. 'foo,test,\ntemplate0,test,blah'
  123. )
  124. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  125. 'foo',
  126. )
  127. def test_database_names_to_dump_with_all_and_psql_command_uses_custom_command():
  128. database = {'name': 'all', 'format': 'custom', 'psql_command': 'docker exec mycontainer psql'}
  129. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  130. (
  131. 'docker',
  132. 'exec',
  133. 'mycontainer',
  134. 'psql',
  135. '--list',
  136. '--no-password',
  137. '--no-psqlrc',
  138. '--csv',
  139. '--tuples-only',
  140. ),
  141. extra_environment=object,
  142. ).and_return('foo,text').once()
  143. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  144. 'foo',
  145. )
  146. def test_dump_data_sources_runs_pg_dump_for_each_database():
  147. databases = [{'name': 'foo'}, {'name': 'bar'}]
  148. processes = [flexmock(), flexmock()]
  149. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  150. flexmock(module).should_receive('make_dump_path').and_return('')
  151. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  152. ('bar',)
  153. )
  154. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  155. 'databases/localhost/foo'
  156. ).and_return('databases/localhost/bar')
  157. flexmock(module.os.path).should_receive('exists').and_return(False)
  158. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  159. for name, process in zip(('foo', 'bar'), processes):
  160. flexmock(module).should_receive('execute_command').with_args(
  161. (
  162. 'pg_dump',
  163. '--no-password',
  164. '--clean',
  165. '--if-exists',
  166. '--format',
  167. 'custom',
  168. name,
  169. '>',
  170. f'databases/localhost/{name}',
  171. ),
  172. shell=True,
  173. extra_environment={'PGSSLMODE': 'disable'},
  174. run_to_completion=False,
  175. ).and_return(process).once()
  176. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  177. def test_dump_data_sources_raises_when_no_database_names_to_dump():
  178. databases = [{'name': 'foo'}, {'name': 'bar'}]
  179. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  180. flexmock(module).should_receive('make_dump_path').and_return('')
  181. flexmock(module).should_receive('database_names_to_dump').and_return(())
  182. with pytest.raises(ValueError):
  183. module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False)
  184. def test_dump_data_sources_does_not_raise_when_no_database_names_to_dump():
  185. databases = [{'name': 'foo'}, {'name': 'bar'}]
  186. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  187. flexmock(module).should_receive('make_dump_path').and_return('')
  188. flexmock(module).should_receive('database_names_to_dump').and_return(())
  189. module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  190. def test_dump_data_sources_with_duplicate_dump_skips_pg_dump():
  191. databases = [{'name': 'foo'}, {'name': 'bar'}]
  192. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  193. flexmock(module).should_receive('make_dump_path').and_return('')
  194. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  195. ('bar',)
  196. )
  197. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  198. 'databases/localhost/foo'
  199. ).and_return('databases/localhost/bar')
  200. flexmock(module.os.path).should_receive('exists').and_return(True)
  201. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  202. flexmock(module).should_receive('execute_command').never()
  203. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == []
  204. def test_dump_data_sources_with_dry_run_skips_pg_dump():
  205. databases = [{'name': 'foo'}, {'name': 'bar'}]
  206. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  207. flexmock(module).should_receive('make_dump_path').and_return('')
  208. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  209. ('bar',)
  210. )
  211. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  212. 'databases/localhost/foo'
  213. ).and_return('databases/localhost/bar')
  214. flexmock(module.os.path).should_receive('exists').and_return(False)
  215. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  216. flexmock(module).should_receive('execute_command').never()
  217. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  218. def test_dump_data_sources_runs_pg_dump_with_hostname_and_port():
  219. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  220. process = flexmock()
  221. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  222. flexmock(module).should_receive('make_dump_path').and_return('')
  223. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  224. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  225. 'databases/database.example.org/foo'
  226. )
  227. flexmock(module.os.path).should_receive('exists').and_return(False)
  228. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  229. flexmock(module).should_receive('execute_command').with_args(
  230. (
  231. 'pg_dump',
  232. '--no-password',
  233. '--clean',
  234. '--if-exists',
  235. '--host',
  236. 'database.example.org',
  237. '--port',
  238. '5433',
  239. '--format',
  240. 'custom',
  241. 'foo',
  242. '>',
  243. 'databases/database.example.org/foo',
  244. ),
  245. shell=True,
  246. extra_environment={'PGSSLMODE': 'disable'},
  247. run_to_completion=False,
  248. ).and_return(process).once()
  249. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  250. def test_dump_data_sources_runs_pg_dump_with_username_and_password():
  251. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  252. process = flexmock()
  253. flexmock(module).should_receive('make_extra_environment').and_return(
  254. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  255. )
  256. flexmock(module).should_receive('make_dump_path').and_return('')
  257. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  258. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  259. 'databases/localhost/foo'
  260. )
  261. flexmock(module.os.path).should_receive('exists').and_return(False)
  262. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  263. flexmock(module).should_receive('execute_command').with_args(
  264. (
  265. 'pg_dump',
  266. '--no-password',
  267. '--clean',
  268. '--if-exists',
  269. '--username',
  270. 'postgres',
  271. '--format',
  272. 'custom',
  273. 'foo',
  274. '>',
  275. 'databases/localhost/foo',
  276. ),
  277. shell=True,
  278. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  279. run_to_completion=False,
  280. ).and_return(process).once()
  281. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  282. def test_dump_data_sources_with_username_injection_attack_gets_escaped():
  283. databases = [{'name': 'foo', 'username': 'postgres; naughty-command', 'password': 'trustsome1'}]
  284. process = flexmock()
  285. flexmock(module).should_receive('make_extra_environment').and_return(
  286. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  287. )
  288. flexmock(module).should_receive('make_dump_path').and_return('')
  289. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  290. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  291. 'databases/localhost/foo'
  292. )
  293. flexmock(module.os.path).should_receive('exists').and_return(False)
  294. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  295. flexmock(module).should_receive('execute_command').with_args(
  296. (
  297. 'pg_dump',
  298. '--no-password',
  299. '--clean',
  300. '--if-exists',
  301. '--username',
  302. "'postgres; naughty-command'",
  303. '--format',
  304. 'custom',
  305. 'foo',
  306. '>',
  307. 'databases/localhost/foo',
  308. ),
  309. shell=True,
  310. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  311. run_to_completion=False,
  312. ).and_return(process).once()
  313. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  314. def test_dump_data_sources_runs_pg_dump_with_directory_format():
  315. databases = [{'name': 'foo', 'format': 'directory'}]
  316. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  317. flexmock(module).should_receive('make_dump_path').and_return('')
  318. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  319. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  320. 'databases/localhost/foo'
  321. )
  322. flexmock(module.os.path).should_receive('exists').and_return(False)
  323. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  324. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  325. flexmock(module).should_receive('execute_command').with_args(
  326. (
  327. 'pg_dump',
  328. '--no-password',
  329. '--clean',
  330. '--if-exists',
  331. '--format',
  332. 'directory',
  333. '--file',
  334. 'databases/localhost/foo',
  335. 'foo',
  336. ),
  337. shell=True,
  338. extra_environment={'PGSSLMODE': 'disable'},
  339. ).and_return(flexmock()).once()
  340. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == []
  341. def test_dump_data_sources_runs_pg_dump_with_options():
  342. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  343. process = flexmock()
  344. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  345. flexmock(module).should_receive('make_dump_path').and_return('')
  346. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  347. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  348. 'databases/localhost/foo'
  349. )
  350. flexmock(module.os.path).should_receive('exists').and_return(False)
  351. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  352. flexmock(module).should_receive('execute_command').with_args(
  353. (
  354. 'pg_dump',
  355. '--no-password',
  356. '--clean',
  357. '--if-exists',
  358. '--format',
  359. 'custom',
  360. '--stuff=such',
  361. 'foo',
  362. '>',
  363. 'databases/localhost/foo',
  364. ),
  365. shell=True,
  366. extra_environment={'PGSSLMODE': 'disable'},
  367. run_to_completion=False,
  368. ).and_return(process).once()
  369. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  370. def test_dump_data_sources_runs_pg_dumpall_for_all_databases():
  371. databases = [{'name': 'all'}]
  372. process = flexmock()
  373. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  374. flexmock(module).should_receive('make_dump_path').and_return('')
  375. flexmock(module).should_receive('database_names_to_dump').and_return(('all',))
  376. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  377. 'databases/localhost/all'
  378. )
  379. flexmock(module.os.path).should_receive('exists').and_return(False)
  380. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  381. flexmock(module).should_receive('execute_command').with_args(
  382. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  383. shell=True,
  384. extra_environment={'PGSSLMODE': 'disable'},
  385. run_to_completion=False,
  386. ).and_return(process).once()
  387. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  388. def test_dump_data_sources_runs_non_default_pg_dump():
  389. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump'}]
  390. process = flexmock()
  391. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  392. flexmock(module).should_receive('make_dump_path').and_return('')
  393. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  394. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  395. 'databases/localhost/foo'
  396. )
  397. flexmock(module.os.path).should_receive('exists').and_return(False)
  398. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  399. flexmock(module).should_receive('execute_command').with_args(
  400. (
  401. 'special_pg_dump',
  402. '--no-password',
  403. '--clean',
  404. '--if-exists',
  405. '--format',
  406. 'custom',
  407. 'foo',
  408. '>',
  409. 'databases/localhost/foo',
  410. ),
  411. shell=True,
  412. extra_environment={'PGSSLMODE': 'disable'},
  413. run_to_completion=False,
  414. ).and_return(process).once()
  415. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  416. def test_restore_data_source_dump_runs_pg_restore():
  417. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  418. extract_process = flexmock(stdout=flexmock())
  419. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  420. flexmock(module).should_receive('make_dump_path')
  421. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  422. flexmock(module).should_receive('execute_command_with_processes').with_args(
  423. (
  424. 'pg_restore',
  425. '--no-password',
  426. '--if-exists',
  427. '--exit-on-error',
  428. '--clean',
  429. '--dbname',
  430. 'foo',
  431. ),
  432. processes=[extract_process],
  433. output_log_level=logging.DEBUG,
  434. input_file=extract_process.stdout,
  435. extra_environment={'PGSSLMODE': 'disable'},
  436. ).once()
  437. flexmock(module).should_receive('execute_command').with_args(
  438. (
  439. 'psql',
  440. '--no-password',
  441. '--no-psqlrc',
  442. '--quiet',
  443. '--dbname',
  444. 'foo',
  445. '--command',
  446. 'ANALYZE',
  447. ),
  448. extra_environment={'PGSSLMODE': 'disable'},
  449. ).once()
  450. module.restore_data_source_dump(
  451. hook_config,
  452. {},
  453. 'test.yaml',
  454. data_source={'name': 'foo'},
  455. dry_run=False,
  456. extract_process=extract_process,
  457. connection_params={
  458. 'hostname': None,
  459. 'port': None,
  460. 'username': None,
  461. 'password': None,
  462. },
  463. )
  464. def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port():
  465. hook_config = [
  466. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  467. ]
  468. extract_process = flexmock(stdout=flexmock())
  469. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  470. flexmock(module).should_receive('make_dump_path')
  471. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  472. flexmock(module).should_receive('execute_command_with_processes').with_args(
  473. (
  474. 'pg_restore',
  475. '--no-password',
  476. '--if-exists',
  477. '--exit-on-error',
  478. '--clean',
  479. '--dbname',
  480. 'foo',
  481. '--host',
  482. 'database.example.org',
  483. '--port',
  484. '5433',
  485. ),
  486. processes=[extract_process],
  487. output_log_level=logging.DEBUG,
  488. input_file=extract_process.stdout,
  489. extra_environment={'PGSSLMODE': 'disable'},
  490. ).once()
  491. flexmock(module).should_receive('execute_command').with_args(
  492. (
  493. 'psql',
  494. '--no-password',
  495. '--no-psqlrc',
  496. '--quiet',
  497. '--host',
  498. 'database.example.org',
  499. '--port',
  500. '5433',
  501. '--dbname',
  502. 'foo',
  503. '--command',
  504. 'ANALYZE',
  505. ),
  506. extra_environment={'PGSSLMODE': 'disable'},
  507. ).once()
  508. module.restore_data_source_dump(
  509. hook_config,
  510. {},
  511. 'test.yaml',
  512. data_source=hook_config[0],
  513. dry_run=False,
  514. extract_process=extract_process,
  515. connection_params={
  516. 'hostname': None,
  517. 'port': None,
  518. 'username': None,
  519. 'password': None,
  520. },
  521. )
  522. def test_restore_data_source_dump_runs_pg_restore_with_username_and_password():
  523. hook_config = [
  524. {'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None}
  525. ]
  526. extract_process = flexmock(stdout=flexmock())
  527. flexmock(module).should_receive('make_extra_environment').and_return(
  528. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  529. )
  530. flexmock(module).should_receive('make_dump_path')
  531. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  532. flexmock(module).should_receive('execute_command_with_processes').with_args(
  533. (
  534. 'pg_restore',
  535. '--no-password',
  536. '--if-exists',
  537. '--exit-on-error',
  538. '--clean',
  539. '--dbname',
  540. 'foo',
  541. '--username',
  542. 'postgres',
  543. ),
  544. processes=[extract_process],
  545. output_log_level=logging.DEBUG,
  546. input_file=extract_process.stdout,
  547. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  548. ).once()
  549. flexmock(module).should_receive('execute_command').with_args(
  550. (
  551. 'psql',
  552. '--no-password',
  553. '--no-psqlrc',
  554. '--quiet',
  555. '--username',
  556. 'postgres',
  557. '--dbname',
  558. 'foo',
  559. '--command',
  560. 'ANALYZE',
  561. ),
  562. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  563. ).once()
  564. module.restore_data_source_dump(
  565. hook_config,
  566. {},
  567. 'test.yaml',
  568. data_source=hook_config[0],
  569. dry_run=False,
  570. extract_process=extract_process,
  571. connection_params={
  572. 'hostname': None,
  573. 'port': None,
  574. 'username': None,
  575. 'password': None,
  576. },
  577. )
  578. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  579. hook_config = [
  580. {
  581. 'name': 'foo',
  582. 'hostname': 'database.example.org',
  583. 'port': 5433,
  584. 'username': 'postgres',
  585. 'password': 'trustsome1',
  586. 'restore_hostname': 'restorehost',
  587. 'restore_port': 'restoreport',
  588. 'restore_username': 'restoreusername',
  589. 'restore_password': 'restorepassword',
  590. 'schemas': None,
  591. }
  592. ]
  593. extract_process = flexmock(stdout=flexmock())
  594. flexmock(module).should_receive('make_extra_environment').and_return(
  595. {'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}
  596. )
  597. flexmock(module).should_receive('make_dump_path')
  598. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  599. flexmock(module).should_receive('execute_command_with_processes').with_args(
  600. (
  601. 'pg_restore',
  602. '--no-password',
  603. '--if-exists',
  604. '--exit-on-error',
  605. '--clean',
  606. '--dbname',
  607. 'foo',
  608. '--host',
  609. 'clihost',
  610. '--port',
  611. 'cliport',
  612. '--username',
  613. 'cliusername',
  614. ),
  615. processes=[extract_process],
  616. output_log_level=logging.DEBUG,
  617. input_file=extract_process.stdout,
  618. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  619. ).once()
  620. flexmock(module).should_receive('execute_command').with_args(
  621. (
  622. 'psql',
  623. '--no-password',
  624. '--no-psqlrc',
  625. '--quiet',
  626. '--host',
  627. 'clihost',
  628. '--port',
  629. 'cliport',
  630. '--username',
  631. 'cliusername',
  632. '--dbname',
  633. 'foo',
  634. '--command',
  635. 'ANALYZE',
  636. ),
  637. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  638. ).once()
  639. module.restore_data_source_dump(
  640. hook_config,
  641. {},
  642. 'test.yaml',
  643. data_source={'name': 'foo'},
  644. dry_run=False,
  645. extract_process=extract_process,
  646. connection_params={
  647. 'hostname': 'clihost',
  648. 'port': 'cliport',
  649. 'username': 'cliusername',
  650. 'password': 'clipassword',
  651. },
  652. )
  653. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  654. hook_config = [
  655. {
  656. 'name': 'foo',
  657. 'hostname': 'database.example.org',
  658. 'port': 5433,
  659. 'username': 'postgres',
  660. 'password': 'trustsome1',
  661. 'schemas': None,
  662. 'restore_hostname': 'restorehost',
  663. 'restore_port': 'restoreport',
  664. 'restore_username': 'restoreusername',
  665. 'restore_password': 'restorepassword',
  666. }
  667. ]
  668. extract_process = flexmock(stdout=flexmock())
  669. flexmock(module).should_receive('make_extra_environment').and_return(
  670. {'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}
  671. )
  672. flexmock(module).should_receive('make_dump_path')
  673. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  674. flexmock(module).should_receive('execute_command_with_processes').with_args(
  675. (
  676. 'pg_restore',
  677. '--no-password',
  678. '--if-exists',
  679. '--exit-on-error',
  680. '--clean',
  681. '--dbname',
  682. 'foo',
  683. '--host',
  684. 'restorehost',
  685. '--port',
  686. 'restoreport',
  687. '--username',
  688. 'restoreusername',
  689. ),
  690. processes=[extract_process],
  691. output_log_level=logging.DEBUG,
  692. input_file=extract_process.stdout,
  693. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  694. ).once()
  695. flexmock(module).should_receive('execute_command').with_args(
  696. (
  697. 'psql',
  698. '--no-password',
  699. '--no-psqlrc',
  700. '--quiet',
  701. '--host',
  702. 'restorehost',
  703. '--port',
  704. 'restoreport',
  705. '--username',
  706. 'restoreusername',
  707. '--dbname',
  708. 'foo',
  709. '--command',
  710. 'ANALYZE',
  711. ),
  712. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  713. ).once()
  714. module.restore_data_source_dump(
  715. hook_config,
  716. {},
  717. 'test.yaml',
  718. data_source=hook_config[0],
  719. dry_run=False,
  720. extract_process=extract_process,
  721. connection_params={
  722. 'hostname': None,
  723. 'port': None,
  724. 'username': None,
  725. 'password': None,
  726. },
  727. )
  728. def test_restore_data_source_dump_runs_pg_restore_with_options():
  729. hook_config = [
  730. {
  731. 'name': 'foo',
  732. 'restore_options': '--harder',
  733. 'analyze_options': '--smarter',
  734. 'schemas': None,
  735. }
  736. ]
  737. extract_process = flexmock(stdout=flexmock())
  738. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  739. flexmock(module).should_receive('make_dump_path')
  740. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  741. flexmock(module).should_receive('execute_command_with_processes').with_args(
  742. (
  743. 'pg_restore',
  744. '--no-password',
  745. '--if-exists',
  746. '--exit-on-error',
  747. '--clean',
  748. '--dbname',
  749. 'foo',
  750. '--harder',
  751. ),
  752. processes=[extract_process],
  753. output_log_level=logging.DEBUG,
  754. input_file=extract_process.stdout,
  755. extra_environment={'PGSSLMODE': 'disable'},
  756. ).once()
  757. flexmock(module).should_receive('execute_command').with_args(
  758. (
  759. 'psql',
  760. '--no-password',
  761. '--no-psqlrc',
  762. '--quiet',
  763. '--dbname',
  764. 'foo',
  765. '--smarter',
  766. '--command',
  767. 'ANALYZE',
  768. ),
  769. extra_environment={'PGSSLMODE': 'disable'},
  770. ).once()
  771. module.restore_data_source_dump(
  772. hook_config,
  773. {},
  774. 'test.yaml',
  775. data_source=hook_config[0],
  776. dry_run=False,
  777. extract_process=extract_process,
  778. connection_params={
  779. 'hostname': None,
  780. 'port': None,
  781. 'username': None,
  782. 'password': None,
  783. },
  784. )
  785. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  786. hook_config = [{'name': 'all', 'schemas': None}]
  787. extract_process = flexmock(stdout=flexmock())
  788. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  789. flexmock(module).should_receive('make_dump_path')
  790. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  791. flexmock(module).should_receive('execute_command_with_processes').with_args(
  792. (
  793. 'psql',
  794. '--no-password',
  795. '--no-psqlrc',
  796. ),
  797. processes=[extract_process],
  798. output_log_level=logging.DEBUG,
  799. input_file=extract_process.stdout,
  800. extra_environment={'PGSSLMODE': 'disable'},
  801. ).once()
  802. flexmock(module).should_receive('execute_command').with_args(
  803. ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
  804. extra_environment={'PGSSLMODE': 'disable'},
  805. ).once()
  806. module.restore_data_source_dump(
  807. hook_config,
  808. {},
  809. 'test.yaml',
  810. data_source={'name': 'all'},
  811. dry_run=False,
  812. extract_process=extract_process,
  813. connection_params={
  814. 'hostname': None,
  815. 'port': None,
  816. 'username': None,
  817. 'password': None,
  818. },
  819. )
  820. def test_restore_data_source_dump_runs_psql_for_plain_database_dump():
  821. hook_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
  822. extract_process = flexmock(stdout=flexmock())
  823. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  824. flexmock(module).should_receive('make_dump_path')
  825. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  826. flexmock(module).should_receive('execute_command_with_processes').with_args(
  827. ('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'),
  828. processes=[extract_process],
  829. output_log_level=logging.DEBUG,
  830. input_file=extract_process.stdout,
  831. extra_environment={'PGSSLMODE': 'disable'},
  832. ).once()
  833. flexmock(module).should_receive('execute_command').with_args(
  834. (
  835. 'psql',
  836. '--no-password',
  837. '--no-psqlrc',
  838. '--quiet',
  839. '--dbname',
  840. 'foo',
  841. '--command',
  842. 'ANALYZE',
  843. ),
  844. extra_environment={'PGSSLMODE': 'disable'},
  845. ).once()
  846. module.restore_data_source_dump(
  847. hook_config,
  848. {},
  849. 'test.yaml',
  850. data_source=hook_config[0],
  851. dry_run=False,
  852. extract_process=extract_process,
  853. connection_params={
  854. 'hostname': None,
  855. 'port': None,
  856. 'username': None,
  857. 'password': None,
  858. },
  859. )
  860. def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql():
  861. hook_config = [
  862. {
  863. 'name': 'foo',
  864. 'pg_restore_command': 'docker exec mycontainer pg_restore',
  865. 'psql_command': 'docker exec mycontainer psql',
  866. 'schemas': None,
  867. }
  868. ]
  869. extract_process = flexmock(stdout=flexmock())
  870. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  871. flexmock(module).should_receive('make_dump_path')
  872. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  873. flexmock(module).should_receive('execute_command_with_processes').with_args(
  874. (
  875. 'docker',
  876. 'exec',
  877. 'mycontainer',
  878. 'pg_restore',
  879. '--no-password',
  880. '--if-exists',
  881. '--exit-on-error',
  882. '--clean',
  883. '--dbname',
  884. 'foo',
  885. ),
  886. processes=[extract_process],
  887. output_log_level=logging.DEBUG,
  888. input_file=extract_process.stdout,
  889. extra_environment={'PGSSLMODE': 'disable'},
  890. ).once()
  891. flexmock(module).should_receive('execute_command').with_args(
  892. (
  893. 'docker',
  894. 'exec',
  895. 'mycontainer',
  896. 'psql',
  897. '--no-password',
  898. '--no-psqlrc',
  899. '--quiet',
  900. '--dbname',
  901. 'foo',
  902. '--command',
  903. 'ANALYZE',
  904. ),
  905. extra_environment={'PGSSLMODE': 'disable'},
  906. ).once()
  907. module.restore_data_source_dump(
  908. hook_config,
  909. {},
  910. 'test.yaml',
  911. data_source=hook_config[0],
  912. dry_run=False,
  913. extract_process=extract_process,
  914. connection_params={
  915. 'hostname': None,
  916. 'port': None,
  917. 'username': None,
  918. 'password': None,
  919. },
  920. )
  921. def test_restore_data_source_dump_with_dry_run_skips_restore():
  922. hook_config = [{'name': 'foo', 'schemas': None}]
  923. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  924. flexmock(module).should_receive('make_dump_path')
  925. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  926. flexmock(module).should_receive('execute_command_with_processes').never()
  927. module.restore_data_source_dump(
  928. hook_config,
  929. {},
  930. 'test.yaml',
  931. data_source={'name': 'foo'},
  932. dry_run=True,
  933. extract_process=flexmock(),
  934. connection_params={
  935. 'hostname': None,
  936. 'port': None,
  937. 'username': None,
  938. 'password': None,
  939. },
  940. )
  941. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  942. hook_config = [{'name': 'foo', 'schemas': None}]
  943. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  944. flexmock(module).should_receive('make_dump_path')
  945. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  946. flexmock(module).should_receive('execute_command_with_processes').with_args(
  947. (
  948. 'pg_restore',
  949. '--no-password',
  950. '--if-exists',
  951. '--exit-on-error',
  952. '--clean',
  953. '--dbname',
  954. 'foo',
  955. '/dump/path',
  956. ),
  957. processes=[],
  958. output_log_level=logging.DEBUG,
  959. input_file=None,
  960. extra_environment={'PGSSLMODE': 'disable'},
  961. ).once()
  962. flexmock(module).should_receive('execute_command').with_args(
  963. (
  964. 'psql',
  965. '--no-password',
  966. '--no-psqlrc',
  967. '--quiet',
  968. '--dbname',
  969. 'foo',
  970. '--command',
  971. 'ANALYZE',
  972. ),
  973. extra_environment={'PGSSLMODE': 'disable'},
  974. ).once()
  975. module.restore_data_source_dump(
  976. hook_config,
  977. {},
  978. 'test.yaml',
  979. data_source={'name': 'foo'},
  980. dry_run=False,
  981. extract_process=None,
  982. connection_params={
  983. 'hostname': None,
  984. 'port': None,
  985. 'username': None,
  986. 'password': None,
  987. },
  988. )
  989. def test_restore_data_source_dump_with_schemas_restores_schemas():
  990. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  991. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  992. flexmock(module).should_receive('make_dump_path')
  993. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  994. flexmock(module).should_receive('execute_command_with_processes').with_args(
  995. (
  996. 'pg_restore',
  997. '--no-password',
  998. '--if-exists',
  999. '--exit-on-error',
  1000. '--clean',
  1001. '--dbname',
  1002. 'foo',
  1003. '/dump/path',
  1004. '--schema',
  1005. 'bar',
  1006. '--schema',
  1007. 'baz',
  1008. ),
  1009. processes=[],
  1010. output_log_level=logging.DEBUG,
  1011. input_file=None,
  1012. extra_environment={'PGSSLMODE': 'disable'},
  1013. ).once()
  1014. flexmock(module).should_receive('execute_command').with_args(
  1015. (
  1016. 'psql',
  1017. '--no-password',
  1018. '--no-psqlrc',
  1019. '--quiet',
  1020. '--dbname',
  1021. 'foo',
  1022. '--command',
  1023. 'ANALYZE',
  1024. ),
  1025. extra_environment={'PGSSLMODE': 'disable'},
  1026. ).once()
  1027. module.restore_data_source_dump(
  1028. hook_config,
  1029. {},
  1030. 'test.yaml',
  1031. data_source=hook_config[0],
  1032. dry_run=False,
  1033. extract_process=None,
  1034. connection_params={
  1035. 'hostname': None,
  1036. 'port': None,
  1037. 'username': None,
  1038. 'password': None,
  1039. },
  1040. )