test_postgresql.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. for name, process in zip(('foo', 'bar'), processes):
  14. flexmock(module).should_receive('execute_command').with_args(
  15. (
  16. 'pg_dump',
  17. '--no-password',
  18. '--clean',
  19. '--if-exists',
  20. '--format',
  21. 'custom',
  22. name,
  23. '>',
  24. 'databases/localhost/{}'.format(name),
  25. ),
  26. shell=True,
  27. extra_environment={'PGSSLMODE': 'disable'},
  28. run_to_completion=False,
  29. ).and_return(process).once()
  30. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  31. def test_dump_databases_with_dry_run_skips_pg_dump():
  32. databases = [{'name': 'foo'}, {'name': 'bar'}]
  33. flexmock(module).should_receive('make_dump_path').and_return('')
  34. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  35. 'databases/localhost/foo'
  36. ).and_return('databases/localhost/bar')
  37. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  38. flexmock(module).should_receive('execute_command').never()
  39. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  40. def test_dump_databases_runs_pg_dump_with_hostname_and_port():
  41. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  42. process = flexmock()
  43. flexmock(module).should_receive('make_dump_path').and_return('')
  44. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  45. 'databases/database.example.org/foo'
  46. )
  47. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  48. flexmock(module).should_receive('execute_command').with_args(
  49. (
  50. 'pg_dump',
  51. '--no-password',
  52. '--clean',
  53. '--if-exists',
  54. '--host',
  55. 'database.example.org',
  56. '--port',
  57. '5433',
  58. '--format',
  59. 'custom',
  60. 'foo',
  61. '>',
  62. 'databases/database.example.org/foo',
  63. ),
  64. shell=True,
  65. extra_environment={'PGSSLMODE': 'disable'},
  66. run_to_completion=False,
  67. ).and_return(process).once()
  68. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  69. def test_dump_databases_runs_pg_dump_with_username_and_password():
  70. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  71. process = flexmock()
  72. flexmock(module).should_receive('make_dump_path').and_return('')
  73. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  74. 'databases/localhost/foo'
  75. )
  76. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  77. flexmock(module).should_receive('execute_command').with_args(
  78. (
  79. 'pg_dump',
  80. '--no-password',
  81. '--clean',
  82. '--if-exists',
  83. '--username',
  84. 'postgres',
  85. '--format',
  86. 'custom',
  87. 'foo',
  88. '>',
  89. 'databases/localhost/foo',
  90. ),
  91. shell=True,
  92. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  93. run_to_completion=False,
  94. ).and_return(process).once()
  95. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  96. def test_make_extra_environment():
  97. database = {
  98. 'name': 'foo',
  99. 'ssl_mode': 'require',
  100. 'ssl_cert': 'cert.crt',
  101. 'ssl_key': 'key.key',
  102. 'ssl_root_cert': 'root.crt',
  103. 'ssl_crl': 'crl.crl',
  104. }
  105. expected = {
  106. 'PGSSLMODE': 'require',
  107. 'PGSSLCERT': 'cert.crt',
  108. 'PGSSLKEY': 'key.key',
  109. 'PGSSLROOTCERT': 'root.crt',
  110. 'PGSSLCRL': 'crl.crl',
  111. }
  112. extra_env = module.make_extra_environment(database)
  113. assert extra_env == expected
  114. def test_dump_databases_runs_pg_dump_with_directory_format():
  115. databases = [{'name': 'foo', 'format': 'directory'}]
  116. process = flexmock()
  117. flexmock(module).should_receive('make_dump_path').and_return('')
  118. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  119. 'databases/localhost/foo'
  120. )
  121. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  122. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  123. flexmock(module).should_receive('execute_command').with_args(
  124. (
  125. 'pg_dump',
  126. '--no-password',
  127. '--clean',
  128. '--if-exists',
  129. '--format',
  130. 'directory',
  131. '--file',
  132. 'databases/localhost/foo',
  133. 'foo',
  134. ),
  135. shell=True,
  136. extra_environment={'PGSSLMODE': 'disable'},
  137. run_to_completion=False,
  138. ).and_return(process).once()
  139. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  140. def test_dump_databases_runs_pg_dump_with_options():
  141. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  142. process = flexmock()
  143. flexmock(module).should_receive('make_dump_path').and_return('')
  144. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  145. 'databases/localhost/foo'
  146. )
  147. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  148. flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
  149. flexmock(module).should_receive('execute_command').with_args(
  150. (
  151. 'pg_dump',
  152. '--no-password',
  153. '--clean',
  154. '--if-exists',
  155. '--format',
  156. 'custom',
  157. '--stuff=such',
  158. 'foo',
  159. '>',
  160. 'databases/localhost/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_dumpall_for_all_databases():
  168. databases = [{'name': 'all'}]
  169. process = flexmock()
  170. flexmock(module).should_receive('make_dump_path').and_return('')
  171. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  172. 'databases/localhost/all'
  173. )
  174. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  175. flexmock(module).should_receive('execute_command').with_args(
  176. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  177. shell=True,
  178. extra_environment={'PGSSLMODE': 'disable'},
  179. run_to_completion=False,
  180. ).and_return(process).once()
  181. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  182. def test_restore_database_dump_runs_pg_restore():
  183. database_config = [{'name': 'foo'}]
  184. extract_process = flexmock(stdout=flexmock())
  185. flexmock(module).should_receive('make_dump_path')
  186. flexmock(module.dump).should_receive('make_database_dump_filename')
  187. flexmock(module).should_receive('execute_command_with_processes').with_args(
  188. (
  189. 'pg_restore',
  190. '--no-password',
  191. '--if-exists',
  192. '--exit-on-error',
  193. '--clean',
  194. '--dbname',
  195. 'foo',
  196. ),
  197. processes=[extract_process],
  198. output_log_level=logging.DEBUG,
  199. input_file=extract_process.stdout,
  200. extra_environment={'PGSSLMODE': 'disable'},
  201. borg_local_path='borg',
  202. ).once()
  203. flexmock(module).should_receive('execute_command').with_args(
  204. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  205. extra_environment={'PGSSLMODE': 'disable'},
  206. ).once()
  207. module.restore_database_dump(
  208. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  209. )
  210. def test_restore_database_dump_errors_on_multiple_database_config():
  211. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  212. flexmock(module).should_receive('make_dump_path')
  213. flexmock(module.dump).should_receive('make_database_dump_filename')
  214. flexmock(module).should_receive('execute_command_with_processes').never()
  215. flexmock(module).should_receive('execute_command').never()
  216. with pytest.raises(ValueError):
  217. module.restore_database_dump(
  218. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  219. )
  220. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  221. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  222. extract_process = flexmock(stdout=flexmock())
  223. flexmock(module).should_receive('make_dump_path')
  224. flexmock(module.dump).should_receive('make_database_dump_filename')
  225. flexmock(module).should_receive('execute_command_with_processes').with_args(
  226. (
  227. 'pg_restore',
  228. '--no-password',
  229. '--if-exists',
  230. '--exit-on-error',
  231. '--clean',
  232. '--dbname',
  233. 'foo',
  234. '--host',
  235. 'database.example.org',
  236. '--port',
  237. '5433',
  238. ),
  239. processes=[extract_process],
  240. output_log_level=logging.DEBUG,
  241. input_file=extract_process.stdout,
  242. extra_environment={'PGSSLMODE': 'disable'},
  243. borg_local_path='borg',
  244. ).once()
  245. flexmock(module).should_receive('execute_command').with_args(
  246. (
  247. 'psql',
  248. '--no-password',
  249. '--quiet',
  250. '--host',
  251. 'database.example.org',
  252. '--port',
  253. '5433',
  254. '--dbname',
  255. 'foo',
  256. '--command',
  257. 'ANALYZE',
  258. ),
  259. extra_environment={'PGSSLMODE': 'disable'},
  260. ).once()
  261. module.restore_database_dump(
  262. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  263. )
  264. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  265. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  266. extract_process = flexmock(stdout=flexmock())
  267. flexmock(module).should_receive('make_dump_path')
  268. flexmock(module.dump).should_receive('make_database_dump_filename')
  269. flexmock(module).should_receive('execute_command_with_processes').with_args(
  270. (
  271. 'pg_restore',
  272. '--no-password',
  273. '--if-exists',
  274. '--exit-on-error',
  275. '--clean',
  276. '--dbname',
  277. 'foo',
  278. '--username',
  279. 'postgres',
  280. ),
  281. processes=[extract_process],
  282. output_log_level=logging.DEBUG,
  283. input_file=extract_process.stdout,
  284. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  285. borg_local_path='borg',
  286. ).once()
  287. flexmock(module).should_receive('execute_command').with_args(
  288. (
  289. 'psql',
  290. '--no-password',
  291. '--quiet',
  292. '--username',
  293. 'postgres',
  294. '--dbname',
  295. 'foo',
  296. '--command',
  297. 'ANALYZE',
  298. ),
  299. extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
  300. ).once()
  301. module.restore_database_dump(
  302. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  303. )
  304. def test_restore_database_dump_runs_psql_for_all_database_dump():
  305. database_config = [{'name': 'all'}]
  306. extract_process = flexmock(stdout=flexmock())
  307. flexmock(module).should_receive('make_dump_path')
  308. flexmock(module.dump).should_receive('make_database_dump_filename')
  309. flexmock(module).should_receive('execute_command_with_processes').with_args(
  310. ('psql', '--no-password'),
  311. processes=[extract_process],
  312. output_log_level=logging.DEBUG,
  313. input_file=extract_process.stdout,
  314. extra_environment={'PGSSLMODE': 'disable'},
  315. borg_local_path='borg',
  316. ).once()
  317. flexmock(module).should_receive('execute_command').with_args(
  318. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'),
  319. extra_environment={'PGSSLMODE': 'disable'},
  320. ).once()
  321. module.restore_database_dump(
  322. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  323. )
  324. def test_restore_database_dump_with_dry_run_skips_restore():
  325. database_config = [{'name': 'foo'}]
  326. flexmock(module).should_receive('make_dump_path')
  327. flexmock(module.dump).should_receive('make_database_dump_filename')
  328. flexmock(module).should_receive('execute_command_with_processes').never()
  329. module.restore_database_dump(
  330. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  331. )
  332. def test_restore_database_dump_without_extract_process_restores_from_disk():
  333. database_config = [{'name': 'foo'}]
  334. flexmock(module).should_receive('make_dump_path')
  335. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  336. flexmock(module).should_receive('execute_command_with_processes').with_args(
  337. (
  338. 'pg_restore',
  339. '--no-password',
  340. '--if-exists',
  341. '--exit-on-error',
  342. '--clean',
  343. '--dbname',
  344. 'foo',
  345. '/dump/path',
  346. ),
  347. processes=[],
  348. output_log_level=logging.DEBUG,
  349. input_file=None,
  350. extra_environment={'PGSSLMODE': 'disable'},
  351. borg_local_path='borg',
  352. ).once()
  353. flexmock(module).should_receive('execute_command').with_args(
  354. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  355. extra_environment={'PGSSLMODE': 'disable'},
  356. ).once()
  357. module.restore_database_dump(
  358. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  359. )