2020-05-09 04:38:33 +02:00
|
|
|
import logging
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
import pytest
|
2019-11-08 20:17:52 +01:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import mysql as module
|
|
|
|
|
|
|
|
|
2020-04-22 21:17:22 +02:00
|
|
|
def test_database_names_to_dump_passes_through_name():
|
|
|
|
extra_environment = flexmock()
|
|
|
|
log_prefix = ''
|
|
|
|
|
|
|
|
names = module.database_names_to_dump(
|
2023-02-27 07:15:12 +01:00
|
|
|
{'name': 'foo'}, extra_environment, log_prefix, dry_run=False
|
2020-04-22 21:17:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert names == ('foo',)
|
|
|
|
|
|
|
|
|
2023-02-27 07:15:12 +01:00
|
|
|
def test_database_names_to_dump_bails_for_dry_run():
|
|
|
|
extra_environment = flexmock()
|
|
|
|
log_prefix = ''
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').never()
|
|
|
|
|
|
|
|
names = module.database_names_to_dump(
|
|
|
|
{'name': 'all'}, extra_environment, log_prefix, dry_run=True
|
|
|
|
)
|
|
|
|
|
|
|
|
assert names == ()
|
|
|
|
|
|
|
|
|
2020-04-22 21:17:22 +02:00
|
|
|
def test_database_names_to_dump_queries_mysql_for_database_names():
|
|
|
|
extra_environment = flexmock()
|
|
|
|
log_prefix = ''
|
2022-10-15 01:19:26 +02:00
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').with_args(
|
2020-04-22 21:17:22 +02:00
|
|
|
('mysql', '--skip-column-names', '--batch', '--execute', 'show schemas'),
|
|
|
|
extra_environment=extra_environment,
|
|
|
|
).and_return('foo\nbar\nmysql\n').once()
|
|
|
|
|
|
|
|
names = module.database_names_to_dump(
|
2023-02-27 07:15:12 +01:00
|
|
|
{'name': 'all'}, extra_environment, log_prefix, dry_run=False
|
2020-04-22 21:17:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
assert names == ('foo', 'bar')
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_dumps_each_database():
|
2019-11-08 20:17:52 +01:00
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
processes = [flexmock(), flexmock()]
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
for name, process in zip(('foo', 'bar'), processes):
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module).should_receive('execute_dump_command').with_args(
|
|
|
|
database={'name': name},
|
|
|
|
log_prefix=object,
|
|
|
|
dump_path=object,
|
|
|
|
database_names=(name,),
|
|
|
|
extra_environment=object,
|
|
|
|
dry_run=object,
|
|
|
|
dry_run_label=object,
|
2020-05-07 20:44:04 +02:00
|
|
|
).and_return(process).once()
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_dumps_with_password():
|
2023-01-26 08:31:07 +01:00
|
|
|
database = {'name': 'foo', 'username': 'root', 'password': 'trustsome1'}
|
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module).should_receive('execute_dump_command').with_args(
|
|
|
|
database=database,
|
|
|
|
log_prefix=object,
|
|
|
|
dump_path=object,
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment={'MYSQL_PWD': 'trustsome1'},
|
|
|
|
dry_run=object,
|
|
|
|
dry_run_label=object,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources([database], {}, 'test.yaml', dry_run=False) == [process]
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_dumps_all_databases_at_once():
|
2023-01-26 08:31:07 +01:00
|
|
|
databases = [{'name': 'all'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
|
|
|
|
flexmock(module).should_receive('execute_dump_command').with_args(
|
|
|
|
database={'name': 'all'},
|
|
|
|
log_prefix=object,
|
|
|
|
dump_path=object,
|
|
|
|
database_names=('foo', 'bar'),
|
|
|
|
extra_environment=object,
|
|
|
|
dry_run=object,
|
|
|
|
dry_run_label=object,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == [process]
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_dumps_all_databases_separately_when_format_configured():
|
2023-01-26 08:31:07 +01:00
|
|
|
databases = [{'name': 'all', 'format': 'sql'}]
|
|
|
|
processes = [flexmock(), flexmock()]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
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'},
|
|
|
|
log_prefix=object,
|
|
|
|
dump_path=object,
|
|
|
|
database_names=(name,),
|
|
|
|
extra_environment=object,
|
|
|
|
dry_run=object,
|
|
|
|
dry_run_label=object,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_runs_mysql_with_list_options():
|
|
|
|
database = {'name': 'all', 'list_options': '--defaults-extra-file=my.cnf'}
|
|
|
|
flexmock(module).should_receive('execute_command_and_capture_output').with_args(
|
|
|
|
(
|
|
|
|
'mysql',
|
|
|
|
'--defaults-extra-file=my.cnf',
|
|
|
|
'--skip-column-names',
|
|
|
|
'--batch',
|
|
|
|
'--execute',
|
|
|
|
'show schemas',
|
|
|
|
),
|
|
|
|
extra_environment=None,
|
|
|
|
).and_return(('foo\nbar')).once()
|
|
|
|
|
|
|
|
assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
|
|
|
|
|
|
|
|
|
|
|
|
def test_execute_dump_command_runs_mysqldump():
|
|
|
|
process = flexmock()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-15 04:35:24 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'--result-file',
|
|
|
|
'dump',
|
|
|
|
),
|
2023-01-26 08:31:07 +01:00
|
|
|
extra_environment=None,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo'},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment=None,
|
|
|
|
dry_run=False,
|
|
|
|
dry_run_label='',
|
|
|
|
)
|
|
|
|
== process
|
2019-11-08 20:17:52 +01:00
|
|
|
)
|
2023-01-26 08:31:07 +01:00
|
|
|
|
|
|
|
|
2023-02-21 00:32:47 +01:00
|
|
|
def test_execute_dump_command_runs_mysqldump_without_add_drop_database():
|
|
|
|
process = flexmock()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-02-21 00:32:47 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-04-15 04:35:24 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'--result-file',
|
|
|
|
'dump',
|
|
|
|
),
|
2023-02-21 00:32:47 +01:00
|
|
|
extra_environment=None,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo', 'add_drop_database': False},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment=None,
|
|
|
|
dry_run=False,
|
|
|
|
dry_run_label='',
|
|
|
|
)
|
|
|
|
== process
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
def test_execute_dump_command_runs_mysqldump_with_hostname_and_port():
|
|
|
|
process = flexmock()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--protocol',
|
|
|
|
'tcp',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
2023-03-27 19:36:39 +02:00
|
|
|
'--result-file',
|
2023-01-26 08:31:07 +01:00
|
|
|
'dump',
|
2019-11-08 20:17:52 +01:00
|
|
|
),
|
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo', 'hostname': 'database.example.org', 'port': 5433},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment=None,
|
|
|
|
dry_run=False,
|
|
|
|
dry_run_label='',
|
|
|
|
)
|
|
|
|
== process
|
|
|
|
)
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
def test_execute_dump_command_runs_mysqldump_with_username_and_password():
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-03-27 19:36:39 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--user',
|
|
|
|
'root',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'--result-file',
|
|
|
|
'dump',
|
|
|
|
),
|
2019-11-08 20:17:52 +01:00
|
|
|
extra_environment={'MYSQL_PWD': 'trustsome1'},
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo', 'username': 'root', 'password': 'trustsome1'},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment={'MYSQL_PWD': 'trustsome1'},
|
|
|
|
dry_run=False,
|
|
|
|
dry_run_label='',
|
|
|
|
)
|
|
|
|
== process
|
|
|
|
)
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
def test_execute_dump_command_runs_mysqldump_with_options():
|
2020-05-07 20:44:04 +02:00
|
|
|
process = flexmock()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-03-27 19:36:39 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--stuff=such',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'--result-file',
|
|
|
|
'dump',
|
|
|
|
),
|
2019-11-08 20:17:52 +01:00
|
|
|
extra_environment=None,
|
2020-05-07 20:44:04 +02:00
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo', 'options': '--stuff=such'},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment=None,
|
|
|
|
dry_run=False,
|
|
|
|
dry_run_label='',
|
|
|
|
)
|
|
|
|
== process
|
|
|
|
)
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
def test_execute_dump_command_with_duplicate_dump_skips_mysqldump():
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(True)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
|
|
|
flexmock(module).should_receive('execute_command').never()
|
2019-11-08 20:17:52 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo'},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment=None,
|
|
|
|
dry_run=True,
|
|
|
|
dry_run_label='SO DRY',
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
def test_execute_dump_command_with_dry_run_skips_mysqldump():
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('dump')
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
flexmock(module).should_receive('execute_command').never()
|
2021-11-12 20:49:09 +01:00
|
|
|
|
2023-01-26 08:31:07 +01:00
|
|
|
assert (
|
|
|
|
module.execute_dump_command(
|
|
|
|
database={'name': 'foo'},
|
|
|
|
log_prefix='log',
|
|
|
|
dump_path=flexmock(),
|
|
|
|
database_names=('foo',),
|
|
|
|
extra_environment=None,
|
|
|
|
dry_run=True,
|
|
|
|
dry_run_label='SO DRY',
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
)
|
2021-11-12 20:49:09 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_errors_for_missing_all_databases():
|
2020-05-26 17:59:04 +02:00
|
|
|
databases = [{'name': 'all'}]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2020-05-26 17:59:04 +02:00
|
|
|
'databases/localhost/all'
|
|
|
|
)
|
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(())
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False)
|
2023-02-27 07:15:12 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_does_not_error_for_missing_all_databases_with_dry_run():
|
2023-02-27 07:15:12 +01:00
|
|
|
databases = [{'name': 'all'}]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2023-02-27 07:15:12 +01:00
|
|
|
'databases/localhost/all'
|
|
|
|
)
|
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(())
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
|
2020-05-26 17:59:04 +02:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_runs_mysql_to_restore():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'name': 'foo'}, {'name': 'bar'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2021-12-09 01:40:25 +01:00
|
|
|
('mysql', '--batch'),
|
2020-05-07 20:44:04 +02:00
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo'},
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_runs_mysql_with_options():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'name': 'foo', 'restore_options': '--harder'}]
|
2023-01-26 23:59:17 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
('mysql', '--batch', '--harder'),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source=hook_config[0],
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2023-01-26 23:59:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_runs_mysql_with_hostname_and_port():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2019-11-12 18:50:26 +01:00
|
|
|
(
|
|
|
|
'mysql',
|
|
|
|
'--batch',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--protocol',
|
|
|
|
'tcp',
|
|
|
|
),
|
2020-05-07 20:44:04 +02:00
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
2019-11-12 18:50:26 +01:00
|
|
|
extra_environment=None,
|
|
|
|
).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source=hook_config[0],
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_runs_mysql_with_username_and_password():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
|
2020-05-07 20:44:04 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2021-12-09 01:40:25 +01:00
|
|
|
('mysql', '--batch', '--user', 'root'),
|
2020-05-07 20:44:04 +02:00
|
|
|
processes=[extract_process],
|
2020-05-09 04:38:33 +02:00
|
|
|
output_log_level=logging.DEBUG,
|
2020-05-07 20:44:04 +02:00
|
|
|
input_file=extract_process.stdout,
|
2019-11-12 18:50:26 +01:00
|
|
|
extra_environment={'MYSQL_PWD': 'trustsome1'},
|
|
|
|
).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source=hook_config[0],
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 20:44:04 +02:00
|
|
|
)
|
2020-05-07 21:14:27 +02:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_with_connection_params_uses_connection_params_for_restore():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [
|
2023-06-20 20:33:07 +02:00
|
|
|
{
|
|
|
|
'name': 'foo',
|
|
|
|
'username': 'root',
|
|
|
|
'password': 'trustsome1',
|
|
|
|
'restore_hostname': 'restorehost',
|
|
|
|
'restore_port': 'restoreport',
|
|
|
|
'restore_username': 'restoreusername',
|
|
|
|
'restore_password': 'restorepassword',
|
|
|
|
}
|
|
|
|
]
|
2023-06-19 21:22:01 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'mysql',
|
|
|
|
'--batch',
|
|
|
|
'--host',
|
|
|
|
'clihost',
|
|
|
|
'--port',
|
|
|
|
'cliport',
|
|
|
|
'--protocol',
|
|
|
|
'tcp',
|
|
|
|
'--user',
|
|
|
|
'cliusername',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'MYSQL_PWD': 'clipassword'},
|
|
|
|
).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-19 21:22:01 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo'},
|
2023-06-19 21:22:01 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': 'clihost',
|
|
|
|
'port': 'cliport',
|
|
|
|
'username': 'cliusername',
|
|
|
|
'password': 'clipassword',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_without_connection_params_uses_restore_params_in_config_for_restore():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [
|
2023-06-19 21:22:01 +02:00
|
|
|
{
|
|
|
|
'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).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'mysql',
|
|
|
|
'--batch',
|
|
|
|
'--host',
|
|
|
|
'restorehost',
|
|
|
|
'--port',
|
|
|
|
'restoreport',
|
|
|
|
'--protocol',
|
|
|
|
'tcp',
|
|
|
|
'--user',
|
|
|
|
'restoreuser',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
extra_environment={'MYSQL_PWD': 'restorepass'},
|
|
|
|
).once()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-19 21:22:01 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source=hook_config[0],
|
2023-06-19 21:22:01 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_with_dry_run_skips_restore():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'name': 'foo'}]
|
2020-05-07 21:14:27 +02:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.restore_data_source_dump(
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-08-24 22:50:10 +02:00
|
|
|
data_source={'name': 'foo'},
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=True,
|
|
|
|
extract_process=flexmock(),
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2020-05-07 21:14:27 +02:00
|
|
|
)
|