2023-06-19 21:22:01 +02:00
|
|
|
import logging
|
2023-06-22 21:40:57 +02:00
|
|
|
|
2023-03-02 19:25:16 +01:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import sqlite as module
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_logs_and_skips_if_dump_already_exists():
|
2023-03-02 19:25:16 +01:00
|
|
|
databases = [{'path': '/path/to/database', 'name': 'database'}]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2023-03-02 19:25:16 +01:00
|
|
|
'/path/to/dump/database'
|
|
|
|
)
|
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(True)
|
2023-12-31 20:07:59 +01:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
2023-03-04 08:13:07 +01:00
|
|
|
flexmock(module).should_receive('execute_command').never()
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == []
|
2023-03-02 19:31:52 +01:00
|
|
|
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_dumps_each_database():
|
2023-03-02 19:25:16 +01:00
|
|
|
databases = [
|
|
|
|
{'path': '/path/to/database1', 'name': 'database1'},
|
|
|
|
{'path': '/path/to/database2', 'name': 'database2'},
|
|
|
|
]
|
2023-03-03 20:57:07 +01:00
|
|
|
processes = [flexmock(), flexmock()]
|
2023-03-02 19:25:16 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2023-03-02 19:25:16 +01:00
|
|
|
'/path/to/dump/database'
|
|
|
|
)
|
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2023-12-31 20:07:59 +01:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2023-03-03 20:57:07 +01:00
|
|
|
flexmock(module).should_receive('execute_command').and_return(processes[0]).and_return(
|
|
|
|
processes[1]
|
|
|
|
)
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-03-03 20:57:07 +01:00
|
|
|
|
2023-12-31 20:07:59 +01:00
|
|
|
def test_dump_data_sources_with_non_existent_path_warns_and_dumps_database():
|
2023-03-04 08:13:07 +01:00
|
|
|
databases = [
|
|
|
|
{'path': '/path/to/database1', 'name': 'database1'},
|
|
|
|
]
|
|
|
|
processes = [flexmock()]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
|
|
|
|
flexmock(module.logger).should_receive('warning').once()
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2023-03-04 08:13:07 +01:00
|
|
|
'/path/to/dump/database'
|
|
|
|
)
|
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2023-12-31 20:07:59 +01:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2023-03-04 08:13:07 +01:00
|
|
|
flexmock(module).should_receive('execute_command').and_return(processes[0])
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
|
2023-03-04 08:13:07 +01:00
|
|
|
|
|
|
|
|
2023-12-31 20:07:59 +01:00
|
|
|
def test_dump_data_sources_with_name_all_warns_and_dumps_all_databases():
|
2023-03-03 20:57:07 +01:00
|
|
|
databases = [
|
|
|
|
{'path': '/path/to/database1', 'name': 'all'},
|
2023-03-02 19:31:52 +01:00
|
|
|
]
|
2023-03-03 20:57:07 +01:00
|
|
|
processes = [flexmock()]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
|
2023-03-04 08:13:07 +01:00
|
|
|
flexmock(module.logger).should_receive(
|
|
|
|
'warning'
|
|
|
|
).twice() # once for the name=all, once for the non-existent path
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2023-03-03 20:57:07 +01:00
|
|
|
'/path/to/dump/database'
|
|
|
|
)
|
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2023-12-31 20:07:59 +01:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
2023-03-03 20:57:07 +01:00
|
|
|
flexmock(module).should_receive('execute_command').and_return(processes[0])
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=False) == processes
|
2023-03-02 19:31:52 +01:00
|
|
|
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_dump_data_sources_does_not_dump_if_dry_run():
|
2023-03-02 19:25:16 +01:00
|
|
|
databases = [{'path': '/path/to/database', 'name': 'database'}]
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('/path/to/dump')
|
2023-08-24 22:50:10 +02:00
|
|
|
flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return(
|
2023-03-02 19:25:16 +01:00
|
|
|
'/path/to/dump/database'
|
|
|
|
)
|
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
2023-12-31 20:07:59 +01:00
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
2023-03-03 20:57:07 +01:00
|
|
|
flexmock(module).should_receive('execute_command').never()
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.dump_data_sources(databases, {}, 'test.yaml', dry_run=True) == []
|
2023-03-02 19:31:52 +01:00
|
|
|
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_restores_database():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'path': '/path/to/database', 'name': 'database'}, {'name': 'other'}]
|
2023-03-02 19:31:52 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-06-19 21:22:01 +02:00
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'sqlite3',
|
|
|
|
'/path/to/database',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
flexmock(module.os).should_receive('remove').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={'restore_path': None},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
{'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
|
|
|
|
]
|
2023-06-19 21:22:01 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'sqlite3',
|
|
|
|
'cli/path/to/database',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
flexmock(module.os).should_receive('remove').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': 'database'},
|
2023-06-19 21:22:01 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={'restore_path': 'cli/path/to/database'},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
{'path': '/path/to/database', 'name': 'database', 'restore_path': 'config/path/to/database'}
|
|
|
|
]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
(
|
|
|
|
'sqlite3',
|
|
|
|
'config/path/to/database',
|
|
|
|
),
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-03-04 08:38:30 +01:00
|
|
|
flexmock(module.os).should_receive('remove').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={'restore_path': None},
|
2023-03-02 19:31:52 +01:00
|
|
|
)
|
|
|
|
|
2023-03-02 19:25:16 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_restore_data_source_dump_does_not_restore_database_if_dry_run():
|
2023-08-14 21:43:21 +02:00
|
|
|
hook_config = [{'path': '/path/to/database', 'name': 'database'}]
|
2023-03-02 19:31:52 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
2023-03-02 19:25:16 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
2023-03-04 08:38:30 +01:00
|
|
|
flexmock(module.os).should_receive('remove').never()
|
2023-03-02 19:25:16 +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': 'database'},
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=True,
|
|
|
|
extract_process=extract_process,
|
|
|
|
connection_params={'restore_path': None},
|
2023-03-02 19:31:52 +01:00
|
|
|
)
|