2
0

test_postgresql.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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(), flexmock()) == ('foo',)
  8. def test_database_names_to_dump_passes_through_individual_database_name_with_format():
  9. database = {'name': 'foo', 'format': 'custom'}
  10. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == ('foo',)
  11. def test_database_names_to_dump_passes_through_all_without_format():
  12. database = {'name': 'all'}
  13. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == ('all',)
  14. def test_database_names_to_dump_with_all_and_format_lists_databases():
  15. database = {'name': 'all', 'format': 'custom'}
  16. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  17. 'foo,test,\nbar,test,"stuff and such"'
  18. )
  19. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == (
  20. 'foo',
  21. 'bar',
  22. )
  23. def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostname_and_port():
  24. database = {'name': 'all', 'format': 'custom', 'hostname': 'localhost', 'port': 1234}
  25. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  26. (
  27. 'psql',
  28. '--list',
  29. '--no-password',
  30. '--csv',
  31. '--tuples-only',
  32. '--host',
  33. 'localhost',
  34. '--port',
  35. '1234',
  36. ),
  37. extra_environment=object,
  38. ).and_return('foo,test,\nbar,test,"stuff and such"')
  39. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == (
  40. 'foo',
  41. 'bar',
  42. )
  43. def test_database_names_to_dump_with_all_and_format_lists_databases_with_username():
  44. database = {'name': 'all', 'format': 'custom', 'username': 'postgres'}
  45. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  46. ('psql', '--list', '--no-password', '--csv', '--tuples-only', '--username', 'postgres'),
  47. extra_environment=object,
  48. ).and_return('foo,test,\nbar,test,"stuff and such"')
  49. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == (
  50. 'foo',
  51. 'bar',
  52. )
  53. def test_database_names_to_dump_with_all_and_format_lists_databases_with_options():
  54. database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'}
  55. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  56. ('psql', '--list', '--no-password', '--csv', '--tuples-only', '--harder'),
  57. extra_environment=object,
  58. ).and_return('foo,test,\nbar,test,"stuff and such"')
  59. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == (
  60. 'foo',
  61. 'bar',
  62. )
  63. def test_database_names_to_dump_with_all_and_format_excludes_particular_databases():
  64. database = {'name': 'all', 'format': 'custom'}
  65. flexmock(module).should_receive('execute_command_and_capture_output').and_return(
  66. 'foo,test,\ntemplate0,test,blah'
  67. )
  68. assert module.database_names_to_dump(database, flexmock(), flexmock(), flexmock()) == ('foo',)
  69. def test_dump_databases_runs_pg_dump_for_each_database():
  70. databases = [{'name': 'foo'}, {'name': 'bar'}]
  71. processes = [flexmock(), flexmock()]
  72. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  73. flexmock(module).should_receive('make_dump_path').and_return('')
  74. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  75. ('bar',)
  76. )
  77. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  78. 'databases/localhost/foo'
  79. ).and_return('databases/localhost/bar')
  80. flexmock(module.os.path).should_receive('exists').and_return(False)
  81. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  82. for name, process in zip(('foo', 'bar'), processes):
  83. flexmock(module).should_receive('execute_command').with_args(
  84. (
  85. 'pg_dump',
  86. '--no-password',
  87. '--clean',
  88. '--if-exists',
  89. '--format',
  90. 'custom',
  91. name,
  92. '>',
  93. 'databases/localhost/{}'.format(name),
  94. ),
  95. shell=True,
  96. extra_environment={'PGSSLMODE': 'disable'},
  97. run_to_completion=False,
  98. ).and_return(process).once()
  99. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  100. def test_dump_databases_raises_when_no_database_names_to_dump():
  101. databases = [{'name': 'foo'}, {'name': 'bar'}]
  102. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  103. flexmock(module).should_receive('make_dump_path').and_return('')
  104. flexmock(module).should_receive('database_names_to_dump').and_return(())
  105. with pytest.raises(ValueError):
  106. module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
  107. def test_dump_databases_with_duplicate_dump_skips_pg_dump():
  108. databases = [{'name': 'foo'}, {'name': 'bar'}]
  109. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  110. flexmock(module).should_receive('make_dump_path').and_return('')
  111. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  112. ('bar',)
  113. )
  114. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  115. 'databases/localhost/foo'
  116. ).and_return('databases/localhost/bar')
  117. flexmock(module.os.path).should_receive('exists').and_return(True)
  118. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  119. flexmock(module).should_receive('execute_command').never()
  120. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
  121. def test_dump_databases_with_dry_run_skips_pg_dump():
  122. databases = [{'name': 'foo'}, {'name': 'bar'}]
  123. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  124. flexmock(module).should_receive('make_dump_path').and_return('')
  125. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
  126. ('bar',)
  127. )
  128. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  129. 'databases/localhost/foo'
  130. ).and_return('databases/localhost/bar')
  131. flexmock(module.os.path).should_receive('exists').and_return(False)
  132. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  133. flexmock(module).should_receive('execute_command').never()
  134. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  135. def test_dump_databases_runs_pg_dump_with_hostname_and_port():
  136. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  137. process = flexmock()
  138. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  139. flexmock(module).should_receive('make_dump_path').and_return('')
  140. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  141. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  142. 'databases/database.example.org/foo'
  143. )
  144. flexmock(module.os.path).should_receive('exists').and_return(False)
  145. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  146. flexmock(module).should_receive('execute_command').with_args(
  147. (
  148. 'pg_dump',
  149. '--no-password',
  150. '--clean',
  151. '--if-exists',
  152. '--host',
  153. 'database.example.org',
  154. '--port',
  155. '5433',
  156. '--format',
  157. 'custom',
  158. 'foo',
  159. '>',
  160. 'databases/database.example.org/foo',
  161. ),
  162. shell=True,
  163. extra_environment={'PGSSLMODE': 'disable'},
  164. run_to_completion=False,
  165. ).and_return(process).once()
  166. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  167. def test_dump_databases_runs_pg_dump_with_username_and_password():
  168. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  169. process = flexmock()
  170. flexmock(module).should_receive('make_extra_environment').and_return(
  171. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  172. )
  173. flexmock(module).should_receive('make_dump_path').and_return('')
  174. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  175. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  176. 'databases/localhost/foo'
  177. )
  178. flexmock(module.os.path).should_receive('exists').and_return(False)
  179. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  180. flexmock(module).should_receive('execute_command').with_args(
  181. (
  182. 'pg_dump',
  183. '--no-password',
  184. '--clean',
  185. '--if-exists',
  186. '--username',
  187. 'postgres',
  188. '--format',
  189. 'custom',
  190. 'foo',
  191. '>',
  192. 'databases/localhost/foo',
  193. ),
  194. shell=True,
  195. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  196. run_to_completion=False,
  197. ).and_return(process).once()
  198. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  199. def test_make_extra_environment_maps_options_to_environment():
  200. database = {
  201. 'name': 'foo',
  202. 'password': 'pass',
  203. 'ssl_mode': 'require',
  204. 'ssl_cert': 'cert.crt',
  205. 'ssl_key': 'key.key',
  206. 'ssl_root_cert': 'root.crt',
  207. 'ssl_crl': 'crl.crl',
  208. }
  209. expected = {
  210. 'PGPASSWORD': 'pass',
  211. 'PGSSLMODE': 'require',
  212. 'PGSSLCERT': 'cert.crt',
  213. 'PGSSLKEY': 'key.key',
  214. 'PGSSLROOTCERT': 'root.crt',
  215. 'PGSSLCRL': 'crl.crl',
  216. }
  217. extra_env = module.make_extra_environment(database)
  218. assert extra_env == expected
  219. def test_dump_databases_runs_pg_dump_with_directory_format():
  220. databases = [{'name': 'foo', 'format': 'directory'}]
  221. process = flexmock()
  222. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  223. flexmock(module).should_receive('make_dump_path').and_return('')
  224. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  225. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  226. 'databases/localhost/foo'
  227. )
  228. flexmock(module.os.path).should_receive('exists').and_return(False)
  229. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  230. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  231. flexmock(module).should_receive('execute_command').with_args(
  232. (
  233. 'pg_dump',
  234. '--no-password',
  235. '--clean',
  236. '--if-exists',
  237. '--format',
  238. 'directory',
  239. '--file',
  240. 'databases/localhost/foo',
  241. 'foo',
  242. ),
  243. shell=True,
  244. extra_environment={'PGSSLMODE': 'disable'},
  245. run_to_completion=False,
  246. ).and_return(process).once()
  247. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  248. def test_dump_databases_runs_pg_dump_with_options():
  249. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  250. process = flexmock()
  251. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  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. '--format',
  266. 'custom',
  267. '--stuff=such',
  268. 'foo',
  269. '>',
  270. 'databases/localhost/foo',
  271. ),
  272. shell=True,
  273. extra_environment={'PGSSLMODE': 'disable'},
  274. run_to_completion=False,
  275. ).and_return(process).once()
  276. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  277. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  278. databases = [{'name': 'all'}]
  279. process = flexmock()
  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(('all',))
  283. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  284. 'databases/localhost/all'
  285. )
  286. flexmock(module.os.path).should_receive('exists').and_return(False)
  287. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  288. flexmock(module).should_receive('execute_command').with_args(
  289. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  290. shell=True,
  291. extra_environment={'PGSSLMODE': 'disable'},
  292. run_to_completion=False,
  293. ).and_return(process).once()
  294. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  295. def test_dump_databases_runs_non_default_pg_dump():
  296. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump'}]
  297. process = flexmock()
  298. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  299. flexmock(module).should_receive('make_dump_path').and_return('')
  300. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  301. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  302. 'databases/localhost/foo'
  303. )
  304. flexmock(module.os.path).should_receive('exists').and_return(False)
  305. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  306. flexmock(module).should_receive('execute_command').with_args(
  307. (
  308. 'special_pg_dump',
  309. '--no-password',
  310. '--clean',
  311. '--if-exists',
  312. '--format',
  313. 'custom',
  314. 'foo',
  315. '>',
  316. 'databases/localhost/foo',
  317. ),
  318. shell=True,
  319. extra_environment={'PGSSLMODE': 'disable'},
  320. run_to_completion=False,
  321. ).and_return(process).once()
  322. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  323. def test_restore_database_dump_runs_pg_restore():
  324. database_config = [{'name': 'foo'}]
  325. extract_process = flexmock(stdout=flexmock())
  326. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  327. flexmock(module).should_receive('make_dump_path')
  328. flexmock(module.dump).should_receive('make_database_dump_filename')
  329. flexmock(module).should_receive('execute_command_with_processes').with_args(
  330. (
  331. 'pg_restore',
  332. '--no-password',
  333. '--if-exists',
  334. '--exit-on-error',
  335. '--clean',
  336. '--dbname',
  337. 'foo',
  338. ),
  339. processes=[extract_process],
  340. output_log_level=logging.DEBUG,
  341. input_file=extract_process.stdout,
  342. extra_environment={'PGSSLMODE': 'disable'},
  343. ).once()
  344. flexmock(module).should_receive('execute_command').with_args(
  345. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  346. extra_environment={'PGSSLMODE': 'disable'},
  347. ).once()
  348. module.restore_database_dump(
  349. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  350. )
  351. def test_restore_database_dump_errors_on_multiple_database_config():
  352. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  353. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  354. flexmock(module).should_receive('make_dump_path')
  355. flexmock(module.dump).should_receive('make_database_dump_filename')
  356. flexmock(module).should_receive('execute_command_with_processes').never()
  357. flexmock(module).should_receive('execute_command').never()
  358. with pytest.raises(ValueError):
  359. module.restore_database_dump(
  360. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  361. )
  362. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  363. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  364. extract_process = flexmock(stdout=flexmock())
  365. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  366. flexmock(module).should_receive('make_dump_path')
  367. flexmock(module.dump).should_receive('make_database_dump_filename')
  368. flexmock(module).should_receive('execute_command_with_processes').with_args(
  369. (
  370. 'pg_restore',
  371. '--no-password',
  372. '--if-exists',
  373. '--exit-on-error',
  374. '--clean',
  375. '--dbname',
  376. 'foo',
  377. '--host',
  378. 'database.example.org',
  379. '--port',
  380. '5433',
  381. ),
  382. processes=[extract_process],
  383. output_log_level=logging.DEBUG,
  384. input_file=extract_process.stdout,
  385. extra_environment={'PGSSLMODE': 'disable'},
  386. ).once()
  387. flexmock(module).should_receive('execute_command').with_args(
  388. (
  389. 'psql',
  390. '--no-password',
  391. '--quiet',
  392. '--host',
  393. 'database.example.org',
  394. '--port',
  395. '5433',
  396. '--dbname',
  397. 'foo',
  398. '--command',
  399. 'ANALYZE',
  400. ),
  401. extra_environment={'PGSSLMODE': 'disable'},
  402. ).once()
  403. module.restore_database_dump(
  404. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  405. )
  406. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  407. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  408. extract_process = flexmock(stdout=flexmock())
  409. flexmock(module).should_receive('make_extra_environment').and_return(
  410. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  411. )
  412. flexmock(module).should_receive('make_dump_path')
  413. flexmock(module.dump).should_receive('make_database_dump_filename')
  414. flexmock(module).should_receive('execute_command_with_processes').with_args(
  415. (
  416. 'pg_restore',
  417. '--no-password',
  418. '--if-exists',
  419. '--exit-on-error',
  420. '--clean',
  421. '--dbname',
  422. 'foo',
  423. '--username',
  424. 'postgres',
  425. ),
  426. processes=[extract_process],
  427. output_log_level=logging.DEBUG,
  428. input_file=extract_process.stdout,
  429. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  430. ).once()
  431. flexmock(module).should_receive('execute_command').with_args(
  432. (
  433. 'psql',
  434. '--no-password',
  435. '--quiet',
  436. '--username',
  437. 'postgres',
  438. '--dbname',
  439. 'foo',
  440. '--command',
  441. 'ANALYZE',
  442. ),
  443. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  444. ).once()
  445. module.restore_database_dump(
  446. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  447. )
  448. def test_restore_database_dump_runs_pg_restore_with_options():
  449. database_config = [
  450. {'name': 'foo', 'restore_options': '--harder', 'analyze_options': '--smarter'}
  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. '--harder',
  466. ),
  467. processes=[extract_process],
  468. output_log_level=logging.DEBUG,
  469. input_file=extract_process.stdout,
  470. extra_environment={'PGSSLMODE': 'disable'},
  471. ).once()
  472. flexmock(module).should_receive('execute_command').with_args(
  473. (
  474. 'psql',
  475. '--no-password',
  476. '--quiet',
  477. '--dbname',
  478. 'foo',
  479. '--smarter',
  480. '--command',
  481. 'ANALYZE',
  482. ),
  483. extra_environment={'PGSSLMODE': 'disable'},
  484. ).once()
  485. module.restore_database_dump(
  486. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  487. )
  488. def test_restore_database_dump_runs_psql_for_all_database_dump():
  489. database_config = [{'name': 'all'}]
  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_database_dump_filename')
  494. flexmock(module).should_receive('execute_command_with_processes').with_args(
  495. ('psql', '--no-password'),
  496. processes=[extract_process],
  497. output_log_level=logging.DEBUG,
  498. input_file=extract_process.stdout,
  499. extra_environment={'PGSSLMODE': 'disable'},
  500. ).once()
  501. flexmock(module).should_receive('execute_command').with_args(
  502. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'),
  503. extra_environment={'PGSSLMODE': 'disable'},
  504. ).once()
  505. module.restore_database_dump(
  506. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  507. )
  508. def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
  509. database_config = [
  510. {'name': 'foo', 'pg_restore_command': 'special_pg_restore', 'psql_command': 'special_psql'}
  511. ]
  512. extract_process = flexmock(stdout=flexmock())
  513. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  514. flexmock(module).should_receive('make_dump_path')
  515. flexmock(module.dump).should_receive('make_database_dump_filename')
  516. flexmock(module).should_receive('execute_command_with_processes').with_args(
  517. (
  518. 'special_pg_restore',
  519. '--no-password',
  520. '--if-exists',
  521. '--exit-on-error',
  522. '--clean',
  523. '--dbname',
  524. 'foo',
  525. ),
  526. processes=[extract_process],
  527. output_log_level=logging.DEBUG,
  528. input_file=extract_process.stdout,
  529. extra_environment={'PGSSLMODE': 'disable'},
  530. ).once()
  531. flexmock(module).should_receive('execute_command').with_args(
  532. ('special_psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  533. extra_environment={'PGSSLMODE': 'disable'},
  534. ).once()
  535. module.restore_database_dump(
  536. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  537. )
  538. def test_restore_database_dump_with_dry_run_skips_restore():
  539. database_config = [{'name': 'foo'}]
  540. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  541. flexmock(module).should_receive('make_dump_path')
  542. flexmock(module.dump).should_receive('make_database_dump_filename')
  543. flexmock(module).should_receive('execute_command_with_processes').never()
  544. module.restore_database_dump(
  545. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  546. )
  547. def test_restore_database_dump_without_extract_process_restores_from_disk():
  548. database_config = [{'name': 'foo'}]
  549. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  550. flexmock(module).should_receive('make_dump_path')
  551. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  552. flexmock(module).should_receive('execute_command_with_processes').with_args(
  553. (
  554. 'pg_restore',
  555. '--no-password',
  556. '--if-exists',
  557. '--exit-on-error',
  558. '--clean',
  559. '--dbname',
  560. 'foo',
  561. '/dump/path',
  562. ),
  563. processes=[],
  564. output_log_level=logging.DEBUG,
  565. input_file=None,
  566. extra_environment={'PGSSLMODE': 'disable'},
  567. ).once()
  568. flexmock(module).should_receive('execute_command').with_args(
  569. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  570. extra_environment={'PGSSLMODE': 'disable'},
  571. ).once()
  572. module.restore_database_dump(
  573. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  574. )