test_postgresql.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  222. flexmock(module).should_receive('make_dump_path').and_return('')
  223. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  224. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  225. 'databases/localhost/foo'
  226. )
  227. flexmock(module.os.path).should_receive('exists').and_return(False)
  228. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  229. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  230. flexmock(module).should_receive('execute_command').with_args(
  231. (
  232. 'pg_dump',
  233. '--no-password',
  234. '--clean',
  235. '--if-exists',
  236. '--format',
  237. 'directory',
  238. '--file',
  239. 'databases/localhost/foo',
  240. 'foo',
  241. ),
  242. shell=True,
  243. extra_environment={'PGSSLMODE': 'disable'},
  244. ).and_return(flexmock()).once()
  245. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
  246. def test_dump_databases_runs_pg_dump_with_options():
  247. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  248. process = flexmock()
  249. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  250. flexmock(module).should_receive('make_dump_path').and_return('')
  251. flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
  252. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  253. 'databases/localhost/foo'
  254. )
  255. flexmock(module.os.path).should_receive('exists').and_return(False)
  256. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  257. flexmock(module).should_receive('execute_command').with_args(
  258. (
  259. 'pg_dump',
  260. '--no-password',
  261. '--clean',
  262. '--if-exists',
  263. '--format',
  264. 'custom',
  265. '--stuff=such',
  266. 'foo',
  267. '>',
  268. 'databases/localhost/foo',
  269. ),
  270. shell=True,
  271. extra_environment={'PGSSLMODE': 'disable'},
  272. run_to_completion=False,
  273. ).and_return(process).once()
  274. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  275. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  276. databases = [{'name': 'all'}]
  277. process = flexmock()
  278. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  279. flexmock(module).should_receive('make_dump_path').and_return('')
  280. flexmock(module).should_receive('database_names_to_dump').and_return(('all',))
  281. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  282. 'databases/localhost/all'
  283. )
  284. flexmock(module.os.path).should_receive('exists').and_return(False)
  285. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  286. flexmock(module).should_receive('execute_command').with_args(
  287. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  288. shell=True,
  289. extra_environment={'PGSSLMODE': 'disable'},
  290. run_to_completion=False,
  291. ).and_return(process).once()
  292. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  293. def test_dump_databases_runs_non_default_pg_dump():
  294. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump'}]
  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. 'special_pg_dump',
  307. '--no-password',
  308. '--clean',
  309. '--if-exists',
  310. '--format',
  311. 'custom',
  312. 'foo',
  313. '>',
  314. 'databases/localhost/foo',
  315. ),
  316. shell=True,
  317. extra_environment={'PGSSLMODE': 'disable'},
  318. run_to_completion=False,
  319. ).and_return(process).once()
  320. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  321. def test_restore_database_dump_runs_pg_restore():
  322. database_config = [{'name': 'foo'}]
  323. extract_process = flexmock(stdout=flexmock())
  324. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  325. flexmock(module).should_receive('make_dump_path')
  326. flexmock(module.dump).should_receive('make_database_dump_filename')
  327. flexmock(module).should_receive('execute_command_with_processes').with_args(
  328. (
  329. 'pg_restore',
  330. '--no-password',
  331. '--if-exists',
  332. '--exit-on-error',
  333. '--clean',
  334. '--dbname',
  335. 'foo',
  336. ),
  337. processes=[extract_process],
  338. output_log_level=logging.DEBUG,
  339. input_file=extract_process.stdout,
  340. extra_environment={'PGSSLMODE': 'disable'},
  341. ).once()
  342. flexmock(module).should_receive('execute_command').with_args(
  343. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  344. extra_environment={'PGSSLMODE': 'disable'},
  345. ).once()
  346. module.restore_database_dump(
  347. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  348. )
  349. def test_restore_database_dump_errors_on_multiple_database_config():
  350. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  351. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  352. flexmock(module).should_receive('make_dump_path')
  353. flexmock(module.dump).should_receive('make_database_dump_filename')
  354. flexmock(module).should_receive('execute_command_with_processes').never()
  355. flexmock(module).should_receive('execute_command').never()
  356. with pytest.raises(ValueError):
  357. module.restore_database_dump(
  358. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  359. )
  360. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  361. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  362. extract_process = flexmock(stdout=flexmock())
  363. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  364. flexmock(module).should_receive('make_dump_path')
  365. flexmock(module.dump).should_receive('make_database_dump_filename')
  366. flexmock(module).should_receive('execute_command_with_processes').with_args(
  367. (
  368. 'pg_restore',
  369. '--no-password',
  370. '--if-exists',
  371. '--exit-on-error',
  372. '--clean',
  373. '--dbname',
  374. 'foo',
  375. '--host',
  376. 'database.example.org',
  377. '--port',
  378. '5433',
  379. ),
  380. processes=[extract_process],
  381. output_log_level=logging.DEBUG,
  382. input_file=extract_process.stdout,
  383. extra_environment={'PGSSLMODE': 'disable'},
  384. ).once()
  385. flexmock(module).should_receive('execute_command').with_args(
  386. (
  387. 'psql',
  388. '--no-password',
  389. '--quiet',
  390. '--host',
  391. 'database.example.org',
  392. '--port',
  393. '5433',
  394. '--dbname',
  395. 'foo',
  396. '--command',
  397. 'ANALYZE',
  398. ),
  399. extra_environment={'PGSSLMODE': 'disable'},
  400. ).once()
  401. module.restore_database_dump(
  402. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  403. )
  404. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  405. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  406. extract_process = flexmock(stdout=flexmock())
  407. flexmock(module).should_receive('make_extra_environment').and_return(
  408. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  409. )
  410. flexmock(module).should_receive('make_dump_path')
  411. flexmock(module.dump).should_receive('make_database_dump_filename')
  412. flexmock(module).should_receive('execute_command_with_processes').with_args(
  413. (
  414. 'pg_restore',
  415. '--no-password',
  416. '--if-exists',
  417. '--exit-on-error',
  418. '--clean',
  419. '--dbname',
  420. 'foo',
  421. '--username',
  422. 'postgres',
  423. ),
  424. processes=[extract_process],
  425. output_log_level=logging.DEBUG,
  426. input_file=extract_process.stdout,
  427. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  428. ).once()
  429. flexmock(module).should_receive('execute_command').with_args(
  430. (
  431. 'psql',
  432. '--no-password',
  433. '--quiet',
  434. '--username',
  435. 'postgres',
  436. '--dbname',
  437. 'foo',
  438. '--command',
  439. 'ANALYZE',
  440. ),
  441. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  442. ).once()
  443. module.restore_database_dump(
  444. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  445. )
  446. def test_restore_database_dump_runs_pg_restore_with_options():
  447. database_config = [
  448. {'name': 'foo', 'restore_options': '--harder', 'analyze_options': '--smarter'}
  449. ]
  450. extract_process = flexmock(stdout=flexmock())
  451. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  452. flexmock(module).should_receive('make_dump_path')
  453. flexmock(module.dump).should_receive('make_database_dump_filename')
  454. flexmock(module).should_receive('execute_command_with_processes').with_args(
  455. (
  456. 'pg_restore',
  457. '--no-password',
  458. '--if-exists',
  459. '--exit-on-error',
  460. '--clean',
  461. '--dbname',
  462. 'foo',
  463. '--harder',
  464. ),
  465. processes=[extract_process],
  466. output_log_level=logging.DEBUG,
  467. input_file=extract_process.stdout,
  468. extra_environment={'PGSSLMODE': 'disable'},
  469. ).once()
  470. flexmock(module).should_receive('execute_command').with_args(
  471. (
  472. 'psql',
  473. '--no-password',
  474. '--quiet',
  475. '--dbname',
  476. 'foo',
  477. '--smarter',
  478. '--command',
  479. 'ANALYZE',
  480. ),
  481. extra_environment={'PGSSLMODE': 'disable'},
  482. ).once()
  483. module.restore_database_dump(
  484. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  485. )
  486. def test_restore_database_dump_runs_psql_for_all_database_dump():
  487. database_config = [{'name': 'all'}]
  488. extract_process = flexmock(stdout=flexmock())
  489. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  490. flexmock(module).should_receive('make_dump_path')
  491. flexmock(module.dump).should_receive('make_database_dump_filename')
  492. flexmock(module).should_receive('execute_command_with_processes').with_args(
  493. ('psql', '--no-password'),
  494. processes=[extract_process],
  495. output_log_level=logging.DEBUG,
  496. input_file=extract_process.stdout,
  497. extra_environment={'PGSSLMODE': 'disable'},
  498. ).once()
  499. flexmock(module).should_receive('execute_command').with_args(
  500. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'),
  501. extra_environment={'PGSSLMODE': 'disable'},
  502. ).once()
  503. module.restore_database_dump(
  504. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  505. )
  506. def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
  507. database_config = [
  508. {'name': 'foo', 'pg_restore_command': 'special_pg_restore', 'psql_command': 'special_psql'}
  509. ]
  510. extract_process = flexmock(stdout=flexmock())
  511. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  512. flexmock(module).should_receive('make_dump_path')
  513. flexmock(module.dump).should_receive('make_database_dump_filename')
  514. flexmock(module).should_receive('execute_command_with_processes').with_args(
  515. (
  516. 'special_pg_restore',
  517. '--no-password',
  518. '--if-exists',
  519. '--exit-on-error',
  520. '--clean',
  521. '--dbname',
  522. 'foo',
  523. ),
  524. processes=[extract_process],
  525. output_log_level=logging.DEBUG,
  526. input_file=extract_process.stdout,
  527. extra_environment={'PGSSLMODE': 'disable'},
  528. ).once()
  529. flexmock(module).should_receive('execute_command').with_args(
  530. ('special_psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  531. extra_environment={'PGSSLMODE': 'disable'},
  532. ).once()
  533. module.restore_database_dump(
  534. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  535. )
  536. def test_restore_database_dump_with_dry_run_skips_restore():
  537. database_config = [{'name': 'foo'}]
  538. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  539. flexmock(module).should_receive('make_dump_path')
  540. flexmock(module.dump).should_receive('make_database_dump_filename')
  541. flexmock(module).should_receive('execute_command_with_processes').never()
  542. module.restore_database_dump(
  543. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  544. )
  545. def test_restore_database_dump_without_extract_process_restores_from_disk():
  546. database_config = [{'name': 'foo'}]
  547. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  548. flexmock(module).should_receive('make_dump_path')
  549. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  550. flexmock(module).should_receive('execute_command_with_processes').with_args(
  551. (
  552. 'pg_restore',
  553. '--no-password',
  554. '--if-exists',
  555. '--exit-on-error',
  556. '--clean',
  557. '--dbname',
  558. 'foo',
  559. '/dump/path',
  560. ),
  561. processes=[],
  562. output_log_level=logging.DEBUG,
  563. input_file=None,
  564. extra_environment={'PGSSLMODE': 'disable'},
  565. ).once()
  566. flexmock(module).should_receive('execute_command').with_args(
  567. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  568. extra_environment={'PGSSLMODE': 'disable'},
  569. ).once()
  570. module.restore_database_dump(
  571. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  572. )