2
0

test_postgresql.py 37 KB

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