2
0

test_postgresql.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.os.path).should_receive('expanduser').and_return('databases')
  7. flexmock(module.os).should_receive('makedirs')
  8. for name in ('foo', 'bar'):
  9. flexmock(module).should_receive('execute_command').with_args(
  10. (
  11. 'pg_dump',
  12. '--no-password',
  13. '--clean',
  14. '--file',
  15. 'databases/localhost/{}'.format(name),
  16. '--format',
  17. 'custom',
  18. name,
  19. ),
  20. extra_environment=None,
  21. ).once()
  22. module.dump_databases(databases, 'test.yaml', dry_run=False)
  23. def test_dump_databases_with_dry_run_skips_pg_dump():
  24. databases = [{'name': 'foo'}, {'name': 'bar'}]
  25. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  26. flexmock(module.os).should_receive('makedirs')
  27. flexmock(module).should_receive('execute_command').never()
  28. module.dump_databases(databases, 'test.yaml', dry_run=True)
  29. def test_dump_databases_without_databases_does_not_raise():
  30. module.dump_databases([], 'test.yaml', dry_run=False)
  31. def test_dump_databases_with_invalid_database_name_raises():
  32. databases = [{'name': 'heehee/../../etc/passwd'}]
  33. with pytest.raises(ValueError):
  34. module.dump_databases(databases, 'test.yaml', dry_run=True)
  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.os.path).should_receive('expanduser').and_return('databases')
  38. flexmock(module.os).should_receive('makedirs')
  39. flexmock(module).should_receive('execute_command').with_args(
  40. (
  41. 'pg_dump',
  42. '--no-password',
  43. '--clean',
  44. '--file',
  45. 'databases/database.example.org/foo',
  46. '--host',
  47. 'database.example.org',
  48. '--port',
  49. '5433',
  50. '--format',
  51. 'custom',
  52. 'foo',
  53. ),
  54. extra_environment=None,
  55. ).once()
  56. module.dump_databases(databases, 'test.yaml', dry_run=False)
  57. def test_dump_databases_runs_pg_dump_with_username_and_password():
  58. databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
  59. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  60. flexmock(module.os).should_receive('makedirs')
  61. flexmock(module).should_receive('execute_command').with_args(
  62. (
  63. 'pg_dump',
  64. '--no-password',
  65. '--clean',
  66. '--file',
  67. 'databases/localhost/foo',
  68. '--username',
  69. 'postgres',
  70. '--format',
  71. 'custom',
  72. 'foo',
  73. ),
  74. extra_environment={'PGPASSWORD': 'trustsome1'},
  75. ).once()
  76. module.dump_databases(databases, 'test.yaml', dry_run=False)
  77. def test_dump_databases_runs_pg_dump_with_format():
  78. databases = [{'name': 'foo', 'format': 'tar'}]
  79. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  80. flexmock(module.os).should_receive('makedirs')
  81. flexmock(module).should_receive('execute_command').with_args(
  82. (
  83. 'pg_dump',
  84. '--no-password',
  85. '--clean',
  86. '--file',
  87. 'databases/localhost/foo',
  88. '--format',
  89. 'tar',
  90. 'foo',
  91. ),
  92. extra_environment=None,
  93. ).once()
  94. module.dump_databases(databases, 'test.yaml', dry_run=False)
  95. def test_dump_databases_runs_pg_dump_with_options():
  96. databases = [{'name': 'foo', 'options': '--stuff=such'}]
  97. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  98. flexmock(module.os).should_receive('makedirs')
  99. flexmock(module).should_receive('execute_command').with_args(
  100. (
  101. 'pg_dump',
  102. '--no-password',
  103. '--clean',
  104. '--file',
  105. 'databases/localhost/foo',
  106. '--format',
  107. 'custom',
  108. '--stuff=such',
  109. 'foo',
  110. ),
  111. extra_environment=None,
  112. ).once()
  113. module.dump_databases(databases, 'test.yaml', dry_run=False)
  114. def test_dump_databases_runs_pg_dumpall_for_all_databases():
  115. databases = [{'name': 'all'}]
  116. flexmock(module.os.path).should_receive('expanduser').and_return('databases')
  117. flexmock(module.os).should_receive('makedirs')
  118. flexmock(module).should_receive('execute_command').with_args(
  119. ('pg_dumpall', '--no-password', '--clean', '--file', 'databases/localhost/all'),
  120. extra_environment=None,
  121. ).once()
  122. module.dump_databases(databases, 'test.yaml', dry_run=False)