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 = ''
|
|
|
|
dry_run_label = ''
|
|
|
|
|
|
|
|
names = module.database_names_to_dump(
|
|
|
|
{'name': 'foo'}, extra_environment, log_prefix, dry_run_label
|
|
|
|
)
|
|
|
|
|
|
|
|
assert names == ('foo',)
|
|
|
|
|
|
|
|
|
|
|
|
def test_database_names_to_dump_queries_mysql_for_database_names():
|
|
|
|
extra_environment = flexmock()
|
|
|
|
log_prefix = ''
|
|
|
|
dry_run_label = ''
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
|
|
|
('mysql', '--skip-column-names', '--batch', '--execute', 'show schemas'),
|
|
|
|
output_log_level=None,
|
|
|
|
extra_environment=extra_environment,
|
|
|
|
).and_return('foo\nbar\nmysql\n').once()
|
|
|
|
|
|
|
|
names = module.database_names_to_dump(
|
|
|
|
{'name': 'all'}, extra_environment, log_prefix, dry_run_label
|
|
|
|
)
|
|
|
|
|
|
|
|
assert names == ('foo', 'bar')
|
|
|
|
|
|
|
|
|
2019-11-08 20:17:52 +01:00
|
|
|
def test_dump_databases_runs_mysqldump_for_each_database():
|
|
|
|
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('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-11-08 20:17:52 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
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
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
for name, process in zip(('foo', 'bar'), processes):
|
2019-11-08 20:17:52 +01:00
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2020-05-07 20:44:04 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--databases',
|
|
|
|
name,
|
|
|
|
'>',
|
|
|
|
'databases/localhost/{}'.format(name),
|
|
|
|
),
|
|
|
|
shell=True,
|
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
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == processes
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_with_dry_run_skips_mysqldump():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
2019-12-11 01:04:34 +01:00
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-11-08 20:17:52 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',)).and_return(
|
|
|
|
('bar',)
|
|
|
|
)
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
2019-11-08 20:17:52 +01:00
|
|
|
flexmock(module).should_receive('execute_command').never()
|
|
|
|
|
2019-12-11 01:04:34 +01:00
|
|
|
module.dump_databases(databases, 'test.yaml', {}, dry_run=True)
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mysqldump_with_hostname_and_port():
|
|
|
|
databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
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('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-11-08 20:17:52 +01:00
|
|
|
'databases/database.example.org/foo'
|
|
|
|
)
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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',
|
2020-05-07 20:44:04 +02:00
|
|
|
'>',
|
|
|
|
'databases/database.example.org/foo',
|
2019-11-08 20:17:52 +01:00
|
|
|
),
|
2020-05-07 20:44:04 +02:00
|
|
|
shell=True,
|
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
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mysqldump_with_username_and_password():
|
|
|
|
databases = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
|
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('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-11-08 20:17:52 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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(
|
2020-05-07 20:44:04 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--user',
|
|
|
|
'root',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
),
|
|
|
|
shell=True,
|
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
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mysqldump_with_options():
|
|
|
|
databases = [{'name': 'foo', 'options': '--stuff=such'}]
|
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('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-11-08 20:17:52 +01:00
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo',))
|
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(
|
2020-05-07 20:44:04 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--stuff=such',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
|
|
|
),
|
|
|
|
shell=True,
|
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
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-11-08 20:17:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mysqldump_for_all_databases():
|
|
|
|
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('')
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
2019-11-08 20:17:52 +01:00
|
|
|
'databases/localhost/all'
|
|
|
|
)
|
2020-04-22 21:17:22 +02:00
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(('foo', 'bar'))
|
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(
|
2020-05-07 20:44:04 +02:00
|
|
|
(
|
|
|
|
'mysqldump',
|
|
|
|
'--add-drop-database',
|
|
|
|
'--databases',
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
'>',
|
|
|
|
'databases/localhost/all',
|
|
|
|
),
|
|
|
|
shell=True,
|
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-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2021-11-12 20:49:09 +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').with_args(
|
|
|
|
(
|
|
|
|
'mysql',
|
|
|
|
'--defaults-extra-file=my.cnf',
|
|
|
|
'--skip-column-names',
|
|
|
|
'--batch',
|
|
|
|
'--execute',
|
|
|
|
'show schemas',
|
|
|
|
),
|
|
|
|
output_log_level=None,
|
|
|
|
extra_environment=None,
|
|
|
|
).and_return(('foo\nbar')).once()
|
|
|
|
|
|
|
|
assert module.database_names_to_dump(database, None, 'test.yaml', '') == ('foo', 'bar')
|
|
|
|
|
|
|
|
|
2020-05-26 17:59:04 +02:00
|
|
|
def test_dump_databases_errors_for_missing_all_databases():
|
|
|
|
databases = [{'name': 'all'}]
|
|
|
|
process = flexmock()
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/all'
|
|
|
|
)
|
|
|
|
flexmock(module).should_receive('database_names_to_dump').and_return(())
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
assert module.dump_databases(databases, 'test.yaml', {}, dry_run=False) == [process]
|
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_mysql_to_restore():
|
|
|
|
database_config = [{'name': 'foo'}]
|
|
|
|
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(
|
2020-05-09 04:38:33 +02:00
|
|
|
('mysql', '--batch', '--verbose'),
|
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,
|
2020-05-15 07:38:38 +02:00
|
|
|
borg_local_path='borg',
|
2020-05-07 20:44:04 +02:00
|
|
|
).once()
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_errors_on_multiple_database_config():
|
|
|
|
database_config = [{'name': 'foo'}, {'name': 'bar'}]
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
flexmock(module).should_receive('execute_command').never()
|
2019-11-12 18:50:26 +01:00
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=flexmock()
|
|
|
|
)
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_mysql_with_hostname_and_port():
|
|
|
|
database_config = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
|
|
|
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',
|
2020-05-09 04:38:33 +02:00
|
|
|
'--verbose',
|
2019-11-12 18:50:26 +01:00
|
|
|
'--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,
|
2020-05-15 07:38:38 +02:00
|
|
|
borg_local_path='borg',
|
2019-11-12 18:50:26 +01:00
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2019-11-12 18:50:26 +01:00
|
|
|
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
def test_restore_database_dump_runs_mysql_with_username_and_password():
|
|
|
|
database_config = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}]
|
|
|
|
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(
|
2020-05-09 04:38:33 +02:00
|
|
|
('mysql', '--batch', '--verbose', '--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'},
|
2020-05-15 07:38:38 +02:00
|
|
|
borg_local_path='borg',
|
2019-11-12 18:50:26 +01:00
|
|
|
).once()
|
|
|
|
|
2020-05-07 20:44:04 +02:00
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=False, extract_process=extract_process
|
|
|
|
)
|
2020-05-07 21:14:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_with_dry_run_skips_restore():
|
|
|
|
database_config = [{'name': 'foo'}]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
|
|
|
database_config, 'test.yaml', {}, dry_run=True, extract_process=flexmock()
|
|
|
|
)
|