borg/darc/test.py

235 lines
10 KiB
Python
Raw Normal View History

2011-06-23 20:47:51 +00:00
from __future__ import with_statement
2010-11-02 21:47:39 +00:00
import doctest
2010-10-31 19:12:32 +00:00
import filecmp
2010-10-16 09:45:36 +00:00
import os
2010-10-30 11:44:25 +00:00
from StringIO 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
import unittest
2010-10-31 19:31:56 +00:00
from xattr import xattr, XATTR_NOFOLLOW
2010-10-19 19:08:42 +00:00
2012-11-30 20:47:35 +00:00
from . import helpers, lrucache
2013-05-28 12:35:55 +00:00
from .chunker import chunkify, buzhash, buzhash_update
2010-10-31 19:12:32 +00:00
from .archiver import Archiver
from .key import suite as KeySuite
2012-11-30 20:47:35 +00:00
from .store import Store, suite as StoreSuite
from .remote import Store, suite as RemoteStoreSuite
2010-10-16 09:45:36 +00:00
class Test(unittest.TestCase):
prefix = ''
2010-10-16 09:45:36 +00:00
def setUp(self):
self.archiver = Archiver()
self.tmpdir = tempfile.mkdtemp()
self.store_path = os.path.join(self.tmpdir, 'store')
self.store_location = self.prefix + self.store_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)
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)
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:
print output.getvalue()
self.assertEqual(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):
src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__), '..')
self.darc('init', self.store_location)
self.darc('create', self.store_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))
with open(filename, 'wbx') as fd:
2010-10-31 19:12:32 +00:00
fd.write('X' * size)
2010-10-31 19:31:56 +00:00
def get_xattrs(self, path):
try:
return dict(xattr(path, XATTR_NOFOLLOW))
except IOError:
return {}
2010-10-31 19:12:32 +00:00
def diff_dirs(self, dir1, dir2):
diff = filecmp.dircmp(dir1, dir2)
self.assertEqual(diff.left_only, [])
self.assertEqual(diff.right_only, [])
self.assertEqual(diff.diff_files, [])
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']
2010-10-31 19:12:32 +00:00
# We can't restore symlink atime/mtime right now
2010-10-31 19:31:56 +00:00
if not os.path.islink(path1):
2010-10-31 19:12:32 +00:00
attrs.append('st_mtime')
d1 = [filename] + [getattr(s1, a) for a in attrs]
d2 = [filename] + [getattr(s2, a) for a in attrs]
if(len(d1) == 6):
d1[-1] = int(d1[-1])
d2[-1] = int(d2[-1])
2010-10-31 19:31:56 +00:00
d1.append(self.get_xattrs(path1))
d2.append(self.get_xattrs(path2))
2010-10-31 19:12:32 +00:00
self.assertEqual(d1, d2)
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
os.chmod('input/file1', 7755)
os.chmod('input/dir2', 0700)
2012-12-09 22:06:33 +00:00
# Block device
os.mknod('input/bdev', 0600 | stat.S_IFBLK, os.makedev(10, 20))
# Char device
os.mknod('input/cdev', 0600 | stat.S_IFCHR, os.makedev(30, 40))
# xattr
2010-10-31 19:31:56 +00:00
x = xattr(os.path.join(self.input_path, 'file1'))
2010-11-01 22:06:56 +00:00
x.set('user.foo', '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'))
self.darc('init', self.store_location)
self.darc('create', self.store_location + '::test', 'input')
self.darc('create', self.store_location + '::test.2', 'input')
self.darc('extract', self.store_location + '::test', 'output')
self.assertEqual(len(self.darc('list', self.store_location).splitlines()), 2)
self.assertEqual(len(self.darc('list', self.store_location + '::test').splitlines()), 9)
2010-10-31 19:12:32 +00:00
self.diff_dirs('input', 'output/input')
info_output = self.darc('info', self.store_location + '::test')
shutil.rmtree(self.cache_path)
info_output2 = self.darc('info', self.store_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
def test_overwrite(self):
self.create_regual_file('file1', size=1024 * 80)
self.create_regual_file('dir2/file2', size=1024 * 80)
self.darc('init', self.store_location)
self.darc('create', self.store_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')
self.darc('extract', self.store_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')
self.darc('extract', self.store_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)
self.darc('init', self.store_location)
2012-11-30 20:47:35 +00:00
self.darc('create', self.store_location + '::test', 'input')
self.darc('create', self.store_location + '::test.2', 'input')
self.darc('verify', self.store_location + '::test')
self.darc('verify', self.store_location + '::test.2')
self.darc('delete', self.store_location + '::test')
self.darc('verify', self.store_location + '::test.2')
self.darc('delete', self.store_location + '::test.2')
# Make sure all data except the manifest has been deleted
store = Store(self.store_path)
self.assertEqual(store._len(), 1)
2010-10-16 09:45:36 +00:00
def test_corrupted_store(self):
self.create_src_archive('test')
self.darc('verify', self.store_location + '::test')
2011-09-10 15:19:02 +00:00
name = sorted(os.listdir(os.path.join(self.tmpdir, 'store', 'data', '0')), reverse=True)[0]
fd = open(os.path.join(self.tmpdir, 'store', '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()
self.darc('verify', self.store_location + '::test', exit_code=1)
2010-10-16 09:45:36 +00:00
2011-11-22 20:47:17 +00:00
def test_prune_store(self):
src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
self.darc('init', self.store_location)
self.darc('create', self.store_location + '::test1', src_dir)
self.darc('create', self.store_location + '::test2', src_dir)
2011-11-22 20:47:17 +00:00
self.darc('prune', self.store_location, '--daily=2')
output = self.darc('list', self.store_location)
assert 'test1' not in output
assert 'test2' in output
2010-10-31 20:55:09 +00:00
2011-10-29 15:01:07 +00:00
class ChunkTest(unittest.TestCase):
def test_chunkify(self):
data = '0' * 1024 * 1024 * 15 + 'Y'
parts = [str(c) for c in chunkify(StringIO(data), 2, 0x3, 2, 0)]
self.assertEqual(len(parts), 2)
self.assertEqual(''.join(parts), data)
self.assertEqual([str(c) for c in chunkify(StringIO(''), 2, 0x3, 2, 0)], [])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 2, 0x3, 2, 0)], ['fooba', 'rboobaz', 'fooba', 'rboobaz', 'fooba', 'rboobaz'])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 2, 0x3, 2, 1)], ['fo', 'obarb', 'oob', 'azf', 'oobarb', 'oob', 'azf', 'oobarb', 'oobaz'])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 2, 0x3, 2, 2)], ['foob', 'ar', 'boobazfoob', 'ar', 'boobazfoob', 'ar', 'boobaz'])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 3, 0x3, 3, 0)], ['foobarboobaz' * 3])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 3, 0x3, 3, 1)], ['foobar', 'boo', 'bazfo', 'obar', 'boo', 'bazfo', 'obar', 'boobaz'])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 3, 0x3, 3, 2)], ['foo', 'barboobaz', 'foo', 'barboobaz', 'foo', 'barboobaz'])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 3, 0x3, 4, 0)], ['foobarboobaz' * 3])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 3, 0x3, 4, 1)], ['foobar', 'boobazfo', 'obar', 'boobazfo', 'obar', 'boobaz'])
self.assertEqual([str(c) for c in chunkify(StringIO('foobarboobaz' * 3), 3, 0x3, 4, 2)], ['foob', 'arboobaz', 'foob', 'arboobaz', 'foob', 'arboobaz'])
def test_buzhash(self):
self.assertEqual(buzhash('abcdefghijklmnop', 0), 3795437769L)
self.assertEqual(buzhash('abcdefghijklmnop', 1), 3795400502L)
self.assertEqual(buzhash('abcdefghijklmnop', 1), buzhash_update(buzhash('Xabcdefghijklmno', 1), ord('X'), ord('p'), 16, 1))
class RemoteTest(Test):
prefix = 'localhost:'
2011-10-29 15:01:07 +00:00
2010-10-19 19:08:42 +00:00
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(ChunkTest))
2010-10-19 19:08:42 +00:00
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(RemoteTest))
suite.addTest(KeySuite())
2012-11-30 20:47:35 +00:00
suite.addTest(StoreSuite())
suite.addTest(RemoteStoreSuite())
2010-11-02 21:47:39 +00:00
suite.addTest(doctest.DocTestSuite(helpers))
suite.addTest(lrucache.suite())
2010-10-19 19:08:42 +00:00
return suite
2010-10-16 09:45:36 +00:00
if __name__ == '__main__':
2010-10-19 19:08:42 +00:00
unittest.TextTestRunner(verbosity=2).run(suite())