12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226 |
- 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/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'),
- 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_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'),
- 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'),
- 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(),
- )
|