borg/darc/testsuite/archiver.py

207 lines
8.8 KiB
Python
Raw Normal View History

2010-10-31 19:12:32 +00:00
import filecmp
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
2010-10-30 11:44:25 +00:00
import sys
2010-10-16 09:45:36 +00:00
import shutil
import tempfile
2013-06-27 11:28:59 +00:00
from darc import xattr
2013-06-24 20:41:05 +00:00
from darc.archiver import Archiver
from darc.repository import Repository
from darc.testsuite import DarcTestCase
2010-10-16 09:45:36 +00:00
2013-06-15 18:56:27 +00:00
has_mtime_ns = sys.version >= '3.3'
2013-06-03 11:45:48 +00:00
utime_supports_fd = os.utime in getattr(os, 'supports_fd', {})
2013-06-24 20:41:05 +00:00
src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__), '..', '..')
2010-10-16 09:45:36 +00:00
2013-06-24 20:41:05 +00:00
class ArchiverTestCase(DarcTestCase):
2010-10-16 09:45:36 +00:00
prefix = ''
2010-10-16 09:45:36 +00:00
def setUp(self):
self.archiver = Archiver()
2013-06-27 11:28:59 +00:00
self.tmpdir = tempfile.mkdtemp(dir=os.getcwd())
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')
os.environ['DARC_KEYS_DIR'] = self.keys_path
os.environ['DARC_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)
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
2010-10-27 18:12:40 +00:00
def darc(self, *args, **kwargs):
2010-10-16 09:45:36 +00:00
exit_code = kwargs.get('exit_code', 0)
2011-08-06 11:01:58 +00:00
args = list(args)
2010-10-30 11:44:25 +00:00
try:
stdout, stderr = sys.stdout, sys.stderr
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-06-20 10:44:58 +00:00
self.darc('init', self.repository_location)
self.darc('create', self.repository_location + '::' + name, src_dir)
2010-10-16 09:45:36 +00:00
2010-10-31 19:12:32 +00:00
def create_regual_file(self, name, size=0):
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:
fd.write(b'X' * size)
2010-10-31 19:12:32 +00:00
2010-10-31 19:31:56 +00:00
def get_xattrs(self, path):
try:
2013-06-27 11:28:59 +00:00
return xattr.get_all(path)
2013-06-03 11:45:48 +00:00
except EnvironmentError:
2010-10-31 19:31:56 +00:00
return {}
2010-10-31 19:12:32 +00:00
def diff_dirs(self, dir1, dir2):
diff = filecmp.dircmp(dir1, dir2)
2013-06-24 20:41:05 +00:00
self.assert_equal(diff.left_only, [])
self.assert_equal(diff.right_only, [])
self.assert_equal(diff.diff_files, [])
2010-10-31 19:12:32 +00:00
for filename in diff.common:
2010-10-31 19:31:56 +00:00
path1 = os.path.join(dir1, filename)
path2 = os.path.join(dir2, filename)
s1 = os.lstat(path1)
s2 = os.lstat(path2)
2012-12-09 22:06:33 +00:00
attrs = ['st_mode', 'st_uid', 'st_gid', 'st_rdev']
2013-06-03 11:45:48 +00:00
if not os.path.islink(path1) or utime_supports_fd:
2013-06-15 18:56:27 +00:00
attrs.append('st_mtime_ns' if has_mtime_ns else 'st_mtime')
2010-10-31 19:12:32 +00:00
d1 = [filename] + [getattr(s1, a) for a in attrs]
d2 = [filename] + [getattr(s2, a) for a in attrs]
2013-06-15 18:56:27 +00:00
# 'st_mtime precision is limited'
if attrs[-1] == 'st_mtime':
d1[-1] = round(d1[-1], 4)
d2[-1] = round(d2[-1], 4)
2010-10-31 19:31:56 +00:00
d1.append(self.get_xattrs(path1))
d2.append(self.get_xattrs(path2))
2013-06-24 20:41:05 +00:00
self.assert_equal(d1, d2)
2010-10-31 19:12:32 +00:00
2010-10-16 09:45:36 +00:00
def test_basic_functionality(self):
2012-12-09 22:06:33 +00:00
# File
2011-10-29 15:01:07 +00:00
self.create_regual_file('file1', size=1024 * 80)
2012-12-09 22:06:33 +00:00
# Directory
2011-10-29 15:01:07 +00:00
self.create_regual_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)
os.chmod('input/dir2', 0o700)
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))
2013-06-27 11:28:59 +00:00
xattr.set(os.path.join(self.input_path, 'file1'), b'foo', b'bar')
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'))
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'))
2013-06-20 10:44:58 +00:00
self.darc('init', self.repository_location)
self.darc('create', self.repository_location + '::test', 'input')
self.darc('create', self.repository_location + '::test.2', 'input')
self.darc('extract', self.repository_location + '::test', 'output')
2013-06-24 20:41:05 +00:00
self.assert_equal(len(self.darc('list', self.repository_location).splitlines()), 2)
self.assert_equal(len(self.darc('list', self.repository_location + '::test').splitlines()), 9)
2010-10-31 19:12:32 +00:00
self.diff_dirs('input', 'output/input')
2013-06-20 10:44:58 +00:00
info_output = self.darc('info', self.repository_location + '::test')
shutil.rmtree(self.cache_path)
2013-06-20 10:44:58 +00:00
info_output2 = self.darc('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):
self.darc('init', self.repository_location)
self.create_regual_file('file1', size=1024 * 80)
self.create_regual_file('file2', size=1024 * 80)
self.create_regual_file('file3', size=1024 * 80)
self.create_regual_file('file4', size=1024 * 80)
self.darc('create', '--exclude=input/file4', self.repository_location + '::test', 'input')
self.darc('extract', '--include=file1', self.repository_location + '::test', 'output')
2013-06-24 20:41:05 +00:00
self.assert_equal(sorted(os.listdir('output/input')), ['file1'])
2013-06-22 11:33:21 +00:00
self.darc('extract', '--exclude=file2', self.repository_location + '::test', 'output')
2013-06-24 20:41:05 +00:00
self.assert_equal(sorted(os.listdir('output/input')), ['file1', 'file3'])
2013-06-22 11:33:21 +00:00
def test_overwrite(self):
self.create_regual_file('file1', size=1024 * 80)
self.create_regual_file('dir2/file2', size=1024 * 80)
2013-06-20 10:44:58 +00:00
self.darc('init', self.repository_location)
self.darc('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-20 10:44:58 +00:00
self.darc('extract', self.repository_location + '::test', 'output')
self.diff_dirs('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-20 10:44:58 +00:00
self.darc('extract', self.repository_location + '::test', 'output', exit_code=1)
2012-11-30 20:47:35 +00:00
def test_delete(self):
self.create_regual_file('file1', size=1024 * 80)
self.create_regual_file('dir2/file2', size=1024 * 80)
2013-06-20 10:44:58 +00:00
self.darc('init', self.repository_location)
self.darc('create', self.repository_location + '::test', 'input')
self.darc('create', self.repository_location + '::test.2', 'input')
self.darc('verify', self.repository_location + '::test')
self.darc('verify', self.repository_location + '::test.2')
self.darc('delete', self.repository_location + '::test')
self.darc('verify', self.repository_location + '::test.2')
self.darc('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)
2013-06-24 20:41:05 +00:00
self.assert_equal(repository._len(), 1)
2012-11-30 20:47:35 +00:00
2013-06-20 10:44:58 +00:00
def test_corrupted_repository(self):
2010-10-16 09:45:36 +00:00
self.create_src_archive('test')
2013-06-20 10:44:58 +00:00
self.darc('verify', self.repository_location + '::test')
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)
2010-10-16 09:45:36 +00:00
fd.write('X')
fd.close()
2013-06-20 10:44:58 +00:00
self.darc('verify', self.repository_location + '::test', exit_code=1)
2010-10-16 09:45:36 +00:00
2013-06-20 10:44:58 +00:00
def test_prune_repository(self):
self.darc('init', self.repository_location)
self.darc('create', self.repository_location + '::test1', src_dir)
self.darc('create', self.repository_location + '::test2', src_dir)
self.darc('prune', self.repository_location, '--daily=2')
output = self.darc('list', self.repository_location)
assert 'test1' not in output
assert 'test2' in output
2010-10-31 20:55:09 +00:00
2013-06-26 19:20:31 +00:00
def test_usage(self):
self.assert_raises(SystemExit, lambda: self.darc())
self.assert_raises(SystemExit, lambda: self.darc('-h'))
2011-10-29 15:01:07 +00:00
2013-06-24 20:41:05 +00:00
class RemoteArchiverTestCase(ArchiverTestCase):
prefix = 'localhost:'