2020-05-09 04:38:33 +02:00
|
|
|
import logging
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
import pytest
|
2019-10-23 06:39:30 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import postgresql as module
|
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dump_for_each_database():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
processes = [flexmock(), flexmock()]
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
for name, process in zip(('foo', 'bar'), processes):
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'pg_dump',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--if-exists',
|
2019-10-23 06:39:30 +02:00
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
name,
|
2020-05-09 04:11:26 +02:00
|
|
|
'>',
|
|
|
|
'databases/localhost/{}'.format(name),
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_with_dry_run_skips_pg_dump():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module).should_receive('execute_command').never()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dump_with_hostname_and_port():
|
|
|
|
databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/database.example.org/foo'
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'pg_dump',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--if-exists',
|
2019-10-23 06:39:30 +02:00
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'foo',
|
2020-05-09 04:11:26 +02:00
|
|
|
'>',
|
|
|
|
'databases/database.example.org/foo',
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dump_with_username_and_password():
|
|
|
|
databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'pg_dump',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--if-exists',
|
2019-10-23 06:39:30 +02:00
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'foo',
|
2020-05-09 04:11:26 +02:00
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1'},
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dump_with_format():
|
|
|
|
databases = [{'name': 'foo', 'format': 'tar'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'pg_dump',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--if-exists',
|
2019-10-23 06:39:30 +02:00
|
|
|
'--format',
|
|
|
|
'tar',
|
|
|
|
'foo',
|
2020-05-09 04:11:26 +02:00
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dump_with_options():
|
|
|
|
databases = [{'name': 'foo', 'options': '--stuff=such'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'pg_dump',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--if-exists',
|
2019-10-23 06:39:30 +02:00
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'--stuff=such',
|
|
|
|
'foo',
|
2020-05-09 04:11:26 +02:00
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dumpall_for_all_databases():
|
|
|
|
databases = [{'name': 'all'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/localhost/all'
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2020-05-09 04:11:26 +02:00
|
|
|
('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'),
|
|
|
|
shell=True,
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-10-31 20:57:36 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_pg_restore():
|
|
|
|
database_config = [{'name': 'foo'}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-10-31 20:57:36 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
('psql', '--no-password', '--quiet', '--dbname', 'foo', '--command', 'ANALYZE'),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
2019-10-31 20:57:36 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_errors_on_multiple_database_config():
|
|
|
|
database_config = [{'name': 'foo'}, {'name': 'bar'}]
|
2019-10-31 22:11:19 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
flexmock(module).should_receive('execute_command').never()
|
2019-10-31 22:11:19 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
|
|
|
|
)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
|
|
|
|
database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-10-31 22:11:19 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2019-10-31 22:11:19 +01:00
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
2019-10-31 22:11:19 +01:00
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
),
|
2020-05-07 20:44:04 +02:00
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
2019-10-31 22:11:19 +01:00
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--quiet',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_pg_restore_with_username_and_password():
|
|
|
|
database_config = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-10-31 22:11:19 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2019-10-31 22:11:19 +01:00
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--clean',
|
2019-10-31 22:11:19 +01:00
|
|
|
'--dbname',
|
|
|
|
'foo',
|
2020-04-25 00:32:53 +02:00
|
|
|
'--username',
|
|
|
|
'postgres',
|
2019-10-31 22:11:19 +01:00
|
|
|
),
|
2020-05-07 20:44:04 +02:00
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
2019-10-31 22:11:19 +01:00
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--quiet',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1'},
|
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2020-04-25 00:32:53 +02:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_psql_for_all_database_dump():
|
|
|
|
database_config = [{'name': 'all'}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2020-04-25 00:32:53 +02:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
('psql', '--no-password'),
|
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment=None,
|
2020-04-25 00:32:53 +02:00
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
|
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2020-05-07 21:14:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_with_dry_run_skips_restore():
|
|
|
|
database_config = [{'name': 'foo'}]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
|
|
|
|
)
|