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'}]
|
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')
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
|
|
|
|
|
|
|
for name in ('foo', 'bar'):
|
|
|
|
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
|
|
|
'--file',
|
|
|
|
'databases/localhost/{}'.format(name),
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
name,
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
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'}]
|
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')
|
2019-11-08 20:17:52 +01:00
|
|
|
flexmock(module.os).should_receive('makedirs').never()
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module).should_receive('execute_command').never()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
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}]
|
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'
|
|
|
|
)
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
|
|
|
|
|
|
|
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
|
|
|
'--file',
|
|
|
|
'databases/database.example.org/foo',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'foo',
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
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_username_and_password():
|
|
|
|
databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
|
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'
|
|
|
|
)
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
|
|
|
|
|
|
|
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
|
|
|
'--file',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
'--username',
|
|
|
|
'postgres',
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'foo',
|
|
|
|
),
|
|
|
|
extra_environment={'PGPASSWORD': 'trustsome1'},
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
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_format():
|
|
|
|
databases = [{'name': 'foo', 'format': 'tar'}]
|
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'
|
|
|
|
)
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
|
|
|
|
|
|
|
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
|
|
|
'--file',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
'--format',
|
|
|
|
'tar',
|
|
|
|
'foo',
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
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'}]
|
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'
|
|
|
|
)
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
|
|
|
|
|
|
|
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
|
|
|
'--file',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
'--format',
|
|
|
|
'custom',
|
|
|
|
'--stuff=such',
|
|
|
|
'foo',
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
|
2019-10-23 06:39:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_pg_dumpall_for_all_databases():
|
|
|
|
databases = [{'name': 'all'}]
|
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'
|
|
|
|
)
|
2019-10-23 06:39:30 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2020-04-25 00:32:53 +02:00
|
|
|
(
|
|
|
|
'pg_dumpall',
|
|
|
|
'--no-password',
|
|
|
|
'--clean',
|
|
|
|
'--if-exists',
|
|
|
|
'--file',
|
|
|
|
'databases/localhost/all',
|
|
|
|
),
|
2019-10-23 06:39:30 +02:00
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
module.dump_databases(databases, 'test.yaml', {}, dry_run=False)
|
2019-10-23 22:36:03 +02:00
|
|
|
|
|
|
|
|
2019-10-31 20:57:36 +01:00
|
|
|
def test_make_database_dump_patterns_converts_names_to_glob_paths():
|
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/*/foo'
|
|
|
|
).and_return('databases/*/bar')
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
assert module.make_database_dump_patterns(flexmock(), flexmock(), {}, ('foo', 'bar')) == [
|
2019-10-31 20:57:36 +01:00
|
|
|
'databases/*/foo',
|
|
|
|
'databases/*/bar',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_database_dump_patterns_treats_empty_names_as_matching_all_databases():
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('/dump/path')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').with_args(
|
2019-12-11 01:04:34 +01:00
|
|
|
'/dump/path', '*', '*'
|
2019-11-08 20:17:52 +01:00
|
|
|
).and_return('databases/*/*')
|
2019-10-31 20:57:36 +01:00
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
assert module.make_database_dump_patterns(flexmock(), flexmock(), {}, ()) == ['databases/*/*']
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dumps_restores_each_database():
|
|
|
|
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 22:11:19 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
|
|
|
|
|
|
|
for name in ('foo', 'bar'):
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'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',
|
|
|
|
name,
|
|
|
|
'databases/localhost/{}'.format(name),
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
('psql', '--no-password', '--quiet', '--dbname', name, '--command', 'ANALYZE'),
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
module.restore_database_dumps(databases, 'test.yaml', {}, dry_run=False)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dumps_runs_pg_restore_with_hostname_and_port():
|
|
|
|
databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
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 22:11:19 +01:00
|
|
|
'databases/localhost/foo'
|
2019-11-12 18:50:26 +01:00
|
|
|
)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'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',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
),
|
|
|
|
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()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
module.restore_database_dumps(databases, 'test.yaml', {}, dry_run=False)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dumps_runs_pg_restore_with_username_and_password():
|
|
|
|
databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}]
|
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 22:11:19 +01:00
|
|
|
'databases/localhost/foo'
|
2019-11-12 18:50:26 +01:00
|
|
|
)
|
2019-10-31 22:11:19 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'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
|
|
|
'databases/localhost/foo',
|
|
|
|
),
|
|
|
|
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()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
module.restore_database_dumps(databases, 'test.yaml', {}, dry_run=False)
|
2020-04-25 00:32:53 +02:00
|
|
|
|
|
|
|
|
2020-04-25 00:56:56 +02:00
|
|
|
def test_restore_database_dumps_runs_psql_for_all_database_dump():
|
2020-04-25 00:32:53 +02:00
|
|
|
databases = [{'name': 'all'}]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/all'
|
|
|
|
)
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
('psql', '--no-password', '-f', 'databases/localhost/all'), extra_environment=None
|
|
|
|
).once()
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
('psql', '--no-password', '--quiet', '--command', 'ANALYZE'), extra_environment=None
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dumps(databases, 'test.yaml', {}, dry_run=False)
|