1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066 |
- import pytest
- from flexmock import flexmock
- import borgmatic.actions.restore as module
- @pytest.mark.parametrize(
- 'first_dump,second_dump,expected_result',
- (
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('mariadb_databases', 'foo'),
- False,
- ),
- (module.Dump('postgresql_databases', 'foo'), module.Dump(module.UNSPECIFIED, 'foo'), True),
- (module.Dump('postgresql_databases', 'foo'), module.Dump(module.UNSPECIFIED, 'bar'), False),
- (
- module.Dump('postgresql_databases', module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- True,
- ),
- (
- module.Dump('postgresql_databases', module.UNSPECIFIED),
- module.Dump('mariadb_databases', 'foo'),
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'foo', 'otherhost'),
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED),
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'bar', module.UNSPECIFIED),
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- module.Dump('postgresql_databases', 'foo', 'myhost', 4321),
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo', 'otherhost', 1234),
- False,
- ),
- (
- module.Dump(
- module.UNSPECIFIED, module.UNSPECIFIED, module.UNSPECIFIED, module.UNSPECIFIED
- ),
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- True,
- ),
- ),
- )
- def test_dumps_match_compares_two_dumps_while_respecting_unspecified_values(
- first_dump, second_dump, expected_result
- ):
- assert module.dumps_match(first_dump, second_dump) == expected_result
- @pytest.mark.parametrize(
- 'dump,expected_result',
- (
- (
- module.Dump('postgresql_databases', 'foo'),
- 'foo@localhost (postgresql_databases)',
- ),
- (
- module.Dump(module.UNSPECIFIED, 'foo'),
- 'foo@localhost',
- ),
- (
- module.Dump('postgresql_databases', module.UNSPECIFIED),
- 'unspecified@localhost (postgresql_databases)',
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'host'),
- 'foo@host (postgresql_databases)',
- ),
- (
- module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED),
- 'foo (postgresql_databases)',
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'host', 1234),
- 'foo@host:1234 (postgresql_databases)',
- ),
- (
- module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED, 1234),
- 'foo@:1234 (postgresql_databases)',
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'host', module.UNSPECIFIED),
- 'foo@host (postgresql_databases)',
- ),
- (
- module.Dump(
- module.UNSPECIFIED, module.UNSPECIFIED, module.UNSPECIFIED, module.UNSPECIFIED
- ),
- 'unspecified',
- ),
- ),
- )
- def test_render_dump_metadata_renders_dump_values_into_string(dump, expected_result):
- assert module.render_dump_metadata(dump) == expected_result
- def test_get_configured_data_source_matches_data_source_with_restore_dump():
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump('postgresql_databases', 'bar'),
- module.Dump('postgresql_databases', 'bar'),
- ).and_return(True)
- assert module.get_configured_data_source(
- config={
- 'other_databases': [{'name': 'other'}],
- 'postgresql_databases': [{'name': 'foo'}, {'name': 'bar'}],
- },
- restore_dump=module.Dump('postgresql_databases', 'bar'),
- ) == {'name': 'bar'}
- def test_get_configured_data_source_matches_nothing_when_nothing_configured():
- flexmock(module).should_receive('dumps_match').and_return(False)
- assert (
- module.get_configured_data_source(
- config={},
- restore_dump=module.Dump('postgresql_databases', 'quux'),
- )
- is None
- )
- def test_get_configured_data_source_matches_nothing_when_restore_dump_does_not_match_configuration():
- flexmock(module).should_receive('dumps_match').and_return(False)
- assert (
- module.get_configured_data_source(
- config={
- 'postgresql_databases': [{'name': 'foo'}],
- },
- restore_dump=module.Dump('postgresql_databases', 'quux'),
- )
- is None
- )
- def test_get_configured_data_source_with_multiple_matching_data_sources_errors():
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump('postgresql_databases', 'bar'),
- module.Dump('postgresql_databases', 'bar'),
- ).and_return(True)
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- with pytest.raises(ValueError):
- module.get_configured_data_source(
- config={
- 'other_databases': [{'name': 'other'}],
- 'postgresql_databases': [
- {'name': 'foo'},
- {'name': 'bar'},
- {'name': 'bar', 'format': 'directory'},
- ],
- },
- restore_dump=module.Dump('postgresql_databases', 'bar'),
- )
- def test_strip_path_prefix_from_extracted_dump_destination_renames_first_matching_databases_subdirectory():
- flexmock(module.os).should_receive('walk').and_return(
- [
- ('/foo', flexmock(), flexmock()),
- ('/foo/bar', flexmock(), flexmock()),
- ('/foo/bar/postgresql_databases', flexmock(), flexmock()),
- ('/foo/bar/mariadb_databases', flexmock(), flexmock()),
- ]
- )
- flexmock(module.shutil).should_receive('move').with_args(
- '/foo/bar/postgresql_databases', '/run/user/0/borgmatic/postgresql_databases'
- ).once()
- flexmock(module.shutil).should_receive('move').with_args(
- '/foo/bar/mariadb_databases', '/run/user/0/borgmatic/mariadb_databases'
- ).never()
- module.strip_path_prefix_from_extracted_dump_destination('/foo', '/run/user/0/borgmatic')
- def test_restore_single_dump_extracts_and_restores_single_file_dump():
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
- 'make_data_source_dump_patterns', object, object, object, object, object
- ).and_return({'postgresql': flexmock()})
- flexmock(module.tempfile).should_receive('mkdtemp').never()
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'convert_glob_patterns_to_borg_pattern'
- ).and_return(flexmock())
- flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
- flexmock()
- ).once()
- flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
- flexmock(module.shutil).should_receive('rmtree').never()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- function_name='restore_data_source_dump',
- config=object,
- log_prefix=object,
- hook_name=object,
- data_source=object,
- dry_run=object,
- extract_process=object,
- connection_params=object,
- borgmatic_runtime_directory=object,
- ).once()
- module.restore_single_dump(
- repository={'path': 'test.borg'},
- config=flexmock(),
- local_borg_version=flexmock(),
- global_arguments=flexmock(dry_run=False),
- local_path=None,
- remote_path=None,
- archive_name=flexmock(),
- hook_name='postgresql',
- data_source={'name': 'test', 'format': 'plain'},
- connection_params=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_single_dump_extracts_and_restores_directory_dump():
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
- 'make_data_source_dump_patterns', object, object, object, object, object
- ).and_return({'postgresql': flexmock()})
- flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
- '/run/user/0/borgmatic/tmp1234'
- )
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'convert_glob_patterns_to_borg_pattern'
- ).and_return(flexmock())
- flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
- flexmock()
- ).once()
- flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').once()
- flexmock(module.shutil).should_receive('rmtree').once()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- function_name='restore_data_source_dump',
- config=object,
- log_prefix=object,
- hook_name=object,
- data_source=object,
- dry_run=object,
- extract_process=object,
- connection_params=object,
- borgmatic_runtime_directory='/run/borgmatic',
- ).once()
- module.restore_single_dump(
- repository={'path': 'test.borg'},
- config=flexmock(),
- local_borg_version=flexmock(),
- global_arguments=flexmock(dry_run=False),
- local_path=None,
- remote_path=None,
- archive_name=flexmock(),
- hook_name='postgresql',
- data_source={'name': 'test', 'format': 'directory'},
- connection_params=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_single_dump_with_directory_dump_error_cleans_up_temporary_directory():
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
- 'make_data_source_dump_patterns', object, object, object, object, object
- ).and_return({'postgresql': flexmock()})
- flexmock(module.tempfile).should_receive('mkdtemp').once().and_return(
- '/run/user/0/borgmatic/tmp1234'
- )
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'convert_glob_patterns_to_borg_pattern'
- ).and_return(flexmock())
- flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_raise(
- ValueError
- ).once()
- flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
- flexmock(module.shutil).should_receive('rmtree').once()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- function_name='restore_data_source_dump',
- config=object,
- log_prefix=object,
- hook_name=object,
- data_source=object,
- dry_run=object,
- extract_process=object,
- connection_params=object,
- borgmatic_runtime_directory='/run/user/0/borgmatic/tmp1234',
- ).never()
- with pytest.raises(ValueError):
- module.restore_single_dump(
- repository={'path': 'test.borg'},
- config=flexmock(),
- local_borg_version=flexmock(),
- global_arguments=flexmock(dry_run=False),
- local_path=None,
- remote_path=None,
- archive_name=flexmock(),
- hook_name='postgresql',
- data_source={'name': 'test', 'format': 'directory'},
- connection_params=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_single_dump_with_directory_dump_and_dry_run_skips_directory_move_and_cleanup():
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').with_args(
- 'make_data_source_dump_patterns', object, object, object, object, object
- ).and_return({'postgresql': flexmock()})
- flexmock(module.tempfile).should_receive('mkdtemp').once().and_return('/run/borgmatic/tmp1234')
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'convert_glob_patterns_to_borg_pattern'
- ).and_return(flexmock())
- flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
- flexmock()
- ).once()
- flexmock(module).should_receive('strip_path_prefix_from_extracted_dump_destination').never()
- flexmock(module.shutil).should_receive('rmtree').never()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').with_args(
- function_name='restore_data_source_dump',
- config=object,
- log_prefix=object,
- hook_name=object,
- data_source=object,
- dry_run=object,
- extract_process=object,
- connection_params=object,
- borgmatic_runtime_directory='/run/borgmatic',
- ).once()
- module.restore_single_dump(
- repository={'path': 'test.borg'},
- config=flexmock(),
- local_borg_version=flexmock(),
- global_arguments=flexmock(dry_run=True),
- local_path=None,
- remote_path=None,
- archive_name=flexmock(),
- hook_name='postgresql',
- data_source={'name': 'test', 'format': 'directory'},
- connection_params=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_collect_dumps_from_archive_parses_archive_paths():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root/.borgmatic')
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'make_data_source_dump_path'
- ).and_return('')
- 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',
- ]
- )
- archive_dumps = module.collect_dumps_from_archive(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(log_json=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert archive_dumps == {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- module.Dump('mysql_databases', 'quux'),
- }
- def test_collect_dumps_from_archive_parses_archive_paths_with_different_base_directories():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root/.borgmatic')
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'make_data_source_dump_path'
- ).and_return('')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- [
- 'borgmatic/postgresql_databases/localhost/foo',
- '.borgmatic/postgresql_databases/localhost/bar',
- '/root/.borgmatic/postgresql_databases/localhost/baz',
- '/var/run/0/borgmatic/mysql_databases/localhost/quux',
- ]
- )
- archive_dumps = module.collect_dumps_from_archive(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(log_json=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert archive_dumps == {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- module.Dump('postgresql_databases', 'baz'),
- module.Dump('mysql_databases', 'quux'),
- }
- def test_collect_dumps_from_archive_parses_directory_format_archive_paths():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root/.borgmatic')
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'make_data_source_dump_path'
- ).and_return('')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- [
- 'borgmatic/postgresql_databases/localhost/foo/table1',
- 'borgmatic/postgresql_databases/localhost/foo/table2',
- ]
- )
- archive_dumps = module.collect_dumps_from_archive(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(log_json=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert archive_dumps == {
- module.Dump('postgresql_databases', 'foo'),
- }
- def test_collect_dumps_from_archive_skips_bad_archive_paths():
- flexmock(module.borgmatic.config.paths).should_receive(
- 'get_borgmatic_source_directory'
- ).and_return('/root/.borgmatic')
- flexmock(module.borgmatic.hooks.data_source.dump).should_receive(
- 'make_data_source_dump_path'
- ).and_return('')
- flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
- [
- 'borgmatic/postgresql_databases/localhost/foo',
- 'borgmatic/invalid',
- 'invalid/as/well',
- '',
- ]
- )
- archive_dumps = module.collect_dumps_from_archive(
- repository={'path': 'repo'},
- archive='archive',
- config={},
- local_borg_version=flexmock(),
- global_arguments=flexmock(log_json=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- borgmatic_runtime_directory='/run/borgmatic',
- )
- assert archive_dumps == {
- module.Dump('postgresql_databases', 'foo'),
- }
- def test_get_dumps_to_restore_gets_requested_dumps_found_in_archive():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- module.Dump('postgresql_databases', 'baz'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'bar'),
- module.Dump('postgresql_databases', 'bar'),
- ).and_return(True)
- assert module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=['foo', 'bar'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- ) == {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- }
- def test_get_dumps_to_restore_raises_for_requested_dumps_missing_from_archive():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- with pytest.raises(ValueError):
- module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=['foo', 'bar'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- )
- def test_get_dumps_to_restore_without_requested_dumps_finds_all_archive_dumps():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- assert (
- module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=[],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- )
- == dumps_from_archive
- )
- def test_get_dumps_to_restore_with_all_in_requested_dumps_finds_all_archive_dumps():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'bar'),
- module.Dump('postgresql_databases', 'bar'),
- ).and_return(True)
- assert (
- module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=['all'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- )
- == dumps_from_archive
- )
- def test_get_dumps_to_restore_with_all_in_requested_dumps_plus_additional_requested_dumps_omits_duplicates():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'bar'),
- module.Dump('postgresql_databases', 'bar'),
- ).and_return(True)
- assert (
- module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=['all', 'foo', 'bar'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- )
- == dumps_from_archive
- )
- def test_get_dumps_to_restore_raises_for_multiple_matching_dumps_in_archive():
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo'),
- module.Dump('mariadb_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- with pytest.raises(ValueError):
- module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=['foo'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive={
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('mariadb_databases', 'foo'),
- },
- )
- def test_get_dumps_to_restore_raises_for_all_in_requested_dumps_and_requested_dumps_missing_from_archive():
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- with pytest.raises(ValueError):
- module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook=None,
- data_sources=['all', 'foo', 'bar'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive={module.Dump('postresql_databases', 'foo')},
- )
- def test_ensure_requested_dumps_restored_with_all_dumps_restored_does_not_raise():
- module.ensure_requested_dumps_restored(
- dumps_to_restore={
- module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- },
- dumps_actually_restored={
- module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- },
- )
- def test_ensure_requested_dumps_restored_with_no_dumps_raises():
- with pytest.raises(ValueError):
- module.ensure_requested_dumps_restored(
- dumps_to_restore={},
- dumps_actually_restored={},
- )
- def test_ensure_requested_dumps_restored_with_missing_dumps_raises():
- flexmock(module).should_receive('render_dump_metadata').and_return('test')
- with pytest.raises(ValueError):
- module.ensure_requested_dumps_restored(
- dumps_to_restore={
- module.Dump(hook_name='postgresql_databases', data_source_name='foo')
- },
- dumps_actually_restored={
- module.Dump(hook_name='postgresql_databases', data_source_name='bar')
- },
- )
- def test_run_restore_restores_each_data_source():
- dumps_to_restore = {
- module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- }
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
- borgmatic_runtime_directory = flexmock()
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- borgmatic_runtime_directory
- )
- flexmock(module.borgmatic.config.paths).should_receive(
- 'make_runtime_directory_glob'
- ).replace_with(lambda path: path)
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- flexmock()
- )
- flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
- flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
- flexmock(module).should_receive('get_configured_data_source').and_return(
- {'name': 'foo'}
- ).and_return({'name': 'bar'})
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'foo', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'bar', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('ensure_requested_dumps_restored')
- module.run_restore(
- repository={'path': 'repo'},
- config=flexmock(),
- local_borg_version=flexmock(),
- restore_arguments=flexmock(
- repository='repo',
- archive='archive',
- data_sources=flexmock(),
- schemas=None,
- hostname=None,
- port=None,
- username=None,
- password=None,
- restore_path=None,
- ),
- 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.config.paths).should_receive('Runtime_directory').and_return(
- flexmock()
- )
- flexmock(module.borgmatic.config.paths).should_receive(
- 'make_runtime_directory_glob'
- ).replace_with(lambda path: path)
- flexmock(module.borgmatic.hooks.dispatch).should_receive(
- 'call_hooks_even_if_unconfigured'
- ).never()
- flexmock(module).should_receive('restore_single_dump').never()
- module.run_restore(
- repository={'path': 'repo'},
- config=flexmock(),
- local_borg_version=flexmock(),
- restore_arguments=flexmock(repository='repo', archive='archive', data_sources=flexmock()),
- global_arguments=flexmock(dry_run=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- )
- def test_run_restore_restores_data_source_configured_with_all_name():
- dumps_to_restore = {
- module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- }
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
- borgmatic_runtime_directory = flexmock()
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- borgmatic_runtime_directory
- )
- flexmock(module.borgmatic.config.paths).should_receive(
- 'make_runtime_directory_glob'
- ).replace_with(lambda path: path)
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- flexmock()
- )
- flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
- flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- ).and_return({'name': 'foo'})
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- ).and_return(None)
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='all'),
- ).and_return({'name': 'bar'})
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'foo', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'bar', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('ensure_requested_dumps_restored')
- module.run_restore(
- repository={'path': 'repo'},
- config=flexmock(),
- local_borg_version=flexmock(),
- restore_arguments=flexmock(
- repository='repo',
- archive='archive',
- data_sources=flexmock(),
- schemas=None,
- hostname=None,
- port=None,
- username=None,
- password=None,
- restore_path=None,
- ),
- global_arguments=flexmock(dry_run=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- )
- def test_run_restore_skips_missing_data_source():
- dumps_to_restore = {
- module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- }
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
- borgmatic_runtime_directory = flexmock()
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- borgmatic_runtime_directory
- )
- flexmock(module.borgmatic.config.paths).should_receive(
- 'make_runtime_directory_glob'
- ).replace_with(lambda path: path)
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- flexmock()
- )
- flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
- flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- ).and_return({'name': 'foo'})
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='bar'),
- ).and_return(None)
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='all'),
- ).and_return(None)
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'foo', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'bar', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).never()
- flexmock(module).should_receive('ensure_requested_dumps_restored')
- module.run_restore(
- repository={'path': 'repo'},
- config=flexmock(),
- local_borg_version=flexmock(),
- restore_arguments=flexmock(
- repository='repo',
- archive='archive',
- data_sources=flexmock(),
- schemas=None,
- hostname=None,
- port=None,
- username=None,
- password=None,
- restore_path=None,
- ),
- global_arguments=flexmock(dry_run=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- )
- def test_run_restore_restores_data_sources_from_different_hooks():
- dumps_to_restore = {
- module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- module.Dump(hook_name='mysql_databases', data_source_name='foo'),
- }
- flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
- borgmatic_runtime_directory = flexmock()
- flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
- borgmatic_runtime_directory
- )
- flexmock(module.borgmatic.config.paths).should_receive(
- 'make_runtime_directory_glob'
- ).replace_with(lambda path: path)
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks_even_if_unconfigured')
- flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
- flexmock()
- )
- flexmock(module).should_receive('collect_dumps_from_archive').and_return(flexmock())
- flexmock(module).should_receive('get_dumps_to_restore').and_return(dumps_to_restore)
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='postgresql_databases', data_source_name='foo'),
- ).and_return({'name': 'foo'})
- flexmock(module).should_receive('get_configured_data_source').with_args(
- config=object,
- restore_dump=module.Dump(hook_name='mysql_databases', data_source_name='foo'),
- ).and_return({'name': 'bar'})
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='postgresql_databases',
- data_source={'name': 'foo', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('restore_single_dump').with_args(
- repository=object,
- config=object,
- local_borg_version=object,
- global_arguments=object,
- local_path=object,
- remote_path=object,
- archive_name=object,
- hook_name='mysql_databases',
- data_source={'name': 'bar', 'schemas': None},
- connection_params=object,
- borgmatic_runtime_directory=borgmatic_runtime_directory,
- ).once()
- flexmock(module).should_receive('ensure_requested_dumps_restored')
- module.run_restore(
- repository={'path': 'repo'},
- config=flexmock(),
- local_borg_version=flexmock(),
- restore_arguments=flexmock(
- repository='repo',
- archive='archive',
- data_sources=flexmock(),
- schemas=None,
- hostname=None,
- port=None,
- username=None,
- password=None,
- restore_path=None,
- ),
- global_arguments=flexmock(dry_run=False),
- local_path=flexmock(),
- remote_path=flexmock(),
- )
|