borg/attic/testsuite/archiver.py

460 lines
21 KiB
Python
Raw Normal View History

2010-10-16 09:45:36 +00:00
import os
2013-06-24 20:41:05 +00:00
from io import StringIO
2012-12-09 22:06:33 +00:00
import stat
2013-07-25 20:54:19 +00:00
import subprocess
2010-10-30 11:44:25 +00:00
import sys
2010-10-16 09:45:36 +00:00
import shutil
import tempfile
2013-07-25 20:54:19 +00:00
import time
import unittest
from hashlib import sha256
2013-07-08 21:38:27 +00:00
from attic import xattr
from attic.archive import Archive, ChunkBuffer
2013-07-08 21:38:27 +00:00
from attic.archiver import Archiver
from attic.crypto import bytes_to_long, num_aes_blocks
from attic.helpers import Manifest
from attic.remote import RemoteRepository, PathNotAllowed
2013-07-08 21:38:27 +00:00
from attic.repository import Repository
from attic.testsuite import AtticTestCase
from attic.testsuite.mock import patch
2010-10-16 09:45:36 +00:00
2013-07-25 20:54:19 +00:00
try:
import llfuse
has_llfuse = True
except ImportError:
has_llfuse = False
has_lchflags = hasattr(os, 'lchflags')
src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__), '..')
2010-10-16 09:45:36 +00:00
2013-07-25 20:54:19 +00:00
2013-06-30 20:32:27 +00:00
class changedir:
def __init__(self, dir):
self.dir = dir
def __enter__(self):
self.old = os.getcwd()
os.chdir(self.dir)
def __exit__(self, *args, **kw):
os.chdir(self.old)
2013-06-24 20:41:05 +00:00
class ArchiverTestCaseBase(AtticTestCase):
2010-10-16 09:45:36 +00:00
prefix = ''
2010-10-16 09:45:36 +00:00
def setUp(self):
os.environ['ATTIC_CHECK_I_KNOW_WHAT_I_AM_DOING'] = '1'
2010-10-16 09:45:36 +00:00
self.archiver = Archiver()
self.tmpdir = tempfile.mkdtemp()
2013-06-20 10:44:58 +00:00
self.repository_path = os.path.join(self.tmpdir, 'repository')
self.repository_location = self.prefix + self.repository_path
2010-10-31 19:12:32 +00:00
self.input_path = os.path.join(self.tmpdir, 'input')
self.output_path = os.path.join(self.tmpdir, 'output')
2011-08-06 11:01:58 +00:00
self.keys_path = os.path.join(self.tmpdir, 'keys')
self.cache_path = os.path.join(self.tmpdir, 'cache')
2014-02-08 17:44:48 +00:00
self.exclude_file_path = os.path.join(self.tmpdir, 'excludes')
2013-07-08 21:38:27 +00:00
os.environ['ATTIC_KEYS_DIR'] = self.keys_path
os.environ['ATTIC_CACHE_DIR'] = self.cache_path
2010-10-31 19:12:32 +00:00
os.mkdir(self.input_path)
os.mkdir(self.output_path)
2011-08-06 11:01:58 +00:00
os.mkdir(self.keys_path)
os.mkdir(self.cache_path)
2014-02-08 17:44:48 +00:00
with open(self.exclude_file_path, 'wb') as fd:
fd.write(b'input/file2\n# A commment line, then a blank line\n\n')
2013-06-27 11:28:59 +00:00
self._old_wd = os.getcwd()
2010-10-31 19:12:32 +00:00
os.chdir(self.tmpdir)
2010-10-16 09:45:36 +00:00
def tearDown(self):
shutil.rmtree(self.tmpdir)
2013-06-27 11:28:59 +00:00
os.chdir(self._old_wd)
2010-10-16 09:45:36 +00:00
2013-07-25 20:54:19 +00:00
def attic(self, *args, **kw):
exit_code = kw.get('exit_code', 0)
fork = kw.get('fork', False)
if fork:
try:
output = subprocess.check_output((sys.executable, '-m', 'attic.archiver') + args)
ret = 0
except subprocess.CalledProcessError as e:
output = e.output
ret = e.returncode
output = os.fsdecode(output)
if ret != exit_code:
print(output)
self.assert_equal(exit_code, ret)
return output
2011-08-06 11:01:58 +00:00
args = list(args)
2013-10-18 19:00:20 +00:00
stdout, stderr = sys.stdout, sys.stderr
2010-10-30 11:44:25 +00:00
try:
output = StringIO()
sys.stdout = sys.stderr = output
2010-10-31 19:12:32 +00:00
ret = self.archiver.run(args)
sys.stdout, sys.stderr = stdout, stderr
if ret != exit_code:
2013-06-03 11:45:48 +00:00
print(output.getvalue())
2013-06-24 20:41:05 +00:00
self.assert_equal(exit_code, ret)
2010-10-30 11:44:25 +00:00
return output.getvalue()
finally:
sys.stdout, sys.stderr = stdout, stderr
2010-10-16 09:45:36 +00:00
def create_src_archive(self, name):
2013-07-08 21:38:27 +00:00
self.attic('create', self.repository_location + '::' + name, src_dir)
2010-10-16 09:45:36 +00:00
class ArchiverTestCase(ArchiverTestCaseBase):
def create_regular_file(self, name, size=0, contents=None):
2010-10-31 19:12:32 +00:00
filename = os.path.join(self.input_path, name)
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
2013-06-03 11:45:48 +00:00
with open(filename, 'wb') as fd:
if contents is None:
contents = b'X' * size
fd.write(contents)
2010-10-31 19:12:32 +00:00
2013-07-25 20:54:19 +00:00
def create_test_files(self):
"""Create a minimal test case including all supported file types
"""
2012-12-09 22:06:33 +00:00
# File
self.create_regular_file('empty', size=0)
# 2600-01-01 > 2**64 ns
os.utime('input/empty', (19880895600, 19880895600))
self.create_regular_file('file1', size=1024 * 80)
self.create_regular_file('flagfile', size=1024)
2012-12-09 22:06:33 +00:00
# Directory
self.create_regular_file('dir2/file2', size=1024 * 80)
2012-12-09 22:06:33 +00:00
# File owner
os.chown('input/file1', 100, 200)
# File mode
2013-06-03 11:45:48 +00:00
os.chmod('input/file1', 0o7755)
2013-07-27 12:31:28 +00:00
os.chmod('input/dir2', 0o555)
2012-12-09 22:06:33 +00:00
# Block device
2013-06-03 11:45:48 +00:00
os.mknod('input/bdev', 0o600 | stat.S_IFBLK, os.makedev(10, 20))
2012-12-09 22:06:33 +00:00
# Char device
2013-06-03 11:45:48 +00:00
os.mknod('input/cdev', 0o600 | stat.S_IFCHR, os.makedev(30, 40))
2012-12-09 22:06:33 +00:00
# Hard link
os.link(os.path.join(self.input_path, 'file1'),
os.path.join(self.input_path, 'hardlink'))
2012-12-09 22:06:33 +00:00
# Symlink
2010-10-31 19:12:32 +00:00
os.symlink('somewhere', os.path.join(self.input_path, 'link1'))
if xattr.is_enabled():
xattr.setxattr(os.path.join(self.input_path, 'file1'), 'user.foo', b'bar')
xattr.setxattr(os.path.join(self.input_path, 'link1'), 'user.foo_symlink', b'bar_symlink', follow_symlinks=False)
2012-12-09 22:06:33 +00:00
# FIFO node
2010-10-31 19:12:32 +00:00
os.mkfifo(os.path.join(self.input_path, 'fifo1'))
if has_lchflags:
os.lchflags(os.path.join(self.input_path, 'flagfile'), stat.UF_NODUMP)
2013-07-25 20:54:19 +00:00
def test_basic_functionality(self):
self.create_test_files()
2013-07-08 21:38:27 +00:00
self.attic('init', self.repository_location)
self.attic('create', self.repository_location + '::test', 'input')
self.attic('create', self.repository_location + '::test.2', 'input')
2013-06-30 20:32:27 +00:00
with changedir('output'):
2013-07-08 21:38:27 +00:00
self.attic('extract', self.repository_location + '::test')
self.assert_equal(len(self.attic('list', self.repository_location).splitlines()), 2)
self.assert_equal(len(self.attic('list', self.repository_location + '::test').splitlines()), 11)
2013-07-26 11:18:57 +00:00
self.assert_dirs_equal('input', 'output/input')
2013-07-08 21:38:27 +00:00
info_output = self.attic('info', self.repository_location + '::test')
shutil.rmtree(self.cache_path)
2013-07-08 21:38:27 +00:00
info_output2 = self.attic('info', self.repository_location + '::test')
# info_output2 starts with some "initializing cache" text but should
# end the same way as info_output
assert info_output2.endswith(info_output)
2010-10-16 09:45:36 +00:00
2013-06-22 11:33:21 +00:00
def test_extract_include_exclude(self):
2013-07-08 21:38:27 +00:00
self.attic('init', self.repository_location)
self.create_regular_file('file1', size=1024 * 80)
self.create_regular_file('file2', size=1024 * 80)
self.create_regular_file('file3', size=1024 * 80)
self.create_regular_file('file4', size=1024 * 80)
2013-07-08 21:38:27 +00:00
self.attic('create', '--exclude=input/file4', self.repository_location + '::test', 'input')
2013-06-30 20:32:27 +00:00
with changedir('output'):
2013-07-08 21:38:27 +00:00
self.attic('extract', self.repository_location + '::test', 'input/file1', )
2013-06-24 20:41:05 +00:00
self.assert_equal(sorted(os.listdir('output/input')), ['file1'])
2013-06-30 20:32:27 +00:00
with changedir('output'):
2013-07-08 21:38:27 +00:00
self.attic('extract', '--exclude=input/file2', self.repository_location + '::test')
2013-06-24 20:41:05 +00:00
self.assert_equal(sorted(os.listdir('output/input')), ['file1', 'file3'])
2014-02-08 17:44:48 +00:00
with changedir('output'):
self.attic('extract', '--exclude-from=' + self.exclude_file_path, self.repository_location + '::test')
self.assert_equal(sorted(os.listdir('output/input')), ['file1', 'file3'])
2013-06-22 11:33:21 +00:00
def test_exclude_caches(self):
self.attic('init', self.repository_location)
self.create_regular_file('file1', size=1024 * 80)
self.create_regular_file('cache1/CACHEDIR.TAG', contents = b'Signature: 8a477f597d28d172789f06886806bc55 extra stuff')
self.create_regular_file('cache2/CACHEDIR.TAG', contents = b'invalid signature')
self.attic('create', '--exclude-caches', self.repository_location + '::test', 'input')
with changedir('output'):
self.attic('extract', self.repository_location + '::test')
self.assert_equal(sorted(os.listdir('output/input')), ['cache2', 'file1'])
self.assert_equal(sorted(os.listdir('output/input/cache2')), ['CACHEDIR.TAG'])
def test_path_normalization(self):
self.attic('init', self.repository_location)
self.create_regular_file('dir1/dir2/file', size=1024 * 80)
with changedir('input/dir1/dir2'):
self.attic('create', self.repository_location + '::test', '../../../input/dir1/../dir1/dir2/..')
output = self.attic('list', self.repository_location + '::test')
self.assert_not_in('..', output)
self.assert_in(' input/dir1/dir2/file', output)
def test_repeated_files(self):
self.create_regular_file('file1', size=1024 * 80)
self.attic('init', self.repository_location)
self.attic('create', self.repository_location + '::test', 'input', 'input')
def test_overwrite(self):
self.create_regular_file('file1', size=1024 * 80)
self.create_regular_file('dir2/file2', size=1024 * 80)
2013-07-08 21:38:27 +00:00
self.attic('init', self.repository_location)
self.attic('create', self.repository_location + '::test', 'input')
# Overwriting regular files and directories should be supported
os.mkdir('output/input')
os.mkdir('output/input/file1')
os.mkdir('output/input/dir2')
2013-06-30 20:32:27 +00:00
with changedir('output'):
2013-07-08 21:38:27 +00:00
self.attic('extract', self.repository_location + '::test')
2013-07-26 11:18:57 +00:00
self.assert_dirs_equal('input', 'output/input')
# But non-empty dirs should fail
os.unlink('output/input/file1')
os.mkdir('output/input/file1')
os.mkdir('output/input/file1/dir')
2013-06-30 20:32:27 +00:00
with changedir('output'):
2013-07-08 21:38:27 +00:00
self.attic('extract', self.repository_location + '::test', exit_code=1)
2012-11-30 20:47:35 +00:00
def test_delete(self):
self.create_regular_file('file1', size=1024 * 80)
self.create_regular_file('dir2/file2', size=1024 * 80)
2013-07-08 21:38:27 +00:00
self.attic('init', self.repository_location)
self.attic('create', self.repository_location + '::test', 'input')
self.attic('create', self.repository_location + '::test.2', 'input')
self.attic('extract', '--dry-run', self.repository_location + '::test')
self.attic('extract', '--dry-run', self.repository_location + '::test.2')
2013-07-08 21:38:27 +00:00
self.attic('delete', self.repository_location + '::test')
self.attic('extract', '--dry-run', self.repository_location + '::test.2')
2013-07-08 21:38:27 +00:00
self.attic('delete', self.repository_location + '::test.2')
2012-11-30 20:47:35 +00:00
# Make sure all data except the manifest has been deleted
2013-06-20 10:44:58 +00:00
repository = Repository(self.repository_path)
2014-02-08 23:17:32 +00:00
self.assert_equal(len(repository), 1)
2012-11-30 20:47:35 +00:00
2013-06-20 10:44:58 +00:00
def test_corrupted_repository(self):
self.attic('init', self.repository_location)
2010-10-16 09:45:36 +00:00
self.create_src_archive('test')
self.attic('extract', '--dry-run', self.repository_location + '::test')
self.attic('check', self.repository_location)
2013-06-20 10:44:58 +00:00
name = sorted(os.listdir(os.path.join(self.tmpdir, 'repository', 'data', '0')), reverse=True)[0]
fd = open(os.path.join(self.tmpdir, 'repository', 'data', '0', name), 'r+')
2010-10-31 19:31:56 +00:00
fd.seek(100)
2013-08-05 18:38:37 +00:00
fd.write('XXXX')
2010-10-16 09:45:36 +00:00
fd.close()
self.attic('check', self.repository_location, exit_code=1)
2010-10-16 09:45:36 +00:00
def test_readonly_repository(self):
self.attic('init', self.repository_location)
self.create_src_archive('test')
os.system('chmod -R ugo-w ' + self.repository_path)
try:
self.attic('extract', '--dry-run', self.repository_location + '::test')
finally:
# Restore permissions so shutil.rmtree is able to delete it
os.system('chmod -R u+w ' + self.repository_path)
def test_cmdline_compatibility(self):
self.create_regular_file('file1', size=1024 * 80)
self.attic('init', self.repository_location)
self.attic('create', self.repository_location + '::test', 'input')
output = self.attic('verify', '-v', self.repository_location + '::test')
self.assert_in('"attic verify" has been deprecated', output)
output = self.attic('prune', self.repository_location, '--hourly=1')
self.assert_in('"--hourly" has been deprecated. Use "--keep-hourly" instead', output)
2013-06-20 10:44:58 +00:00
def test_prune_repository(self):
2013-07-08 21:38:27 +00:00
self.attic('init', self.repository_location)
self.attic('create', self.repository_location + '::test1', src_dir)
self.attic('create', self.repository_location + '::test2', src_dir)
2014-02-20 02:33:05 +00:00
output = self.attic('prune', '-v', '--dry-run', self.repository_location, '--keep-daily=2')
self.assert_in('Keeping archive: test2', output)
self.assert_in('Would prune: test1', output)
2013-07-08 21:38:27 +00:00
output = self.attic('list', self.repository_location)
2014-02-20 02:33:05 +00:00
self.assert_in('test1', output)
self.assert_in('test2', output)
self.attic('prune', self.repository_location, '--keep-daily=2')
output = self.attic('list', self.repository_location)
self.assert_not_in('test1', output)
self.assert_in('test2', output)
2010-10-31 20:55:09 +00:00
2013-06-26 19:20:31 +00:00
def test_usage(self):
2013-07-08 21:38:27 +00:00
self.assert_raises(SystemExit, lambda: self.attic())
self.assert_raises(SystemExit, lambda: self.attic('-h'))
2013-06-26 19:20:31 +00:00
2013-07-25 20:54:19 +00:00
@unittest.skipUnless(has_llfuse, 'llfuse not installed')
def test_fuse_mount_repository(self):
mountpoint = os.path.join(self.tmpdir, 'mountpoint')
os.mkdir(mountpoint)
self.attic('init', self.repository_location)
self.create_test_files()
self.attic('create', self.repository_location + '::archive', 'input')
self.attic('create', self.repository_location + '::archive2', 'input')
try:
self.attic('mount', self.repository_location, mountpoint, fork=True)
self.wait_for_mount(mountpoint)
self.assert_dirs_equal(self.input_path, os.path.join(mountpoint, 'archive', 'input'))
self.assert_dirs_equal(self.input_path, os.path.join(mountpoint, 'archive2', 'input'))
finally:
if sys.platform.startswith('linux'):
os.system('fusermount -u ' + mountpoint)
else:
os.system('umount ' + mountpoint)
os.rmdir(mountpoint)
# Give the daemon some time to exit
time.sleep(.2)
@unittest.skipUnless(has_llfuse, 'llfuse not installed')
def test_fuse_mount_archive(self):
2013-07-25 20:54:19 +00:00
mountpoint = os.path.join(self.tmpdir, 'mountpoint')
os.mkdir(mountpoint)
self.attic('init', self.repository_location)
self.create_test_files()
self.attic('create', self.repository_location + '::archive', 'input')
try:
self.attic('mount', self.repository_location + '::archive', mountpoint, fork=True)
2013-07-26 11:18:57 +00:00
self.wait_for_mount(mountpoint)
self.assert_dirs_equal(self.input_path, os.path.join(mountpoint, 'input'))
2013-07-25 20:54:19 +00:00
finally:
2013-07-27 12:31:28 +00:00
if sys.platform.startswith('linux'):
os.system('fusermount -u ' + mountpoint)
else:
os.system('umount ' + mountpoint)
2013-07-25 20:54:19 +00:00
os.rmdir(mountpoint)
# Give the daemon some time to exit
time.sleep(.2)
def verify_aes_counter_uniqueness(self, method):
seen = set() # Chunks already seen
used = set() # counter values already used
def verify_uniqueness():
repository = Repository(self.repository_path)
for key, _ in repository.open_index(repository.get_transaction_id()).iteritems():
data = repository.get(key)
hash = sha256(data).digest()
if not hash in seen:
seen.add(hash)
num_blocks = num_aes_blocks(len(data) - 41)
nonce = bytes_to_long(data[33:41])
for counter in range(nonce, nonce + num_blocks):
self.assert_not_in(counter, used)
used.add(counter)
self.create_test_files()
os.environ['ATTIC_PASSPHRASE'] = 'passphrase'
self.attic('init', '--encryption=' + method, self.repository_location)
verify_uniqueness()
self.attic('create', self.repository_location + '::test', 'input')
verify_uniqueness()
self.attic('create', self.repository_location + '::test.2', 'input')
verify_uniqueness()
self.attic('delete', self.repository_location + '::test.2')
verify_uniqueness()
self.assert_equal(used, set(range(len(used))))
def test_aes_counter_uniqueness_keyfile(self):
self.verify_aes_counter_uniqueness('keyfile')
def test_aes_counter_uniqueness_passphrase(self):
self.verify_aes_counter_uniqueness('passphrase')
class ArchiverCheckTestCase(ArchiverTestCaseBase):
def setUp(self):
super(ArchiverCheckTestCase, self).setUp()
with patch.object(ChunkBuffer, 'BUFFER_SIZE', 10):
self.attic('init', self.repository_location)
self.create_src_archive('archive1')
self.create_src_archive('archive2')
def open_archive(self, name):
repository = Repository(self.repository_path)
manifest, key = Manifest.load(repository)
archive = Archive(repository, key, manifest, name)
return archive, repository
def test_check_usage(self):
output = self.attic('check', self.repository_location, exit_code=0)
self.assert_in('Starting repository check', output)
self.assert_in('Starting archive consistency check', output)
output = self.attic('check', '--repository-only', self.repository_location, exit_code=0)
self.assert_in('Starting repository check', output)
self.assert_not_in('Starting archive consistency check', output)
output = self.attic('check', '--archives-only', self.repository_location, exit_code=0)
self.assert_not_in('Starting repository check', output)
self.assert_in('Starting archive consistency check', output)
def test_missing_file_chunk(self):
archive, repository = self.open_archive('archive1')
for item in archive.iter_items():
if item[b'path'].endswith('testsuite/archiver.py'):
repository.delete(item[b'chunks'][-1][0])
break
repository.commit()
self.attic('check', self.repository_location, exit_code=1)
self.attic('check', '--repair', self.repository_location, exit_code=0)
self.attic('check', self.repository_location, exit_code=0)
def test_missing_archive_item_chunk(self):
archive, repository = self.open_archive('archive1')
repository.delete(archive.metadata[b'items'][-5])
repository.commit()
self.attic('check', self.repository_location, exit_code=1)
self.attic('check', '--repair', self.repository_location, exit_code=0)
self.attic('check', self.repository_location, exit_code=0)
def test_missing_archive_metadata(self):
archive, repository = self.open_archive('archive1')
repository.delete(archive.id)
repository.commit()
self.attic('check', self.repository_location, exit_code=1)
self.attic('check', '--repair', self.repository_location, exit_code=0)
self.attic('check', self.repository_location, exit_code=0)
def test_missing_manifest(self):
archive, repository = self.open_archive('archive1')
repository.delete(Manifest.MANIFEST_ID)
repository.commit()
self.attic('check', self.repository_location, exit_code=1)
output = self.attic('check', '--repair', self.repository_location, exit_code=0)
self.assert_in('archive1', output)
self.assert_in('archive2', output)
self.attic('check', self.repository_location, exit_code=0)
def test_extra_chunks(self):
self.attic('check', self.repository_location, exit_code=0)
repository = Repository(self.repository_location)
repository.put(b'01234567890123456789012345678901', b'xxxx')
repository.commit()
repository.close()
self.attic('check', self.repository_location, exit_code=1)
self.attic('check', self.repository_location, exit_code=1)
self.attic('check', '--repair', self.repository_location, exit_code=0)
self.attic('check', self.repository_location, exit_code=0)
self.attic('extract', '--dry-run', self.repository_location + '::archive1', exit_code=0)
2011-10-29 15:01:07 +00:00
2013-06-24 20:41:05 +00:00
class RemoteArchiverTestCase(ArchiverTestCase):
prefix = '__testsuite__:'
def test_remote_repo_restrict_to_path(self):
self.attic('init', self.repository_location)
path_prefix = os.path.dirname(self.repository_path)
with patch.object(RemoteRepository, 'extra_test_args', ['--restrict-to-path', '/foo']):
self.assert_raises(PathNotAllowed, lambda: self.attic('init', self.repository_location + '_1'))
with patch.object(RemoteRepository, 'extra_test_args', ['--restrict-to-path', path_prefix]):
self.attic('init', self.repository_location + '_2')
with patch.object(RemoteRepository, 'extra_test_args', ['--restrict-to-path', '/foo', '--restrict-to-path', path_prefix]):
self.attic('init', self.repository_location + '_3')