test_postgresql.py 37 KB

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