2014-12-07 03:35:20 +01:00
|
|
|
from collections import OrderedDict
|
2015-09-03 07:48:07 +02:00
|
|
|
import os
|
2014-12-07 03:35:20 +01:00
|
|
|
|
2014-11-18 07:19:34 +01:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
2015-07-19 03:35:29 +02:00
|
|
|
from atticmatic.backends import shared as module
|
2015-06-14 20:00:46 +02:00
|
|
|
from atticmatic.tests.builtins import builtins_mock
|
2015-07-18 06:58:50 +02:00
|
|
|
from atticmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
2014-11-18 07:19:34 +01:00
|
|
|
|
|
|
|
|
2015-09-03 07:48:07 +02:00
|
|
|
def test_initialize_with_passphrase_should_set_environment():
|
|
|
|
orig_environ = os.environ
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.environ = {}
|
|
|
|
module.initialize({'encryption_passphrase': 'pass'}, command='attic')
|
|
|
|
assert os.environ.get('ATTIC_PASSPHRASE') == 'pass'
|
|
|
|
finally:
|
|
|
|
os.environ = orig_environ
|
|
|
|
|
|
|
|
|
|
|
|
def test_initialize_without_passphrase_should_not_set_environment():
|
|
|
|
orig_environ = os.environ
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.environ = {}
|
|
|
|
module.initialize({}, command='attic')
|
|
|
|
assert os.environ.get('ATTIC_PASSPHRASE') == None
|
|
|
|
finally:
|
|
|
|
os.environ = orig_environ
|
|
|
|
|
|
|
|
|
2015-03-15 18:44:18 +01:00
|
|
|
def insert_subprocess_mock(check_call_command, **kwargs):
|
2014-11-18 07:19:34 +01:00
|
|
|
subprocess = flexmock()
|
2015-03-15 18:44:18 +01:00
|
|
|
subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
|
2014-11-18 07:19:34 +01:00
|
|
|
flexmock(module).subprocess = subprocess
|
|
|
|
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
def insert_subprocess_never():
|
|
|
|
subprocess = flexmock()
|
|
|
|
subprocess.should_receive('check_call').never()
|
|
|
|
flexmock(module).subprocess = subprocess
|
|
|
|
|
|
|
|
|
2014-11-18 07:19:34 +01:00
|
|
|
def insert_platform_mock():
|
2015-06-14 20:00:46 +02:00
|
|
|
flexmock(module.platform).should_receive('node').and_return('host')
|
2014-11-18 07:19:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def insert_datetime_mock():
|
|
|
|
flexmock(module).datetime = flexmock().should_receive('now').and_return(
|
|
|
|
flexmock().should_receive('isoformat').and_return('now').mock
|
|
|
|
).mock
|
|
|
|
|
|
|
|
|
2015-07-27 06:06:03 +02:00
|
|
|
CREATE_COMMAND_WITHOUT_EXCLUDES = ('attic', 'create', 'repo::host-now', 'foo', 'bar')
|
|
|
|
CREATE_COMMAND = CREATE_COMMAND_WITHOUT_EXCLUDES + ('--exclude-from', 'excludes')
|
2015-07-18 06:58:50 +02:00
|
|
|
|
|
|
|
|
2014-11-18 07:19:34 +01:00
|
|
|
def test_create_archive_should_call_attic_with_parameters():
|
2015-07-18 06:58:50 +02:00
|
|
|
insert_subprocess_mock(CREATE_COMMAND)
|
2014-11-18 07:19:34 +01:00
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=None,
|
2015-09-03 07:48:07 +02:00
|
|
|
storage_config={},
|
2014-11-18 07:19:34 +01:00
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2014-11-18 07:19:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-01-31 11:42:07 +01:00
|
|
|
def test_create_archive_with_two_spaces_in_source_directories():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND)
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
|
|
|
verbosity=None,
|
|
|
|
storage_config={},
|
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
|
|
|
command='attic',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-27 06:06:03 +02:00
|
|
|
def test_create_archive_with_none_excludes_filename_should_call_attic_without_excludes():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND_WITHOUT_EXCLUDES)
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename=None,
|
|
|
|
verbosity=None,
|
2015-09-03 07:48:07 +02:00
|
|
|
storage_config={},
|
2015-07-27 06:06:03 +02:00
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
|
|
|
command='attic',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-18 06:58:50 +02:00
|
|
|
def test_create_archive_with_verbosity_some_should_call_attic_with_stats_parameter():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND + ('--stats',))
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
|
|
|
verbosity=VERBOSITY_SOME,
|
2015-09-03 07:48:07 +02:00
|
|
|
storage_config={},
|
2015-07-18 06:58:50 +02:00
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2014-11-18 07:19:34 +01:00
|
|
|
)
|
2015-07-18 06:58:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_archive_with_verbosity_lots_should_call_attic_with_verbose_parameter():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND + ('--verbose', '--stats'))
|
2014-11-18 07:19:34 +01:00
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=VERBOSITY_LOTS,
|
2015-09-03 07:48:07 +02:00
|
|
|
storage_config={},
|
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
|
|
|
command='attic',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_archive_with_compression_should_call_attic_with_compression_parameters():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND + ('--compression', 'rle'))
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
|
|
|
verbosity=None,
|
|
|
|
storage_config={'compression': 'rle'},
|
2014-11-18 07:19:34 +01:00
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2014-11-18 07:19:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-02-13 19:43:31 +01:00
|
|
|
def test_create_archive_with_one_file_system_should_call_attic_with_one_file_system_parameters():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND + ('--one-file-system',))
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
|
|
|
verbosity=None,
|
|
|
|
storage_config={},
|
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
|
|
|
command='attic',
|
|
|
|
one_file_system=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-02-13 19:59:43 +01:00
|
|
|
def test_create_archive_with_umask_should_call_attic_with_umask_parameters():
|
|
|
|
insert_subprocess_mock(CREATE_COMMAND + ('--umask', '740'))
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename='excludes',
|
|
|
|
verbosity=None,
|
|
|
|
storage_config={'umask': 740},
|
|
|
|
source_directories='foo bar',
|
|
|
|
repository='repo',
|
|
|
|
command='attic',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-01-25 23:52:16 +01:00
|
|
|
def test_create_archive_with_globs():
|
|
|
|
insert_subprocess_mock(('attic', 'create', 'repo::host-now', 'setup.py', 'setup.cfg'))
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.create_archive(
|
|
|
|
excludes_filename=None,
|
|
|
|
verbosity=None,
|
|
|
|
storage_config={},
|
|
|
|
source_directories='setup*',
|
|
|
|
repository='repo',
|
|
|
|
command='attic',
|
|
|
|
source_directories_glob=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
BASE_PRUNE_FLAGS = (
|
|
|
|
('--keep-daily', '1'),
|
|
|
|
('--keep-weekly', '2'),
|
|
|
|
('--keep-monthly', '3'),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_prune_flags_should_return_flags_from_config():
|
|
|
|
retention_config = OrderedDict(
|
|
|
|
(
|
|
|
|
('keep_daily', 1),
|
|
|
|
('keep_weekly', 2),
|
|
|
|
('keep_monthly', 3),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
result = module._make_prune_flags(retention_config)
|
2014-12-07 03:35:20 +01:00
|
|
|
|
|
|
|
assert tuple(result) == BASE_PRUNE_FLAGS
|
|
|
|
|
|
|
|
|
2015-07-18 06:58:50 +02:00
|
|
|
PRUNE_COMMAND = (
|
|
|
|
'attic', 'prune', 'repo', '--keep-daily', '1', '--keep-weekly', '2', '--keep-monthly', '3',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-11-18 07:19:34 +01:00
|
|
|
def test_prune_archives_should_call_attic_with_parameters():
|
2014-12-07 03:35:20 +01:00
|
|
|
retention_config = flexmock()
|
2015-05-11 07:00:31 +02:00
|
|
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
2014-12-07 03:35:20 +01:00
|
|
|
BASE_PRUNE_FLAGS,
|
|
|
|
)
|
2015-07-18 06:58:50 +02:00
|
|
|
insert_subprocess_mock(PRUNE_COMMAND)
|
2014-11-18 07:19:34 +01:00
|
|
|
|
|
|
|
module.prune_archives(
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=None,
|
2014-12-07 03:35:20 +01:00
|
|
|
repository='repo',
|
|
|
|
retention_config=retention_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2014-11-18 07:19:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-18 06:58:50 +02:00
|
|
|
def test_prune_archives_with_verbosity_some_should_call_attic_with_stats_parameter():
|
2014-12-07 03:35:20 +01:00
|
|
|
retention_config = flexmock()
|
2015-05-11 07:00:31 +02:00
|
|
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
2014-12-07 03:35:20 +01:00
|
|
|
BASE_PRUNE_FLAGS,
|
|
|
|
)
|
2015-07-18 06:58:50 +02:00
|
|
|
insert_subprocess_mock(PRUNE_COMMAND + ('--stats',))
|
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
repository='repo',
|
|
|
|
verbosity=VERBOSITY_SOME,
|
|
|
|
retention_config=retention_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2015-07-18 06:58:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_prune_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
|
|
|
|
retention_config = flexmock()
|
|
|
|
flexmock(module).should_receive('_make_prune_flags').with_args(retention_config).and_return(
|
|
|
|
BASE_PRUNE_FLAGS,
|
2014-11-18 07:19:34 +01:00
|
|
|
)
|
2015-07-18 06:58:50 +02:00
|
|
|
insert_subprocess_mock(PRUNE_COMMAND + ('--verbose', '--stats',))
|
2014-11-18 07:19:34 +01:00
|
|
|
|
|
|
|
module.prune_archives(
|
|
|
|
repository='repo',
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=VERBOSITY_LOTS,
|
2014-12-07 03:35:20 +01:00
|
|
|
retention_config=retention_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2014-11-18 07:19:34 +01:00
|
|
|
)
|
2015-02-14 18:23:40 +01:00
|
|
|
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
def test_parse_checks_returns_them_as_tuple():
|
|
|
|
checks = module._parse_checks({'checks': 'foo disabled bar'})
|
|
|
|
|
|
|
|
assert checks == ('foo', 'bar')
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_checks_with_missing_value_returns_defaults():
|
|
|
|
checks = module._parse_checks({})
|
|
|
|
|
|
|
|
assert checks == module.DEFAULT_CHECKS
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_checks_with_blank_value_returns_defaults():
|
|
|
|
checks = module._parse_checks({'checks': ''})
|
|
|
|
|
|
|
|
assert checks == module.DEFAULT_CHECKS
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_checks_with_disabled_returns_no_checks():
|
|
|
|
checks = module._parse_checks({'checks': 'disabled'})
|
|
|
|
|
|
|
|
assert checks == ()
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_check_flags_with_checks_returns_flags():
|
|
|
|
flags = module._make_check_flags(('foo', 'bar'))
|
|
|
|
|
|
|
|
assert flags == ('--foo-only', '--bar-only')
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_check_flags_with_default_checks_returns_no_flags():
|
|
|
|
flags = module._make_check_flags(module.DEFAULT_CHECKS)
|
|
|
|
|
|
|
|
assert flags == ()
|
|
|
|
|
|
|
|
|
2015-07-28 06:47:52 +02:00
|
|
|
def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
|
|
|
|
flags = module._make_check_flags(('foo', 'bar'), check_last=3)
|
|
|
|
|
|
|
|
assert flags == ('--foo-only', '--bar-only', '--last', 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_check_flags_with_last_returns_last_flag():
|
|
|
|
flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
|
|
|
|
|
|
|
|
assert flags == ('--last', 3)
|
|
|
|
|
|
|
|
|
2015-02-14 18:23:40 +01:00
|
|
|
def test_check_archives_should_call_attic_with_parameters():
|
2015-07-28 06:47:52 +02:00
|
|
|
checks = flexmock()
|
|
|
|
check_last = flexmock()
|
|
|
|
consistency_config = flexmock().should_receive('get').and_return(check_last).mock
|
|
|
|
flexmock(module).should_receive('_parse_checks').and_return(checks)
|
|
|
|
flexmock(module).should_receive('_make_check_flags').with_args(checks, check_last).and_return(())
|
2015-02-14 18:23:40 +01:00
|
|
|
stdout = flexmock()
|
2015-03-15 18:44:18 +01:00
|
|
|
insert_subprocess_mock(
|
2015-02-14 18:23:40 +01:00
|
|
|
('attic', 'check', 'repo'),
|
|
|
|
stdout=stdout,
|
|
|
|
)
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
2015-06-14 20:00:46 +02:00
|
|
|
builtins_mock().should_receive('open').and_return(stdout)
|
|
|
|
flexmock(module.os).should_receive('devnull')
|
2015-02-14 18:23:40 +01:00
|
|
|
|
|
|
|
module.check_archives(
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=None,
|
|
|
|
repository='repo',
|
|
|
|
consistency_config=consistency_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2015-07-18 06:58:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_archives_with_verbosity_some_should_call_attic_with_verbose_parameter():
|
2015-07-28 06:47:52 +02:00
|
|
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
2015-07-18 06:58:50 +02:00
|
|
|
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
|
|
flexmock(module).should_receive('_make_check_flags').and_return(())
|
|
|
|
insert_subprocess_mock(
|
|
|
|
('attic', 'check', 'repo', '--verbose'),
|
|
|
|
stdout=None,
|
|
|
|
)
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.check_archives(
|
|
|
|
verbosity=VERBOSITY_SOME,
|
2015-02-14 18:23:40 +01:00
|
|
|
repository='repo',
|
2015-05-11 07:00:31 +02:00
|
|
|
consistency_config=consistency_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2015-02-14 18:23:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-18 06:58:50 +02:00
|
|
|
def test_check_archives_with_verbosity_lots_should_call_attic_with_verbose_parameter():
|
2015-07-28 06:47:52 +02:00
|
|
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
2015-05-11 07:00:31 +02:00
|
|
|
flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
|
|
flexmock(module).should_receive('_make_check_flags').and_return(())
|
2015-03-15 18:44:18 +01:00
|
|
|
insert_subprocess_mock(
|
2015-02-14 18:23:40 +01:00
|
|
|
('attic', 'check', 'repo', '--verbose'),
|
|
|
|
stdout=None,
|
|
|
|
)
|
|
|
|
insert_platform_mock()
|
|
|
|
insert_datetime_mock()
|
|
|
|
|
|
|
|
module.check_archives(
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=VERBOSITY_LOTS,
|
2015-02-14 18:23:40 +01:00
|
|
|
repository='repo',
|
2015-05-11 07:00:31 +02:00
|
|
|
consistency_config=consistency_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2015-02-14 18:23:40 +01:00
|
|
|
)
|
2015-05-11 07:00:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_archives_without_any_checks_should_bail():
|
2015-07-28 06:47:52 +02:00
|
|
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
2015-05-11 07:00:31 +02:00
|
|
|
flexmock(module).should_receive('_parse_checks').and_return(())
|
|
|
|
insert_subprocess_never()
|
|
|
|
|
|
|
|
module.check_archives(
|
2015-07-18 06:58:50 +02:00
|
|
|
verbosity=None,
|
2015-05-11 07:00:31 +02:00
|
|
|
repository='repo',
|
|
|
|
consistency_config=consistency_config,
|
2015-07-19 03:35:29 +02:00
|
|
|
command='attic',
|
2015-05-11 07:00:31 +02:00
|
|
|
)
|