test_postgresql.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.hooks import postgresql as module
  4. def test_dump_databases_runs_pg_dump_for_each_database():
  5. databases = [{'name': 'foo'}, {'name': 'bar'}]
  6. processes = [flexmock(), flexmock()]
  7. flexmock(module).should_receive('make_dump_path').and_return('')
  8. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  9. 'databases/localhost/foo'
  10. ).and_return('databases/localhost/bar')
  11. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  12. for name, process in zip(('foo', 'bar'), processes):
  13. flexmock(module).should_receive('execute_command').with_args(
  14. (
  15. 'pg_dump',
  16. '--no-password',
  17. '--clean',
  18. '--if-exists',
  19. '--format',
  20. 'custom',
  21. name,
  22. '>',
  23. 'databases/localhost/{}'.format(name),
  24. ),
  25. shell=True,
  26. extra_environment=None,
  27. run_to_completion=False,
  28. ).and_return(process).once()
  29. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
  30. def test_dump_databases_with_dry_run_skips_pg_dump():
  31. databases = [{'name': 'foo'}, {'name': 'bar'}]
  32. flexmock(module).should_receive('make_dump_path').and_return('')
  33. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  34. 'databases/localhost/foo'
  35. ).and_return('databases/localhost/bar')
  36. flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
  37. flexmock(module).should_receive('execute_command').never()
  38. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
  39. def test_dump_databases_runs_pg_dump_with_hostname_and_port():
  40. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  41. process = flexmock()
  42. flexmock(module).should_receive('make_dump_path').and_return('')
  43. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  44. 'databases/database.example.org/foo'
  45. )
  46. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  47. flexmock(module).should_receive('execute_command').with_args(
  48. (
  49. 'pg_dump',
  50. '--no-password',
  51. '--clean',
  52. '--if-exists',
  53. '--host',
  54. 'database.example.org',
  55. '--port',
  56. '5433',
  57. '--format',
  58. 'custom',
  59. 'foo',
  60. '>',
  61. 'databases/database.example.org/foo',
  62. ),
  63. shell=True,
  64. extra_environment=None,
  65. run_to_completion=False,
  66. ).and_return(process).once()
  67. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  68. def test_dump_databases_runs_pg_dump_with_username_and_password():
  69. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  70. process = flexmock()
  71. flexmock(module).should_receive('make_dump_path').and_return('')
  72. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  73. 'databases/localhost/foo'
  74. )
  75. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  76. flexmock(module).should_receive('execute_command').with_args(
  77. (
  78. 'pg_dump',
  79. '--no-password',
  80. '--clean',
  81. '--if-exists',
  82. '--username',
  83. 'postgres',
  84. '--format',
  85. 'custom',
  86. 'foo',
  87. '>',
  88. 'databases/localhost/foo',
  89. ),
  90. shell=True,
  91. extra_environment={'PGPASSWORD': 'trustsome1'},
  92. run_to_completion=False,
  93. ).and_return(process).once()
  94. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  95. def test_dump_databases_runs_pg_dump_with_format():
  96. databases = [{'name': 'foo', 'format': 'tar'}]
  97. process = flexmock()
  98. flexmock(module).should_receive('make_dump_path').and_return('')
  99. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  100. 'databases/localhost/foo'
  101. )
  102. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  103. flexmock(module).should_receive('execute_command').with_args(
  104. (
  105. 'pg_dump',
  106. '--no-password',
  107. '--clean',
  108. '--if-exists',
  109. '--format',
  110. 'tar',
  111. 'foo',
  112. '>',
  113. 'databases/localhost/foo',
  114. ),
  115. shell=True,
  116. extra_environment=None,
  117. run_to_completion=False,
  118. ).and_return(process).once()
  119. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  120. def test_dump_databases_runs_pg_dump_with_options():
  121. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  122. process = flexmock()
  123. flexmock(module).should_receive('make_dump_path').and_return('')
  124. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  125. 'databases/localhost/foo'
  126. )
  127. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  128. flexmock(module).should_receive('execute_command').with_args(
  129. (
  130. 'pg_dump',
  131. '--no-password',
  132. '--clean',
  133. '--if-exists',
  134. '--format',
  135. 'custom',
  136. '--stuff=such',
  137. 'foo',
  138. '>',
  139. 'databases/localhost/foo',
  140. ),
  141. shell=True,
  142. extra_environment=None,
  143. run_to_completion=False,
  144. ).and_return(process).once()
  145. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  146. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  147. databases = [{'name': 'all'}]
  148. process = flexmock()
  149. flexmock(module).should_receive('make_dump_path').and_return('')
  150. flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
  151. 'databases/localhost/all'
  152. )
  153. flexmock(module.dump).should_receive('create_named_pipe_for_dump')
  154. flexmock(module).should_receive('execute_command').with_args(
  155. ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
  156. shell=True,
  157. extra_environment=None,
  158. run_to_completion=False,
  159. ).and_return(process).once()
  160. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  161. def test_restore_database_dump_runs_pg_restore():
  162. database_config = [{'name': 'foo'}]
  163. extract_process = flexmock(stdout=flexmock())
  164. flexmock(module).should_receive('execute_command_with_processes').with_args(
  165. (
  166. 'pg_restore',
  167. '--no-password',
  168. '--if-exists',
  169. '--exit-on-error',
  170. '--clean',
  171. '--dbname',
  172. 'foo',
  173. ),
  174. processes=[extract_process],
  175. input_file=extract_process.stdout,
  176. extra_environment=None,
  177. ).once()
  178. flexmock(module).should_receive('execute_command').with_args(
  179. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  180. extra_environment=None,
  181. ).once()
  182. module.restore_database_dump(
  183. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  184. )
  185. def test_restore_database_dump_errors_on_multiple_database_config():
  186. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  187. flexmock(module).should_receive('execute_command_with_processes').never()
  188. flexmock(module).should_receive('execute_command').never()
  189. with pytest.raises(ValueError):
  190. module.restore_database_dump(
  191. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  192. )
  193. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  194. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  195. extract_process = flexmock(stdout=flexmock())
  196. flexmock(module).should_receive('execute_command_with_processes').with_args(
  197. (
  198. 'pg_restore',
  199. '--no-password',
  200. '--if-exists',
  201. '--exit-on-error',
  202. '--clean',
  203. '--dbname',
  204. 'foo',
  205. '--host',
  206. 'database.example.org',
  207. '--port',
  208. '5433',
  209. ),
  210. processes=[extract_process],
  211. input_file=extract_process.stdout,
  212. extra_environment=None,
  213. ).once()
  214. flexmock(module).should_receive('execute_command').with_args(
  215. (
  216. 'psql',
  217. '--no-password',
  218. '--quiet',
  219. '--host',
  220. 'database.example.org',
  221. '--port',
  222. '5433',
  223. '--dbname',
  224. 'foo',
  225. '--command',
  226. 'ANALYZE',
  227. ),
  228. extra_environment=None,
  229. ).once()
  230. module.restore_database_dump(
  231. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  232. )
  233. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  234. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  235. extract_process = flexmock(stdout=flexmock())
  236. flexmock(module).should_receive('execute_command_with_processes').with_args(
  237. (
  238. 'pg_restore',
  239. '--no-password',
  240. '--if-exists',
  241. '--exit-on-error',
  242. '--clean',
  243. '--dbname',
  244. 'foo',
  245. '--username',
  246. 'postgres',
  247. ),
  248. processes=[extract_process],
  249. input_file=extract_process.stdout,
  250. extra_environment={'PGPASSWORD': 'trustsome1'},
  251. ).once()
  252. flexmock(module).should_receive('execute_command').with_args(
  253. (
  254. 'psql',
  255. '--no-password',
  256. '--quiet',
  257. '--username',
  258. 'postgres',
  259. '--dbname',
  260. 'foo',
  261. '--command',
  262. 'ANALYZE',
  263. ),
  264. extra_environment={'PGPASSWORD': 'trustsome1'},
  265. ).once()
  266. module.restore_database_dump(
  267. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  268. )
  269. def test_restore_database_dump_runs_psql_for_all_database_dump():
  270. database_config = [{'name': 'all'}]
  271. extract_process = flexmock(stdout=flexmock())
  272. flexmock(module).should_receive('execute_command_with_processes').with_args(
  273. ('psql', '--no-password'),
  274. processes=[extract_process],
  275. input_file=extract_process.stdout,
  276. extra_environment=None,
  277. ).once()
  278. flexmock(module).should_receive('execute_command').with_args(
  279. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
  280. ).once()
  281. module.restore_database_dump(
  282. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  283. )
  284. def test_restore_database_dump_with_dry_run_skips_restore():
  285. database_config = [{'name': 'foo'}]
  286. flexmock(module).should_receive('execute_command_with_processes').never()
  287. module.restore_database_dump(
  288. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  289. )