2023-04-01 00:21:08 +02:00
|
|
|
import pytest
|
2019-06-25 19:18:30 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.borg import flags as module
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_formats_string_value():
|
|
|
|
assert module.make_flags('foo', 'bar') == ('--foo', 'bar')
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_formats_integer_value():
|
|
|
|
assert module.make_flags('foo', 3) == ('--foo', '3')
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_formats_true_value():
|
|
|
|
assert module.make_flags('foo', True) == ('--foo',)
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_omits_false_value():
|
|
|
|
assert module.make_flags('foo', False) == ()
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_formats_name_with_underscore():
|
|
|
|
assert module.make_flags('posix_me_harder', 'okay') == ('--posix-me-harder', 'okay')
|
|
|
|
|
|
|
|
|
2019-06-25 20:04:10 +02:00
|
|
|
def test_make_flags_from_arguments_flattens_and_sorts_multiple_arguments():
|
2019-06-25 19:18:30 +02:00
|
|
|
flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
|
|
|
|
flexmock(module).should_receive('make_flags').with_args('baz', 'quux').and_return(
|
|
|
|
('baz', 'quux')
|
|
|
|
)
|
|
|
|
arguments = flexmock(foo='bar', baz='quux')
|
|
|
|
|
2019-06-25 20:04:10 +02:00
|
|
|
assert module.make_flags_from_arguments(arguments) == ('baz', 'quux', 'foo', 'bar')
|
2019-06-25 19:18:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_from_arguments_excludes_underscored_argument_names():
|
|
|
|
flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
|
|
|
|
arguments = flexmock(foo='bar', _baz='quux')
|
|
|
|
|
|
|
|
assert module.make_flags_from_arguments(arguments) == ('foo', 'bar')
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_flags_from_arguments_omits_excludes():
|
|
|
|
flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
|
|
|
|
arguments = flexmock(foo='bar', baz='quux')
|
|
|
|
|
|
|
|
assert module.make_flags_from_arguments(arguments, excludes=('baz', 'other')) == ('foo', 'bar')
|
2022-08-14 07:07:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_repository_flags_with_borg_features_includes_repo_flag():
|
|
|
|
flexmock(module.feature).should_receive('available').and_return(True)
|
|
|
|
|
2023-03-26 20:22:25 +02:00
|
|
|
assert module.make_repository_flags(repository_path='repo', local_borg_version='1.2.3') == (
|
2022-08-14 07:07:15 +02:00
|
|
|
'--repo',
|
|
|
|
'repo',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_repository_flags_without_borg_features_includes_omits_flag():
|
|
|
|
flexmock(module.feature).should_receive('available').and_return(False)
|
|
|
|
|
2023-03-26 20:22:25 +02:00
|
|
|
assert module.make_repository_flags(repository_path='repo', local_borg_version='1.2.3') == (
|
|
|
|
'repo',
|
|
|
|
)
|
2022-08-14 07:50:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_repository_archive_flags_with_borg_features_separates_repository_and_archive():
|
|
|
|
flexmock(module.feature).should_receive('available').and_return(True)
|
|
|
|
|
|
|
|
assert module.make_repository_archive_flags(
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo', archive='archive', local_borg_version='1.2.3'
|
2023-04-15 04:35:24 +02:00
|
|
|
) == (
|
|
|
|
'--repo',
|
|
|
|
'repo',
|
|
|
|
'archive',
|
|
|
|
)
|
2022-08-14 07:50:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_repository_archive_flags_with_borg_features_joins_repository_and_archive():
|
|
|
|
flexmock(module.feature).should_receive('available').and_return(False)
|
|
|
|
|
|
|
|
assert module.make_repository_archive_flags(
|
2023-03-26 20:22:25 +02:00
|
|
|
repository_path='repo', archive='archive', local_borg_version='1.2.3'
|
2022-08-14 07:50:14 +02:00
|
|
|
) == ('repo::archive',)
|
2023-04-01 00:21:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-08-02 19:23:22 +02:00
|
|
|
'match_archives,archive_name_format,feature_available,expected_result',
|
2023-04-01 00:21:08 +02:00
|
|
|
(
|
2023-04-02 08:57:55 +02:00
|
|
|
(None, None, True, ()),
|
|
|
|
(None, '', True, ()),
|
2023-04-15 04:35:24 +02:00
|
|
|
(
|
|
|
|
're:foo-.*',
|
2023-08-02 19:23:22 +02:00
|
|
|
'{hostname}-{now}', # noqa: FS003
|
2023-04-15 04:35:24 +02:00
|
|
|
True,
|
|
|
|
('--match-archives', 're:foo-.*'),
|
2023-08-02 19:23:22 +02:00
|
|
|
),
|
2023-04-15 04:35:24 +02:00
|
|
|
(
|
|
|
|
'sh:foo-*',
|
2023-08-02 19:23:22 +02:00
|
|
|
'{hostname}-{now}', # noqa: FS003
|
2023-04-15 04:35:24 +02:00
|
|
|
False,
|
|
|
|
('--glob-archives', 'foo-*'),
|
2023-08-02 19:23:22 +02:00
|
|
|
),
|
2023-04-15 04:35:24 +02:00
|
|
|
(
|
|
|
|
'foo-*',
|
2023-08-02 19:23:22 +02:00
|
|
|
'{hostname}-{now}', # noqa: FS003
|
2023-04-15 04:35:24 +02:00
|
|
|
False,
|
|
|
|
('--glob-archives', 'foo-*'),
|
2023-08-02 19:23:22 +02:00
|
|
|
),
|
2023-04-01 00:21:08 +02:00
|
|
|
(
|
2023-04-02 08:57:55 +02:00
|
|
|
None,
|
2023-04-01 00:21:08 +02:00
|
|
|
'{hostname}-docs-{now}', # noqa: FS003
|
|
|
|
True,
|
|
|
|
('--match-archives', 'sh:{hostname}-docs-*'), # noqa: FS003
|
|
|
|
),
|
|
|
|
(
|
2023-04-02 08:57:55 +02:00
|
|
|
None,
|
|
|
|
'{utcnow}-docs-{user}', # noqa: FS003
|
|
|
|
True,
|
|
|
|
('--match-archives', 'sh:*-docs-{user}'), # noqa: FS003
|
|
|
|
),
|
|
|
|
(None, '{fqdn}-{pid}', True, ('--match-archives', 'sh:{fqdn}-*')), # noqa: FS003
|
|
|
|
(
|
|
|
|
None,
|
2023-04-01 00:21:08 +02:00
|
|
|
'stuff-{now:%Y-%m-%dT%H:%M:%S.%f}', # noqa: FS003
|
|
|
|
True,
|
|
|
|
('--match-archives', 'sh:stuff-*'),
|
|
|
|
),
|
2023-04-02 08:57:55 +02:00
|
|
|
(
|
|
|
|
None,
|
|
|
|
'{hostname}-docs-{now}', # noqa: FS003
|
|
|
|
False,
|
|
|
|
('--glob-archives', '{hostname}-docs-*'), # noqa: FS003
|
|
|
|
),
|
2023-08-02 19:23:22 +02:00
|
|
|
(
|
|
|
|
None,
|
|
|
|
'{now}', # noqa: FS003
|
|
|
|
False,
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
None,
|
|
|
|
'{now}', # noqa: FS003
|
|
|
|
True,
|
|
|
|
(),
|
|
|
|
),
|
2023-04-02 08:57:55 +02:00
|
|
|
(None, '{utcnow}-docs-{user}', False, ('--glob-archives', '*-docs-{user}')), # noqa: FS003
|
2023-04-01 00:21:08 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_make_match_archives_flags_makes_flags_with_globs(
|
2023-04-02 08:57:55 +02:00
|
|
|
match_archives, archive_name_format, feature_available, expected_result
|
2023-04-01 00:21:08 +02:00
|
|
|
):
|
|
|
|
flexmock(module.feature).should_receive('available').and_return(feature_available)
|
|
|
|
|
|
|
|
assert (
|
2023-04-02 08:57:55 +02:00
|
|
|
module.make_match_archives_flags(
|
|
|
|
match_archives, archive_name_format, local_borg_version=flexmock()
|
|
|
|
)
|
2023-04-01 00:21:08 +02:00
|
|
|
== expected_result
|
|
|
|
)
|