test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from __future__ import with_statement
  2. import doctest
  3. import filecmp
  4. import os
  5. from StringIO import StringIO
  6. import sys
  7. import shutil
  8. import tempfile
  9. import unittest
  10. from xattr import xattr, XATTR_NOFOLLOW
  11. from . import helpers, lrucache
  12. from .archiver import Archiver
  13. from .store import Store, suite as StoreSuite
  14. from .remote import Store, suite as RemoteStoreSuite
  15. class Test(unittest.TestCase):
  16. prefix = ''
  17. def setUp(self):
  18. self.archiver = Archiver()
  19. self.tmpdir = tempfile.mkdtemp()
  20. self.store_path = os.path.join(self.tmpdir, 'store')
  21. self.store_location = self.prefix + self.store_path
  22. self.input_path = os.path.join(self.tmpdir, 'input')
  23. self.output_path = os.path.join(self.tmpdir, 'output')
  24. self.keys_path = os.path.join(self.tmpdir, 'keys')
  25. self.cache_path = os.path.join(self.tmpdir, 'cache')
  26. os.environ['DARC_KEYS_DIR'] = self.keys_path
  27. os.environ['DARC_CACHE_DIR'] = self.cache_path
  28. os.mkdir(self.input_path)
  29. os.mkdir(self.output_path)
  30. os.mkdir(self.keys_path)
  31. os.mkdir(self.cache_path)
  32. os.chdir(self.tmpdir)
  33. def tearDown(self):
  34. shutil.rmtree(self.tmpdir)
  35. def darc(self, *args, **kwargs):
  36. exit_code = kwargs.get('exit_code', 0)
  37. args = list(args)
  38. try:
  39. stdout, stderr = sys.stdout, sys.stderr
  40. output = StringIO()
  41. sys.stdout = sys.stderr = output
  42. ret = self.archiver.run(args)
  43. sys.stdout, sys.stderr = stdout, stderr
  44. if ret != exit_code:
  45. print output.getvalue()
  46. self.assertEqual(exit_code, ret)
  47. return output.getvalue()
  48. finally:
  49. sys.stdout, sys.stderr = stdout, stderr
  50. def create_src_archive(self, name):
  51. src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
  52. self.darc('init', '--password', '', self.store_location)
  53. self.darc('create', self.store_location + '::' + name, src_dir)
  54. def create_regual_file(self, name, size=0):
  55. filename = os.path.join(self.input_path, name)
  56. if not os.path.exists(os.path.dirname(filename)):
  57. os.makedirs(os.path.dirname(filename))
  58. with open(filename, 'wb') as fd:
  59. fd.write('X' * size)
  60. def get_xattrs(self, path):
  61. try:
  62. return dict(xattr(path, XATTR_NOFOLLOW))
  63. except IOError:
  64. return {}
  65. def diff_dirs(self, dir1, dir2):
  66. diff = filecmp.dircmp(dir1, dir2)
  67. self.assertEqual(diff.left_only, [])
  68. self.assertEqual(diff.right_only, [])
  69. self.assertEqual(diff.diff_files, [])
  70. for filename in diff.common:
  71. path1 = os.path.join(dir1, filename)
  72. path2 = os.path.join(dir2, filename)
  73. s1 = os.lstat(path1)
  74. s2 = os.lstat(path2)
  75. attrs = ['st_mode', 'st_uid', 'st_gid']
  76. # We can't restore symlink atime/mtime right now
  77. if not os.path.islink(path1):
  78. attrs.append('st_mtime')
  79. d1 = [filename] + [getattr(s1, a) for a in attrs]
  80. d2 = [filename] + [getattr(s2, a) for a in attrs]
  81. d1.append(self.get_xattrs(path1))
  82. d2.append(self.get_xattrs(path2))
  83. self.assertEqual(d1, d2)
  84. def test_basic_functionality(self):
  85. self.create_regual_file('file1', size=1024 * 80)
  86. self.create_regual_file('dir2/file2', size=1024 * 80)
  87. x = xattr(os.path.join(self.input_path, 'file1'))
  88. x.set('user.foo', 'bar')
  89. os.link(os.path.join(self.input_path, 'file1'),
  90. os.path.join(self.input_path, 'hardlink'))
  91. os.symlink('somewhere', os.path.join(self.input_path, 'link1'))
  92. os.mkfifo(os.path.join(self.input_path, 'fifo1'))
  93. self.darc('init', '-p', '', self.store_location)
  94. self.darc('create', self.store_location + '::test', 'input')
  95. self.darc('create', self.store_location + '::test.2', 'input')
  96. self.darc('extract', self.store_location + '::test', 'output')
  97. self.diff_dirs('input', 'output/input')
  98. info_output = self.darc('info', self.store_location + '::test')
  99. shutil.rmtree(self.cache_path)
  100. info_output2 = self.darc('info', self.store_location + '::test')
  101. # info_output2 starts with some "initializing cache" text but should
  102. # end the same way as info_output
  103. assert info_output2.endswith(info_output)
  104. def test_delete(self):
  105. self.create_regual_file('file1', size=1024 * 80)
  106. self.create_regual_file('dir2/file2', size=1024 * 80)
  107. self.darc('init', '-p', '', self.store_location)
  108. self.darc('create', self.store_location + '::test', 'input')
  109. self.darc('create', self.store_location + '::test.2', 'input')
  110. self.darc('verify', self.store_location + '::test')
  111. self.darc('verify', self.store_location + '::test.2')
  112. self.darc('delete', self.store_location + '::test')
  113. self.darc('verify', self.store_location + '::test.2')
  114. self.darc('delete', self.store_location + '::test.2')
  115. # Make sure all data except the manifest has been deleted
  116. store = Store(self.store_path)
  117. self.assertEqual(store._len(), 1)
  118. def test_corrupted_store(self):
  119. self.create_src_archive('test')
  120. self.darc('verify', self.store_location + '::test')
  121. name = sorted(os.listdir(os.path.join(self.tmpdir, 'store', 'data', '0')), reverse=True)[0]
  122. fd = open(os.path.join(self.tmpdir, 'store', 'data', '0', name), 'r+')
  123. fd.seek(100)
  124. fd.write('X')
  125. fd.close()
  126. self.darc('verify', self.store_location + '::test', exit_code=1)
  127. def test_prune_store(self):
  128. src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
  129. self.darc('init', '-p', '', self.store_location)
  130. self.darc('create', self.store_location + '::test1', src_dir)
  131. self.darc('create', self.store_location + '::test2', src_dir)
  132. self.darc('prune', self.store_location, '--daily=2')
  133. output = self.darc('list', self.store_location)
  134. assert 'test1' not in output
  135. assert 'test2' in output
  136. class RemoteTest(Test):
  137. prefix = 'localhost:'
  138. def suite():
  139. suite = unittest.TestSuite()
  140. suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test))
  141. suite.addTest(unittest.TestLoader().loadTestsFromTestCase(RemoteTest))
  142. suite.addTest(StoreSuite())
  143. suite.addTest(RemoteStoreSuite())
  144. suite.addTest(doctest.DocTestSuite(helpers))
  145. suite.addTest(lrucache.suite())
  146. return suite
  147. if __name__ == '__main__':
  148. unittest.TextTestRunner(verbosity=2).run(suite())