test_postgresql.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 = {
  129. 'name': 'all',
  130. 'format': 'custom',
  131. 'psql_command': 'docker exec --workdir * mycontainer psql',
  132. }
  133. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  134. (
  135. 'docker',
  136. 'exec',
  137. '--workdir',
  138. "'*'", # Should get shell escaped to prevent injection attacks.
  139. 'mycontainer',
  140. 'psql',
  141. '--list',
  142. '--no-password',
  143. '--no-psqlrc',
  144. '--csv',
  145. '--tuples-only',
  146. ),
  147. extra_environment=object,
  148. ).and_return('foo,text').once()
  149. assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
  150. 'foo',
  151. )
  152. def test_use_streaming_true_for_any_non_directory_format_databases():
  153. assert module.use_streaming(
  154. databases=[{'format': 'stuff'}, {'format': 'directory'}, {}],
  155. config=flexmock(),
  156. log_prefix=flexmock(),
  157. )
  158. def test_use_streaming_false_for_all_directory_format_databases():
  159. assert not module.use_streaming(
  160. databases=[{'format': 'directory'}, {'format': 'directory'}],
  161. config=flexmock(),
  162. log_prefix=flexmock(),
  163. )
  164. def test_use_streaming_false_for_no_databases():
  165. assert not module.use_streaming(databases=[], config=flexmock(), log_prefix=flexmock())
  166. def test_dump_data_sources_runs_pg_dump_for_each_database():
  167. databases = [{'name': 'foo'}, {'name': 'bar'}]
  168. processes = [flexmock(), flexmock()]
  169. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  170. flexmock(module).should_receive('make_dump_path').and_return('')
  171. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  172. ('bar',)
  173. )
  174. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  175. 'databases/localhost/foo'
  176. ).and_return('databases/localhost/bar')
  177. flexmock(module.os.path).should_receive('exists').and_return(False)
  178. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  179. for name, process in zip(('foo', 'bar'), processes):
  180. flexmock(module).should_receive('execute_command').with_args(
  181. (
  182. 'pg_dump',
  183. '--no-password',
  184. '--clean',
  185. '--if-exists',
  186. '--format',
  187. 'custom',
  188. name,
  189. '>',
  190. f'databases/localhost/{name}',
  191. ),
  192. shell=True,
  193. extra_environment={'PGSSLMODE': 'disable'},
  194. run_to_completion=False,
  195. ).and_return(process).once()
  196. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
  197. def test_dump_data_sources_raises_when_no_database_names_to_dump():
  198. databases = [{'name': 'foo'}, {'name': 'bar'}]
  199. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  200. flexmock(module).should_receive('make_dump_path').and_return('')
  201. flexmock(module).should_receive('database_names_to_dump').and_return(())
  202. with pytest.raises(ValueError):
  203. module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False)
  204. def test_dump_data_sources_does_not_raise_when_no_database_names_to_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(())
  209. module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  210. def test_dump_data_sources_with_duplicate_dump_skips_pg_dump():
  211. databases = [{'name': 'foo'}, {'name': 'bar'}]
  212. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  213. flexmock(module).should_receive('make_dump_path').and_return('')
  214. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  215. ('bar',)
  216. )
  217. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  218. 'databases/localhost/foo'
  219. ).and_return('databases/localhost/bar')
  220. flexmock(module.os.path).should_receive('exists').and_return(True)
  221. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  222. flexmock(module).should_receive('execute_command').never()
  223. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == []
  224. def test_dump_data_sources_with_dry_run_skips_pg_dump():
  225. databases = [{'name': 'foo'}, {'name': 'bar'}]
  226. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  227. flexmock(module).should_receive('make_dump_path').and_return('')
  228. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  229. ('bar',)
  230. )
  231. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  232. 'databases/localhost/foo'
  233. ).and_return('databases/localhost/bar')
  234. flexmock(module.os.path).should_receive('exists').and_return(False)
  235. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  236. flexmock(module).should_receive('execute_command').never()
  237. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
  238. def test_dump_data_sources_runs_pg_dump_with_hostname_and_port():
  239. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  240. process = flexmock()
  241. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  242. flexmock(module).should_receive('make_dump_path').and_return('')
  243. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  244. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  245. 'databases/database.example.org/foo'
  246. )
  247. flexmock(module.os.path).should_receive('exists').and_return(False)
  248. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  249. flexmock(module).should_receive('execute_command').with_args(
  250. (
  251. 'pg_dump',
  252. '--no-password',
  253. '--clean',
  254. '--if-exists',
  255. '--host',
  256. 'database.example.org',
  257. '--port',
  258. '5433',
  259. '--format',
  260. 'custom',
  261. 'foo',
  262. '>',
  263. 'databases/database.example.org/foo',
  264. ),
  265. shell=True,
  266. extra_environment={'PGSSLMODE': 'disable'},
  267. run_to_completion=False,
  268. ).and_return(process).once()
  269. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  270. def test_dump_data_sources_runs_pg_dump_with_username_and_password():
  271. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  272. process = flexmock()
  273. flexmock(module).should_receive('make_extra_environment').and_return(
  274. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  275. )
  276. flexmock(module).should_receive('make_dump_path').and_return('')
  277. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  278. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  279. 'databases/localhost/foo'
  280. )
  281. flexmock(module.os.path).should_receive('exists').and_return(False)
  282. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  283. flexmock(module).should_receive('execute_command').with_args(
  284. (
  285. 'pg_dump',
  286. '--no-password',
  287. '--clean',
  288. '--if-exists',
  289. '--username',
  290. 'postgres',
  291. '--format',
  292. 'custom',
  293. 'foo',
  294. '>',
  295. 'databases/localhost/foo',
  296. ),
  297. shell=True,
  298. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  299. run_to_completion=False,
  300. ).and_return(process).once()
  301. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  302. def test_dump_data_sources_with_username_injection_attack_gets_escaped():
  303. databases = [{'name': 'foo', 'username': 'postgres; naughty-command', 'password': 'trustsome1'}]
  304. process = flexmock()
  305. flexmock(module).should_receive('make_extra_environment').and_return(
  306. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  307. )
  308. flexmock(module).should_receive('make_dump_path').and_return('')
  309. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  310. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  311. 'databases/localhost/foo'
  312. )
  313. flexmock(module.os.path).should_receive('exists').and_return(False)
  314. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  315. flexmock(module).should_receive('execute_command').with_args(
  316. (
  317. 'pg_dump',
  318. '--no-password',
  319. '--clean',
  320. '--if-exists',
  321. '--username',
  322. "'postgres; naughty-command'",
  323. '--format',
  324. 'custom',
  325. 'foo',
  326. '>',
  327. 'databases/localhost/foo',
  328. ),
  329. shell=True,
  330. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  331. run_to_completion=False,
  332. ).and_return(process).once()
  333. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  334. def test_dump_data_sources_runs_pg_dump_with_directory_format():
  335. databases = [{'name': 'foo', 'format': 'directory'}]
  336. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  337. flexmock(module).should_receive('make_dump_path').and_return('')
  338. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  339. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  340. 'databases/localhost/foo'
  341. )
  342. flexmock(module.os.path).should_receive('exists').and_return(False)
  343. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  344. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  345. flexmock(module).should_receive('execute_command').with_args(
  346. (
  347. 'pg_dump',
  348. '--no-password',
  349. '--clean',
  350. '--if-exists',
  351. '--format',
  352. 'directory',
  353. '--file',
  354. 'databases/localhost/foo',
  355. 'foo',
  356. ),
  357. shell=True,
  358. extra_environment={'PGSSLMODE': 'disable'},
  359. ).and_return(flexmock()).once()
  360. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == []
  361. def test_dump_data_sources_runs_pg_dump_with_options():
  362. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  363. process = flexmock()
  364. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  365. flexmock(module).should_receive('make_dump_path').and_return('')
  366. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  367. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  368. 'databases/localhost/foo'
  369. )
  370. flexmock(module.os.path).should_receive('exists').and_return(False)
  371. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  372. flexmock(module).should_receive('execute_command').with_args(
  373. (
  374. 'pg_dump',
  375. '--no-password',
  376. '--clean',
  377. '--if-exists',
  378. '--format',
  379. 'custom',
  380. '--stuff=such',
  381. 'foo',
  382. '>',
  383. 'databases/localhost/foo',
  384. ),
  385. shell=True,
  386. extra_environment={'PGSSLMODE': 'disable'},
  387. run_to_completion=False,
  388. ).and_return(process).once()
  389. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  390. def test_dump_data_sources_runs_pg_dumpall_for_all_databases():
  391. databases = [{'name': 'all'}]
  392. process = flexmock()
  393. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  394. flexmock(module).should_receive('make_dump_path').and_return('')
  395. flexmock(module).should_receive('database_names_to_dump').and_return(('all',))
  396. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  397. 'databases/localhost/all'
  398. )
  399. flexmock(module.os.path).should_receive('exists').and_return(False)
  400. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  401. flexmock(module).should_receive('execute_command').with_args(
  402. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  403. shell=True,
  404. extra_environment={'PGSSLMODE': 'disable'},
  405. run_to_completion=False,
  406. ).and_return(process).once()
  407. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  408. def test_dump_data_sources_runs_non_default_pg_dump():
  409. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump --compress *'}]
  410. process = flexmock()
  411. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  412. flexmock(module).should_receive('make_dump_path').and_return('')
  413. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  414. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
  415. 'databases/localhost/foo'
  416. )
  417. flexmock(module.os.path).should_receive('exists').and_return(False)
  418. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  419. flexmock(module).should_receive('execute_command').with_args(
  420. (
  421. 'special_pg_dump',
  422. '--compress',
  423. "'*'", # Should get shell escaped to prevent injection attacks.
  424. '--no-password',
  425. '--clean',
  426. '--if-exists',
  427. '--format',
  428. 'custom',
  429. 'foo',
  430. '>',
  431. 'databases/localhost/foo',
  432. ),
  433. shell=True,
  434. extra_environment={'PGSSLMODE': 'disable'},
  435. run_to_completion=False,
  436. ).and_return(process).once()
  437. assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
  438. def test_restore_data_source_dump_runs_pg_restore():
  439. hook_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
  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_data_source_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. ),
  454. processes=[extract_process],
  455. output_log_level=logging.DEBUG,
  456. input_file=extract_process.stdout,
  457. extra_environment={'PGSSLMODE': 'disable'},
  458. ).once()
  459. flexmock(module).should_receive('execute_command').with_args(
  460. (
  461. 'psql',
  462. '--no-password',
  463. '--no-psqlrc',
  464. '--quiet',
  465. '--dbname',
  466. 'foo',
  467. '--command',
  468. 'ANALYZE',
  469. ),
  470. extra_environment={'PGSSLMODE': 'disable'},
  471. ).once()
  472. module.restore_data_source_dump(
  473. hook_config,
  474. {},
  475. 'test.yaml',
  476. data_source={'name': 'foo'},
  477. dry_run=False,
  478. extract_process=extract_process,
  479. connection_params={
  480. 'hostname': None,
  481. 'port': None,
  482. 'username': None,
  483. 'password': None,
  484. },
  485. )
  486. def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port():
  487. hook_config = [
  488. {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
  489. ]
  490. extract_process = flexmock(stdout=flexmock())
  491. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  492. flexmock(module).should_receive('make_dump_path')
  493. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  494. flexmock(module).should_receive('execute_command_with_processes').with_args(
  495. (
  496. 'pg_restore',
  497. '--no-password',
  498. '--if-exists',
  499. '--exit-on-error',
  500. '--clean',
  501. '--dbname',
  502. 'foo',
  503. '--host',
  504. 'database.example.org',
  505. '--port',
  506. '5433',
  507. ),
  508. processes=[extract_process],
  509. output_log_level=logging.DEBUG,
  510. input_file=extract_process.stdout,
  511. extra_environment={'PGSSLMODE': 'disable'},
  512. ).once()
  513. flexmock(module).should_receive('execute_command').with_args(
  514. (
  515. 'psql',
  516. '--no-password',
  517. '--no-psqlrc',
  518. '--quiet',
  519. '--host',
  520. 'database.example.org',
  521. '--port',
  522. '5433',
  523. '--dbname',
  524. 'foo',
  525. '--command',
  526. 'ANALYZE',
  527. ),
  528. extra_environment={'PGSSLMODE': 'disable'},
  529. ).once()
  530. module.restore_data_source_dump(
  531. hook_config,
  532. {},
  533. 'test.yaml',
  534. data_source=hook_config[0],
  535. dry_run=False,
  536. extract_process=extract_process,
  537. connection_params={
  538. 'hostname': None,
  539. 'port': None,
  540. 'username': None,
  541. 'password': None,
  542. },
  543. )
  544. def test_restore_data_source_dump_runs_pg_restore_with_username_and_password():
  545. hook_config = [
  546. {'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None}
  547. ]
  548. extract_process = flexmock(stdout=flexmock())
  549. flexmock(module).should_receive('make_extra_environment').and_return(
  550. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  551. )
  552. flexmock(module).should_receive('make_dump_path')
  553. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  554. flexmock(module).should_receive('execute_command_with_processes').with_args(
  555. (
  556. 'pg_restore',
  557. '--no-password',
  558. '--if-exists',
  559. '--exit-on-error',
  560. '--clean',
  561. '--dbname',
  562. 'foo',
  563. '--username',
  564. 'postgres',
  565. ),
  566. processes=[extract_process],
  567. output_log_level=logging.DEBUG,
  568. input_file=extract_process.stdout,
  569. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  570. ).once()
  571. flexmock(module).should_receive('execute_command').with_args(
  572. (
  573. 'psql',
  574. '--no-password',
  575. '--no-psqlrc',
  576. '--quiet',
  577. '--username',
  578. 'postgres',
  579. '--dbname',
  580. 'foo',
  581. '--command',
  582. 'ANALYZE',
  583. ),
  584. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  585. ).once()
  586. module.restore_data_source_dump(
  587. hook_config,
  588. {},
  589. 'test.yaml',
  590. data_source=hook_config[0],
  591. dry_run=False,
  592. extract_process=extract_process,
  593. connection_params={
  594. 'hostname': None,
  595. 'port': None,
  596. 'username': None,
  597. 'password': None,
  598. },
  599. )
  600. def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
  601. hook_config = [
  602. {
  603. 'name': 'foo',
  604. 'hostname': 'database.example.org',
  605. 'port': 5433,
  606. 'username': 'postgres',
  607. 'password': 'trustsome1',
  608. 'restore_hostname': 'restorehost',
  609. 'restore_port': 'restoreport',
  610. 'restore_username': 'restoreusername',
  611. 'restore_password': 'restorepassword',
  612. 'schemas': None,
  613. }
  614. ]
  615. extract_process = flexmock(stdout=flexmock())
  616. flexmock(module).should_receive('make_extra_environment').and_return(
  617. {'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}
  618. )
  619. flexmock(module).should_receive('make_dump_path')
  620. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  621. flexmock(module).should_receive('execute_command_with_processes').with_args(
  622. (
  623. 'pg_restore',
  624. '--no-password',
  625. '--if-exists',
  626. '--exit-on-error',
  627. '--clean',
  628. '--dbname',
  629. 'foo',
  630. '--host',
  631. 'clihost',
  632. '--port',
  633. 'cliport',
  634. '--username',
  635. 'cliusername',
  636. ),
  637. processes=[extract_process],
  638. output_log_level=logging.DEBUG,
  639. input_file=extract_process.stdout,
  640. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  641. ).once()
  642. flexmock(module).should_receive('execute_command').with_args(
  643. (
  644. 'psql',
  645. '--no-password',
  646. '--no-psqlrc',
  647. '--quiet',
  648. '--host',
  649. 'clihost',
  650. '--port',
  651. 'cliport',
  652. '--username',
  653. 'cliusername',
  654. '--dbname',
  655. 'foo',
  656. '--command',
  657. 'ANALYZE',
  658. ),
  659. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  660. ).once()
  661. module.restore_data_source_dump(
  662. hook_config,
  663. {},
  664. 'test.yaml',
  665. data_source={'name': 'foo'},
  666. dry_run=False,
  667. extract_process=extract_process,
  668. connection_params={
  669. 'hostname': 'clihost',
  670. 'port': 'cliport',
  671. 'username': 'cliusername',
  672. 'password': 'clipassword',
  673. },
  674. )
  675. def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  676. hook_config = [
  677. {
  678. 'name': 'foo',
  679. 'hostname': 'database.example.org',
  680. 'port': 5433,
  681. 'username': 'postgres',
  682. 'password': 'trustsome1',
  683. 'schemas': None,
  684. 'restore_hostname': 'restorehost',
  685. 'restore_port': 'restoreport',
  686. 'restore_username': 'restoreusername',
  687. 'restore_password': 'restorepassword',
  688. }
  689. ]
  690. extract_process = flexmock(stdout=flexmock())
  691. flexmock(module).should_receive('make_extra_environment').and_return(
  692. {'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}
  693. )
  694. flexmock(module).should_receive('make_dump_path')
  695. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  696. flexmock(module).should_receive('execute_command_with_processes').with_args(
  697. (
  698. 'pg_restore',
  699. '--no-password',
  700. '--if-exists',
  701. '--exit-on-error',
  702. '--clean',
  703. '--dbname',
  704. 'foo',
  705. '--host',
  706. 'restorehost',
  707. '--port',
  708. 'restoreport',
  709. '--username',
  710. 'restoreusername',
  711. ),
  712. processes=[extract_process],
  713. output_log_level=logging.DEBUG,
  714. input_file=extract_process.stdout,
  715. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  716. ).once()
  717. flexmock(module).should_receive('execute_command').with_args(
  718. (
  719. 'psql',
  720. '--no-password',
  721. '--no-psqlrc',
  722. '--quiet',
  723. '--host',
  724. 'restorehost',
  725. '--port',
  726. 'restoreport',
  727. '--username',
  728. 'restoreusername',
  729. '--dbname',
  730. 'foo',
  731. '--command',
  732. 'ANALYZE',
  733. ),
  734. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  735. ).once()
  736. module.restore_data_source_dump(
  737. hook_config,
  738. {},
  739. 'test.yaml',
  740. data_source=hook_config[0],
  741. dry_run=False,
  742. extract_process=extract_process,
  743. connection_params={
  744. 'hostname': None,
  745. 'port': None,
  746. 'username': None,
  747. 'password': None,
  748. },
  749. )
  750. def test_restore_data_source_dump_runs_pg_restore_with_options():
  751. hook_config = [
  752. {
  753. 'name': 'foo',
  754. 'restore_options': '--harder',
  755. 'analyze_options': '--smarter',
  756. 'schemas': None,
  757. }
  758. ]
  759. extract_process = flexmock(stdout=flexmock())
  760. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  761. flexmock(module).should_receive('make_dump_path')
  762. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  763. flexmock(module).should_receive('execute_command_with_processes').with_args(
  764. (
  765. 'pg_restore',
  766. '--no-password',
  767. '--if-exists',
  768. '--exit-on-error',
  769. '--clean',
  770. '--dbname',
  771. 'foo',
  772. '--harder',
  773. ),
  774. processes=[extract_process],
  775. output_log_level=logging.DEBUG,
  776. input_file=extract_process.stdout,
  777. extra_environment={'PGSSLMODE': 'disable'},
  778. ).once()
  779. flexmock(module).should_receive('execute_command').with_args(
  780. (
  781. 'psql',
  782. '--no-password',
  783. '--no-psqlrc',
  784. '--quiet',
  785. '--dbname',
  786. 'foo',
  787. '--smarter',
  788. '--command',
  789. 'ANALYZE',
  790. ),
  791. extra_environment={'PGSSLMODE': 'disable'},
  792. ).once()
  793. module.restore_data_source_dump(
  794. hook_config,
  795. {},
  796. 'test.yaml',
  797. data_source=hook_config[0],
  798. dry_run=False,
  799. extract_process=extract_process,
  800. connection_params={
  801. 'hostname': None,
  802. 'port': None,
  803. 'username': None,
  804. 'password': None,
  805. },
  806. )
  807. def test_restore_data_source_dump_runs_psql_for_all_database_dump():
  808. hook_config = [{'name': 'all', 'schemas': None}]
  809. extract_process = flexmock(stdout=flexmock())
  810. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  811. flexmock(module).should_receive('make_dump_path')
  812. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  813. flexmock(module).should_receive('execute_command_with_processes').with_args(
  814. (
  815. 'psql',
  816. '--no-password',
  817. '--no-psqlrc',
  818. ),
  819. processes=[extract_process],
  820. output_log_level=logging.DEBUG,
  821. input_file=extract_process.stdout,
  822. extra_environment={'PGSSLMODE': 'disable'},
  823. ).once()
  824. flexmock(module).should_receive('execute_command').with_args(
  825. ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
  826. extra_environment={'PGSSLMODE': 'disable'},
  827. ).once()
  828. module.restore_data_source_dump(
  829. hook_config,
  830. {},
  831. 'test.yaml',
  832. data_source={'name': 'all'},
  833. dry_run=False,
  834. extract_process=extract_process,
  835. connection_params={
  836. 'hostname': None,
  837. 'port': None,
  838. 'username': None,
  839. 'password': None,
  840. },
  841. )
  842. def test_restore_data_source_dump_runs_psql_for_plain_database_dump():
  843. hook_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
  844. extract_process = flexmock(stdout=flexmock())
  845. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  846. flexmock(module).should_receive('make_dump_path')
  847. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  848. flexmock(module).should_receive('execute_command_with_processes').with_args(
  849. ('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'),
  850. processes=[extract_process],
  851. output_log_level=logging.DEBUG,
  852. input_file=extract_process.stdout,
  853. extra_environment={'PGSSLMODE': 'disable'},
  854. ).once()
  855. flexmock(module).should_receive('execute_command').with_args(
  856. (
  857. 'psql',
  858. '--no-password',
  859. '--no-psqlrc',
  860. '--quiet',
  861. '--dbname',
  862. 'foo',
  863. '--command',
  864. 'ANALYZE',
  865. ),
  866. extra_environment={'PGSSLMODE': 'disable'},
  867. ).once()
  868. module.restore_data_source_dump(
  869. hook_config,
  870. {},
  871. 'test.yaml',
  872. data_source=hook_config[0],
  873. dry_run=False,
  874. extract_process=extract_process,
  875. connection_params={
  876. 'hostname': None,
  877. 'port': None,
  878. 'username': None,
  879. 'password': None,
  880. },
  881. )
  882. def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql():
  883. hook_config = [
  884. {
  885. 'name': 'foo',
  886. 'pg_restore_command': 'docker exec --workdir * mycontainer pg_restore',
  887. 'psql_command': 'docker exec --workdir * mycontainer psql',
  888. 'schemas': None,
  889. }
  890. ]
  891. extract_process = flexmock(stdout=flexmock())
  892. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  893. flexmock(module).should_receive('make_dump_path')
  894. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  895. flexmock(module).should_receive('execute_command_with_processes').with_args(
  896. (
  897. 'docker',
  898. 'exec',
  899. '--workdir',
  900. "'*'", # Should get shell escaped to prevent injection attacks.
  901. 'mycontainer',
  902. 'pg_restore',
  903. '--no-password',
  904. '--if-exists',
  905. '--exit-on-error',
  906. '--clean',
  907. '--dbname',
  908. 'foo',
  909. ),
  910. processes=[extract_process],
  911. output_log_level=logging.DEBUG,
  912. input_file=extract_process.stdout,
  913. extra_environment={'PGSSLMODE': 'disable'},
  914. ).once()
  915. flexmock(module).should_receive('execute_command').with_args(
  916. (
  917. 'docker',
  918. 'exec',
  919. '--workdir',
  920. "'*'", # Should get shell escaped to prevent injection attacks.
  921. 'mycontainer',
  922. 'psql',
  923. '--no-password',
  924. '--no-psqlrc',
  925. '--quiet',
  926. '--dbname',
  927. 'foo',
  928. '--command',
  929. 'ANALYZE',
  930. ),
  931. extra_environment={'PGSSLMODE': 'disable'},
  932. ).once()
  933. module.restore_data_source_dump(
  934. hook_config,
  935. {},
  936. 'test.yaml',
  937. data_source=hook_config[0],
  938. dry_run=False,
  939. extract_process=extract_process,
  940. connection_params={
  941. 'hostname': None,
  942. 'port': None,
  943. 'username': None,
  944. 'password': None,
  945. },
  946. )
  947. def test_restore_data_source_dump_with_dry_run_skips_restore():
  948. hook_config = [{'name': 'foo', 'schemas': None}]
  949. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  950. flexmock(module).should_receive('make_dump_path')
  951. flexmock(module.dump).should_receive('make_data_source_dump_filename')
  952. flexmock(module).should_receive('execute_command_with_processes').never()
  953. module.restore_data_source_dump(
  954. hook_config,
  955. {},
  956. 'test.yaml',
  957. data_source={'name': 'foo'},
  958. dry_run=True,
  959. extract_process=flexmock(),
  960. connection_params={
  961. 'hostname': None,
  962. 'port': None,
  963. 'username': None,
  964. 'password': None,
  965. },
  966. )
  967. def test_restore_data_source_dump_without_extract_process_restores_from_disk():
  968. hook_config = [{'name': 'foo', 'schemas': None}]
  969. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  970. flexmock(module).should_receive('make_dump_path')
  971. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  972. flexmock(module).should_receive('execute_command_with_processes').with_args(
  973. (
  974. 'pg_restore',
  975. '--no-password',
  976. '--if-exists',
  977. '--exit-on-error',
  978. '--clean',
  979. '--dbname',
  980. 'foo',
  981. '/dump/path',
  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_data_source_dump(
  1002. hook_config,
  1003. {},
  1004. 'test.yaml',
  1005. data_source={'name': 'foo'},
  1006. dry_run=False,
  1007. extract_process=None,
  1008. connection_params={
  1009. 'hostname': None,
  1010. 'port': None,
  1011. 'username': None,
  1012. 'password': None,
  1013. },
  1014. )
  1015. def test_restore_data_source_dump_with_schemas_restores_schemas():
  1016. hook_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  1017. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  1018. flexmock(module).should_receive('make_dump_path')
  1019. flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path')
  1020. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1021. (
  1022. 'pg_restore',
  1023. '--no-password',
  1024. '--if-exists',
  1025. '--exit-on-error',
  1026. '--clean',
  1027. '--dbname',
  1028. 'foo',
  1029. '/dump/path',
  1030. '--schema',
  1031. 'bar',
  1032. '--schema',
  1033. 'baz',
  1034. ),
  1035. processes=[],
  1036. output_log_level=logging.DEBUG,
  1037. input_file=None,
  1038. extra_environment={'PGSSLMODE': 'disable'},
  1039. ).once()
  1040. flexmock(module).should_receive('execute_command').with_args(
  1041. (
  1042. 'psql',
  1043. '--no-password',
  1044. '--no-psqlrc',
  1045. '--quiet',
  1046. '--dbname',
  1047. 'foo',
  1048. '--command',
  1049. 'ANALYZE',
  1050. ),
  1051. extra_environment={'PGSSLMODE': 'disable'},
  1052. ).once()
  1053. module.restore_data_source_dump(
  1054. hook_config,
  1055. {},
  1056. 'test.yaml',
  1057. data_source=hook_config[0],
  1058. dry_run=False,
  1059. extract_process=None,
  1060. connection_params={
  1061. 'hostname': None,
  1062. 'port': None,
  1063. 'username': None,
  1064. 'password': None,
  1065. },
  1066. )