2018-12-11 07:20:57 +01:00
|
|
|
import logging
|
2019-06-13 05:56:20 +02:00
|
|
|
import subprocess
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-06-13 19:01:55 +02:00
|
|
|
import pytest
|
2018-12-11 07:20:57 +01:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.borg import init as module
|
|
|
|
|
2019-05-13 23:39:10 +02:00
|
|
|
from ..test_verbosity import insert_logging_mock
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-06-13 19:01:55 +02:00
|
|
|
INFO_SOME_UNKNOWN_EXIT_CODE = -999
|
2019-09-13 00:27:04 +02:00
|
|
|
INIT_COMMAND = ('borg', 'init', '--encryption', 'repokey')
|
2018-12-25 07:28:02 +01:00
|
|
|
|
|
|
|
|
2019-06-13 05:56:20 +02:00
|
|
|
def insert_info_command_found_mock():
|
2022-05-27 22:51:11 +02:00
|
|
|
flexmock(module.info).should_receive('display_archives_info')
|
2019-06-13 05:56:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def insert_info_command_not_found_mock():
|
2022-05-27 22:51:11 +02:00
|
|
|
flexmock(module.info).should_receive('display_archives_info').and_raise(
|
2019-06-13 19:01:55 +02:00
|
|
|
subprocess.CalledProcessError(module.INFO_REPOSITORY_NOT_FOUND_EXIT_CODE, [])
|
2019-06-13 05:56:20 +02:00
|
|
|
)
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
2018-12-25 07:28:02 +01:00
|
|
|
def insert_init_command_mock(init_command, **kwargs):
|
2022-06-30 22:42:17 +02:00
|
|
|
flexmock(module.environment).should_receive('make_environment')
|
2020-05-10 06:53:16 +02:00
|
|
|
flexmock(module).should_receive('execute_command').with_args(
|
2022-06-30 22:42:17 +02:00
|
|
|
init_command,
|
|
|
|
output_file=module.DO_NOT_CAPTURE,
|
|
|
|
borg_local_path=init_command[0],
|
|
|
|
extra_environment=None,
|
2019-06-13 05:56:20 +02:00
|
|
|
).once()
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_calls_borg_with_parameters():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(INIT_COMMAND + ('repo',))
|
2018-12-25 07:28:02 +01:00
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
|
2018-12-25 07:28:02 +01:00
|
|
|
|
|
|
|
|
2019-08-04 00:13:54 +02:00
|
|
|
def test_initialize_repository_raises_for_borg_init_error():
|
|
|
|
insert_info_command_not_found_mock()
|
2022-06-30 22:42:17 +02:00
|
|
|
flexmock(module.environment).should_receive('make_environment')
|
2020-05-10 06:53:16 +02:00
|
|
|
flexmock(module).should_receive('execute_command').and_raise(
|
2019-08-04 00:13:54 +02:00
|
|
|
module.subprocess.CalledProcessError(2, 'borg init')
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(subprocess.CalledProcessError):
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo', storage_config={}, encryption_mode='repokey'
|
|
|
|
)
|
2019-08-04 00:13:54 +02:00
|
|
|
|
|
|
|
|
2018-12-25 07:28:02 +01:00
|
|
|
def test_initialize_repository_skips_initialization_when_repository_already_exists():
|
2022-05-27 22:51:11 +02:00
|
|
|
insert_info_command_found_mock()
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
2019-06-13 19:01:55 +02:00
|
|
|
def test_initialize_repository_raises_for_unknown_info_command_error():
|
2022-05-27 22:51:11 +02:00
|
|
|
flexmock(module.info).should_receive('display_archives_info').and_raise(
|
2019-06-13 19:01:55 +02:00
|
|
|
subprocess.CalledProcessError(INFO_SOME_UNKNOWN_EXIT_CODE, [])
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(subprocess.CalledProcessError):
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo', storage_config={}, encryption_mode='repokey'
|
|
|
|
)
|
2019-06-13 19:01:55 +02:00
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_initialize_repository_with_append_only_calls_borg_with_append_only_parameter():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(INIT_COMMAND + ('--append-only', 'repo'))
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo', storage_config={}, encryption_mode='repokey', append_only=True
|
|
|
|
)
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_with_storage_quota_calls_borg_with_storage_quota_parameter():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(INIT_COMMAND + ('--storage-quota', '5G', 'repo'))
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo', storage_config={}, encryption_mode='repokey', storage_quota='5G'
|
|
|
|
)
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_with_log_info_calls_borg_with_info_parameter():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(INIT_COMMAND + ('--info', 'repo'))
|
2018-12-11 07:20:57 +01:00
|
|
|
insert_logging_mock(logging.INFO)
|
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_with_log_debug_calls_borg_with_debug_parameter():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(INIT_COMMAND + ('--debug', 'repo'))
|
2018-12-11 07:20:57 +01:00
|
|
|
insert_logging_mock(logging.DEBUG)
|
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(repository='repo', storage_config={}, encryption_mode='repokey')
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_with_local_path_calls_borg_via_local_path():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(('borg1',) + INIT_COMMAND[1:] + ('repo',))
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo', storage_config={}, encryption_mode='repokey', local_path='borg1'
|
|
|
|
)
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_with_remote_path_calls_borg_with_remote_path_parameter():
|
2019-06-13 05:56:20 +02:00
|
|
|
insert_info_command_not_found_mock()
|
2019-09-13 00:27:04 +02:00
|
|
|
insert_init_command_mock(INIT_COMMAND + ('--remote-path', 'borg1', 'repo'))
|
2018-12-11 07:20:57 +01:00
|
|
|
|
2019-12-05 00:48:10 +01:00
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo', storage_config={}, encryption_mode='repokey', remote_path='borg1'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_repository_with_extra_borg_options_calls_borg_with_extra_options():
|
|
|
|
insert_info_command_not_found_mock()
|
|
|
|
insert_init_command_mock(INIT_COMMAND + ('--extra', '--options', 'repo'))
|
|
|
|
|
|
|
|
module.initialize_repository(
|
|
|
|
repository='repo',
|
|
|
|
storage_config={'extra_borg_options': {'init': '--extra --options'}},
|
|
|
|
encryption_mode='repokey',
|
|
|
|
)
|