test_postgresql.py 20 KB

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