1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069 |
- import logging
- import pytest
- from flexmock import flexmock
- from borgmatic.hooks.data_source import mariadb as module
- def test_parse_extra_options_passes_through_empty_options():
- assert module.parse_extra_options('') == ((), None)
- def test_parse_extra_options_with_defaults_extra_file_removes_and_and_parses_out_filename():
- assert module.parse_extra_options('--defaults-extra-file=extra.cnf --skip-ssl') == (
- ('--skip-ssl',),
- 'extra.cnf',
- )
- def test_parse_extra_options_without_defaults_extra_file_passes_through_options():
- assert module.parse_extra_options('--skip-ssl --and=stuff') == (
- ('--skip-ssl', '--and=stuff'),
- None,
- )
- def test_make_defaults_file_pipe_without_username_or_password_bails():
- flexmock(module.os).should_receive('pipe').never()
- assert module.make_defaults_file_options(username=None, password=None) == ()
- def test_make_defaults_file_option_with_username_and_password_writes_them_to_file_descriptor():
- read_descriptor = 99
- write_descriptor = flexmock()
- flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)
- flexmock(module.os).should_receive('write').with_args(
- write_descriptor, b'[client]\nuser=root\npassword=trustsome1'
- ).once()
- flexmock(module.os).should_receive('close')
- flexmock(module.os).should_receive('set_inheritable')
- assert module.make_defaults_file_options(username='root', password='trustsome1') == (
- '--defaults-extra-file=/dev/fd/99',
- )
- def test_make_defaults_file_pipe_with_only_username_writes_it_to_file_descriptor():
- read_descriptor = 99
- write_descriptor = flexmock()
- flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)
- flexmock(module.os).should_receive('write').with_args(
- write_descriptor, b'[client]\nuser=root'
- ).once()
- flexmock(module.os).should_receive('close')
- flexmock(module.os).should_receive('set_inheritable')
- assert module.make_defaults_file_options(username='root', password=None) == (
- '--defaults-extra-file=/dev/fd/99',
- )
- def test_make_defaults_file_pipe_with_only_password_writes_it_to_file_descriptor():
- read_descriptor = 99
- write_descriptor = flexmock()
- flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)
- flexmock(module.os).should_receive('write').with_args(
- write_descriptor, b'[client]\npassword=trustsome1'
- ).once()
- flexmock(module.os).should_receive('close')
- flexmock(module.os).should_receive('set_inheritable')
- assert module.make_defaults_file_options(username=None, password='trustsome1') == (
- '--defaults-extra-file=/dev/fd/99',
- )
- def test_make_defaults_file_option_with_defaults_extra_filename_includes_it_in_file_descriptor():
- read_descriptor = 99
- write_descriptor = flexmock()
- flexmock(module.os).should_receive('pipe').and_return(read_descriptor, write_descriptor)
- flexmock(module.os).should_receive('write').with_args(
- write_descriptor, b'!include extra.cnf\n[client]\nuser=root\npassword=trustsome1'
- ).once()
- flexmock(module.os).should_receive('close')
- flexmock(module.os).should_receive('set_inheritable')
- assert module.make_defaults_file_options(
- username='root', password='trustsome1', defaults_extra_filename='extra.cnf'
- ) == ('--defaults-extra-file=/dev/fd/99',)
- def test_make_defaults_file_option_with_only_defaults_extra_filename_uses_it_instead_of_file_descriptor():
- flexmock(module.os).should_receive('pipe').never()
- assert module.make_defaults_file_options(
- username=None, password=None, defaults_extra_filename='extra.cnf'
- ) == ('--defaults-extra-file=extra.cnf',)
- def test_database_names_to_dump_passes_through_name():
- environment = flexmock()
- names = module.database_names_to_dump(
- {'name': 'foo'}, {}, 'root', 'trustsome1', environment, dry_run=False
- )
- assert names == ('foo',)
- def test_database_names_to_dump_bails_for_dry_run():
- environment = flexmock()
- flexmock(module).should_receive('execute_command_and_capture_output').never()
- names = module.database_names_to_dump(
- {'name': 'all'}, {}, 'root', 'trustsome1', environment, dry_run=True
- )
- assert names == ()
- def test_database_names_to_dump_queries_mariadb_for_database_names():
- environment = flexmock()
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- (
- 'mariadb',
- '--defaults-extra-file=/dev/fd/99',
- '--skip-column-names',
- '--batch',
- '--execute',
- 'show schemas',
- ),
- environment=environment,
- ).and_return('foo\nbar\nmysql\n').once()
- names = module.database_names_to_dump(
- {'name': 'all'}, {}, 'root', 'trustsome1', environment, dry_run=False
- )
- assert names == ('foo', 'bar')
- def test_use_streaming_true_for_any_databases():
- assert module.use_streaming(
- databases=[flexmock(), flexmock()],
- config=flexmock(),
- )
- def test_use_streaming_false_for_no_databases():
- assert not module.use_streaming(databases=[], config=flexmock())
- def test_dump_data_sources_dumps_each_database():
- databases = [{'name': 'foo'}, {'name': 'bar'}]
- processes = [flexmock(), flexmock()]
- flexmock(module).should_receive('make_dump_path').and_return('')
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).and_return(None)
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
- ('bar',)
- )
- for name, process in zip(('foo', 'bar'), processes):
- flexmock(module).should_receive('execute_dump_command').with_args(
- database={'name': name},
- config={},
- username=None,
- password=None,
- dump_path=object,
- database_names=(name,),
- environment={'USER': 'root'},
- dry_run=object,
- dry_run_label=object,
- ).and_return(process).once()
- assert (
- module.dump_data_sources(
- databases,
- {},
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=[],
- dry_run=False,
- )
- == processes
- )
- def test_dump_data_sources_dumps_with_password():
- database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
- process = flexmock()
- flexmock(module).should_receive('make_dump_path').and_return('')
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
- ('bar',)
- )
- flexmock(module).should_receive('execute_dump_command').with_args(
- database=database,
- config={},
- username='root',
- password='trustsome1',
- dump_path=object,
- database_names=('foo',),
- environment={'USER': 'root'},
- dry_run=object,
- dry_run_label=object,
- ).and_return(process).once()
- assert module.dump_data_sources(
- [database],
- {},
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=[],
- dry_run=False,
- ) == [process]
- def test_dump_data_sources_dumps_all_databases_at_once():
- databases = [{'name': 'all'}]
- process = flexmock()
- flexmock(module).should_receive('make_dump_path').and_return('')
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
- flexmock(module).should_receive('execute_dump_command').with_args(
- database={'name': 'all'},
- config={},
- username=None,
- password=None,
- dump_path=object,
- database_names=('foo', 'bar'),
- environment={'USER': 'root'},
- dry_run=object,
- dry_run_label=object,
- ).and_return(process).once()
- assert module.dump_data_sources(
- databases,
- {},
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=[],
- dry_run=False,
- ) == [process]
- def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
- databases = [{'name': 'all', 'format': 'sql'}]
- processes = [flexmock(), flexmock()]
- flexmock(module).should_receive('make_dump_path').and_return('')
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).and_return(None)
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
- for name, process in zip(('foo', 'bar'), processes):
- flexmock(module).should_receive('execute_dump_command').with_args(
- database={'name': name, 'format': 'sql'},
- config={},
- username=None,
- password=None,
- dump_path=object,
- database_names=(name,),
- environment={'USER': 'root'},
- dry_run=object,
- dry_run_label=object,
- ).and_return(process).once()
- assert (
- module.dump_data_sources(
- databases,
- {},
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=[],
- dry_run=False,
- )
- == processes
- )
- def test_database_names_to_dump_runs_mariadb_with_list_options():
- database = {'name': 'all', 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl'}
- flexmock(module).should_receive('parse_extra_options').and_return(
- ('--skip-ssl',), 'mariadb.cnf'
- )
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', 'mariadb.cnf'
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- (
- 'mariadb',
- '--defaults-extra-file=/dev/fd/99',
- '--skip-ssl',
- '--skip-column-names',
- '--batch',
- '--execute',
- 'show schemas',
- ),
- environment=None,
- ).and_return(('foo\nbar')).once()
- assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
- 'foo',
- 'bar',
- )
- def test_database_names_to_dump_runs_non_default_mariadb_with_list_options():
- database = {
- 'name': 'all',
- 'list_options': '--defaults-extra-file=mariadb.cnf --skip-ssl',
- 'mariadb_command': 'custom_mariadb',
- }
- flexmock(module).should_receive('parse_extra_options').and_return(
- ('--skip-ssl',), 'mariadb.cnf'
- )
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', 'mariadb.cnf'
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module).should_receive('execute_command_and_capture_output').with_args(
- environment=None,
- full_command=(
- 'custom_mariadb', # Custom MariaDB command
- '--defaults-extra-file=/dev/fd/99',
- '--skip-ssl',
- '--skip-column-names',
- '--batch',
- '--execute',
- 'show schemas',
- ),
- ).and_return(('foo\nbar')).once()
- assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == (
- 'foo',
- 'bar',
- )
- def test_execute_dump_command_runs_mariadb_dump():
- process = flexmock()
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').with_args(
- (
- 'mariadb-dump',
- '--defaults-extra-file=/dev/fd/99',
- '--add-drop-database',
- '--databases',
- 'foo',
- '--result-file',
- 'dump',
- ),
- environment=None,
- run_to_completion=False,
- ).and_return(process).once()
- assert (
- module.execute_dump_command(
- database={'name': 'foo'},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=False,
- dry_run_label='',
- )
- == process
- )
- def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database():
- process = flexmock()
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').with_args(
- (
- 'mariadb-dump',
- '--defaults-extra-file=/dev/fd/99',
- '--databases',
- 'foo',
- '--result-file',
- 'dump',
- ),
- environment=None,
- run_to_completion=False,
- ).and_return(process).once()
- assert (
- module.execute_dump_command(
- database={'name': 'foo', 'add_drop_database': False},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=False,
- dry_run_label='',
- )
- == process
- )
- def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port():
- process = flexmock()
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').with_args(
- (
- 'mariadb-dump',
- '--defaults-extra-file=/dev/fd/99',
- '--add-drop-database',
- '--host',
- 'database.example.org',
- '--port',
- '5433',
- '--protocol',
- 'tcp',
- '--databases',
- 'foo',
- '--result-file',
- 'dump',
- ),
- environment=None,
- run_to_completion=False,
- ).and_return(process).once()
- assert (
- module.execute_dump_command(
- database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=False,
- dry_run_label='',
- )
- == process
- )
- def test_execute_dump_command_runs_mariadb_dump_with_username_and_password():
- process = flexmock()
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').with_args(
- (
- 'mariadb-dump',
- '--defaults-extra-file=/dev/fd/99',
- '--add-drop-database',
- '--databases',
- 'foo',
- '--result-file',
- 'dump',
- ),
- environment={},
- run_to_completion=False,
- ).and_return(process).once()
- assert (
- module.execute_dump_command(
- database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment={},
- dry_run=False,
- dry_run_label='',
- )
- == process
- )
- def test_execute_dump_command_runs_mariadb_dump_with_options():
- process = flexmock()
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').with_args(
- (
- 'mariadb-dump',
- '--defaults-extra-file=/dev/fd/99',
- '--stuff=such',
- '--add-drop-database',
- '--databases',
- 'foo',
- '--result-file',
- 'dump',
- ),
- environment=None,
- run_to_completion=False,
- ).and_return(process).once()
- assert (
- module.execute_dump_command(
- database={'name': 'foo', 'options': '--stuff=such'},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=False,
- dry_run_label='',
- )
- == process
- )
- def test_execute_dump_command_runs_non_default_mariadb_dump_with_options():
- process = flexmock()
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return(('--stuff=such',), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').with_args(
- (
- 'custom_mariadb_dump', # Custom MariaDB dump command
- '--defaults-extra-file=/dev/fd/99',
- '--stuff=such',
- '--add-drop-database',
- '--databases',
- 'foo',
- '--result-file',
- 'dump',
- ),
- environment=None,
- run_to_completion=False,
- ).and_return(process).once()
- assert (
- module.execute_dump_command(
- database={
- 'name': 'foo',
- 'mariadb_dump_command': 'custom_mariadb_dump',
- 'options': '--stuff=such',
- }, # Custom MariaDB dump command specified
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=False,
- dry_run_label='',
- )
- == process
- )
- def test_execute_dump_command_with_duplicate_dump_skips_mariadb_dump():
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(True)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
- flexmock(module).should_receive('execute_command').never()
- assert (
- module.execute_dump_command(
- database={'name': 'foo'},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=True,
- dry_run_label='SO DRY',
- )
- is None
- )
- def test_execute_dump_command_with_dry_run_skips_mariadb_dump():
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
- flexmock(module.os.path).should_receive('exists').and_return(False)
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.dump).should_receive('create_named_pipe_for_dump')
- flexmock(module).should_receive('execute_command').never()
- assert (
- module.execute_dump_command(
- database={'name': 'foo'},
- config={},
- username='root',
- password='trustsome1',
- dump_path=flexmock(),
- database_names=('foo',),
- environment=None,
- dry_run=True,
- dry_run_label='SO DRY',
- )
- is None
- )
- def test_dump_data_sources_errors_for_missing_all_databases():
- databases = [{'name': 'all'}]
- flexmock(module).should_receive('make_dump_path').and_return('')
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
- 'databases/localhost/all'
- )
- flexmock(module).should_receive('database_names_to_dump').and_return(())
- with pytest.raises(ValueError):
- assert module.dump_data_sources(
- databases,
- {},
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=[],
- dry_run=False,
- )
- def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
- databases = [{'name': 'all'}]
- flexmock(module).should_receive('make_dump_path').and_return('')
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
- 'databases/localhost/all'
- )
- flexmock(module).should_receive('database_names_to_dump').and_return(())
- assert (
- module.dump_data_sources(
- databases,
- {},
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=[],
- dry_run=True,
- )
- == []
- )
- def test_restore_data_source_dump_runs_mariadb_to_restore():
- hook_config = [{'name': 'foo'}, {'name': 'bar'}]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- None, None, None
- ).and_return(())
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- ('mariadb', '--batch'),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source={'name': 'foo'},
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_runs_mariadb_with_options():
- hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- None, None, None
- ).and_return(())
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- ('mariadb', '--harder', '--batch'),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source=hook_config[0],
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_runs_non_default_mariadb_with_options():
- hook_config = [
- {'name': 'foo', 'restore_options': '--harder', 'mariadb_command': 'custom_mariadb'}
- ]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return(('--harder',), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- None, None, None
- ).and_return(())
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- ('custom_mariadb', '--harder', '--batch'),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source=hook_config[0],
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port():
- hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- None, None, None
- ).and_return(())
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- (
- 'mariadb',
- '--batch',
- '--host',
- 'database.example.org',
- '--port',
- '5433',
- '--protocol',
- 'tcp',
- ),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source=hook_config[0],
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_runs_mariadb_with_username_and_password():
- hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'root', 'trustsome1', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- ('mariadb', '--defaults-extra-file=/dev/fd/99', '--batch'),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source=hook_config[0],
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
- hook_config = [
- {
- 'name': 'foo',
- 'username': 'root',
- 'password': 'trustsome1',
- 'restore_hostname': 'restorehost',
- 'restore_port': 'restoreport',
- 'restore_username': 'restoreusername',
- 'restore_password': 'restorepassword',
- }
- ]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'cliusername', 'clipassword', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- (
- 'mariadb',
- '--defaults-extra-file=/dev/fd/99',
- '--batch',
- '--host',
- 'clihost',
- '--port',
- 'cliport',
- '--protocol',
- 'tcp',
- ),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source=hook_config[0],
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': 'clihost',
- 'port': 'cliport',
- 'username': 'cliusername',
- 'password': 'clipassword',
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
- hook_config = [
- {
- 'name': 'foo',
- 'username': 'root',
- 'password': 'trustsome1',
- 'hostname': 'dbhost',
- 'port': 'dbport',
- 'restore_username': 'restoreuser',
- 'restore_password': 'restorepass',
- 'restore_hostname': 'restorehost',
- 'restore_port': 'restoreport',
- }
- ]
- extract_process = flexmock(stdout=flexmock())
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- 'restoreuser', 'restorepass', None
- ).and_return(('--defaults-extra-file=/dev/fd/99',))
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').with_args(
- (
- 'mariadb',
- '--defaults-extra-file=/dev/fd/99',
- '--batch',
- '--host',
- 'restorehost',
- '--port',
- 'restoreport',
- '--protocol',
- 'tcp',
- ),
- processes=[extract_process],
- output_log_level=logging.DEBUG,
- input_file=extract_process.stdout,
- environment={'USER': 'root'},
- ).once()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source=hook_config[0],
- dry_run=False,
- extract_process=extract_process,
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
- def test_restore_data_source_dump_with_dry_run_skips_restore():
- hook_config = [{'name': 'foo'}]
- flexmock(module.borgmatic.hooks.credential.parse).should_receive(
- 'resolve_credential'
- ).replace_with(lambda value, config: value)
- flexmock(module).should_receive('parse_extra_options').and_return((), None)
- flexmock(module).should_receive('make_defaults_file_options').with_args(
- None, None, None
- ).and_return(())
- flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
- flexmock(module).should_receive('execute_command_with_processes').never()
- module.restore_data_source_dump(
- hook_config,
- {},
- data_source={'name': 'foo'},
- dry_run=True,
- extract_process=flexmock(),
- connection_params={
- 'hostname': None,
- 'port': None,
- 'username': None,
- 'password': None,
- },
- borgmatic_runtime_directory='/run/borgmatic',
- )
|