test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import filecmp
  2. import os
  3. import time
  4. from StringIO import StringIO
  5. import sys
  6. import shutil
  7. import tempfile
  8. import unittest
  9. from . import store
  10. from .archiver import Archiver
  11. class Test(unittest.TestCase):
  12. def setUp(self):
  13. self.archiver = Archiver()
  14. self.tmpdir = tempfile.mkdtemp()
  15. self.store_path = os.path.join(self.tmpdir, 'store')
  16. self.input_path = os.path.join(self.tmpdir, 'input')
  17. self.output_path = os.path.join(self.tmpdir, 'output')
  18. os.mkdir(self.input_path)
  19. os.mkdir(self.output_path)
  20. os.chdir(self.tmpdir)
  21. self.keychain = '/tmp/_test_dedupstore.keychain'
  22. if not os.path.exists(self.keychain):
  23. self.darc('keychain', 'generate')
  24. self.darc('init', self.store_path)
  25. def tearDown(self):
  26. shutil.rmtree(self.tmpdir)
  27. def darc(self, *args, **kwargs):
  28. exit_code = kwargs.get('exit_code', 0)
  29. args = ['--keychain', self.keychain] + list(args)
  30. try:
  31. stdout, stderr = sys.stdout, sys.stderr
  32. output = StringIO()
  33. sys.stdout = sys.stderr = output
  34. ret = self.archiver.run(args)
  35. sys.stdout, sys.stderr = stdout, stderr
  36. if ret != exit_code:
  37. print output.getvalue()
  38. self.assertEqual(exit_code, ret)
  39. return output.getvalue()
  40. finally:
  41. sys.stdout, sys.stderr = stdout, stderr
  42. def create_src_archive(self, name):
  43. src_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
  44. self.darc('create', self.store_path + '::' + name, src_dir)
  45. def create_regual_file(self, name, size=0):
  46. filename = os.path.join(self.input_path, name)
  47. if not os.path.exists(os.path.dirname(filename)):
  48. os.makedirs(os.path.dirname(filename))
  49. with open(filename, 'wb') as fd:
  50. fd.write('X' * size)
  51. def diff_dirs(self, dir1, dir2):
  52. diff = filecmp.dircmp(dir1, dir2)
  53. self.assertEqual(diff.left_only, [])
  54. self.assertEqual(diff.right_only, [])
  55. self.assertEqual(diff.diff_files, [])
  56. for filename in diff.common:
  57. s1 = os.lstat(os.path.join(dir1, filename))
  58. s2 = os.lstat(os.path.join(dir2, filename))
  59. attrs = ['st_mode', 'st_uid', 'st_gid']
  60. # We can't restore symlink atime/mtime right now
  61. if not os.path.islink(os.path.join(dir1, filename)):
  62. attrs.append('st_mtime')
  63. d1 = [filename] + [getattr(s1, a) for a in attrs]
  64. d2 = [filename] + [getattr(s2, a) for a in attrs]
  65. self.assertEqual(d1, d2)
  66. def test_basic_functionality(self):
  67. self.create_regual_file('file1', size=1024*80)
  68. self.create_regual_file('dir2/file2', size=1024*80)
  69. os.symlink('somewhere', os.path.join(self.input_path, 'link1'))
  70. os.mkfifo(os.path.join(self.input_path, 'fifo1'))
  71. self.darc('create', self.store_path + '::test', 'input')
  72. time.sleep(1)
  73. self.darc('extract', self.store_path + '::test', 'output')
  74. self.diff_dirs('input', 'output/input')
  75. def test_corrupted_store(self):
  76. self.create_src_archive('test')
  77. self.darc('verify', self.store_path + '::test')
  78. fd = open(os.path.join(self.tmpdir, 'store', 'bands', '0', '0'), 'r+')
  79. fd.seek(1000)
  80. fd.write('X')
  81. fd.close()
  82. self.darc('verify', self.store_path + '::test', exit_code=1)
  83. def suite():
  84. suite = unittest.TestSuite()
  85. suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test))
  86. suite.addTest(store.suite())
  87. return suite
  88. if __name__ == '__main__':
  89. unittest.TextTestRunner(verbosity=2).run(suite())