test_postgresql.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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=None,
  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=None,
  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'},
  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_dump_databases_runs_pg_dump_with_directory_format():
  97. databases = [{'name': 'foo', 'format': 'directory'}]
  98. process = flexmock()
  99. flexmock(module).should_receive('make_dump_path').and_return('')
  100. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  101. 'databases/localhost/foo'
  102. )
  103. flexmock(module.dump).should_receive('create_parent_directory_for_dump')
  104. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  105. flexmock(module).should_receive('execute_command').with_args(
  106. (
  107. 'pg_dump',
  108. '--no-password',
  109. '--clean',
  110. '--if-exists',
  111. '--format',
  112. 'directory',
  113. '--file',
  114. 'databases/localhost/foo',
  115. 'foo',
  116. ),
  117. shell=True,
  118. extra_environment=None,
  119. run_to_completion=False,
  120. ).and_return(process).once()
  121. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  122. def test_dump_databases_runs_pg_dump_with_options():
  123. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  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_named_pipe_for_dump')
  130. flexmock(module).should_receive('execute_command').with_args(
  131. (
  132. 'pg_dump',
  133. '--no-password',
  134. '--clean',
  135. '--if-exists',
  136. '--format',
  137. 'custom',
  138. '--stuff=such',
  139. 'foo',
  140. '>',
  141. 'databases/localhost/foo',
  142. ),
  143. shell=True,
  144. extra_environment=None,
  145. run_to_completion=False,
  146. ).and_return(process).once()
  147. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  148. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  149. databases = [{'name': 'all'}]
  150. process = flexmock()
  151. flexmock(module).should_receive('make_dump_path').and_return('')
  152. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  153. 'databases/localhost/all'
  154. )
  155. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  156. flexmock(module).should_receive('execute_command').with_args(
  157. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  158. shell=True,
  159. extra_environment=None,
  160. run_to_completion=False,
  161. ).and_return(process).once()
  162. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  163. def test_restore_database_dump_runs_pg_restore():
  164. database_config = [{'name': 'foo'}]
  165. extract_process = flexmock(stdout=flexmock())
  166. flexmock(module).should_receive('make_dump_path')
  167. flexmock(module.dump).should_receive('make_database_dump_filename')
  168. flexmock(module).should_receive('execute_command_with_processes').with_args(
  169. (
  170. 'pg_restore',
  171. '--no-password',
  172. '--if-exists',
  173. '--exit-on-error',
  174. '--clean',
  175. '--dbname',
  176. 'foo',
  177. ),
  178. processes=[extract_process],
  179. output_log_level=logging.DEBUG,
  180. input_file=extract_process.stdout,
  181. extra_environment=None,
  182. borg_local_path='borg',
  183. ).once()
  184. flexmock(module).should_receive('execute_command').with_args(
  185. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  186. extra_environment=None,
  187. ).once()
  188. module.restore_database_dump(
  189. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  190. )
  191. def test_restore_database_dump_errors_on_multiple_database_config():
  192. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  193. flexmock(module).should_receive('make_dump_path')
  194. flexmock(module.dump).should_receive('make_database_dump_filename')
  195. flexmock(module).should_receive('execute_command_with_processes').never()
  196. flexmock(module).should_receive('execute_command').never()
  197. with pytest.raises(ValueError):
  198. module.restore_database_dump(
  199. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  200. )
  201. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  202. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  203. extract_process = flexmock(stdout=flexmock())
  204. flexmock(module).should_receive('make_dump_path')
  205. flexmock(module.dump).should_receive('make_database_dump_filename')
  206. flexmock(module).should_receive('execute_command_with_processes').with_args(
  207. (
  208. 'pg_restore',
  209. '--no-password',
  210. '--if-exists',
  211. '--exit-on-error',
  212. '--clean',
  213. '--dbname',
  214. 'foo',
  215. '--host',
  216. 'database.example.org',
  217. '--port',
  218. '5433',
  219. ),
  220. processes=[extract_process],
  221. output_log_level=logging.DEBUG,
  222. input_file=extract_process.stdout,
  223. extra_environment=None,
  224. borg_local_path='borg',
  225. ).once()
  226. flexmock(module).should_receive('execute_command').with_args(
  227. (
  228. 'psql',
  229. '--no-password',
  230. '--quiet',
  231. '--host',
  232. 'database.example.org',
  233. '--port',
  234. '5433',
  235. '--dbname',
  236. 'foo',
  237. '--command',
  238. 'ANALYZE',
  239. ),
  240. extra_environment=None,
  241. ).once()
  242. module.restore_database_dump(
  243. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  244. )
  245. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  246. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  247. extract_process = flexmock(stdout=flexmock())
  248. flexmock(module).should_receive('make_dump_path')
  249. flexmock(module.dump).should_receive('make_database_dump_filename')
  250. flexmock(module).should_receive('execute_command_with_processes').with_args(
  251. (
  252. 'pg_restore',
  253. '--no-password',
  254. '--if-exists',
  255. '--exit-on-error',
  256. '--clean',
  257. '--dbname',
  258. 'foo',
  259. '--username',
  260. 'postgres',
  261. ),
  262. processes=[extract_process],
  263. output_log_level=logging.DEBUG,
  264. input_file=extract_process.stdout,
  265. extra_environment={'PGPASSWORD': 'trustsome1'},
  266. borg_local_path='borg',
  267. ).once()
  268. flexmock(module).should_receive('execute_command').with_args(
  269. (
  270. 'psql',
  271. '--no-password',
  272. '--quiet',
  273. '--username',
  274. 'postgres',
  275. '--dbname',
  276. 'foo',
  277. '--command',
  278. 'ANALYZE',
  279. ),
  280. extra_environment={'PGPASSWORD': 'trustsome1'},
  281. ).once()
  282. module.restore_database_dump(
  283. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  284. )
  285. def test_restore_database_dump_runs_psql_for_all_database_dump():
  286. database_config = [{'name': 'all'}]
  287. extract_process = flexmock(stdout=flexmock())
  288. flexmock(module).should_receive('make_dump_path')
  289. flexmock(module.dump).should_receive('make_database_dump_filename')
  290. flexmock(module).should_receive('execute_command_with_processes').with_args(
  291. ('psql', '--no-password'),
  292. processes=[extract_process],
  293. output_log_level=logging.DEBUG,
  294. input_file=extract_process.stdout,
  295. extra_environment=None,
  296. borg_local_path='borg',
  297. ).once()
  298. flexmock(module).should_receive('execute_command').with_args(
  299. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
  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_with_dry_run_skips_restore():
  305. database_config = [{'name': 'foo'}]
  306. flexmock(module).should_receive('make_dump_path')
  307. flexmock(module.dump).should_receive('make_database_dump_filename')
  308. flexmock(module).should_receive('execute_command_with_processes').never()
  309. module.restore_database_dump(
  310. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  311. )
  312. def test_restore_database_dump_without_extract_process_restores_from_disk():
  313. database_config = [{'name': 'foo'}]
  314. flexmock(module).should_receive('make_dump_path')
  315. flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
  316. flexmock(module).should_receive('execute_command_with_processes').with_args(
  317. (
  318. 'pg_restore',
  319. '--no-password',
  320. '--if-exists',
  321. '--exit-on-error',
  322. '--clean',
  323. '--dbname',
  324. 'foo',
  325. '/dump/path',
  326. ),
  327. processes=[],
  328. output_log_level=logging.DEBUG,
  329. input_file=None,
  330. extra_environment=None,
  331. borg_local_path='borg',
  332. ).once()
  333. flexmock(module).should_receive('execute_command').with_args(
  334. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  335. extra_environment=None,
  336. ).once()
  337. module.restore_database_dump(
  338. database_config, 'test.yaml', {}, dry_run=False, extract_process=None
  339. )