test_postgresql.py 11 KB

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