test_postgresql.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. '--no-sync',
  20. '--file',
  21. 'databases/localhost/{}'.format(name),
  22. '--format',
  23. 'custom',
  24. name,
  25. ),
  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. '--no-sync',
  54. '--file',
  55. 'databases/database.example.org/foo',
  56. '--host',
  57. 'database.example.org',
  58. '--port',
  59. '5433',
  60. '--format',
  61. 'custom',
  62. 'foo',
  63. ),
  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. '--no-sync',
  83. '--file',
  84. 'databases/localhost/foo',
  85. '--username',
  86. 'postgres',
  87. '--format',
  88. 'custom',
  89. 'foo',
  90. ),
  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. '--no-sync',
  110. '--file',
  111. 'databases/localhost/foo',
  112. '--format',
  113. 'tar',
  114. 'foo',
  115. ),
  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. '--no-sync',
  135. '--file',
  136. 'databases/localhost/foo',
  137. '--format',
  138. 'custom',
  139. '--stuff=such',
  140. 'foo',
  141. ),
  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. (
  156. 'pg_dumpall',
  157. '--no-password',
  158. '--clean',
  159. '--if-exists',
  160. '--no-sync',
  161. '--file',
  162. 'databases/localhost/all',
  163. ),
  164. extra_environment=None,
  165. run_to_completion=False,
  166. ).and_return(process).once()
  167. assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
  168. def test_restore_database_dump_runs_pg_restore():
  169. database_config = [{'name': 'foo'}]
  170. extract_process = flexmock(stdout=flexmock())
  171. flexmock(module).should_receive('execute_command_with_processes').with_args(
  172. (
  173. 'pg_restore',
  174. '--no-password',
  175. '--if-exists',
  176. '--exit-on-error',
  177. '--clean',
  178. '--dbname',
  179. 'foo',
  180. ),
  181. processes=[extract_process],
  182. input_file=extract_process.stdout,
  183. extra_environment=None,
  184. ).once()
  185. flexmock(module).should_receive('execute_command').with_args(
  186. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  187. extra_environment=None,
  188. ).once()
  189. module.restore_database_dump(
  190. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  191. )
  192. def test_restore_database_dump_errors_on_multiple_database_config():
  193. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  194. flexmock(module).should_receive('execute_command_with_processes').never()
  195. flexmock(module).should_receive('execute_command').never()
  196. with pytest.raises(ValueError):
  197. module.restore_database_dump(
  198. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  199. )
  200. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  201. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  202. extract_process = flexmock(stdout=flexmock())
  203. flexmock(module).should_receive('execute_command_with_processes').with_args(
  204. (
  205. 'pg_restore',
  206. '--no-password',
  207. '--if-exists',
  208. '--exit-on-error',
  209. '--clean',
  210. '--dbname',
  211. 'foo',
  212. '--host',
  213. 'database.example.org',
  214. '--port',
  215. '5433',
  216. ),
  217. processes=[extract_process],
  218. input_file=extract_process.stdout,
  219. extra_environment=None,
  220. ).once()
  221. flexmock(module).should_receive('execute_command').with_args(
  222. (
  223. 'psql',
  224. '--no-password',
  225. '--quiet',
  226. '--host',
  227. 'database.example.org',
  228. '--port',
  229. '5433',
  230. '--dbname',
  231. 'foo',
  232. '--command',
  233. 'ANALYZE',
  234. ),
  235. extra_environment=None,
  236. ).once()
  237. module.restore_database_dump(
  238. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  239. )
  240. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  241. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  242. extract_process = flexmock(stdout=flexmock())
  243. flexmock(module).should_receive('execute_command_with_processes').with_args(
  244. (
  245. 'pg_restore',
  246. '--no-password',
  247. '--if-exists',
  248. '--exit-on-error',
  249. '--clean',
  250. '--dbname',
  251. 'foo',
  252. '--username',
  253. 'postgres',
  254. ),
  255. processes=[extract_process],
  256. input_file=extract_process.stdout,
  257. extra_environment={'PGPASSWORD': 'trustsome1'},
  258. ).once()
  259. flexmock(module).should_receive('execute_command').with_args(
  260. (
  261. 'psql',
  262. '--no-password',
  263. '--quiet',
  264. '--username',
  265. 'postgres',
  266. '--dbname',
  267. 'foo',
  268. '--command',
  269. 'ANALYZE',
  270. ),
  271. extra_environment={'PGPASSWORD': 'trustsome1'},
  272. ).once()
  273. module.restore_database_dump(
  274. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  275. )
  276. def test_restore_database_dump_runs_psql_for_all_database_dump():
  277. database_config = [{'name': 'all'}]
  278. extract_process = flexmock(stdout=flexmock())
  279. flexmock(module).should_receive('execute_command_with_processes').with_args(
  280. ('psql', '--no-password'),
  281. processes=[extract_process],
  282. input_file=extract_process.stdout,
  283. extra_environment=None,
  284. ).once()
  285. flexmock(module).should_receive('execute_command').with_args(
  286. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
  287. ).once()
  288. module.restore_database_dump(
  289. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  290. )