2023-01-26 08:31:07 +01:00
|
|
|
import pytest
|
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
import borgmatic.actions.restore as module
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_get_configured_data_source_matches_data_source_by_name():
|
|
|
|
assert module.get_configured_data_source(
|
2023-07-09 08:14:30 +02:00
|
|
|
config={
|
2023-01-26 08:31:07 +01:00
|
|
|
'other_databases': [{'name': 'other'}],
|
|
|
|
'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
|
|
|
|
},
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names={'postgresql_databases': ['other', 'foo', 'bar']},
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
2023-01-26 08:31:07 +01:00
|
|
|
) == ('postgresql_databases', {'name': 'bar'})
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_get_configured_data_source_matches_nothing_when_nothing_configured():
|
|
|
|
assert module.get_configured_data_source(
|
2023-08-14 21:43:21 +02:00
|
|
|
config={},
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names={'postgresql_databases': ['foo']},
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='quux',
|
2023-08-14 21:43:21 +02:00
|
|
|
) == (None, None)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_get_configured_data_source_matches_nothing_when_data_source_name_not_configured():
|
|
|
|
assert module.get_configured_data_source(
|
2023-07-09 08:14:30 +02:00
|
|
|
config={'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]},
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names={'postgresql_databases': ['foo']},
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='quux',
|
2023-01-26 08:31:07 +01:00
|
|
|
) == (None, None)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_get_configured_data_source_matches_nothing_when_data_source_name_not_in_archive():
|
|
|
|
assert module.get_configured_data_source(
|
2023-07-09 08:14:30 +02:00
|
|
|
config={'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}]},
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names={'postgresql_databases': ['bar']},
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='foo',
|
2023-01-26 08:31:07 +01:00
|
|
|
) == (None, None)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_get_configured_data_source_matches_data_source_by_configuration_data_source_name():
|
|
|
|
assert module.get_configured_data_source(
|
2023-07-09 08:14:30 +02:00
|
|
|
config={'postgresql_databases': [{'name': 'all'}, {'name': 'bar'}]},
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names={'postgresql_databases': ['foo']},
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='foo',
|
|
|
|
configuration_data_source_name='all',
|
2023-01-26 08:31:07 +01:00
|
|
|
) == ('postgresql_databases', {'name': 'all'})
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_get_configured_data_source_with_unspecified_hook_matches_data_source_by_name():
|
|
|
|
assert module.get_configured_data_source(
|
2023-07-09 08:14:30 +02:00
|
|
|
config={
|
2023-01-26 08:31:07 +01:00
|
|
|
'other_databases': [{'name': 'other'}],
|
|
|
|
'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
|
|
|
|
},
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names={'postgresql_databases': ['other', 'foo', 'bar']},
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name=module.UNSPECIFIED_HOOK,
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
2023-01-26 08:31:07 +01:00
|
|
|
) == ('postgresql_databases', {'name': 'bar'})
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_collect_archive_data_source_names_parses_archive_paths():
|
|
|
|
flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
|
|
|
|
''
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
|
|
|
|
[
|
|
|
|
'.borgmatic/postgresql_databases/localhost/foo',
|
|
|
|
'.borgmatic/postgresql_databases/localhost/bar',
|
|
|
|
'.borgmatic/mysql_databases/localhost/quux',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names = module.collect_archive_data_source_names(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-01-26 08:31:07 +01:00
|
|
|
archive='archive',
|
2023-07-09 08:14:30 +02:00
|
|
|
config={'borgmatic_source_directory': '.borgmatic'},
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-05-09 08:00:49 +02:00
|
|
|
global_arguments=flexmock(log_json=False),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert archive_data_source_names == {
|
2023-01-26 08:31:07 +01:00
|
|
|
'postgresql_databases': ['foo', 'bar'],
|
|
|
|
'mysql_databases': ['quux'],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_collect_archive_data_source_names_parses_directory_format_archive_paths():
|
|
|
|
flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
|
|
|
|
''
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
|
|
|
|
[
|
|
|
|
'.borgmatic/postgresql_databases/localhost/foo/table1',
|
|
|
|
'.borgmatic/postgresql_databases/localhost/foo/table2',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names = module.collect_archive_data_source_names(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-01-26 08:31:07 +01:00
|
|
|
archive='archive',
|
2023-07-09 08:14:30 +02:00
|
|
|
config={'borgmatic_source_directory': '.borgmatic'},
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-05-09 08:00:49 +02:00
|
|
|
global_arguments=flexmock(log_json=False),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert archive_data_source_names == {
|
2023-01-26 08:31:07 +01:00
|
|
|
'postgresql_databases': ['foo'],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_collect_archive_data_source_names_skips_bad_archive_paths():
|
|
|
|
flexmock(module.borgmatic.hooks.dump).should_receive('make_data_source_dump_path').and_return(
|
|
|
|
''
|
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
|
|
|
|
['.borgmatic/postgresql_databases/localhost/foo', '.borgmatic/invalid', 'invalid/as/well']
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names = module.collect_archive_data_source_names(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-01-26 08:31:07 +01:00
|
|
|
archive='archive',
|
2023-07-09 08:14:30 +02:00
|
|
|
config={'borgmatic_source_directory': '.borgmatic'},
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-05-09 08:00:49 +02:00
|
|
|
global_arguments=flexmock(log_json=False),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert archive_data_source_names == {
|
2023-01-26 08:31:07 +01:00
|
|
|
'postgresql_databases': ['foo'],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_find_data_sources_to_restore_passes_through_requested_names_found_in_archive():
|
|
|
|
restore_names = module.find_data_sources_to_restore(
|
|
|
|
requested_data_source_names=['foo', 'bar'],
|
|
|
|
archive_data_source_names={'postresql_databases': ['foo', 'bar', 'baz']},
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
assert restore_names == {module.UNSPECIFIED_HOOK: ['foo', 'bar']}
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_find_data_sources_to_restore_raises_for_requested_names_missing_from_archive():
|
2023-01-26 08:31:07 +01:00
|
|
|
with pytest.raises(ValueError):
|
2023-08-24 22:50:10 +02:00
|
|
|
module.find_data_sources_to_restore(
|
|
|
|
requested_data_source_names=['foo', 'bar'],
|
|
|
|
archive_data_source_names={'postresql_databases': ['foo']},
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_find_data_sources_to_restore_without_requested_names_finds_all_archive_data_sources():
|
|
|
|
archive_data_source_names = {'postresql_databases': ['foo', 'bar']}
|
2023-01-26 08:31:07 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
restore_names = module.find_data_sources_to_restore(
|
|
|
|
requested_data_source_names=[],
|
|
|
|
archive_data_source_names=archive_data_source_names,
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert restore_names == archive_data_source_names
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_find_data_sources_to_restore_with_all_in_requested_names_finds_all_archive_data_sources():
|
|
|
|
archive_data_source_names = {'postresql_databases': ['foo', 'bar']}
|
2023-01-26 08:31:07 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
restore_names = module.find_data_sources_to_restore(
|
|
|
|
requested_data_source_names=['all'],
|
|
|
|
archive_data_source_names=archive_data_source_names,
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert restore_names == archive_data_source_names
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_find_data_sources_to_restore_with_all_in_requested_names_plus_additional_requested_names_omits_duplicates():
|
|
|
|
archive_data_source_names = {'postresql_databases': ['foo', 'bar']}
|
2023-01-26 08:31:07 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
restore_names = module.find_data_sources_to_restore(
|
|
|
|
requested_data_source_names=['all', 'foo', 'bar'],
|
|
|
|
archive_data_source_names=archive_data_source_names,
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert restore_names == archive_data_source_names
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_find_data_sources_to_restore_raises_for_all_in_requested_names_and_requested_named_missing_from_archives():
|
2023-01-26 08:31:07 +01:00
|
|
|
with pytest.raises(ValueError):
|
2023-08-24 22:50:10 +02:00
|
|
|
module.find_data_sources_to_restore(
|
|
|
|
requested_data_source_names=['all', 'foo', 'bar'],
|
|
|
|
archive_data_source_names={'postresql_databases': ['foo']},
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_ensure_data_sources_found_with_all_data_sources_found_does_not_raise():
|
|
|
|
module.ensure_data_sources_found(
|
2023-01-26 08:31:07 +01:00
|
|
|
restore_names={'postgresql_databases': ['foo']},
|
|
|
|
remaining_restore_names={'postgresql_databases': ['bar']},
|
|
|
|
found_names=['foo', 'bar'],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_ensure_data_sources_found_with_no_data_sources_raises():
|
2023-01-26 08:31:07 +01:00
|
|
|
with pytest.raises(ValueError):
|
2023-08-24 22:50:10 +02:00
|
|
|
module.ensure_data_sources_found(
|
2023-04-15 04:35:24 +02:00
|
|
|
restore_names={'postgresql_databases': []},
|
|
|
|
remaining_restore_names={},
|
|
|
|
found_names=[],
|
2023-01-26 08:31:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_ensure_data_sources_found_with_missing_data_sources_raises():
|
2023-01-26 08:31:07 +01:00
|
|
|
with pytest.raises(ValueError):
|
2023-08-24 22:50:10 +02:00
|
|
|
module.ensure_data_sources_found(
|
2023-01-26 08:31:07 +01:00
|
|
|
restore_names={'postgresql_databases': ['foo']},
|
|
|
|
remaining_restore_names={'postgresql_databases': ['bar']},
|
|
|
|
found_names=['foo'],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_run_restore_restores_each_data_source():
|
2023-01-26 08:31:07 +01:00
|
|
|
restore_names = {
|
|
|
|
'postgresql_databases': ['foo', 'bar'],
|
|
|
|
}
|
|
|
|
|
|
|
|
flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
|
|
|
|
flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
|
|
|
|
flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
|
|
|
|
flexmock()
|
|
|
|
)
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
|
|
|
|
flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
|
|
|
|
flexmock(module).should_receive('get_configured_data_source').and_return(
|
2023-01-26 08:31:07 +01:00
|
|
|
('postgresql_databases', {'name': 'foo'})
|
|
|
|
).and_return(('postgresql_databases', {'name': 'bar'}))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'bar', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('ensure_data_sources_found')
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
module.run_restore(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-07-09 08:14:30 +02:00
|
|
|
config=flexmock(),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-04-14 23:27:51 +02:00
|
|
|
restore_arguments=flexmock(
|
2023-06-16 11:44:00 +02:00
|
|
|
repository='repo',
|
|
|
|
archive='archive',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_sources=flexmock(),
|
2023-06-16 11:44:00 +02:00
|
|
|
schemas=None,
|
|
|
|
hostname=None,
|
|
|
|
port=None,
|
|
|
|
username=None,
|
|
|
|
password=None,
|
|
|
|
restore_path=None,
|
2023-04-14 23:27:51 +02:00
|
|
|
),
|
2023-01-26 08:31:07 +01:00
|
|
|
global_arguments=flexmock(dry_run=False),
|
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_restore_bails_for_non_matching_repository():
|
|
|
|
flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(
|
|
|
|
False
|
|
|
|
)
|
|
|
|
flexmock(module.borgmatic.hooks.dispatch).should_receive(
|
|
|
|
'call_hooks_even_if_unconfigured'
|
|
|
|
).never()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').never()
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
module.run_restore(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-07-09 08:14:30 +02:00
|
|
|
config=flexmock(),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-08-24 22:50:10 +02:00
|
|
|
restore_arguments=flexmock(repository='repo', archive='archive', data_sources=flexmock()),
|
2023-01-26 08:31:07 +01:00
|
|
|
global_arguments=flexmock(dry_run=False),
|
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_run_restore_restores_data_source_configured_with_all_name():
|
2023-01-26 08:31:07 +01:00
|
|
|
restore_names = {
|
|
|
|
'postgresql_databases': ['foo', 'bar'],
|
|
|
|
}
|
|
|
|
|
|
|
|
flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
|
|
|
|
flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
|
|
|
|
flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
|
|
|
|
flexmock()
|
|
|
|
)
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
|
|
|
|
flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
|
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='foo',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return(('postgresql_databases', {'name': 'foo'}))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return((None, None))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
|
|
|
configuration_data_source_name='all',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return(('postgresql_databases', {'name': 'bar'}))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'bar', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('ensure_data_sources_found')
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
module.run_restore(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-07-09 08:14:30 +02:00
|
|
|
config=flexmock(),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-04-14 23:27:51 +02:00
|
|
|
restore_arguments=flexmock(
|
2023-06-16 11:44:00 +02:00
|
|
|
repository='repo',
|
|
|
|
archive='archive',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_sources=flexmock(),
|
2023-06-16 11:44:00 +02:00
|
|
|
schemas=None,
|
|
|
|
hostname=None,
|
|
|
|
port=None,
|
|
|
|
username=None,
|
|
|
|
password=None,
|
|
|
|
restore_path=None,
|
2023-04-14 23:27:51 +02:00
|
|
|
),
|
2023-01-26 08:31:07 +01:00
|
|
|
global_arguments=flexmock(dry_run=False),
|
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_run_restore_skips_missing_data_source():
|
2023-01-26 08:31:07 +01:00
|
|
|
restore_names = {
|
|
|
|
'postgresql_databases': ['foo', 'bar'],
|
|
|
|
}
|
|
|
|
|
|
|
|
flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
|
|
|
|
flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
|
|
|
|
flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
|
|
|
|
flexmock()
|
|
|
|
)
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
|
|
|
|
flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
|
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='foo',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return(('postgresql_databases', {'name': 'foo'}))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return((None, None))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
|
|
|
configuration_data_source_name='all',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return((None, None))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'bar', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).never()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('ensure_data_sources_found')
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
module.run_restore(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-07-09 08:14:30 +02:00
|
|
|
config=flexmock(),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-04-14 23:27:51 +02:00
|
|
|
restore_arguments=flexmock(
|
2023-06-16 11:44:00 +02:00
|
|
|
repository='repo',
|
|
|
|
archive='archive',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_sources=flexmock(),
|
2023-06-16 11:44:00 +02:00
|
|
|
schemas=None,
|
|
|
|
hostname=None,
|
|
|
|
port=None,
|
|
|
|
username=None,
|
|
|
|
password=None,
|
|
|
|
restore_path=None,
|
2023-04-14 23:27:51 +02:00
|
|
|
),
|
2023-01-26 08:31:07 +01:00
|
|
|
global_arguments=flexmock(dry_run=False),
|
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_run_restore_restores_data_sources_from_different_hooks():
|
2023-01-26 08:31:07 +01:00
|
|
|
restore_names = {
|
|
|
|
'postgresql_databases': ['foo'],
|
|
|
|
'mysql_databases': ['bar'],
|
|
|
|
}
|
|
|
|
|
|
|
|
flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
|
|
|
|
flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
|
|
|
|
flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
|
|
|
|
flexmock()
|
|
|
|
)
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('collect_archive_data_source_names').and_return(flexmock())
|
|
|
|
flexmock(module).should_receive('find_data_sources_to_restore').and_return(restore_names)
|
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='foo',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return(('postgresql_databases', {'name': 'foo'}))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('get_configured_data_source').with_args(
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-08-24 22:50:10 +02:00
|
|
|
archive_data_source_names=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
hook_name='mysql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source_name='bar',
|
2023-01-26 08:31:07 +01:00
|
|
|
).and_return(('mysql_databases', {'name': 'bar'}))
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='postgresql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('restore_single_data_source').with_args(
|
2023-01-26 08:31:07 +01:00
|
|
|
repository=object,
|
2023-07-09 08:14:30 +02:00
|
|
|
config=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=object,
|
|
|
|
global_arguments=object,
|
|
|
|
local_path=object,
|
|
|
|
remote_path=object,
|
|
|
|
archive_name=object,
|
|
|
|
hook_name='mysql_databases',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'bar', 'schemas': None},
|
2023-06-16 11:44:00 +02:00
|
|
|
connection_params=object,
|
2023-01-26 08:31:07 +01:00
|
|
|
).once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module).should_receive('ensure_data_sources_found')
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
module.run_restore(
|
2023-03-24 20:34:57 +01:00
|
|
|
repository={'path': 'repo'},
|
2023-07-09 08:14:30 +02:00
|
|
|
config=flexmock(),
|
2023-01-26 08:31:07 +01:00
|
|
|
local_borg_version=flexmock(),
|
2023-04-14 23:27:51 +02:00
|
|
|
restore_arguments=flexmock(
|
2023-06-16 11:44:00 +02:00
|
|
|
repository='repo',
|
|
|
|
archive='archive',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_sources=flexmock(),
|
2023-06-16 11:44:00 +02:00
|
|
|
schemas=None,
|
|
|
|
hostname=None,
|
|
|
|
port=None,
|
|
|
|
username=None,
|
|
|
|
password=None,
|
|
|
|
restore_path=None,
|
2023-04-14 23:27:51 +02:00
|
|
|
),
|
2023-01-26 08:31:07 +01:00
|
|
|
global_arguments=flexmock(dry_run=False),
|
|
|
|
local_path=flexmock(),
|
|
|
|
remote_path=flexmock(),
|
|
|
|
)
|