test_postgresql.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import postgresql as module
  5. def test_dump_databases_runs_pg_dump_for_each_database():
  6. databases = [{'name': 'foo'}, {'name': 'bar'}]
  7. processes = [flexmock(), flexmock()]
  8. flexmock(module).should_receive('make_dump_path').and_return('')
  9. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  10. 'databases/localhost/foo'
  11. ).and_return('databases/localhost/bar')
  12. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  13. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  14. for name, process in zip(('foo', 'bar'), processes):
  15. flexmock(module).should_receive('execute_command').with_args(
  16. (
  17. 'pg_dump',
  18. '--no-password',
  19. '--clean',
  20. '--if-exists',
  21. '--format',
  22. 'custom',
  23. name,
  24. '>',
  25. 'databases/localhost/{}'.format(name),
  26. ),
  27. shell=True,
  28. extra_environment={'PGSSLMODE': 'disable'},
  29. run_to_completion=False,
  30. ).and_return(process).once()
  31. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  32. def test_dump_databases_with_dry_run_skips_pg_dump():
  33. databases = [{'name': 'foo'}, {'name': 'bar'}]
  34. flexmock(module).should_receive('make_dump_path').and_return('')
  35. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  36. 'databases/localhost/foo'
  37. ).and_return('databases/localhost/bar')
  38. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  39. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  40. flexmock(module).should_receive('execute_command').never()
  41. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  42. def test_dump_databases_runs_pg_dump_with_hostname_and_port():
  43. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  44. process = flexmock()
  45. flexmock(module).should_receive('make_dump_path').and_return('')
  46. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  47. 'databases/database.example.org/foo'
  48. )
  49. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  50. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  51. flexmock(module).should_receive('execute_command').with_args(
  52. (
  53. 'pg_dump',
  54. '--no-password',
  55. '--clean',
  56. '--if-exists',
  57. '--host',
  58. 'database.example.org',
  59. '--port',
  60. '5433',
  61. '--format',
  62. 'custom',
  63. 'foo',
  64. '>',
  65. 'databases/database.example.org/foo',
  66. ),
  67. shell=True,
  68. extra_environment={'PGSSLMODE': 'disable'},
  69. run_to_completion=False,
  70. ).and_return(process).once()
  71. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  72. def test_dump_databases_runs_pg_dump_with_username_and_password():
  73. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  74. process = flexmock()
  75. flexmock(module).should_receive('make_dump_path').and_return('')
  76. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  77. 'databases/localhost/foo'
  78. )
  79. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  80. flexmock(module).should_receive('make_extra_environment').and_return(
  81. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  82. )
  83. flexmock(module).should_receive('execute_command').with_args(
  84. (
  85. 'pg_dump',
  86. '--no-password',
  87. '--clean',
  88. '--if-exists',
  89. '--username',
  90. 'postgres',
  91. '--format',
  92. 'custom',
  93. 'foo',
  94. '>',
  95. 'databases/localhost/foo',
  96. ),
  97. shell=True,
  98. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  99. run_to_completion=False,
  100. ).and_return(process).once()
  101. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  102. def test_make_extra_environment_maps_options_to_environment():
  103. database = {
  104. 'name': 'foo',
  105. 'password': 'pass',
  106. 'ssl_mode': 'require',
  107. 'ssl_cert': 'cert.crt',
  108. 'ssl_key': 'key.key',
  109. 'ssl_root_cert': 'root.crt',
  110. 'ssl_crl': 'crl.crl',
  111. }
  112. expected = {
  113. 'PGPASSWORD': 'pass',
  114. 'PGSSLMODE': 'require',
  115. 'PGSSLCERT': 'cert.crt',
  116. 'PGSSLKEY': 'key.key',
  117. 'PGSSLROOTCERT': 'root.crt',
  118. 'PGSSLCRL': 'crl.crl',
  119. }
  120. extra_env = module.make_extra_environment(database)
  121. assert extra_env == expected
  122. def test_dump_databases_runs_pg_dump_with_directory_format():
  123. databases = [{'name': 'foo', 'format': 'directory'}]
  124. process = flexmock()
  125. flexmock(module).should_receive('make_dump_path').and_return('')
  126. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  127. 'databases/localhost/foo'
  128. )
  129. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  130. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  131. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  132. flexmock(module).should_receive('execute_command').with_args(
  133. (
  134. 'pg_dump',
  135. '--no-password',
  136. '--clean',
  137. '--if-exists',
  138. '--format',
  139. 'directory',
  140. '--file',
  141. 'databases/localhost/foo',
  142. 'foo',
  143. ),
  144. shell=True,
  145. extra_environment={'PGSSLMODE': 'disable'},
  146. run_to_completion=False,
  147. ).and_return(process).once()
  148. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  149. def test_dump_databases_runs_pg_dump_with_options():
  150. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  151. process = flexmock()
  152. flexmock(module).should_receive('make_dump_path').and_return('')
  153. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  154. 'databases/localhost/foo'
  155. )
  156. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  157. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  158. flexmock(module).should_receive('execute_command').with_args(
  159. (
  160. 'pg_dump',
  161. '--no-password',
  162. '--clean',
  163. '--if-exists',
  164. '--format',
  165. 'custom',
  166. '--stuff=such',
  167. 'foo',
  168. '>',
  169. 'databases/localhost/foo',
  170. ),
  171. shell=True,
  172. extra_environment={'PGSSLMODE': 'disable'},
  173. run_to_completion=False,
  174. ).and_return(process).once()
  175. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  176. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  177. databases = [{'name': 'all'}]
  178. process = flexmock()
  179. flexmock(module).should_receive('make_dump_path').and_return('')
  180. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  181. 'databases/localhost/all'
  182. )
  183. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  184. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  185. flexmock(module).should_receive('execute_command').with_args(
  186. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  187. shell=True,
  188. extra_environment={'PGSSLMODE': 'disable'},
  189. run_to_completion=False,
  190. ).and_return(process).once()
  191. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  192. def test_dump_databases_runs_non_default_pg_dump():
  193. databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump'}]
  194. process = flexmock()
  195. flexmock(module).should_receive('make_dump_path').and_return('')
  196. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  197. 'databases/localhost/foo'
  198. )
  199. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  200. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  201. flexmock(module).should_receive('execute_command').with_args(
  202. (
  203. 'special_pg_dump',
  204. '--no-password',
  205. '--clean',
  206. '--if-exists',
  207. '--format',
  208. 'custom',
  209. 'foo',
  210. '>',
  211. 'databases/localhost/foo',
  212. ),
  213. shell=True,
  214. extra_environment={'PGSSLMODE': 'disable'},
  215. run_to_completion=False,
  216. ).and_return(process).once()
  217. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  218. def test_restore_database_dump_runs_pg_restore():
  219. database_config = [{'name': 'foo'}]
  220. extract_process = flexmock(stdout=flexmock())
  221. flexmock(module).should_receive('make_dump_path')
  222. flexmock(module.dump).should_receive('make_database_dump_filename')
  223. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  224. flexmock(module).should_receive('execute_command_with_processes').with_args(
  225. (
  226. 'pg_restore',
  227. '--no-password',
  228. '--if-exists',
  229. '--exit-on-error',
  230. '--clean',
  231. '--dbname',
  232. 'foo',
  233. ),
  234. processes=[extract_process],
  235. output_log_level=logging.DEBUG,
  236. input_file=extract_process.stdout,
  237. extra_environment={'PGSSLMODE': 'disable'},
  238. ).once()
  239. flexmock(module).should_receive('execute_command').with_args(
  240. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  241. extra_environment={'PGSSLMODE': 'disable'},
  242. ).once()
  243. module.restore_database_dump(
  244. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  245. )
  246. def test_restore_database_dump_errors_on_multiple_database_config():
  247. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  248. flexmock(module).should_receive('make_dump_path')
  249. flexmock(module.dump).should_receive('make_database_dump_filename')
  250. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  251. flexmock(module).should_receive('execute_command_with_processes').never()
  252. flexmock(module).should_receive('execute_command').never()
  253. with pytest.raises(ValueError):
  254. module.restore_database_dump(
  255. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  256. )
  257. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  258. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  259. extract_process = flexmock(stdout=flexmock())
  260. flexmock(module).should_receive('make_dump_path')
  261. flexmock(module.dump).should_receive('make_database_dump_filename')
  262. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  263. flexmock(module).should_receive('execute_command_with_processes').with_args(
  264. (
  265. 'pg_restore',
  266. '--no-password',
  267. '--if-exists',
  268. '--exit-on-error',
  269. '--clean',
  270. '--dbname',
  271. 'foo',
  272. '--host',
  273. 'database.example.org',
  274. '--port',
  275. '5433',
  276. ),
  277. processes=[extract_process],
  278. output_log_level=logging.DEBUG,
  279. input_file=extract_process.stdout,
  280. extra_environment={'PGSSLMODE': 'disable'},
  281. ).once()
  282. flexmock(module).should_receive('execute_command').with_args(
  283. (
  284. 'psql',
  285. '--no-password',
  286. '--quiet',
  287. '--host',
  288. 'database.example.org',
  289. '--port',
  290. '5433',
  291. '--dbname',
  292. 'foo',
  293. '--command',
  294. 'ANALYZE',
  295. ),
  296. extra_environment={'PGSSLMODE': 'disable'},
  297. ).once()
  298. module.restore_database_dump(
  299. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  300. )
  301. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  302. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  303. extract_process = flexmock(stdout=flexmock())
  304. flexmock(module).should_receive('make_dump_path')
  305. flexmock(module.dump).should_receive('make_database_dump_filename')
  306. flexmock(module).should_receive('make_extra_environment').and_return(
  307. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  308. )
  309. flexmock(module).should_receive('execute_command_with_processes').with_args(
  310. (
  311. 'pg_restore',
  312. '--no-password',
  313. '--if-exists',
  314. '--exit-on-error',
  315. '--clean',
  316. '--dbname',
  317. 'foo',
  318. '--username',
  319. 'postgres',
  320. ),
  321. processes=[extract_process],
  322. output_log_level=logging.DEBUG,
  323. input_file=extract_process.stdout,
  324. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  325. ).once()
  326. flexmock(module).should_receive('execute_command').with_args(
  327. (
  328. 'psql',
  329. '--no-password',
  330. '--quiet',
  331. '--username',
  332. 'postgres',
  333. '--dbname',
  334. 'foo',
  335. '--command',
  336. 'ANALYZE',
  337. ),
  338. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  339. ).once()
  340. module.restore_database_dump(
  341. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  342. )
  343. def test_restore_database_dump_runs_psql_for_all_database_dump():
  344. database_config = [{'name': 'all'}]
  345. extract_process = flexmock(stdout=flexmock())
  346. flexmock(module).should_receive('make_dump_path')
  347. flexmock(module.dump).should_receive('make_database_dump_filename')
  348. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  349. flexmock(module).should_receive('execute_command_with_processes').with_args(
  350. ('psql', '--no-password'),
  351. processes=[extract_process],
  352. output_log_level=logging.DEBUG,
  353. input_file=extract_process.stdout,
  354. extra_environment={'PGSSLMODE': 'disable'},
  355. ).once()
  356. flexmock(module).should_receive('execute_command').with_args(
  357. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'),
  358. extra_environment={'PGSSLMODE': 'disable'},
  359. ).once()
  360. module.restore_database_dump(
  361. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  362. )
  363. def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
  364. database_config = [
  365. {'name': 'foo', 'pg_restore_command': 'special_pg_restore', 'psql_command': 'special_psql'}
  366. ]
  367. extract_process = flexmock(stdout=flexmock())
  368. flexmock(module).should_receive('make_dump_path')
  369. flexmock(module.dump).should_receive('make_database_dump_filename')
  370. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  371. flexmock(module).should_receive('execute_command_with_processes').with_args(
  372. (
  373. 'special_pg_restore',
  374. '--no-password',
  375. '--if-exists',
  376. '--exit-on-error',
  377. '--clean',
  378. '--dbname',
  379. 'foo',
  380. ),
  381. processes=[extract_process],
  382. output_log_level=logging.DEBUG,
  383. input_file=extract_process.stdout,
  384. extra_environment={'PGSSLMODE': 'disable'},
  385. ).once()
  386. flexmock(module).should_receive('execute_command').with_args(
  387. ('special_psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  388. extra_environment={'PGSSLMODE': 'disable'},
  389. ).once()
  390. module.restore_database_dump(
  391. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  392. )
  393. def test_restore_database_dump_with_dry_run_skips_restore():
  394. database_config = [{'name': 'foo'}]
  395. flexmock(module).should_receive('make_dump_path')
  396. flexmock(module.dump).should_receive('make_database_dump_filename')
  397. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  398. flexmock(module).should_receive('execute_command_with_processes').never()
  399. module.restore_database_dump(
  400. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  401. )
  402. def test_restore_database_dump_without_extract_process_restores_from_disk():
  403. database_config = [{'name': 'foo'}]
  404. flexmock(module).should_receive('make_dump_path')
  405. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  406. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  407. flexmock(module).should_receive('execute_command_with_processes').with_args(
  408. (
  409. 'pg_restore',
  410. '--no-password',
  411. '--if-exists',
  412. '--exit-on-error',
  413. '--clean',
  414. '--dbname',
  415. 'foo',
  416. '/dump/path',
  417. ),
  418. processes=[],
  419. output_log_level=logging.DEBUG,
  420. input_file=None,
  421. extra_environment={'PGSSLMODE': 'disable'},
  422. ).once()
  423. flexmock(module).should_receive('execute_command').with_args(
  424. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  425. extra_environment={'PGSSLMODE': 'disable'},
  426. ).once()
  427. module.restore_database_dump(
  428. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  429. )