2018-12-26 06:01:08 +01:00
|
|
|
import subprocess
|
|
|
|
|
2016-04-11 00:59:36 +02:00
|
|
|
import pytest
|
2019-05-13 23:39:10 +02:00
|
|
|
from flexmock import flexmock
|
2014-12-20 20:37:25 +01:00
|
|
|
|
2017-07-09 07:33:51 +02:00
|
|
|
from borgmatic.commands import borgmatic as module
|
2015-07-19 03:35:29 +02:00
|
|
|
|
|
|
|
|
2014-12-20 20:37:25 +01:00
|
|
|
def test_parse_arguments_with_no_arguments_uses_defaults():
|
2018-07-25 03:34:05 +02:00
|
|
|
config_paths = ['default']
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
|
|
|
|
|
2016-06-10 20:21:53 +02:00
|
|
|
parser = module.parse_arguments()
|
2014-12-20 20:37:25 +01:00
|
|
|
|
2018-07-25 03:34:05 +02:00
|
|
|
assert parser.config_paths == config_paths
|
2018-09-30 08:15:18 +02:00
|
|
|
assert parser.excludes_filename is None
|
2018-09-18 07:31:27 +02:00
|
|
|
assert parser.verbosity is 0
|
2018-07-28 23:21:38 +02:00
|
|
|
assert parser.json is False
|
2014-12-20 20:37:25 +01:00
|
|
|
|
|
|
|
|
2017-07-26 06:18:51 +02:00
|
|
|
def test_parse_arguments_with_multiple_config_paths_parses_as_list():
|
2018-07-25 03:34:05 +02:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2017-07-26 06:18:51 +02:00
|
|
|
parser = module.parse_arguments('--config', 'myconfig', 'otherconfig')
|
|
|
|
|
|
|
|
assert parser.config_paths == ['myconfig', 'otherconfig']
|
2018-09-18 07:31:27 +02:00
|
|
|
assert parser.verbosity is 0
|
2017-07-26 06:18:51 +02:00
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_with_verbosity_overrides_default():
|
2018-07-25 03:34:05 +02:00
|
|
|
config_paths = ['default']
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(config_paths)
|
|
|
|
|
2016-06-10 20:21:53 +02:00
|
|
|
parser = module.parse_arguments('--verbosity', '1')
|
2014-12-20 20:37:25 +01:00
|
|
|
|
2018-07-25 03:34:05 +02:00
|
|
|
assert parser.config_paths == config_paths
|
2018-09-30 08:15:18 +02:00
|
|
|
assert parser.excludes_filename is None
|
2015-07-18 06:58:50 +02:00
|
|
|
assert parser.verbosity == 1
|
2014-12-20 20:37:25 +01:00
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_with_json_overrides_default():
|
2018-07-28 23:21:38 +02:00
|
|
|
parser = module.parse_arguments('--list', '--json')
|
|
|
|
assert parser.json is True
|
|
|
|
|
|
|
|
|
2017-07-29 07:02:18 +02:00
|
|
|
def test_parse_arguments_with_no_actions_defaults_to_all_actions_enabled():
|
2018-07-25 03:34:05 +02:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2017-07-29 07:02:18 +02:00
|
|
|
parser = module.parse_arguments()
|
|
|
|
|
|
|
|
assert parser.prune is True
|
|
|
|
assert parser.create is True
|
|
|
|
assert parser.check is True
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_with_prune_action_leaves_other_actions_disabled():
|
2018-07-25 03:34:05 +02:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2017-07-29 07:02:18 +02:00
|
|
|
parser = module.parse_arguments('--prune')
|
|
|
|
|
|
|
|
assert parser.prune is True
|
|
|
|
assert parser.create is False
|
|
|
|
assert parser.check is False
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_with_multiple_actions_leaves_other_action_disabled():
|
2018-07-25 03:34:05 +02:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2017-07-29 07:02:18 +02:00
|
|
|
parser = module.parse_arguments('--create', '--check')
|
|
|
|
|
|
|
|
assert parser.prune is False
|
|
|
|
assert parser.create is True
|
|
|
|
assert parser.check is True
|
|
|
|
|
|
|
|
|
2014-12-20 20:37:25 +01:00
|
|
|
def test_parse_arguments_with_invalid_arguments_exits():
|
2018-07-25 03:34:05 +02:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2017-07-10 18:43:25 +02:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
module.parse_arguments('--posix-me-harder')
|
2018-07-28 23:21:38 +02:00
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_disallows_deprecated_excludes_option():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--excludes', 'myexcludes')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_disallows_encryption_mode_without_init():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--encryption', 'repokey')
|
|
|
|
|
|
|
|
|
2019-02-18 18:52:56 +01:00
|
|
|
def test_parse_arguments_allows_encryption_mode_with_init():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey')
|
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_requires_encryption_mode_with_init():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--init')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_disallows_append_only_without_init():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--append-only')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_disallows_storage_quota_without_init():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--storage-quota', '5G')
|
|
|
|
|
|
|
|
|
2018-12-25 07:28:02 +01:00
|
|
|
def test_parse_arguments_allows_init_and_prune():
|
2018-12-11 07:20:57 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-25 07:28:02 +01:00
|
|
|
module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--prune')
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
2018-12-25 07:28:02 +01:00
|
|
|
def test_parse_arguments_allows_init_and_create():
|
2018-12-11 07:20:57 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-25 07:28:02 +01:00
|
|
|
module.parse_arguments('--config', 'myconfig', '--init', '--encryption', 'repokey', '--create')
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_disallows_init_and_dry_run():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2018-12-25 07:28:02 +01:00
|
|
|
module.parse_arguments(
|
|
|
|
'--config', 'myconfig', '--init', '--encryption', 'repokey', '--dry-run'
|
|
|
|
)
|
2018-12-11 07:20:57 +01:00
|
|
|
|
|
|
|
|
2019-02-24 08:02:17 +01:00
|
|
|
def test_parse_arguments_disallows_repository_without_extract_or_list():
|
2019-02-18 18:52:56 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg')
|
|
|
|
|
|
|
|
|
2019-02-24 08:02:17 +01:00
|
|
|
def test_parse_arguments_allows_repository_with_extract():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
module.parse_arguments(
|
|
|
|
'--config', 'myconfig', '--extract', '--repository', 'test.borg', '--archive', 'test'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_allows_repository_with_list():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--list', '--repository', 'test.borg')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_disallows_archive_without_extract_or_list():
|
2019-02-18 18:52:56 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--archive', 'test')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_disallows_restore_paths_without_extract():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--restore-path', 'test')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_allows_archive_with_extract():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--extract', '--archive', 'test')
|
|
|
|
|
|
|
|
|
2019-02-24 08:02:17 +01:00
|
|
|
def test_parse_arguments_allows_archive_with_list():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--list', '--archive', 'test')
|
|
|
|
|
|
|
|
|
2019-02-18 18:52:56 +01:00
|
|
|
def test_parse_arguments_requires_archive_with_extract():
|
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--config', 'myconfig', '--extract')
|
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_allows_progress_and_create():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-03 00:08:42 +01:00
|
|
|
module.parse_arguments('--progress', '--create', '--list')
|
|
|
|
|
|
|
|
|
2019-02-18 18:52:56 +01:00
|
|
|
def test_parse_arguments_allows_progress_and_extract():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2019-02-18 18:52:56 +01:00
|
|
|
module.parse_arguments('--progress', '--extract', '--archive', 'test', '--list')
|
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_disallows_progress_without_create():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-03 00:08:42 +01:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--progress', '--list')
|
|
|
|
|
|
|
|
|
2018-12-06 23:58:14 +01:00
|
|
|
def test_parse_arguments_with_stats_and_create_flags_does_not_raise():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-06 23:58:14 +01:00
|
|
|
module.parse_arguments('--stats', '--create', '--list')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_with_stats_and_prune_flags_does_not_raise():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-06 23:58:14 +01:00
|
|
|
module.parse_arguments('--stats', '--prune', '--list')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_arguments_with_stats_flag_but_no_create_or_prune_flag_raises_value_error():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-06 23:58:14 +01:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--stats', '--list')
|
|
|
|
|
|
|
|
|
2019-01-27 21:15:47 +01:00
|
|
|
def test_parse_arguments_with_just_stats_flag_does_not_raise():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2019-01-27 21:15:47 +01:00
|
|
|
module.parse_arguments('--stats')
|
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_allows_json_with_list_or_info():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-07-29 05:17:45 +02:00
|
|
|
module.parse_arguments('--list', '--json')
|
|
|
|
module.parse_arguments('--info', '--json')
|
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_disallows_json_without_list_or_info():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-07-28 23:21:38 +02:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--json')
|
2018-07-29 05:17:45 +02:00
|
|
|
|
|
|
|
|
2018-12-11 07:20:57 +01:00
|
|
|
def test_parse_arguments_disallows_json_with_both_list_and_info():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-07-29 05:17:45 +02:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.parse_arguments('--list', '--info', '--json')
|
2018-12-26 06:01:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_borgmatic_version_matches_news_version():
|
2019-02-24 08:02:17 +01:00
|
|
|
flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default'])
|
|
|
|
|
2018-12-26 06:01:08 +01:00
|
|
|
borgmatic_version = subprocess.check_output(('borgmatic', '--version')).decode('ascii')
|
|
|
|
news_version = open('NEWS').readline()
|
|
|
|
|
|
|
|
assert borgmatic_version == news_version
|