2021-12-26 01:00:58 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import mongodb as module
|
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mongodump_for_each_database():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
|
|
processes = [flexmock(), flexmock()]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
for name, process in zip(('foo', 'bar'), processes):
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-08-04 22:22:44 +02:00
|
|
|
('mongodump', '--db', name, '--archive', '>', f'databases/localhost/{name}'),
|
2021-12-26 01:00:58 +01:00
|
|
|
shell=True,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=False) == processes
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_with_dry_run_skips_mongodump():
|
|
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
).and_return('databases/localhost/bar')
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
|
|
|
flexmock(module).should_receive('execute_command').never()
|
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=True) == []
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mongodump_with_hostname_and_port():
|
|
|
|
databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}]
|
|
|
|
process = flexmock()
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/database.example.org/foo'
|
|
|
|
)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-08-04 22:22:44 +02:00
|
|
|
(
|
2021-12-26 01:00:58 +01:00
|
|
|
'mongodump',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
'--db',
|
|
|
|
'foo',
|
2023-02-21 00:18:51 +01:00
|
|
|
'--archive',
|
2021-12-26 01:00:58 +01:00
|
|
|
'>',
|
|
|
|
'databases/database.example.org/foo',
|
2023-08-04 22:22:44 +02:00
|
|
|
),
|
2021-12-26 01:00:58 +01:00
|
|
|
shell=True,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=False) == [process]
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mongodump_with_username_and_password():
|
2022-01-05 01:26:38 +01:00
|
|
|
databases = [
|
|
|
|
{
|
|
|
|
'name': 'foo',
|
|
|
|
'username': 'mongo',
|
|
|
|
'password': 'trustsome1',
|
2023-03-07 23:08:35 +01:00
|
|
|
'authentication_database': 'admin',
|
2022-01-05 01:26:38 +01:00
|
|
|
}
|
|
|
|
]
|
2021-12-26 01:00:58 +01:00
|
|
|
process = flexmock()
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-08-04 22:22:44 +02:00
|
|
|
(
|
2021-12-26 01:00:58 +01:00
|
|
|
'mongodump',
|
|
|
|
'--username',
|
|
|
|
'mongo',
|
|
|
|
'--password',
|
|
|
|
'trustsome1',
|
2021-12-29 22:18:50 +01:00
|
|
|
'--authenticationDatabase',
|
|
|
|
'admin',
|
2021-12-26 01:00:58 +01:00
|
|
|
'--db',
|
|
|
|
'foo',
|
2023-02-21 00:18:51 +01:00
|
|
|
'--archive',
|
2021-12-26 01:00:58 +01:00
|
|
|
'>',
|
|
|
|
'databases/localhost/foo',
|
2023-08-04 22:22:44 +02:00
|
|
|
),
|
2021-12-26 01:00:58 +01:00
|
|
|
shell=True,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=False) == [process]
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mongodump_with_directory_format():
|
|
|
|
databases = [{'name': 'foo', 'format': 'directory'}]
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
|
|
|
flexmock(module.dump).should_receive('create_parent_directory_for_dump')
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump').never()
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-08-04 22:22:44 +02:00
|
|
|
('mongodump', '--out', 'databases/localhost/foo', '--db', 'foo'),
|
2023-04-15 04:35:24 +02:00
|
|
|
shell=True,
|
2023-02-21 00:18:51 +01:00
|
|
|
).and_return(flexmock()).once()
|
2021-12-26 01:00:58 +01:00
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=False) == []
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mongodump_with_options():
|
|
|
|
databases = [{'name': 'foo', 'options': '--stuff=such'}]
|
|
|
|
process = flexmock()
|
|
|
|
flexmock(module).should_receive('make_dump_path').and_return('')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return(
|
|
|
|
'databases/localhost/foo'
|
|
|
|
)
|
|
|
|
flexmock(module.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-08-04 22:22:44 +02:00
|
|
|
('mongodump', '--db', 'foo', '--stuff=such', '--archive', '>', 'databases/localhost/foo'),
|
2021-12-26 01:00:58 +01:00
|
|
|
shell=True,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=False) == [process]
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dump_databases_runs_mongodumpall_for_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.dump).should_receive('create_named_pipe_for_dump')
|
|
|
|
|
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2023-08-04 22:22:44 +02:00
|
|
|
('mongodump', '--archive', '>', 'databases/localhost/all'),
|
2021-12-26 01:00:58 +01:00
|
|
|
shell=True,
|
|
|
|
run_to_completion=False,
|
|
|
|
).and_return(process).once()
|
|
|
|
|
2023-07-09 08:14:30 +02:00
|
|
|
assert module.dump_databases(databases, {}, 'test.yaml', dry_run=False) == [process]
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
|
2022-07-07 08:21:24 +02:00
|
|
|
def test_restore_database_dump_runs_mongorestore():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [{'name': 'foo', 'schemas': None}, {'name': 'bar'}]
|
2021-12-26 01:00:58 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
['mongorestore', '--archive', '--drop', '--db', 'foo'],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-07-11 09:37:36 +02:00
|
|
|
def test_restore_database_dump_errors_on_empty_databases_config():
|
|
|
|
databases_config = []
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
flexmock(module).should_receive('execute_command').never()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_name='foo',
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=flexmock(),
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-07 08:21:24 +02:00
|
|
|
def test_restore_database_dump_runs_mongorestore_with_hostname_and_port():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [
|
2023-04-14 23:27:51 +02:00
|
|
|
{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433, 'schemas': None}
|
|
|
|
]
|
2021-12-26 01:00:58 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
[
|
|
|
|
'mongorestore',
|
|
|
|
'--archive',
|
|
|
|
'--drop',
|
|
|
|
'--db',
|
|
|
|
'foo',
|
|
|
|
'--host',
|
|
|
|
'database.example.org',
|
|
|
|
'--port',
|
|
|
|
'5433',
|
|
|
|
],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-07 08:21:24 +02:00
|
|
|
def test_restore_database_dump_runs_mongorestore_with_username_and_password():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [
|
2022-01-05 01:26:38 +01:00
|
|
|
{
|
|
|
|
'name': 'foo',
|
|
|
|
'username': 'mongo',
|
|
|
|
'password': 'trustsome1',
|
|
|
|
'authentication_database': 'admin',
|
2023-04-14 23:27:51 +02:00
|
|
|
'schemas': None,
|
2022-01-05 01:26:38 +01:00
|
|
|
}
|
2021-12-29 22:18:50 +01:00
|
|
|
]
|
2021-12-26 01:00:58 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
[
|
|
|
|
'mongorestore',
|
|
|
|
'--archive',
|
|
|
|
'--drop',
|
|
|
|
'--db',
|
|
|
|
'foo',
|
|
|
|
'--username',
|
|
|
|
'mongo',
|
|
|
|
'--password',
|
|
|
|
'trustsome1',
|
2021-12-29 22:18:50 +01:00
|
|
|
'--authenticationDatabase',
|
|
|
|
'admin',
|
2021-12-26 01:00:58 +01:00
|
|
|
],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-06-19 21:22:01 +02:00
|
|
|
def test_restore_database_dump_with_connection_params_uses_connection_params_for_restore():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [
|
2023-06-19 21:22:01 +02:00
|
|
|
{
|
|
|
|
'name': 'foo',
|
|
|
|
'username': 'mongo',
|
|
|
|
'password': 'trustsome1',
|
|
|
|
'authentication_database': 'admin',
|
2023-06-20 20:33:07 +02:00
|
|
|
'restore_hostname': 'restorehost',
|
|
|
|
'restore_port': 'restoreport',
|
|
|
|
'restore_username': 'restoreusername',
|
|
|
|
'restore_password': 'restorepassword',
|
2023-06-19 21:22:01 +02:00
|
|
|
'schemas': None,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
[
|
|
|
|
'mongorestore',
|
|
|
|
'--archive',
|
|
|
|
'--drop',
|
|
|
|
'--db',
|
|
|
|
'foo',
|
|
|
|
'--host',
|
|
|
|
'clihost',
|
|
|
|
'--port',
|
|
|
|
'cliport',
|
|
|
|
'--username',
|
|
|
|
'cliusername',
|
|
|
|
'--password',
|
|
|
|
'clipassword',
|
|
|
|
'--authenticationDatabase',
|
|
|
|
'admin',
|
|
|
|
],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-19 21:22:01 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_without_connection_params_uses_restore_params_in_config_for_restore():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [
|
2023-06-19 21:22:01 +02:00
|
|
|
{
|
|
|
|
'name': 'foo',
|
|
|
|
'username': 'mongo',
|
|
|
|
'password': 'trustsome1',
|
|
|
|
'authentication_database': 'admin',
|
|
|
|
'schemas': None,
|
|
|
|
'restore_hostname': 'restorehost',
|
|
|
|
'restore_port': 'restoreport',
|
|
|
|
'restore_username': 'restoreuser',
|
|
|
|
'restore_password': 'restorepass',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
[
|
|
|
|
'mongorestore',
|
|
|
|
'--archive',
|
|
|
|
'--drop',
|
|
|
|
'--db',
|
|
|
|
'foo',
|
|
|
|
'--host',
|
|
|
|
'restorehost',
|
|
|
|
'--port',
|
|
|
|
'restoreport',
|
|
|
|
'--username',
|
|
|
|
'restoreuser',
|
|
|
|
'--password',
|
|
|
|
'restorepass',
|
|
|
|
'--authenticationDatabase',
|
|
|
|
'admin',
|
|
|
|
],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-19 21:22:01 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_name='foo',
|
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-01-26 23:59:17 +01:00
|
|
|
def test_restore_database_dump_runs_mongorestore_with_options():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [{'name': 'foo', 'restore_options': '--harder', 'schemas': None}]
|
2023-01-26 23:59:17 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2023-01-27 00:09:35 +01:00
|
|
|
['mongorestore', '--archive', '--drop', '--db', 'foo', '--harder'],
|
2023-01-26 23:59:17 +01:00
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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,
|
|
|
|
},
|
2023-01-26 23:59:17 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-14 23:27:51 +02:00
|
|
|
def test_restore_databases_dump_runs_mongorestore_with_schemas():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [{'name': 'foo', 'schemas': ['bar', 'baz']}]
|
2023-04-14 23:27:51 +02:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
[
|
|
|
|
'mongorestore',
|
|
|
|
'--archive',
|
|
|
|
'--drop',
|
|
|
|
'--db',
|
|
|
|
'foo',
|
|
|
|
'--nsInclude',
|
|
|
|
'bar',
|
|
|
|
'--nsInclude',
|
|
|
|
'baz',
|
|
|
|
],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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,
|
|
|
|
},
|
2023-04-14 23:27:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-26 01:00:58 +01:00
|
|
|
def test_restore_database_dump_runs_psql_for_all_database_dump():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [{'name': 'all', 'schemas': None}]
|
2021-12-26 01:00:58 +01:00
|
|
|
extract_process = flexmock(stdout=flexmock())
|
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
|
|
|
['mongorestore', '--archive'],
|
|
|
|
processes=[extract_process],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=extract_process.stdout,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_name='all',
|
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,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_with_dry_run_skips_restore():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [{'name': 'foo', 'schemas': None}]
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').never()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_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,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_restore_database_dump_without_extract_process_restores_from_disk():
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config = [{'name': 'foo', 'format': 'directory', 'schemas': None}]
|
2021-12-26 01:00:58 +01:00
|
|
|
|
|
|
|
flexmock(module).should_receive('make_dump_path')
|
|
|
|
flexmock(module.dump).should_receive('make_database_dump_filename').and_return('/dump/path')
|
|
|
|
flexmock(module).should_receive('execute_command_with_processes').with_args(
|
2023-02-21 00:18:51 +01:00
|
|
|
['mongorestore', '--dir', '/dump/path', '--drop', '--db', 'foo'],
|
2021-12-26 01:00:58 +01:00
|
|
|
processes=[],
|
|
|
|
output_log_level=logging.DEBUG,
|
|
|
|
input_file=None,
|
|
|
|
).once()
|
|
|
|
|
|
|
|
module.restore_database_dump(
|
2023-07-11 09:37:36 +02:00
|
|
|
databases_config,
|
2023-06-16 11:44:00 +02:00
|
|
|
{},
|
2023-07-09 08:14:30 +02:00
|
|
|
'test.yaml',
|
2023-07-11 09:37:36 +02:00
|
|
|
database_name='foo',
|
2023-06-16 11:44:00 +02:00
|
|
|
dry_run=False,
|
|
|
|
extract_process=None,
|
|
|
|
connection_params={
|
|
|
|
'hostname': None,
|
|
|
|
'port': None,
|
|
|
|
'username': None,
|
|
|
|
'password': None,
|
|
|
|
},
|
2021-12-26 01:00:58 +01:00
|
|
|
)
|