test_postgresql.py 12 KB

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