Fix last end-to-end database test (#721).

This commit is contained in:
Dan Helfman 2023-07-11 00:37:36 -07:00
parent 003d4eac93
commit ecd9e62147
9 changed files with 178 additions and 160 deletions

View file

@ -109,8 +109,9 @@ def restore_single_database(
# Run a single database restore, consuming the extract stdout (if any). # Run a single database restore, consuming the extract stdout (if any).
borgmatic.hooks.dispatch.call_hooks( borgmatic.hooks.dispatch.call_hooks(
'restore_database_dump', 'restore_database_dump',
{hook_name: [database]}, config,
repository['path'], repository['path'],
database['name'],
borgmatic.hooks.dump.DATABASE_HOOK_NAMES, borgmatic.hooks.dump.DATABASE_HOOK_NAMES,
global_arguments.dry_run, global_arguments.dry_run,
extract_process, extract_process,

View file

@ -100,24 +100,32 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra
def restore_database_dump( def restore_database_dump(
database_config, config, log_prefix, dry_run, extract_process, connection_params databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params
): ):
''' '''
Restore the given MongoDB database from an extract stream. The database is supplied as a Restore the given MongoDB database from an extract stream. The databases are supplied as a
one-element sequence containing a dict describing the database, as per the configuration schema. sequence containing one dict describing each database (as per the configuration schema), but
Use the configuration dict to construct the destination path and the given log prefix in any log only the database corresponding to the given database name is restored. Use the configuration
entries. If this is a dry run, then don't actually restore anything. Trigger the given active dict to construct the destination path and the given log prefix in any log entries. If this is a
extract process (an instance of subprocess.Popen) to produce output to consume. dry run, then don't actually restore anything. Trigger the given active extract process (an
instance of subprocess.Popen) to produce output to consume.
If the extract process is None, then restore the dump from the filesystem rather than from an If the extract process is None, then restore the dump from the filesystem rather than from an
extract stream. extract stream.
''' '''
dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else ''
if len(database_config) != 1: try:
raise ValueError('The database configuration value is invalid') database = next(
database_config
for database_config in databases_config
if database_config.get('name') == database_name
)
except StopIteration:
raise ValueError(
f'A database named "{database_name}" could not be found in the configuration'
)
database = database_config[0]
dump_filename = dump.make_database_dump_filename( dump_filename = dump.make_database_dump_filename(
make_dump_path(config), database['name'], database.get('hostname') make_dump_path(config), database['name'], database.get('hostname')
) )

View file

@ -181,21 +181,27 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra
def restore_database_dump( def restore_database_dump(
database_config, config, log_prefix, dry_run, extract_process, connection_params databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params
): ):
''' '''
Restore the given MySQL/MariaDB database from an extract stream. The database is supplied as a Restore the given MySQL/MariaDB database from an extract stream. The databases are supplied as a
one-element sequence containing a dict describing the database, as per the configuration schema. sequence containing one dict describing each database (as per the configuration schema), but
Use the given log prefix in any log entries. If this is a dry run, then don't actually restore only the database corresponding to the given database name is restored. Use the given log
anything. Trigger the given active extract process (an instance of subprocess.Popen) to produce prefix in any log entries. If this is a dry run, then don't actually restore anything. Trigger
output to consume. the given active extract process (an instance of subprocess.Popen) to produce output to consume.
''' '''
dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else ''
if len(database_config) != 1: try:
raise ValueError('The database configuration value is invalid') database = next(
database_config
database = database_config[0] for database_config in databases_config
if database_config.get('name') == database_name
)
except StopIteration:
raise ValueError(
f'A database named "{database_name}" could not be found in the configuration'
)
hostname = connection_params['hostname'] or database.get( hostname = connection_params['hostname'] or database.get(
'restore_hostname', database.get('hostname') 'restore_hostname', database.get('hostname')

View file

@ -202,14 +202,15 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra
def restore_database_dump( def restore_database_dump(
database_config, config, log_prefix, dry_run, extract_process, connection_params databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params
): ):
''' '''
Restore the given PostgreSQL database from an extract stream. The database is supplied as a Restore the given PostgreSQL database from an extract stream. The databases are supplied as a
one-element sequence containing a dict describing the database, as per the configuration schema. sequence containing one dict describing each database (as per the configuration schema), but
Use the given configuration dict to construct the destination path and the given log prefix in only the database corresponding to the given database name is restored. Use the given
any log entries. If this is a dry run, then don't actually restore anything. Trigger the given configuration dict to construct the destination path and the given log prefix in any log
active extract process (an instance of subprocess.Popen) to produce output to consume. entries. If this is a dry run, then don't actually restore anything. Trigger the given active
extract process (an instance of subprocess.Popen) to produce output to consume.
If the extract process is None, then restore the dump from the filesystem rather than from an If the extract process is None, then restore the dump from the filesystem rather than from an
extract stream. extract stream.
@ -219,10 +220,16 @@ def restore_database_dump(
''' '''
dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else ''
if len(database_config) != 1: try:
raise ValueError('The database configuration value is invalid') database = next(
database_config
database = database_config[0] for database_config in databases_config
if database_config.get('name') == database_name
)
except StopIteration:
raise ValueError(
f'A database named "{database_name}" could not be found in the configuration'
)
hostname = connection_params['hostname'] or database.get( hostname = connection_params['hostname'] or database.get(
'restore_hostname', database.get('hostname') 'restore_hostname', database.get('hostname')
@ -262,7 +269,7 @@ def restore_database_dump(
+ (() if extract_process else (dump_filename,)) + (() if extract_process else (dump_filename,))
+ tuple( + tuple(
itertools.chain.from_iterable(('--schema', schema) for schema in database['schemas']) itertools.chain.from_iterable(('--schema', schema) for schema in database['schemas'])
if database['schemas'] if database.get('schemas')
else () else ()
) )
) )

View file

@ -84,22 +84,30 @@ def make_database_dump_pattern(databases, config, log_prefix, name=None): # pra
def restore_database_dump( def restore_database_dump(
database_config, config, log_prefix, dry_run, extract_process, connection_params databases_config, config, log_prefix, database_name, dry_run, extract_process, connection_params
): ):
''' '''
Restore the given SQLite3 database from an extract stream. The database is supplied as a Restore the given SQLite3 database from an extract stream. The databases are supplied as a
one-element sequence containing a dict describing the database, as per the configuration schema. sequence containing one dict describing each database (as per the configuration schema), but
Use the given log prefix in any log entries. If this is a dry run, then don't actually restore only the database corresponding to the given database name is restored. Use the given log prefix
anything. Trigger the given active extract process (an instance of subprocess.Popen) to produce in any log entries. If this is a dry run, then don't actually restore anything. Trigger the
output to consume. given active extract process (an instance of subprocess.Popen) to produce output to consume.
''' '''
dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else '' dry_run_label = ' (dry run; not actually restoring anything)' if dry_run else ''
if len(database_config) != 1: try:
raise ValueError('The database configuration value is invalid') database = next(
database_config
for database_config in databases_config
if database_config.get('name') == database_name
)
except StopIteration:
raise ValueError(
f'A database named "{database_name}" could not be found in the configuration'
)
database_path = connection_params['restore_path'] or database_config[0].get( database_path = connection_params['restore_path'] or database.get(
'restore_path', database_config[0].get('path') 'restore_path', database.get('path')
) )
logger.debug(f'{log_prefix}: Restoring SQLite database at {database_path}{dry_run_label}') logger.debug(f'{log_prefix}: Restoring SQLite database at {database_path}{dry_run_label}')

View file

@ -158,7 +158,7 @@ def test_dump_databases_runs_mongodumpall_for_all_databases():
def test_restore_database_dump_runs_mongorestore(): def test_restore_database_dump_runs_mongorestore():
database_config = [{'name': 'foo', 'schemas': None}] databases_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -171,9 +171,10 @@ def test_restore_database_dump_runs_mongorestore():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -185,8 +186,8 @@ def test_restore_database_dump_runs_mongorestore():
) )
def test_restore_database_dump_errors_on_multiple_database_config(): def test_restore_database_dump_errors_on_empty_databases_config():
database_config = [{'name': 'foo'}, {'name': 'bar'}] databases_config = []
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
flexmock(module.dump).should_receive('make_database_dump_filename') flexmock(module.dump).should_receive('make_database_dump_filename')
@ -195,9 +196,10 @@ def test_restore_database_dump_errors_on_multiple_database_config():
with pytest.raises(ValueError): with pytest.raises(ValueError):
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=flexmock(), extract_process=flexmock(),
connection_params={ connection_params={
@ -210,7 +212,7 @@ def test_restore_database_dump_errors_on_multiple_database_config():
def test_restore_database_dump_runs_mongorestore_with_hostname_and_port(): def test_restore_database_dump_runs_mongorestore_with_hostname_and_port():
database_config = [ databases_config = [
{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None} {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
] ]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
@ -235,9 +237,10 @@ def test_restore_database_dump_runs_mongorestore_with_hostname_and_port():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -250,7 +253,7 @@ def test_restore_database_dump_runs_mongorestore_with_hostname_and_port():
def test_restore_database_dump_runs_mongorestore_with_username_and_password(): def test_restore_database_dump_runs_mongorestore_with_username_and_password():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'username': 'mongo', 'username': 'mongo',
@ -283,9 +286,10 @@ def test_restore_database_dump_runs_mongorestore_with_username_and_password():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -298,7 +302,7 @@ def test_restore_database_dump_runs_mongorestore_with_username_and_password():
def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'username': 'mongo', 'username': 'mongo',
@ -339,9 +343,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -354,7 +359,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'username': 'mongo', 'username': 'mongo',
@ -395,9 +400,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -410,7 +416,7 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
def test_restore_database_dump_runs_mongorestore_with_options(): def test_restore_database_dump_runs_mongorestore_with_options():
database_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}] databases_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -423,9 +429,10 @@ def test_restore_database_dump_runs_mongorestore_with_options():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -438,7 +445,7 @@ def test_restore_database_dump_runs_mongorestore_with_options():
def test_restore_databases_dump_runs_mongorestore_with_schemas(): def test_restore_databases_dump_runs_mongorestore_with_schemas():
database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}] databases_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -461,9 +468,10 @@ def test_restore_databases_dump_runs_mongorestore_with_schemas():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -476,7 +484,7 @@ def test_restore_databases_dump_runs_mongorestore_with_schemas():
def test_restore_database_dump_runs_psql_for_all_database_dump(): def test_restore_database_dump_runs_psql_for_all_database_dump():
database_config = [{'name': 'all', 'schemas': None}] databases_config = [{'name': 'all', 'schemas': None}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -489,9 +497,10 @@ def test_restore_database_dump_runs_psql_for_all_database_dump():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='all',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -504,16 +513,17 @@ def test_restore_database_dump_runs_psql_for_all_database_dump():
def test_restore_database_dump_with_dry_run_skips_restore(): def test_restore_database_dump_with_dry_run_skips_restore():
database_config = [{'name': 'foo', 'schemas': None}] databases_config = [{'name': 'foo', 'schemas': None}]
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
flexmock(module.dump).should_receive('make_database_dump_filename') flexmock(module.dump).should_receive('make_database_dump_filename')
flexmock(module).should_receive('execute_command_with_processes').never() flexmock(module).should_receive('execute_command_with_processes').never()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=True, dry_run=True,
extract_process=flexmock(), extract_process=flexmock(),
connection_params={ connection_params={
@ -526,7 +536,7 @@ def test_restore_database_dump_with_dry_run_skips_restore():
def test_restore_database_dump_without_extract_process_restores_from_disk(): def test_restore_database_dump_without_extract_process_restores_from_disk():
database_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}] databases_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path') flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
@ -538,9 +548,10 @@ def test_restore_database_dump_without_extract_process_restores_from_disk():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=None, extract_process=None,
connection_params={ connection_params={

View file

@ -380,7 +380,7 @@ def test_dump_databases_does_not_error_for_missing_all_databases_with_dry_run():
def test_restore_database_dump_runs_mysql_to_restore(): def test_restore_database_dump_runs_mysql_to_restore():
database_config = [{'name': 'foo'}] databases_config = [{'name': 'foo'}, {'name': 'bar'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('execute_command_with_processes').with_args( flexmock(module).should_receive('execute_command_with_processes').with_args(
@ -392,9 +392,10 @@ def test_restore_database_dump_runs_mysql_to_restore():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -406,30 +407,8 @@ def test_restore_database_dump_runs_mysql_to_restore():
) )
def test_restore_database_dump_errors_on_multiple_database_config():
database_config = [{'name': 'foo'}, {'name': 'bar'}]
flexmock(module).should_receive('execute_command_with_processes').never()
flexmock(module).should_receive('execute_command').never()
with pytest.raises(ValueError):
module.restore_database_dump(
database_config,
{},
'test.yaml',
dry_run=False,
extract_process=flexmock(),
connection_params={
'hostname': None,
'port': None,
'username': None,
'password': None,
},
)
def test_restore_database_dump_runs_mysql_with_options(): def test_restore_database_dump_runs_mysql_with_options():
database_config = [{'name': 'foo', 'restore_options': '--harder'}] databases_config = [{'name': 'foo', 'restore_options': '--harder'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('execute_command_with_processes').with_args( flexmock(module).should_receive('execute_command_with_processes').with_args(
@ -441,9 +420,10 @@ def test_restore_database_dump_runs_mysql_with_options():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -456,7 +436,7 @@ def test_restore_database_dump_runs_mysql_with_options():
def test_restore_database_dump_runs_mysql_with_hostname_and_port(): def test_restore_database_dump_runs_mysql_with_hostname_and_port():
database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}] databases_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('execute_command_with_processes').with_args( flexmock(module).should_receive('execute_command_with_processes').with_args(
@ -477,9 +457,10 @@ def test_restore_database_dump_runs_mysql_with_hostname_and_port():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -492,7 +473,7 @@ def test_restore_database_dump_runs_mysql_with_hostname_and_port():
def test_restore_database_dump_runs_mysql_with_username_and_password(): def test_restore_database_dump_runs_mysql_with_username_and_password():
database_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}] databases_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('execute_command_with_processes').with_args( flexmock(module).should_receive('execute_command_with_processes').with_args(
@ -504,9 +485,10 @@ def test_restore_database_dump_runs_mysql_with_username_and_password():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -519,7 +501,7 @@ def test_restore_database_dump_runs_mysql_with_username_and_password():
def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'username': 'root', 'username': 'root',
@ -552,9 +534,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -567,7 +550,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'username': 'root', 'username': 'root',
@ -602,9 +585,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -617,14 +601,15 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
def test_restore_database_dump_with_dry_run_skips_restore(): def test_restore_database_dump_with_dry_run_skips_restore():
database_config = [{'name': 'foo'}] databases_config = [{'name': 'foo'}]
flexmock(module).should_receive('execute_command_with_processes').never() flexmock(module).should_receive('execute_command_with_processes').never()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=True, dry_run=True,
extract_process=flexmock(), extract_process=flexmock(),
connection_params={ connection_params={

View file

@ -464,7 +464,7 @@ def test_dump_databases_runs_non_default_pg_dump():
def test_restore_database_dump_runs_pg_restore(): def test_restore_database_dump_runs_pg_restore():
database_config = [{'name': 'foo', 'schemas': None}] databases_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
@ -500,9 +500,10 @@ def test_restore_database_dump_runs_pg_restore():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -514,33 +515,8 @@ def test_restore_database_dump_runs_pg_restore():
) )
def test_restore_database_dump_errors_on_multiple_database_config():
database_config = [{'name': 'foo'}, {'name': 'bar'}]
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').never()
flexmock(module).should_receive('execute_command').never()
with pytest.raises(ValueError):
module.restore_database_dump(
database_config,
{},
'test.yaml',
dry_run=False,
extract_process=flexmock(),
connection_params={
'restore_hostname': None,
'restore_port': None,
'restore_username': None,
'restore_password': None,
},
)
def test_restore_database_dump_runs_pg_restore_with_hostname_and_port(): def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
database_config = [ databases_config = [
{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None} {'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
] ]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
@ -586,9 +562,10 @@ def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -601,7 +578,7 @@ def test_restore_database_dump_runs_pg_restore_with_hostname_and_port():
def test_restore_database_dump_runs_pg_restore_with_username_and_password(): def test_restore_database_dump_runs_pg_restore_with_username_and_password():
database_config = [ databases_config = [
{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None} {'name': 'foo', 'username': 'postgres', 'password': 'trustsome1', 'schemas': None}
] ]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
@ -645,9 +622,10 @@ def test_restore_database_dump_runs_pg_restore_with_username_and_password():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -660,7 +638,7 @@ def test_restore_database_dump_runs_pg_restore_with_username_and_password():
def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'hostname': 'database.example.org', 'hostname': 'database.example.org',
@ -723,9 +701,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -738,7 +717,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'hostname': 'database.example.org', 'hostname': 'database.example.org',
@ -801,9 +780,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -816,7 +796,7 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
def test_restore_database_dump_runs_pg_restore_with_options(): def test_restore_database_dump_runs_pg_restore_with_options():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'restore_options': '--harder', 'restore_options': '--harder',
@ -861,9 +841,10 @@ def test_restore_database_dump_runs_pg_restore_with_options():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -876,7 +857,7 @@ def test_restore_database_dump_runs_pg_restore_with_options():
def test_restore_database_dump_runs_psql_for_all_database_dump(): def test_restore_database_dump_runs_psql_for_all_database_dump():
database_config = [{'name': 'all', 'schemas': None}] databases_config = [{'name': 'all', 'schemas': None}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
@ -899,9 +880,10 @@ def test_restore_database_dump_runs_psql_for_all_database_dump():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='all',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -914,7 +896,7 @@ def test_restore_database_dump_runs_psql_for_all_database_dump():
def test_restore_database_dump_runs_psql_for_plain_database_dump(): def test_restore_database_dump_runs_psql_for_plain_database_dump():
database_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}] databases_config = [{'name': 'foo', 'format': 'plain', 'schemas': None}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
@ -942,9 +924,10 @@ def test_restore_database_dump_runs_psql_for_plain_database_dump():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -957,7 +940,7 @@ def test_restore_database_dump_runs_psql_for_plain_database_dump():
def test_restore_database_dump_runs_non_default_pg_restore_and_psql(): def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
database_config = [ databases_config = [
{ {
'name': 'foo', 'name': 'foo',
'pg_restore_command': 'docker exec mycontainer pg_restore', 'pg_restore_command': 'docker exec mycontainer pg_restore',
@ -1006,9 +989,10 @@ def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={ connection_params={
@ -1021,7 +1005,7 @@ def test_restore_database_dump_runs_non_default_pg_restore_and_psql():
def test_restore_database_dump_with_dry_run_skips_restore(): def test_restore_database_dump_with_dry_run_skips_restore():
database_config = [{'name': 'foo', 'schemas': None}] databases_config = [{'name': 'foo', 'schemas': None}]
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -1029,9 +1013,10 @@ def test_restore_database_dump_with_dry_run_skips_restore():
flexmock(module).should_receive('execute_command_with_processes').never() flexmock(module).should_receive('execute_command_with_processes').never()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=True, dry_run=True,
extract_process=flexmock(), extract_process=flexmock(),
connection_params={ connection_params={
@ -1044,7 +1029,7 @@ def test_restore_database_dump_with_dry_run_skips_restore():
def test_restore_database_dump_without_extract_process_restores_from_disk(): def test_restore_database_dump_without_extract_process_restores_from_disk():
database_config = [{'name': 'foo', 'schemas': None}] databases_config = [{'name': 'foo', 'schemas': None}]
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -1080,9 +1065,10 @@ def test_restore_database_dump_without_extract_process_restores_from_disk():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=None, extract_process=None,
connection_params={ connection_params={
@ -1095,7 +1081,7 @@ def test_restore_database_dump_without_extract_process_restores_from_disk():
def test_restore_database_dump_with_schemas_restores_schemas(): def test_restore_database_dump_with_schemas_restores_schemas():
database_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}] databases_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_extra_environment').and_return({'PGSSLMODE': 'disable'})
flexmock(module).should_receive('make_dump_path') flexmock(module).should_receive('make_dump_path')
@ -1135,9 +1121,10 @@ def test_restore_database_dump_with_schemas_restores_schemas():
).once() ).once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='foo',
dry_run=False, dry_run=False,
extract_process=None, extract_process=None,
connection_params={ connection_params={

View file

@ -93,7 +93,7 @@ def test_dump_databases_does_not_dump_if_dry_run():
def test_restore_database_dump_restores_database(): def test_restore_database_dump_restores_database():
database_config = [{'path': '/path/to/database', 'name': 'database'}] databases_config = [{'path': '/path/to/database', 'name': 'database'}, {'name': 'other'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('execute_command_with_processes').with_args( flexmock(module).should_receive('execute_command_with_processes').with_args(
@ -109,9 +109,10 @@ def test_restore_database_dump_restores_database():
flexmock(module.os).should_receive('remove').once() flexmock(module.os).should_receive('remove').once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='database',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={'restore_path': None}, connection_params={'restore_path': None},
@ -119,7 +120,7 @@ def test_restore_database_dump_restores_database():
def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore(): def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
database_config = [ databases_config = [
{'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'} {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
] ]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
@ -137,9 +138,10 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
flexmock(module.os).should_receive('remove').once() flexmock(module.os).should_receive('remove').once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='database',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={'restore_path': 'cli/path/to/database'}, connection_params={'restore_path': 'cli/path/to/database'},
@ -147,7 +149,7 @@ def test_restore_database_dump_with_connection_params_uses_connection_params_for
def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore(): def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
database_config = [ databases_config = [
{'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'} {'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
] ]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
@ -165,9 +167,10 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
flexmock(module.os).should_receive('remove').once() flexmock(module.os).should_receive('remove').once()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='database',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={'restore_path': None}, connection_params={'restore_path': None},
@ -175,31 +178,33 @@ def test_restore_database_dump_without_connection_params_uses_restore_params_in_
def test_restore_database_dump_does_not_restore_database_if_dry_run(): def test_restore_database_dump_does_not_restore_database_if_dry_run():
database_config = [{'path': '/path/to/database', 'name': 'database'}] databases_config = [{'path': '/path/to/database', 'name': 'database'}]
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
flexmock(module).should_receive('execute_command_with_processes').never() flexmock(module).should_receive('execute_command_with_processes').never()
flexmock(module.os).should_receive('remove').never() flexmock(module.os).should_receive('remove').never()
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='database',
dry_run=True, dry_run=True,
extract_process=extract_process, extract_process=extract_process,
connection_params={'restore_path': None}, connection_params={'restore_path': None},
) )
def test_restore_database_dump_raises_error_if_database_config_is_invalid(): def test_restore_database_dump_raises_error_if_database_config_is_empty():
database_config = [] databases_config = []
extract_process = flexmock(stdout=flexmock()) extract_process = flexmock(stdout=flexmock())
with pytest.raises(ValueError): with pytest.raises(ValueError):
module.restore_database_dump( module.restore_database_dump(
database_config, databases_config,
{}, {},
'test.yaml', 'test.yaml',
database_name='database',
dry_run=False, dry_run=False,
extract_process=extract_process, extract_process=extract_process,
connection_params={'restore_path': None}, connection_params={'restore_path': None},