test_postgresql.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. 'schemas': None,
  563. }
  564. ]
  565. extract_process = flexmock(stdout=flexmock())
  566. flexmock(module).should_receive('make_extra_environment').and_return(
  567. {'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}
  568. )
  569. flexmock(module).should_receive('make_dump_path')
  570. flexmock(module.dump).should_receive('make_database_dump_filename')
  571. flexmock(module).should_receive('execute_command_with_processes').with_args(
  572. (
  573. 'pg_restore',
  574. '--no-password',
  575. '--if-exists',
  576. '--exit-on-error',
  577. '--clean',
  578. '--dbname',
  579. 'foo',
  580. '--host',
  581. 'clihost',
  582. '--port',
  583. 'cliport',
  584. '--username',
  585. 'cliusername',
  586. ),
  587. processes=[extract_process],
  588. output_log_level=logging.DEBUG,
  589. input_file=extract_process.stdout,
  590. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  591. ).once()
  592. flexmock(module).should_receive('execute_command').with_args(
  593. (
  594. 'psql',
  595. '--no-password',
  596. '--no-psqlrc',
  597. '--quiet',
  598. '--host',
  599. 'clihost',
  600. '--port',
  601. 'cliport',
  602. '--username',
  603. 'cliusername',
  604. '--dbname',
  605. 'foo',
  606. '--command',
  607. 'ANALYZE',
  608. ),
  609. extra_environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'},
  610. ).once()
  611. module.restore_database_dump(
  612. database_config,
  613. 'test.yaml',
  614. {},
  615. dry_run=False,
  616. extract_process=extract_process,
  617. connection_params={
  618. 'hostname': 'clihost',
  619. 'port': 'cliport',
  620. 'username': 'cliusername',
  621. 'password': 'clipassword',
  622. },
  623. )
  624. def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
  625. database_config = [
  626. {
  627. 'name': 'foo',
  628. 'hostname': 'database.example.org',
  629. 'port': 5433,
  630. 'username': 'postgres',
  631. 'password': 'trustsome1',
  632. 'schemas': None,
  633. 'restore_hostname': 'restorehost',
  634. 'restore_port': 'restoreport',
  635. 'restore_username': 'restoreusername',
  636. 'restore_password': 'restorepassword',
  637. }
  638. ]
  639. extract_process = flexmock(stdout=flexmock())
  640. flexmock(module).should_receive('make_extra_environment').and_return(
  641. {'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}
  642. )
  643. flexmock(module).should_receive('make_dump_path')
  644. flexmock(module.dump).should_receive('make_database_dump_filename')
  645. flexmock(module).should_receive('execute_command_with_processes').with_args(
  646. (
  647. 'pg_restore',
  648. '--no-password',
  649. '--if-exists',
  650. '--exit-on-error',
  651. '--clean',
  652. '--dbname',
  653. 'foo',
  654. '--host',
  655. 'restorehost',
  656. '--port',
  657. 'restoreport',
  658. '--username',
  659. 'restoreusername',
  660. ),
  661. processes=[extract_process],
  662. output_log_level=logging.DEBUG,
  663. input_file=extract_process.stdout,
  664. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  665. ).once()
  666. flexmock(module).should_receive('execute_command').with_args(
  667. (
  668. 'psql',
  669. '--no-password',
  670. '--no-psqlrc',
  671. '--quiet',
  672. '--host',
  673. 'restorehost',
  674. '--port',
  675. 'restoreport',
  676. '--username',
  677. 'restoreusername',
  678. '--dbname',
  679. 'foo',
  680. '--command',
  681. 'ANALYZE',
  682. ),
  683. extra_environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'},
  684. ).once()
  685. module.restore_database_dump(
  686. database_config,
  687. 'test.yaml',
  688. {},
  689. dry_run=False,
  690. extract_process=extract_process,
  691. connection_params={
  692. 'hostname': None,
  693. 'port': None,
  694. 'username': None,
  695. 'password': None,
  696. },
  697. )
  698. def test_restore_database_dump_with_no_passwords_runs_pg_restore_without_password():
  699. database_config = [{'name': 'foo', 'username': 'postgres', 'schemas': None}]
  700. extract_process = flexmock(stdout=flexmock())
  701. flexmock(module).should_receive('make_dump_path')
  702. flexmock(module.dump).should_receive('make_database_dump_filename')
  703. flexmock(module).should_receive('execute_command_with_processes').with_args(
  704. (
  705. 'pg_restore',
  706. '--no-password',
  707. '--if-exists',
  708. '--exit-on-error',
  709. '--clean',
  710. '--dbname',
  711. 'foo',
  712. '--username',
  713. 'postgres',
  714. ),
  715. processes=[extract_process],
  716. output_log_level=logging.DEBUG,
  717. input_file=extract_process.stdout,
  718. extra_environment={'PGSSLMODE': 'disable'},
  719. ).once()
  720. flexmock(module).should_receive('execute_command').with_args(
  721. (
  722. 'psql',
  723. '--no-password',
  724. '--no-psqlrc',
  725. '--quiet',
  726. '--username',
  727. 'postgres',
  728. '--dbname',
  729. 'foo',
  730. '--command',
  731. 'ANALYZE',
  732. ),
  733. extra_environment={'PGSSLMODE': 'disable'},
  734. ).once()
  735. module.restore_database_dump(
  736. database_config,
  737. 'test.yaml',
  738. {},
  739. dry_run=False,
  740. extract_process=extract_process,
  741. connection_params={
  742. 'hostname': None,
  743. 'port': None,
  744. 'username': None,
  745. 'password': None,
  746. },
  747. )
  748. def test_restore_database_dump_runs_pg_restore_with_options():
  749. database_config = [
  750. {
  751. 'name': 'foo',
  752. 'restore_options': '--harder',
  753. 'analyze_options': '--smarter',
  754. 'schemas': None,
  755. }
  756. ]
  757. extract_process = flexmock(stdout=flexmock())
  758. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  759. flexmock(module).should_receive('make_dump_path')
  760. flexmock(module.dump).should_receive('make_database_dump_filename')
  761. flexmock(module).should_receive('execute_command_with_processes').with_args(
  762. (
  763. 'pg_restore',
  764. '--no-password',
  765. '--if-exists',
  766. '--exit-on-error',
  767. '--clean',
  768. '--dbname',
  769. 'foo',
  770. '--harder',
  771. ),
  772. processes=[extract_process],
  773. output_log_level=logging.DEBUG,
  774. input_file=extract_process.stdout,
  775. extra_environment={'PGSSLMODE': 'disable'},
  776. ).once()
  777. flexmock(module).should_receive('execute_command').with_args(
  778. (
  779. 'psql',
  780. '--no-password',
  781. '--no-psqlrc',
  782. '--quiet',
  783. '--dbname',
  784. 'foo',
  785. '--smarter',
  786. '--command',
  787. 'ANALYZE',
  788. ),
  789. extra_environment={'PGSSLMODE': 'disable'},
  790. ).once()
  791. module.restore_database_dump(
  792. database_config,
  793. 'test.yaml',
  794. {},
  795. dry_run=False,
  796. extract_process=extract_process,
  797. connection_params={
  798. 'hostname': None,
  799. 'port': None,
  800. 'username': None,
  801. 'password': None,
  802. },
  803. )
  804. def test_restore_database_dump_runs_psql_for_all_database_dump():
  805. database_config = [{'name': 'all', 'schemas': None}]
  806. extract_process = flexmock(stdout=flexmock())
  807. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  808. flexmock(module).should_receive('make_dump_path')
  809. flexmock(module.dump).should_receive('make_database_dump_filename')
  810. flexmock(module).should_receive('execute_command_with_processes').with_args(
  811. (
  812. 'psql',
  813. '--no-password',
  814. '--no-psqlrc',
  815. ),
  816. processes=[extract_process],
  817. output_log_level=logging.DEBUG,
  818. input_file=extract_process.stdout,
  819. extra_environment={'PGSSLMODE': 'disable'},
  820. ).once()
  821. flexmock(module).should_receive('execute_command').with_args(
  822. ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
  823. extra_environment={'PGSSLMODE': 'disable'},
  824. ).once()
  825. module.restore_database_dump(
  826. database_config,
  827. 'test.yaml',
  828. {},
  829. dry_run=False,
  830. extract_process=extract_process,
  831. connection_params={
  832. 'hostname': None,
  833. 'port': None,
  834. 'username': None,
  835. 'password': None,
  836. },
  837. )
  838. def test_restore_database_dump_runs_psql_for_plain_database_dump():
  839. database_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
  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. ('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'),
  846. processes=[extract_process],
  847. output_log_level=logging.DEBUG,
  848. input_file=extract_process.stdout,
  849. extra_environment={'PGSSLMODE': 'disable'},
  850. ).once()
  851. flexmock(module).should_receive('execute_command').with_args(
  852. (
  853. 'psql',
  854. '--no-password',
  855. '--no-psqlrc',
  856. '--quiet',
  857. '--dbname',
  858. 'foo',
  859. '--command',
  860. 'ANALYZE',
  861. ),
  862. extra_environment={'PGSSLMODE': 'disable'},
  863. ).once()
  864. module.restore_database_dump(
  865. database_config,
  866. 'test.yaml',
  867. {},
  868. dry_run=False,
  869. extract_process=extract_process,
  870. connection_params={
  871. 'hostname': None,
  872. 'port': None,
  873. 'username': None,
  874. 'password': None,
  875. },
  876. )
  877. def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
  878. database_config = [
  879. {
  880. 'name': 'foo',
  881. 'pg_restore_command': 'docker exec mycontainer pg_restore',
  882. 'psql_command': 'docker exec mycontainer psql',
  883. 'schemas': None,
  884. }
  885. ]
  886. extract_process = flexmock(stdout=flexmock())
  887. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  888. flexmock(module).should_receive('make_dump_path')
  889. flexmock(module.dump).should_receive('make_database_dump_filename')
  890. flexmock(module).should_receive('execute_command_with_processes').with_args(
  891. (
  892. 'docker',
  893. 'exec',
  894. 'mycontainer',
  895. 'pg_restore',
  896. '--no-password',
  897. '--if-exists',
  898. '--exit-on-error',
  899. '--clean',
  900. '--dbname',
  901. 'foo',
  902. ),
  903. processes=[extract_process],
  904. output_log_level=logging.DEBUG,
  905. input_file=extract_process.stdout,
  906. extra_environment={'PGSSLMODE': 'disable'},
  907. ).once()
  908. flexmock(module).should_receive('execute_command').with_args(
  909. (
  910. 'docker',
  911. 'exec',
  912. 'mycontainer',
  913. 'psql',
  914. '--no-password',
  915. '--no-psqlrc',
  916. '--quiet',
  917. '--dbname',
  918. 'foo',
  919. '--command',
  920. 'ANALYZE',
  921. ),
  922. extra_environment={'PGSSLMODE': 'disable'},
  923. ).once()
  924. module.restore_database_dump(
  925. database_config,
  926. 'test.yaml',
  927. {},
  928. dry_run=False,
  929. extract_process=extract_process,
  930. connection_params={
  931. 'hostname': None,
  932. 'port': None,
  933. 'username': None,
  934. 'password': None,
  935. },
  936. )
  937. def test_restore_database_dump_with_dry_run_skips_restore():
  938. database_config = [{'name': 'foo', 'schemas': None}]
  939. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  940. flexmock(module).should_receive('make_dump_path')
  941. flexmock(module.dump).should_receive('make_database_dump_filename')
  942. flexmock(module).should_receive('execute_command_with_processes').never()
  943. module.restore_database_dump(
  944. database_config,
  945. 'test.yaml',
  946. {},
  947. dry_run=True,
  948. extract_process=flexmock(),
  949. connection_params={
  950. 'hostname': None,
  951. 'port': None,
  952. 'username': None,
  953. 'password': None,
  954. },
  955. )
  956. def test_restore_database_dump_without_extract_process_restores_from_disk():
  957. database_config = [{'name': 'foo', 'schemas': None}]
  958. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  959. flexmock(module).should_receive('make_dump_path')
  960. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  961. flexmock(module).should_receive('execute_command_with_processes').with_args(
  962. (
  963. 'pg_restore',
  964. '--no-password',
  965. '--if-exists',
  966. '--exit-on-error',
  967. '--clean',
  968. '--dbname',
  969. 'foo',
  970. '/dump/path',
  971. ),
  972. processes=[],
  973. output_log_level=logging.DEBUG,
  974. input_file=None,
  975. extra_environment={'PGSSLMODE': 'disable'},
  976. ).once()
  977. flexmock(module).should_receive('execute_command').with_args(
  978. (
  979. 'psql',
  980. '--no-password',
  981. '--no-psqlrc',
  982. '--quiet',
  983. '--dbname',
  984. 'foo',
  985. '--command',
  986. 'ANALYZE',
  987. ),
  988. extra_environment={'PGSSLMODE': 'disable'},
  989. ).once()
  990. module.restore_database_dump(
  991. database_config,
  992. 'test.yaml',
  993. {},
  994. dry_run=False,
  995. extract_process=None,
  996. connection_params={
  997. 'hostname': None,
  998. 'port': None,
  999. 'username': None,
  1000. 'password': None,
  1001. },
  1002. )
  1003. def test_restore_database_dump_with_schemas_restores_schemas():
  1004. database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
  1005. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  1006. flexmock(module).should_receive('make_dump_path')
  1007. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  1008. flexmock(module).should_receive('execute_command_with_processes').with_args(
  1009. (
  1010. 'pg_restore',
  1011. '--no-password',
  1012. '--if-exists',
  1013. '--exit-on-error',
  1014. '--clean',
  1015. '--dbname',
  1016. 'foo',
  1017. '/dump/path',
  1018. '--schema',
  1019. 'bar',
  1020. '--schema',
  1021. 'baz',
  1022. ),
  1023. processes=[],
  1024. output_log_level=logging.DEBUG,
  1025. input_file=None,
  1026. extra_environment={'PGSSLMODE': 'disable'},
  1027. ).once()
  1028. flexmock(module).should_receive('execute_command').with_args(
  1029. (
  1030. 'psql',
  1031. '--no-password',
  1032. '--no-psqlrc',
  1033. '--quiet',
  1034. '--dbname',
  1035. 'foo',
  1036. '--command',
  1037. 'ANALYZE',
  1038. ),
  1039. extra_environment={'PGSSLMODE': 'disable'},
  1040. ).once()
  1041. module.restore_database_dump(
  1042. database_config,
  1043. 'test.yaml',
  1044. {},
  1045. dry_run=False,
  1046. extract_process=None,
  1047. connection_params={
  1048. 'hostname': None,
  1049. 'port': None,
  1050. 'username': None,
  1051. 'password': None,
  1052. },
  1053. )