test_postgresql.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_format():
  97. databases = [{'name': 'foo', 'format': 'tar'}]
  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_named_pipe_for_dump')
  104. flexmock(module).should_receive('execute_command').with_args(
  105. (
  106. 'pg_dump',
  107. '--no-password',
  108. '--clean',
  109. '--if-exists',
  110. '--format',
  111. 'tar',
  112. 'foo',
  113. '>',
  114. 'databases/localhost/foo',
  115. ),
  116. shell=True,
  117. extra_environment=None,
  118. run_to_completion=False,
  119. ).and_return(process).once()
  120. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  121. def test_dump_databases_runs_pg_dump_with_options():
  122. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  123. process = flexmock()
  124. flexmock(module).should_receive('make_dump_path').and_return('')
  125. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  126. 'databases/localhost/foo'
  127. )
  128. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  129. flexmock(module).should_receive('execute_command').with_args(
  130. (
  131. 'pg_dump',
  132. '--no-password',
  133. '--clean',
  134. '--if-exists',
  135. '--format',
  136. 'custom',
  137. '--stuff=such',
  138. 'foo',
  139. '>',
  140. 'databases/localhost/foo',
  141. ),
  142. shell=True,
  143. extra_environment=None,
  144. run_to_completion=False,
  145. ).and_return(process).once()
  146. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  147. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  148. databases = [{'name': 'all'}]
  149. process = flexmock()
  150. flexmock(module).should_receive('make_dump_path').and_return('')
  151. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  152. 'databases/localhost/all'
  153. )
  154. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  155. flexmock(module).should_receive('execute_command').with_args(
  156. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  157. shell=True,
  158. extra_environment=None,
  159. run_to_completion=False,
  160. ).and_return(process).once()
  161. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  162. def test_restore_database_dump_runs_pg_restore():
  163. database_config = [{'name': 'foo'}]
  164. extract_process = flexmock(stdout=flexmock())
  165. flexmock(module).should_receive('execute_command_with_processes').with_args(
  166. (
  167. 'pg_restore',
  168. '--no-password',
  169. '--if-exists',
  170. '--exit-on-error',
  171. '--clean',
  172. '--dbname',
  173. 'foo',
  174. ),
  175. processes=[extract_process],
  176. output_log_level=logging.DEBUG,
  177. input_file=extract_process.stdout,
  178. extra_environment=None,
  179. ).once()
  180. flexmock(module).should_receive('execute_command').with_args(
  181. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  182. extra_environment=None,
  183. ).once()
  184. module.restore_database_dump(
  185. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  186. )
  187. def test_restore_database_dump_errors_on_multiple_database_config():
  188. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  189. flexmock(module).should_receive('execute_command_with_processes').never()
  190. flexmock(module).should_receive('execute_command').never()
  191. with pytest.raises(ValueError):
  192. module.restore_database_dump(
  193. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  194. )
  195. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  196. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  197. extract_process = flexmock(stdout=flexmock())
  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. '--host',
  208. 'database.example.org',
  209. '--port',
  210. '5433',
  211. ),
  212. processes=[extract_process],
  213. output_log_level=logging.DEBUG,
  214. input_file=extract_process.stdout,
  215. extra_environment=None,
  216. ).once()
  217. flexmock(module).should_receive('execute_command').with_args(
  218. (
  219. 'psql',
  220. '--no-password',
  221. '--quiet',
  222. '--host',
  223. 'database.example.org',
  224. '--port',
  225. '5433',
  226. '--dbname',
  227. 'foo',
  228. '--command',
  229. 'ANALYZE',
  230. ),
  231. extra_environment=None,
  232. ).once()
  233. module.restore_database_dump(
  234. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  235. )
  236. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  237. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  238. extract_process = flexmock(stdout=flexmock())
  239. flexmock(module).should_receive('execute_command_with_processes').with_args(
  240. (
  241. 'pg_restore',
  242. '--no-password',
  243. '--if-exists',
  244. '--exit-on-error',
  245. '--clean',
  246. '--dbname',
  247. 'foo',
  248. '--username',
  249. 'postgres',
  250. ),
  251. processes=[extract_process],
  252. output_log_level=logging.DEBUG,
  253. input_file=extract_process.stdout,
  254. extra_environment={'PGPASSWORD': 'trustsome1'},
  255. ).once()
  256. flexmock(module).should_receive('execute_command').with_args(
  257. (
  258. 'psql',
  259. '--no-password',
  260. '--quiet',
  261. '--username',
  262. 'postgres',
  263. '--dbname',
  264. 'foo',
  265. '--command',
  266. 'ANALYZE',
  267. ),
  268. extra_environment={'PGPASSWORD': 'trustsome1'},
  269. ).once()
  270. module.restore_database_dump(
  271. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  272. )
  273. def test_restore_database_dump_runs_psql_for_all_database_dump():
  274. database_config = [{'name': 'all'}]
  275. extract_process = flexmock(stdout=flexmock())
  276. flexmock(module).should_receive('execute_command_with_processes').with_args(
  277. ('psql', '--no-password'),
  278. processes=[extract_process],
  279. output_log_level=logging.DEBUG,
  280. input_file=extract_process.stdout,
  281. extra_environment=None,
  282. ).once()
  283. flexmock(module).should_receive('execute_command').with_args(
  284. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
  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_with_dry_run_skips_restore():
  290. database_config = [{'name': 'foo'}]
  291. flexmock(module).should_receive('execute_command_with_processes').never()
  292. module.restore_database_dump(
  293. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  294. )