test_postgresql.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_restore_database_dump_runs_pg_restore():
  193. database_config = [{'name': 'foo'}]
  194. extract_process = flexmock(stdout=flexmock())
  195. flexmock(module).should_receive('make_dump_path')
  196. flexmock(module.dump).should_receive('make_database_dump_filename')
  197. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  198. flexmock(module).should_receive('execute_command_with_processes').with_args(
  199. (
  200. 'pg_restore',
  201. '--no-password',
  202. '--if-exists',
  203. '--exit-on-error',
  204. '--clean',
  205. '--dbname',
  206. 'foo',
  207. ),
  208. processes=[extract_process],
  209. output_log_level=logging.DEBUG,
  210. input_file=extract_process.stdout,
  211. extra_environment={'PGSSLMODE': 'disable'},
  212. ).once()
  213. flexmock(module).should_receive('execute_command').with_args(
  214. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  215. extra_environment={'PGSSLMODE': 'disable'},
  216. ).once()
  217. module.restore_database_dump(
  218. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  219. )
  220. def test_restore_database_dump_errors_on_multiple_database_config():
  221. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  222. flexmock(module).should_receive('make_dump_path')
  223. flexmock(module.dump).should_receive('make_database_dump_filename')
  224. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  225. flexmock(module).should_receive('execute_command_with_processes').never()
  226. flexmock(module).should_receive('execute_command').never()
  227. with pytest.raises(ValueError):
  228. module.restore_database_dump(
  229. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  230. )
  231. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  232. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  233. extract_process = flexmock(stdout=flexmock())
  234. flexmock(module).should_receive('make_dump_path')
  235. flexmock(module.dump).should_receive('make_database_dump_filename')
  236. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  237. flexmock(module).should_receive('execute_command_with_processes').with_args(
  238. (
  239. 'pg_restore',
  240. '--no-password',
  241. '--if-exists',
  242. '--exit-on-error',
  243. '--clean',
  244. '--dbname',
  245. 'foo',
  246. '--host',
  247. 'database.example.org',
  248. '--port',
  249. '5433',
  250. ),
  251. processes=[extract_process],
  252. output_log_level=logging.DEBUG,
  253. input_file=extract_process.stdout,
  254. extra_environment={'PGSSLMODE': 'disable'},
  255. ).once()
  256. flexmock(module).should_receive('execute_command').with_args(
  257. (
  258. 'psql',
  259. '--no-password',
  260. '--quiet',
  261. '--host',
  262. 'database.example.org',
  263. '--port',
  264. '5433',
  265. '--dbname',
  266. 'foo',
  267. '--command',
  268. 'ANALYZE',
  269. ),
  270. extra_environment={'PGSSLMODE': 'disable'},
  271. ).once()
  272. module.restore_database_dump(
  273. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  274. )
  275. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  276. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  277. extract_process = flexmock(stdout=flexmock())
  278. flexmock(module).should_receive('make_dump_path')
  279. flexmock(module.dump).should_receive('make_database_dump_filename')
  280. flexmock(module).should_receive('make_extra_environment').and_return(
  281. {'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
  282. )
  283. flexmock(module).should_receive('execute_command_with_processes').with_args(
  284. (
  285. 'pg_restore',
  286. '--no-password',
  287. '--if-exists',
  288. '--exit-on-error',
  289. '--clean',
  290. '--dbname',
  291. 'foo',
  292. '--username',
  293. 'postgres',
  294. ),
  295. processes=[extract_process],
  296. output_log_level=logging.DEBUG,
  297. input_file=extract_process.stdout,
  298. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  299. ).once()
  300. flexmock(module).should_receive('execute_command').with_args(
  301. (
  302. 'psql',
  303. '--no-password',
  304. '--quiet',
  305. '--username',
  306. 'postgres',
  307. '--dbname',
  308. 'foo',
  309. '--command',
  310. 'ANALYZE',
  311. ),
  312. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  313. ).once()
  314. module.restore_database_dump(
  315. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  316. )
  317. def test_restore_database_dump_runs_psql_for_all_database_dump():
  318. database_config = [{'name': 'all'}]
  319. extract_process = flexmock(stdout=flexmock())
  320. flexmock(module).should_receive('make_dump_path')
  321. flexmock(module.dump).should_receive('make_database_dump_filename')
  322. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  323. flexmock(module).should_receive('execute_command_with_processes').with_args(
  324. ('psql', '--no-password'),
  325. processes=[extract_process],
  326. output_log_level=logging.DEBUG,
  327. input_file=extract_process.stdout,
  328. extra_environment={'PGSSLMODE': 'disable'},
  329. ).once()
  330. flexmock(module).should_receive('execute_command').with_args(
  331. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'),
  332. extra_environment={'PGSSLMODE': 'disable'},
  333. ).once()
  334. module.restore_database_dump(
  335. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  336. )
  337. def test_restore_database_dump_with_dry_run_skips_restore():
  338. database_config = [{'name': 'foo'}]
  339. flexmock(module).should_receive('make_dump_path')
  340. flexmock(module.dump).should_receive('make_database_dump_filename')
  341. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  342. flexmock(module).should_receive('execute_command_with_processes').never()
  343. module.restore_database_dump(
  344. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  345. )
  346. def test_restore_database_dump_without_extract_process_restores_from_disk():
  347. database_config = [{'name': 'foo'}]
  348. flexmock(module).should_receive('make_dump_path')
  349. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  350. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  351. flexmock(module).should_receive('execute_command_with_processes').with_args(
  352. (
  353. 'pg_restore',
  354. '--no-password',
  355. '--if-exists',
  356. '--exit-on-error',
  357. '--clean',
  358. '--dbname',
  359. 'foo',
  360. '/dump/path',
  361. ),
  362. processes=[],
  363. output_log_level=logging.DEBUG,
  364. input_file=None,
  365. extra_environment={'PGSSLMODE': 'disable'},
  366. ).once()
  367. flexmock(module).should_receive('execute_command').with_args(
  368. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  369. extra_environment={'PGSSLMODE': 'disable'},
  370. ).once()
  371. module.restore_database_dump(
  372. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  373. )