__init__.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. """ Swift tests """
  2. import sys
  3. import os
  4. import copy
  5. import logging
  6. from sys import exc_info
  7. from contextlib import contextmanager
  8. from tempfile import NamedTemporaryFile
  9. from eventlet.green import socket
  10. from tempfile import mkdtemp
  11. from shutil import rmtree
  12. from tests import get_config
  13. from ConfigParser import MissingSectionHeaderError
  14. from StringIO import StringIO
  15. from swift.common.utils import readconf, TRUE_VALUES
  16. from logging import Handler
  17. import logging.handlers
  18. def readuntil2crlfs(fd):
  19. rv = ''
  20. lc = ''
  21. crlfs = 0
  22. while crlfs < 2:
  23. c = fd.read(1)
  24. rv = rv + c
  25. if c == '\r' and lc != '\n':
  26. crlfs = 0
  27. if lc == '\r' and c == '\n':
  28. crlfs += 1
  29. lc = c
  30. return rv
  31. def connect_tcp(hostport):
  32. rv = socket.socket()
  33. rv.connect(hostport)
  34. return rv
  35. @contextmanager
  36. def tmpfile(content):
  37. with NamedTemporaryFile('w', delete=False) as f:
  38. file_name = f.name
  39. f.write(str(content))
  40. try:
  41. yield file_name
  42. finally:
  43. os.unlink(file_name)
  44. xattr_data = {}
  45. def _get_inode(fd):
  46. if not isinstance(fd, int):
  47. try:
  48. fd = fd.fileno()
  49. except AttributeError:
  50. return os.stat(fd).st_ino
  51. return os.fstat(fd).st_ino
  52. def _setxattr(fd, k, v):
  53. inode = _get_inode(fd)
  54. data = xattr_data.get(inode, {})
  55. data[k] = v
  56. xattr_data[inode] = data
  57. def _getxattr(fd, k):
  58. inode = _get_inode(fd)
  59. data = xattr_data.get(inode, {}).get(k)
  60. if not data:
  61. raise IOError
  62. return data
  63. import xattr
  64. xattr.setxattr = _setxattr
  65. xattr.getxattr = _getxattr
  66. @contextmanager
  67. def temptree(files, contents=''):
  68. # generate enough contents to fill the files
  69. c = len(files)
  70. contents = (list(contents) + [''] * c)[:c]
  71. tempdir = mkdtemp()
  72. for path, content in zip(files, contents):
  73. if os.path.isabs(path):
  74. path = '.' + path
  75. new_path = os.path.join(tempdir, path)
  76. subdir = os.path.dirname(new_path)
  77. if not os.path.exists(subdir):
  78. os.makedirs(subdir)
  79. with open(new_path, 'w') as f:
  80. f.write(str(content))
  81. try:
  82. yield tempdir
  83. finally:
  84. rmtree(tempdir)
  85. class NullLoggingHandler(logging.Handler):
  86. def emit(self, record):
  87. pass
  88. class FakeLogger(object):
  89. # a thread safe logger
  90. def __init__(self, *args, **kwargs):
  91. self._clear()
  92. self.level = logging.NOTSET
  93. if 'facility' in kwargs:
  94. self.facility = kwargs['facility']
  95. def _clear(self):
  96. self.log_dict = dict(
  97. error=[], info=[], warning=[], debug=[], exception=[])
  98. def error(self, *args, **kwargs):
  99. self.log_dict['error'].append((args, kwargs))
  100. def info(self, *args, **kwargs):
  101. self.log_dict['info'].append((args, kwargs))
  102. def warning(self, *args, **kwargs):
  103. self.log_dict['warning'].append((args, kwargs))
  104. def debug(self, *args, **kwargs):
  105. self.log_dict['debug'].append((args, kwargs))
  106. def exception(self, *args, **kwargs):
  107. self.log_dict['exception'].append((args, kwargs, str(exc_info()[1])))
  108. # mock out the StatsD logging methods:
  109. def set_statsd_prefix(self, *a, **kw):
  110. pass
  111. increment = decrement = timing = timing_since = update_stats = \
  112. set_statsd_prefix
  113. def setFormatter(self, obj):
  114. self.formatter = obj
  115. def close(self):
  116. self._clear()
  117. def set_name(self, name):
  118. # don't touch _handlers
  119. self._name = name
  120. def acquire(self):
  121. pass
  122. def release(self):
  123. pass
  124. def createLock(self):
  125. pass
  126. def emit(self, record):
  127. pass
  128. def handle(self, record):
  129. pass
  130. def flush(self):
  131. pass
  132. def handleError(self, record):
  133. pass
  134. original_syslog_handler = logging.handlers.SysLogHandler
  135. def fake_syslog_handler():
  136. for attr in dir(original_syslog_handler):
  137. if attr.startswith('LOG'):
  138. setattr(FakeLogger, attr,
  139. copy.copy(getattr(logging.handlers.SysLogHandler, attr)))
  140. FakeLogger.priority_map = \
  141. copy.deepcopy(logging.handlers.SysLogHandler.priority_map)
  142. logging.handlers.SysLogHandler = FakeLogger
  143. if get_config('unit_test').get('fake_syslog', 'False').lower() in TRUE_VALUES:
  144. fake_syslog_handler()
  145. class MockTrue(object):
  146. """
  147. Instances of MockTrue evaluate like True
  148. Any attr accessed on an instance of MockTrue will return a MockTrue
  149. instance. Any method called on an instance of MockTrue will return
  150. a MockTrue instance.
  151. >>> thing = MockTrue()
  152. >>> thing
  153. True
  154. >>> thing == True # True == True
  155. True
  156. >>> thing == False # True == False
  157. False
  158. >>> thing != True # True != True
  159. False
  160. >>> thing != False # True != False
  161. True
  162. >>> thing.attribute
  163. True
  164. >>> thing.method()
  165. True
  166. >>> thing.attribute.method()
  167. True
  168. >>> thing.method().attribute
  169. True
  170. """
  171. def __getattribute__(self, *args, **kwargs):
  172. return self
  173. def __call__(self, *args, **kwargs):
  174. return self
  175. def __repr__(*args, **kwargs):
  176. return repr(True)
  177. def __eq__(self, other):
  178. return other is True
  179. def __ne__(self, other):
  180. return other is not True