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
|
|
|
|
|
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
def test_database_names_to_dump_passes_through_individual_database_name():
|
|
|
|
database = {'name': 'foo'}
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
|
|
|
'foo',
|
|
|
|
)
|
2022-12-16 07:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_passes_through_individual_database_name_with_format():
|
|
|
|
database = {'name': 'foo', 'format': 'custom'}
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
|
|
|
'foo',
|
|
|
|
)
|
2022-12-16 07:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_passes_through_all_without_format():
|
|
|
|
database = {'name': 'all'}
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
|
|
|
'all',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_with_all_and_format_and_dry_run_bails():
|
|
|
|
database = {'name': 'all', 'format': 'custom'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').never()
|
|
|
|
|
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=True) == ()
|
2022-12-16 07:59:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_with_all_and_format_lists_databases():
|
|
|
|
database = {'name': 'all', 'format': 'custom'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').and_return(
|
|
|
|
'foo,test,\nbar,test,"stuff and such"'
|
|
|
|
)
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
2022-12-16 07:59:42 +01:00
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-26 23:59:17 +01:00
|
|
|
def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostname_and_port():
|
|
|
|
database = {'name': 'all', 'format': 'custom', 'hostname': 'localhost', 'port': 1234}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--list',
|
|
|
|
'--no-password',
|
2023-04-14 15:49:49 +02:00
|
|
|
'--no-psqlrc',
|
2023-01-26 23:59:17 +01:00
|
|
|
'--csv',
|
|
|
|
'--tuples-only',
|
|
|
|
'--host',
|
|
|
|
'localhost',
|
|
|
|
'--port',
|
|
|
|
'1234',
|
|
|
|
),
|
|
|
|
extra_environment=object,
|
|
|
|
).and_return('foo,test,\nbar,test,"stuff and such"')
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
2023-01-26 23:59:17 +01:00
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_with_all_and_format_lists_databases_with_username():
|
|
|
|
database = {'name': 'all', 'format': 'custom', 'username': 'postgres'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').with_args(
|
2023-04-21 06:44:42 +02:00
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--list',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--csv',
|
|
|
|
'--tuples-only',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
),
|
2023-01-26 23:59:17 +01:00
|
|
|
extra_environment=object,
|
|
|
|
).and_return('foo,test,\nbar,test,"stuff and such"')
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
2023-01-26 23:59:17 +01:00
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_with_all_and_format_lists_databases_with_options():
|
|
|
|
database = {'name': 'all', 'format': 'custom', 'list_options': '--harder'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').with_args(
|
2023-04-14 15:49:49 +02:00
|
|
|
('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'),
|
2023-01-26 23:59:17 +01:00
|
|
|
extra_environment=object,
|
|
|
|
).and_return('foo,test,\nbar,test,"stuff and such"')
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
2023-01-26 23:59:17 +01:00
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
def test_database_names_to_dump_with_all_and_format_excludes_particular_databases():
|
|
|
|
database = {'name': 'all', 'format': 'custom'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').and_return(
|
|
|
|
'foo,test,\ntemplate0,test,blah'
|
|
|
|
)
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
|
|
|
'foo',
|
|
|
|
)
|
2022-12-16 07:59:42 +01:00
|
|
|
|
|
|
|
|
2023-04-21 06:44:42 +02:00
|
|
|
def test_database_names_to_dump_with_all_and_psql_command_uses_custom_command():
|
|
|
|
database = {'name': 'all', 'format': 'custom', 'psql_command': 'docker exec mycontainer psql'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').with_args(
|
|
|
|
(
|
|
|
|
'docker',
|
|
|
|
'exec',
|
|
|
|
'mycontainer',
|
|
|
|
'psql',
|
|
|
|
'--list',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--csv',
|
|
|
|
'--tuples-only',
|
|
|
|
),
|
|
|
|
extra_environment=object,
|
|
|
|
).and_return('foo,text').once()
|
|
|
|
|
|
|
|
assert module.database_names_to_dump(database, flexmock(), flexmock(), dry_run=False) == (
|
|
|
|
'foo',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-23 06:39:30 +02:00
|
|
|
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()]
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
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')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
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
|
|
|
'>',
|
2023-03-24 07:11:14 +01:00
|
|
|
f'databases/localhost/{name}',
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
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
|
|
|
|
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
def test_dump_databases_raises_when_no_database_names_to_dump():
|
2022-12-16 07:59:42 +01:00
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(())
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
|
|
|
|
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
def test_dump_databases_does_not_raise_when_no_database_names_to_dump():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(())
|
|
|
|
|
|
|
|
module.dump_databases(databases, 'test.yaml', {}, dry_run=True) == []
|
|
|
|
|
|
|
|
|
2023-01-26 23:59:17 +01:00
|
|
|
def test_dump_databases_with_duplicate_dump_skips_pg_dump():
|
2023-01-26 08:31:07 +01:00
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(True)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
|
|
|
flexmock(module).should_receive('execute_command').never()
|
|
|
|
|
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
|
|
|
|
|
|
|
|
|
2019-10-23 06:39:30 +02:00
|
|
|
def test_dump_databases_with_dry_run_skips_pg_dump():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
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')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
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()
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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'
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
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()
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return(
|
|
|
|
{'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
|
|
|
|
)
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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'
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
|
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
|
|
|
|
|
|
|
|
2020-06-20 23:39:16 +02:00
|
|
|
def test_make_extra_environment_maps_options_to_environment():
|
2020-06-19 15:26:48 +02:00
|
|
|
database = {
|
|
|
|
'name': 'foo',
|
2020-06-20 23:39:16 +02:00
|
|
|
'password': 'pass',
|
2020-06-19 15:26:48 +02:00
|
|
|
'ssl_mode': 'require',
|
|
|
|
'ssl_cert': 'cert.crt',
|
|
|
|
'ssl_key': 'key.key',
|
|
|
|
'ssl_root_cert': 'root.crt',
|
|
|
|
'ssl_crl': 'crl.crl',
|
|
|
|
}
|
|
|
|
expected = {
|
2020-06-20 23:39:16 +02:00
|
|
|
'PGPASSWORD': 'pass',
|
2020-06-19 15:26:48 +02:00
|
|
|
'PGSSLMODE': 'require',
|
|
|
|
'PGSSLCERT': 'cert.crt',
|
|
|
|
'PGSSLKEY': 'key.key',
|
|
|
|
'PGSSLROOTCERT': 'root.crt',
|
|
|
|
'PGSSLCRL': 'crl.crl',
|
|
|
|
}
|
|
|
|
|
|
|
|
extra_env = module.make_extra_environment(database)
|
|
|
|
assert extra_env == expected
|
|
|
|
|
|
|
|
|
2020-05-18 20:31:29 +02:00
|
|
|
def test_dump_databases_runs_pg_dump_with_directory_format():
|
|
|
|
databases = [{'name': 'foo', 'format': 'directory'}]
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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'
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module.dump).should_receive('create_parent_directory_for_dump')
|
|
|
|
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').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',
|
2020-05-18 20:31:29 +02:00
|
|
|
'directory',
|
|
|
|
'--file',
|
2020-05-09 04:11:26 +02:00
|
|
|
'databases/localhost/foo',
|
2020-05-18 20:31:29 +02:00
|
|
|
'foo',
|
2019-10-23 06:39:30 +02:00
|
|
|
),
|
2020-05-09 04:11:26 +02:00
|
|
|
shell=True,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2023-02-21 00:18:51 +01:00
|
|
|
).and_return(flexmock()).once()
|
2019-10-23 06:39:30 +02:00
|
|
|
|
2023-02-21 00:18:51 +01:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == []
|
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()
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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'
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2020-06-19 15:26:48 +02:00
|
|
|
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
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()
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('all',))
|
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'
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
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
|
|
|
|
|
|
|
|
2022-11-06 11:11:05 +01:00
|
|
|
def test_dump_databases_runs_non_default_pg_dump():
|
|
|
|
databases = [{'name': 'foo', 'pg_dump_command': 'special_pg_dump'}]
|
|
|
|
process = flexmock()
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2022-11-06 11:11:05 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
2022-11-06 11:11:05 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2022-11-06 11:11:05 +01:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'special_pg_dump',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
|
|
|
'--if-exists',
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'foo',
|
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
),
|
|
|
|
shell=True,
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_pg_restore():
|
2023-04-14 23:27:51 +02:00
|
|
|
database_config = [{'name': 'foo', 'schemas': None}]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-10-31 20:57:36 +01:00
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2020-05-07 20:44:04 +02:00
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-21 06:44:42 +02:00
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2020-05-07 20:44:04 +02:00
|
|
|
).once()
|
2019-10-31 20:57:36 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
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
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
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(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=flexmock(),
|
|
|
|
connection_params={
|
|
|
|
'restore_hostname': None,
|
|
|
|
'restore_port': None,
|
|
|
|
'restore_username': None,
|
|
|
|
'restore_password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
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():
|
2023-04-14 23:27:51 +02:00
|
|
|
database_config = [
|
|
|
|
{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
|
|
|
|
]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-10-31 22:11:19 +01:00
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2019-10-31 22:11:19 +01:00
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
2023-04-14 15:49:49 +02:00
|
|
|
'--no-psqlrc',
|
2019-10-31 22:11:19 +01:00
|
|
|
'--quiet',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2019-10-31 22:11:19 +01:00
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
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():
|
2023-04-14 23:27:51 +02:00
|
|
|
database_config = [
|
|
|
|
{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None}
|
|
|
|
]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-10-31 22:11:19 +01:00
|
|
|
|
2020-06-20 23:39:16 +02:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return(
|
|
|
|
{'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}
|
|
|
|
)
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
|
2019-10-31 22:11:19 +01:00
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
2023-04-14 15:49:49 +02:00
|
|
|
'--no-psqlrc',
|
2019-10-31 22:11:19 +01:00
|
|
|
'--quiet',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
|
2019-10-31 22:11:19 +01:00
|
|
|
).once()
|
|
|
|
|
2023-06-16 21:17:15 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_with_cli_password_runs_pg_restore_with_password():
|
|
|
|
database_config = [{'name': 'foo', 'username': 'postgres', 'schemas': None}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': 'trustsome1',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_with_no_passwords_runs_pg_restore_without_password():
|
|
|
|
database_config = [{'name': 'foo', 'username': 'postgres', 'schemas': None}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
|
2023-01-26 23:59:17 +01:00
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2023-01-26 23:59:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_runs_pg_restore_with_options():
|
|
|
|
database_config = [
|
2023-04-14 23:27:51 +02:00
|
|
|
{
|
|
|
|
'name': 'foo',
|
|
|
|
'restore_options': '--harder',
|
|
|
|
'analyze_options': '--smarter',
|
|
|
|
'schemas': None,
|
|
|
|
}
|
2023-01-26 23:59:17 +01:00
|
|
|
]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--harder',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
2023-04-14 15:49:49 +02:00
|
|
|
'--no-psqlrc',
|
2023-01-26 23:59:17 +01:00
|
|
|
'--quiet',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--smarter',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
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():
|
2023-04-14 23:27:51 +02:00
|
|
|
database_config = [{'name': 'all', 'schemas': None}]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2020-04-25 00:32:53 +02:00
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2023-04-21 19:34:50 +02:00
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
),
|
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,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2020-04-25 00:32:53 +02:00
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-14 15:49:49 +02:00
|
|
|
('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'),
|
2020-06-19 15:26:48 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2020-04-25 00:32:53 +02:00
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
2020-05-07 21:14:27 +02:00
|
|
|
|
|
|
|
|
2023-04-21 06:44:42 +02:00
|
|
|
def test_restore_database_dump_runs_psql_for_plain_database_dump():
|
|
|
|
database_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2023-04-21 19:34:50 +02:00
|
|
|
('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'),
|
2023-04-21 06:44:42 +02:00
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2023-04-21 06:44:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-06 11:11:05 +01:00
|
|
|
def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
|
|
|
|
database_config = [
|
2023-04-14 23:27:51 +02:00
|
|
|
{
|
|
|
|
'name': 'foo',
|
2023-04-21 06:44:42 +02:00
|
|
|
'pg_restore_command': 'docker exec mycontainer pg_restore',
|
|
|
|
'psql_command': 'docker exec mycontainer psql',
|
2023-04-14 23:27:51 +02:00
|
|
|
'schemas': None,
|
|
|
|
}
|
2022-11-06 11:11:05 +01:00
|
|
|
]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2022-11-06 11:11:05 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
2023-04-21 06:44:42 +02:00
|
|
|
'docker',
|
|
|
|
'exec',
|
|
|
|
'mycontainer',
|
|
|
|
'pg_restore',
|
2022-11-06 11:11:05 +01:00
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-21 06:44:42 +02:00
|
|
|
(
|
|
|
|
'docker',
|
|
|
|
'exec',
|
|
|
|
'mycontainer',
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
2022-11-06 11:11:05 +01:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2022-11-06 11:11:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-07 21:14:27 +02:00
|
|
|
def test_restore_database_dump_with_dry_run_skips_restore():
|
2023-04-14 23:27:51 +02:00
|
|
|
database_config = [{'name': 'foo', 'schemas': None}]
|
2020-05-07 21:14:27 +02:00
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
2020-05-07 21:14:27 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=True,
|
|
|
|
extract_process=flexmock(),
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 21:14:27 +02:00
|
|
|
)
|
2020-05-18 20:31:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_without_extract_process_restores_from_disk():
|
2023-04-14 23:27:51 +02:00
|
|
|
database_config = [{'name': 'foo', 'schemas': None}]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'/dump/path',
|
|
|
|
),
|
|
|
|
processes=[],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=None,
|
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-21 06:44:42 +02:00
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
2023-04-14 23:27:51 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=None,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2023-04-14 23:27:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_with_schemas_restores_schemas():
|
|
|
|
database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
|
2020-05-18 20:31:29 +02:00
|
|
|
|
2022-12-16 07:59:42 +01:00
|
|
|
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'pg_restore',
|
|
|
|
'--no-password',
|
|
|
|
'--if-exists',
|
|
|
|
'--exit-on-error',
|
|
|
|
'--clean',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'/dump/path',
|
2023-04-14 23:27:51 +02:00
|
|
|
'--schema',
|
|
|
|
'bar',
|
|
|
|
'--schema',
|
|
|
|
'baz',
|
2020-05-18 20:31:29 +02:00
|
|
|
),
|
|
|
|
processes=[],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=None,
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2020-05-18 20:31:29 +02:00
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-21 06:44:42 +02:00
|
|
|
(
|
|
|
|
'psql',
|
|
|
|
'--no-password',
|
|
|
|
'--no-psqlrc',
|
|
|
|
'--quiet',
|
|
|
|
'--dbname',
|
|
|
|
'foo',
|
|
|
|
'--command',
|
|
|
|
'ANALYZE',
|
|
|
|
),
|
2020-06-18 00:16:34 +02:00
|
|
|
extra_environment={'PGSSLMODE': 'disable'},
|
2020-05-18 20:31:29 +02:00
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-06-16 11:44:00 +02:00
|
|
|
database_config,
|
|
|
|
'test.yaml',
|
|
|
|
{},
|
|
|
|
dry_run=False,
|
|
|
|
extract_process=None,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-18 20:31:29 +02:00
|
|
|
)
|