123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272 |
- import pytest
- from flexmock import flexmock
- import borgmatic.actions.restore as module
- @pytest.mark.parametrize(
- 'first_dump,second_dump,default_port,expected_result',
- (
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'bar'),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('mariadb_databases', 'foo'),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump(module.UNSPECIFIED, 'foo'),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo'),
- module.Dump(module.UNSPECIFIED, 'bar'),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', module.UNSPECIFIED),
- module.Dump('mariadb_databases', 'foo'),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'foo', 'otherhost'),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'foo', module.UNSPECIFIED),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost'),
- module.Dump('postgresql_databases', 'bar', module.UNSPECIFIED),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- module.Dump('postgresql_databases', 'foo', 'myhost', 4321),
- None,
- False,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo', 'otherhost', 1234),
- None,
- False,
- ),
- (
- module.Dump(
- module.UNSPECIFIED, module.UNSPECIFIED, module.UNSPECIFIED, module.UNSPECIFIED
- ),
- module.Dump('postgresql_databases', 'foo', 'myhost', 1234),
- None,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', 5432),
- module.Dump('postgresql_databases', 'foo', 'myhost', None),
- 5432,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', None),
- module.Dump('postgresql_databases', 'foo', 'myhost', 5432),
- 5432,
- True,
- ),
- (
- module.Dump('postgresql_databases', 'foo', 'myhost', 5433),
- module.Dump('postgresql_databases', 'foo', 'myhost', None),
- 5432,
- False,
- ),
- ),
- )
- def test_dumps_match_compares_two_dumps_while_respecting_unspecified_values(
- first_dump, second_dump, default_port, expected_result
- ):
- assert module.dumps_match(first_dump, second_dump, default_port) == 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():
- default_port = flexmock()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(default_port)
- 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'),
- default_port=default_port,
- ).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.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(flexmock())
- 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.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(flexmock())
- 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():
- default_port = flexmock()
- flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hook').and_return(default_port)
- 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'),
- default_port=default_port,
- ).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
- ).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,
- 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
- ).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,
- 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
- ).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,
- 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
- ).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,
- 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/host:1234/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', 'host', 1234),
- 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_or_bad_path_components():
- 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:abcd/bar',
- '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'),
- module.Dump('postgresql_databases', 'bar'),
- }
- 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', hostname=module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'bar', hostname=module.UNSPECIFIED),
- 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', hostname=module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'bar', hostname=module.UNSPECIFIED),
- 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', hostname=module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'bar', hostname=module.UNSPECIFIED),
- 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', hostname=module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump(module.UNSPECIFIED, 'foo', hostname=module.UNSPECIFIED),
- 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', hostname=module.UNSPECIFIED),
- 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_get_dumps_to_restore_with_requested_hook_name_filters_dumps_found_in_archive():
- dumps_from_archive = {
- module.Dump('mariadb_databases', 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('sqlite_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump('postgresql_databases', 'foo', hostname=module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- assert module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook='postgresql_databases',
- data_sources=['foo'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- ) == {
- module.Dump('postgresql_databases', 'foo'),
- }
- def test_get_dumps_to_restore_with_requested_shortened_hook_name_filters_dumps_found_in_archive():
- dumps_from_archive = {
- module.Dump('mariadb_databases', 'foo'),
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('sqlite_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump('postgresql_databases', 'foo', hostname=module.UNSPECIFIED),
- module.Dump('postgresql_databases', 'foo'),
- ).and_return(True)
- assert module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook='postgresql',
- data_sources=['foo'],
- original_hostname=None,
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- ) == {
- module.Dump('postgresql_databases', 'foo'),
- }
- def test_get_dumps_to_restore_with_requested_hostname_filters_dumps_found_in_archive():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo'),
- module.Dump('postgresql_databases', 'foo', 'host'),
- module.Dump('postgresql_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump('postgresql_databases', 'foo', 'host'),
- module.Dump('postgresql_databases', 'foo', 'host'),
- ).and_return(True)
- assert module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook='postgresql_databases',
- data_sources=['foo'],
- original_hostname='host',
- original_port=None,
- ),
- dumps_from_archive=dumps_from_archive,
- ) == {
- module.Dump('postgresql_databases', 'foo', 'host'),
- }
- def test_get_dumps_to_restore_with_requested_port_filters_dumps_found_in_archive():
- dumps_from_archive = {
- module.Dump('postgresql_databases', 'foo', 'host'),
- module.Dump('postgresql_databases', 'foo', 'host', 1234),
- module.Dump('postgresql_databases', 'bar'),
- }
- flexmock(module).should_receive('dumps_match').and_return(False)
- flexmock(module).should_receive('dumps_match').with_args(
- module.Dump('postgresql_databases', 'foo', 'host', 1234),
- module.Dump('postgresql_databases', 'foo', 'host', 1234),
- ).and_return(True)
- assert module.get_dumps_to_restore(
- restore_arguments=flexmock(
- hook='postgresql_databases',
- data_sources=['foo'],
- original_hostname='host',
- original_port=1234,
- ),
- dumps_from_archive=dumps_from_archive,
- ) == {
- module.Dump('postgresql_databases', 'foo', 'host', 1234),
- }
- 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_by_falling_back_to_all_name():
- dumps_to_restore = {
- module.Dump(hook_name='postgresql_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').and_return(
- {'name': 'foo'}
- ).and_return({'name': 'all'})
- 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('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_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(),
- )
|