__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import filecmp
  2. import os
  3. import posix
  4. import sys
  5. import sysconfig
  6. import time
  7. import unittest
  8. from attic.helpers import st_mtime_ns
  9. from attic.xattr import get_all
  10. try:
  11. import llfuse
  12. # Does this version of llfuse support ns precision?
  13. have_fuse_mtime_ns = hasattr(llfuse.EntryAttributes, 'st_mtime_ns')
  14. except ImportError:
  15. have_fuse_mtime_ns = False
  16. # The mtime get/set precison varies on different OS and Python versions
  17. if 'HAVE_FUTIMENS' in getattr(posix, '_have_functions', []):
  18. st_mtime_ns_round = 0
  19. elif 'HAVE_UTIMES' in sysconfig.get_config_vars():
  20. st_mtime_ns_round = -6
  21. else:
  22. st_mtime_ns_round = -9
  23. has_mtime_ns = sys.version >= '3.3'
  24. utime_supports_fd = os.utime in getattr(os, 'supports_fd', {})
  25. class AtticTestCase(unittest.TestCase):
  26. """
  27. """
  28. assert_in = unittest.TestCase.assertIn
  29. assert_not_in = unittest.TestCase.assertNotIn
  30. assert_equal = unittest.TestCase.assertEqual
  31. assert_not_equal = unittest.TestCase.assertNotEqual
  32. assert_raises = unittest.TestCase.assertRaises
  33. assert_true = unittest.TestCase.assertTrue
  34. def assert_dirs_equal(self, dir1, dir2):
  35. diff = filecmp.dircmp(dir1, dir2)
  36. self._assert_dirs_equal_cmp(diff)
  37. def _assert_dirs_equal_cmp(self, diff):
  38. self.assert_equal(diff.left_only, [])
  39. self.assert_equal(diff.right_only, [])
  40. self.assert_equal(diff.diff_files, [])
  41. self.assert_equal(diff.funny_files, [])
  42. for filename in diff.common:
  43. path1 = os.path.join(diff.left, filename)
  44. path2 = os.path.join(diff.right, filename)
  45. s1 = os.lstat(path1)
  46. s2 = os.lstat(path2)
  47. # Assume path2 is on FUSE if st_dev is different
  48. fuse = s1.st_dev != s2.st_dev
  49. attrs = ['st_mode', 'st_uid', 'st_gid', 'st_rdev']
  50. if not fuse or not os.path.isdir(path1):
  51. # dir nlink is always 1 on our fuse fileystem
  52. attrs.append('st_nlink')
  53. d1 = [filename] + [getattr(s1, a) for a in attrs]
  54. d2 = [filename] + [getattr(s2, a) for a in attrs]
  55. if not os.path.islink(path1) or utime_supports_fd:
  56. # Older versions of llfuse does not support ns precision properly
  57. if fuse and not have_fuse_mtime_ns:
  58. d1.append(round(st_mtime_ns(s1), -4))
  59. d2.append(round(st_mtime_ns(s2), -4))
  60. d1.append(round(st_mtime_ns(s1), st_mtime_ns_round))
  61. d2.append(round(st_mtime_ns(s2), st_mtime_ns_round))
  62. d1.append(get_all(path1, follow_symlinks=False))
  63. d2.append(get_all(path2, follow_symlinks=False))
  64. self.assert_equal(d1, d2)
  65. for sub_diff in diff.subdirs.values():
  66. self._assert_dirs_equal_cmp(sub_diff)
  67. def wait_for_mount(self, path, timeout=5):
  68. """Wait until a filesystem is mounted on `path`
  69. """
  70. timeout += time.time()
  71. while timeout > time.time():
  72. if os.path.ismount(path):
  73. return
  74. time.sleep(.1)
  75. raise Exception('wait_for_mount(%s) timeout' % path)
  76. def get_tests(suite):
  77. """Generates a sequence of tests from a test suite
  78. """
  79. for item in suite:
  80. try:
  81. # TODO: This could be "yield from..." with Python 3.3+
  82. for i in get_tests(item):
  83. yield i
  84. except TypeError:
  85. yield item
  86. class TestLoader(unittest.TestLoader):
  87. """A customzied test loader that properly detects and filters our test cases
  88. """
  89. def loadTestsFromName(self, pattern, module=None):
  90. suite = self.discover('attic.testsuite', '*.py')
  91. tests = unittest.TestSuite()
  92. for test in get_tests(suite):
  93. if pattern.lower() in test.id().lower():
  94. tests.addTest(test)
  95. return tests