borg/dedupestore/test.py

71 lines
2.6 KiB
Python
Raw Normal View History

2010-10-16 09:45:36 +00:00
import os
import shutil
import tempfile
import unittest
2010-10-19 19:08:42 +00:00
from .archiver import Archiver
2010-10-26 19:25:25 +00:00
from . import store
2010-10-16 09:45:36 +00:00
class Test(unittest.TestCase):
def setUp(self):
self.archiver = Archiver()
self.tmpdir = tempfile.mkdtemp()
self.store_path = os.path.join(self.tmpdir, 'store')
self.keychain = '/tmp/_test_dedupstore.keychain'
if not os.path.exists(self.keychain):
2010-10-25 18:22:20 +00:00
self.dedupestore('keychain', 'generate')
2010-10-16 09:45:36 +00:00
def tearDown(self):
shutil.rmtree(self.tmpdir)
def dedupestore(self, *args, **kwargs):
exit_code = kwargs.get('exit_code', 0)
2010-10-25 18:22:20 +00:00
args = ['--keychain', self.keychain] + list(args)
2010-10-16 09:45:36 +00:00
self.assertEqual(exit_code, self.archiver.run(args))
def create_src_archive(self, name):
src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
self.dedupestore('create', self.store_path + '::' + name, src_dir)
def test_basic_functionality(self):
self.create_src_archive('test')
self.dedupestore('list', self.store_path)
self.dedupestore('list', self.store_path + '::test')
self.dedupestore('info', self.store_path + '::test')
self.dedupestore('verify', self.store_path + '::test')
dest_dir = os.path.join(self.tmpdir, 'dest')
self.dedupestore('extract', self.store_path + '::test', dest_dir)
self.dedupestore('delete', self.store_path + '::test')
def test_corrupted_store(self):
self.create_src_archive('test')
self.dedupestore('verify', self.store_path + '::test')
fd = open(os.path.join(self.tmpdir, 'store', 'bands', '0', '0'), 'r+')
2010-10-16 09:45:36 +00:00
fd.seek(1000)
fd.write('X')
fd.close()
self.dedupestore('verify', self.store_path + '::test', exit_code=1)
2010-10-18 17:43:54 +00:00
def test_symlinks(self):
testdir = os.path.join(self.tmpdir, 'linktest')
os.mkdir(testdir)
os.symlink('/tmp/somewhere', os.path.join(testdir, 'link'))
self.dedupestore('create', self.store_path + '::symlinktest', testdir)
dest_dir = os.path.join(self.tmpdir, 'dest')
self.dedupestore('extract', self.store_path + '::symlinktest', dest_dir)
dest = os.path.join(dest_dir, testdir[1:])
self.assertEqual(os.path.islink(os.path.join(dest, 'link')), True)
self.assertEqual(os.readlink(os.path.join(dest, 'link')), '/tmp/somewhere')
2010-10-16 09:45:36 +00:00
2010-10-19 19:08:42 +00:00
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test))
2010-10-26 19:25:25 +00:00
suite.addTest(store.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())