2
0

test_postgresql.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. borg_local_path='borg',
  180. ).once()
  181. flexmock(module).should_receive('execute_command').with_args(
  182. ('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
  183. extra_environment=None,
  184. ).once()
  185. module.restore_database_dump(
  186. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  187. )
  188. def test_restore_database_dump_errors_on_multiple_database_config():
  189. database_config = [{'name': 'foo'}, {'name': 'bar'}]
  190. flexmock(module).should_receive('execute_command_with_processes').never()
  191. flexmock(module).should_receive('execute_command').never()
  192. with pytest.raises(ValueError):
  193. module.restore_database_dump(
  194. database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
  195. )
  196. def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
  197. database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  198. extract_process = flexmock(stdout=flexmock())
  199. flexmock(module).should_receive('execute_command_with_processes').with_args(
  200. (
  201. 'pg_restore',
  202. '--no-password',
  203. '--if-exists',
  204. '--exit-on-error',
  205. '--clean',
  206. '--dbname',
  207. 'foo',
  208. '--host',
  209. 'database.example.org',
  210. '--port',
  211. '5433',
  212. ),
  213. processes=[extract_process],
  214. output_log_level=logging.DEBUG,
  215. input_file=extract_process.stdout,
  216. extra_environment=None,
  217. borg_local_path='borg',
  218. ).once()
  219. flexmock(module).should_receive('execute_command').with_args(
  220. (
  221. 'psql',
  222. '--no-password',
  223. '--quiet',
  224. '--host',
  225. 'database.example.org',
  226. '--port',
  227. '5433',
  228. '--dbname',
  229. 'foo',
  230. '--command',
  231. 'ANALYZE',
  232. ),
  233. extra_environment=None,
  234. ).once()
  235. module.restore_database_dump(
  236. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  237. )
  238. def test_restore_database_dump_runs_pg_restore_with_username_and_password():
  239. database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  240. extract_process = flexmock(stdout=flexmock())
  241. flexmock(module).should_receive('execute_command_with_processes').with_args(
  242. (
  243. 'pg_restore',
  244. '--no-password',
  245. '--if-exists',
  246. '--exit-on-error',
  247. '--clean',
  248. '--dbname',
  249. 'foo',
  250. '--username',
  251. 'postgres',
  252. ),
  253. processes=[extract_process],
  254. output_log_level=logging.DEBUG,
  255. input_file=extract_process.stdout,
  256. extra_environment={'PGPASSWORD': 'trustsome1'},
  257. borg_local_path='borg',
  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. output_log_level=logging.DEBUG,
  283. input_file=extract_process.stdout,
  284. extra_environment=None,
  285. borg_local_path='borg',
  286. ).once()
  287. flexmock(module).should_receive('execute_command').with_args(
  288. ('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
  289. ).once()
  290. module.restore_database_dump(
  291. database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
  292. )
  293. def test_restore_database_dump_with_dry_run_skips_restore():
  294. database_config = [{'name': 'foo'}]
  295. flexmock(module).should_receive('execute_command_with_processes').never()
  296. module.restore_database_dump(
  297. database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
  298. )