test_postgresql.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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).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).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).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).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).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).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).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_remove_database_dumps_removes_dump_for_each_database():
  134. databases = [{'name': 'foo'}, {'name': 'bar'}]
  135. flexmock(module).should_receive('make_database_dump_filename').and_return(
  136. 'databases/localhost/foo'
  137. ).and_return('databases/localhost/bar')
  138. flexmock(module.os).should_receive('listdir').and_return([])
  139. flexmock(module.os).should_receive('rmdir')
  140. for name in ('foo', 'bar'):
  141. flexmock(module.os).should_receive('remove').with_args(
  142. 'databases/localhost/{}'.format(name)
  143. ).once()
  144. module.remove_database_dumps(databases, 'test.yaml', dry_run=False)
  145. def test_remove_database_dumps_with_dry_run_skips_removal():
  146. databases = [{'name': 'foo'}, {'name': 'bar'}]
  147. flexmock(module.os).should_receive('rmdir').never()
  148. flexmock(module.os).should_receive('remove').never()
  149. module.remove_database_dumps(databases, 'test.yaml', dry_run=True)
  150. def test_remove_database_dumps_without_databases_does_not_raise():
  151. module.remove_database_dumps([], 'test.yaml', dry_run=False)
  152. def test_make_database_dump_patterns_converts_names_to_glob_paths():
  153. flexmock(module).should_receive('make_database_dump_filename').and_return(
  154. 'databases/*/foo'
  155. ).and_return('databases/*/bar')
  156. assert module.make_database_dump_patterns(('foo', 'bar')) == [
  157. 'databases/*/foo',
  158. 'databases/*/bar',
  159. ]
  160. def test_make_database_dump_patterns_treats_empty_names_as_matching_all_databases():
  161. flexmock(module).should_receive('make_database_dump_filename').with_args(
  162. module.DUMP_PATH, '*', '*'
  163. ).and_return('databases/*/*')
  164. assert module.make_database_dump_patterns(()) == ['databases/*/*']
  165. def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
  166. assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']
  167. def test_get_database_names_from_dumps_gets_names_from_filenames_matching_globs():
  168. flexmock(module.glob).should_receive('glob').and_return(
  169. ('databases/localhost/foo',)
  170. ).and_return(('databases/localhost/bar',)).and_return(())
  171. assert module.get_database_names_from_dumps(
  172. ('databases/*/foo', 'databases/*/bar', 'databases/*/baz')
  173. ) == ['foo', 'bar']
  174. def test_get_database_configurations_only_produces_named_databases():
  175. databases = [
  176. {'name': 'foo', 'hostname': 'example.org'},
  177. {'name': 'bar', 'hostname': 'example.com'},
  178. {'name': 'baz', 'hostname': 'example.org'},
  179. ]
  180. assert list(module.get_database_configurations(databases, ('foo', 'baz'))) == [
  181. {'name': 'foo', 'hostname': 'example.org'},
  182. {'name': 'baz', 'hostname': 'example.org'},
  183. ]
  184. def test_get_database_configurations_matches_all_database():
  185. databases = [
  186. {'name': 'foo', 'hostname': 'example.org'},
  187. {'name': 'all', 'hostname': 'example.com'},
  188. ]
  189. assert list(module.get_database_configurations(databases, ('foo', 'bar', 'baz'))) == [
  190. {'name': 'foo', 'hostname': 'example.org'},
  191. {'name': 'bar', 'hostname': 'example.com'},
  192. {'name': 'baz', 'hostname': 'example.com'},
  193. ]
  194. def test_get_database_configurations_with_unknown_database_name_raises():
  195. databases = [{'name': 'foo', 'hostname': 'example.org'}]
  196. with pytest.raises(ValueError):
  197. list(module.get_database_configurations(databases, ('foo', 'bar')))
  198. def test_restore_database_dumps_restores_each_database():
  199. databases = [{'name': 'foo'}, {'name': 'bar'}]
  200. flexmock(module).should_receive('make_database_dump_filename').and_return(
  201. 'databases/localhost/foo'
  202. ).and_return('databases/localhost/bar')
  203. for name in ('foo', 'bar'):
  204. flexmock(module).should_receive('execute_command').with_args(
  205. (
  206. 'pg_restore',
  207. '--no-password',
  208. '--clean',
  209. '--if-exists',
  210. '--exit-on-error',
  211. '--dbname',
  212. name,
  213. 'databases/localhost/{}'.format(name),
  214. ),
  215. extra_environment=None,
  216. ).once()
  217. flexmock(module).should_receive('execute_command').with_args(
  218. ('psql', '--no-password', '--quiet', '--dbname', name, '--command', 'ANALYZE'),
  219. extra_environment=None,
  220. ).once()
  221. module.restore_database_dumps(databases, 'test.yaml', dry_run=False)
  222. def test_restore_database_dumps_without_databases_does_not_raise():
  223. module.restore_database_dumps({}, 'test.yaml', dry_run=False)
  224. def test_restore_database_dumps_runs_pg_restore_with_hostname_and_port():
  225. databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
  226. flexmock(module).should_receive('make_database_dump_filename').and_return(
  227. 'databases/localhost/foo'
  228. ).and_return('databases/localhost/bar')
  229. flexmock(module).should_receive('execute_command').with_args(
  230. (
  231. 'pg_restore',
  232. '--no-password',
  233. '--clean',
  234. '--if-exists',
  235. '--exit-on-error',
  236. '--host',
  237. 'database.example.org',
  238. '--port',
  239. '5433',
  240. '--dbname',
  241. 'foo',
  242. 'databases/localhost/foo',
  243. ),
  244. extra_environment=None,
  245. ).once()
  246. flexmock(module).should_receive('execute_command').with_args(
  247. (
  248. 'psql',
  249. '--no-password',
  250. '--quiet',
  251. '--host',
  252. 'database.example.org',
  253. '--port',
  254. '5433',
  255. '--dbname',
  256. 'foo',
  257. '--command',
  258. 'ANALYZE',
  259. ),
  260. extra_environment=None,
  261. ).once()
  262. module.restore_database_dumps(databases, 'test.yaml', dry_run=False)
  263. def test_restore_database_dumps_runs_pg_restore_with_username_and_password():
  264. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  265. flexmock(module).should_receive('make_database_dump_filename').and_return(
  266. 'databases/localhost/foo'
  267. ).and_return('databases/localhost/bar')
  268. flexmock(module).should_receive('execute_command').with_args(
  269. (
  270. 'pg_restore',
  271. '--no-password',
  272. '--clean',
  273. '--if-exists',
  274. '--exit-on-error',
  275. '--username',
  276. 'postgres',
  277. '--dbname',
  278. 'foo',
  279. 'databases/localhost/foo',
  280. ),
  281. extra_environment={'PGPASSWORD': 'trustsome1'},
  282. ).once()
  283. flexmock(module).should_receive('execute_command').with_args(
  284. (
  285. 'psql',
  286. '--no-password',
  287. '--quiet',
  288. '--username',
  289. 'postgres',
  290. '--dbname',
  291. 'foo',
  292. '--command',
  293. 'ANALYZE',
  294. ),
  295. extra_environment={'PGPASSWORD': 'trustsome1'},
  296. ).once()
  297. module.restore_database_dumps(databases, 'test.yaml', dry_run=False)