2010-07-12 17:03:45 -05:00
|
|
|
""" Swift tests """
|
|
|
|
|
2012-04-30 16:38:15 -04:00
|
|
|
import sys
|
2010-11-11 16:41:07 -06:00
|
|
|
import os
|
2012-04-30 16:38:15 -04:00
|
|
|
import copy
|
2010-11-11 16:41:07 -06:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from tempfile import NamedTemporaryFile
|
2010-07-12 17:03:45 -05:00
|
|
|
from eventlet.green import socket
|
2011-02-11 13:18:19 -06:00
|
|
|
from tempfile import mkdtemp
|
|
|
|
from shutil import rmtree
|
2012-04-30 16:38:15 -04:00
|
|
|
from test import get_config
|
|
|
|
from ConfigParser import MissingSectionHeaderError
|
|
|
|
from StringIO import StringIO
|
|
|
|
from swift.common.utils import readconf, TRUE_VALUES
|
|
|
|
from logging import Handler
|
|
|
|
import logging.handlers
|
2010-07-12 17:03:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
def readuntil2crlfs(fd):
|
|
|
|
rv = ''
|
|
|
|
lc = ''
|
|
|
|
crlfs = 0
|
|
|
|
while crlfs < 2:
|
|
|
|
c = fd.read(1)
|
|
|
|
rv = rv + c
|
|
|
|
if c == '\r' and lc != '\n':
|
|
|
|
crlfs = 0
|
|
|
|
if lc == '\r' and c == '\n':
|
|
|
|
crlfs += 1
|
|
|
|
lc = c
|
|
|
|
return rv
|
|
|
|
|
|
|
|
|
|
|
|
def connect_tcp(hostport):
|
|
|
|
rv = socket.socket()
|
|
|
|
rv.connect(hostport)
|
|
|
|
return rv
|
2010-07-29 13:30:16 -05:00
|
|
|
|
2010-11-11 16:41:07 -06:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def tmpfile(content):
|
|
|
|
with NamedTemporaryFile('w', delete=False) as f:
|
|
|
|
file_name = f.name
|
|
|
|
f.write(str(content))
|
|
|
|
try:
|
|
|
|
yield file_name
|
|
|
|
finally:
|
|
|
|
os.unlink(file_name)
|
|
|
|
|
2011-01-19 14:18:37 -06:00
|
|
|
xattr_data = {}
|
|
|
|
|
2011-01-19 16:19:43 -06:00
|
|
|
|
2011-01-19 14:18:37 -06:00
|
|
|
def _get_inode(fd):
|
|
|
|
if not isinstance(fd, int):
|
|
|
|
try:
|
|
|
|
fd = fd.fileno()
|
|
|
|
except AttributeError:
|
|
|
|
return os.stat(fd).st_ino
|
|
|
|
return os.fstat(fd).st_ino
|
|
|
|
|
2011-01-19 16:19:43 -06:00
|
|
|
|
2011-01-19 14:18:37 -06:00
|
|
|
def _setxattr(fd, k, v):
|
|
|
|
inode = _get_inode(fd)
|
|
|
|
data = xattr_data.get(inode, {})
|
|
|
|
data[k] = v
|
|
|
|
xattr_data[inode] = data
|
|
|
|
|
2011-01-19 16:19:43 -06:00
|
|
|
|
2011-01-19 14:18:37 -06:00
|
|
|
def _getxattr(fd, k):
|
|
|
|
inode = _get_inode(fd)
|
|
|
|
data = xattr_data.get(inode, {}).get(k)
|
|
|
|
if not data:
|
|
|
|
raise IOError
|
|
|
|
return data
|
|
|
|
|
|
|
|
import xattr
|
|
|
|
xattr.setxattr = _setxattr
|
|
|
|
xattr.getxattr = _getxattr
|
|
|
|
|
2010-11-11 16:41:07 -06:00
|
|
|
|
2011-02-11 13:18:19 -06:00
|
|
|
@contextmanager
|
|
|
|
def temptree(files, contents=''):
|
|
|
|
# generate enough contents to fill the files
|
|
|
|
c = len(files)
|
|
|
|
contents = (list(contents) + [''] * c)[:c]
|
|
|
|
tempdir = mkdtemp()
|
|
|
|
for path, content in zip(files, contents):
|
|
|
|
if os.path.isabs(path):
|
|
|
|
path = '.' + path
|
|
|
|
new_path = os.path.join(tempdir, path)
|
|
|
|
subdir = os.path.dirname(new_path)
|
|
|
|
if not os.path.exists(subdir):
|
|
|
|
os.makedirs(subdir)
|
|
|
|
with open(new_path, 'w') as f:
|
|
|
|
f.write(str(content))
|
|
|
|
try:
|
|
|
|
yield tempdir
|
|
|
|
finally:
|
|
|
|
rmtree(tempdir)
|
|
|
|
|
|
|
|
|
2012-04-30 16:38:15 -04:00
|
|
|
class FakeLogger(Handler):
|
2011-03-15 22:12:03 -07:00
|
|
|
# a thread safe logger
|
|
|
|
|
2011-05-10 15:36:01 -07:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.log_dict = dict(error=[], info=[], warning=[], debug=[])
|
2012-04-30 16:38:15 -04:00
|
|
|
self.level = logging.NOTSET
|
|
|
|
if 'facility' in kwargs:
|
|
|
|
self.facility = kwargs['facility']
|
2011-03-15 22:12:03 -07:00
|
|
|
|
|
|
|
def error(self, *args, **kwargs):
|
|
|
|
self.log_dict['error'].append((args, kwargs))
|
|
|
|
|
|
|
|
def info(self, *args, **kwargs):
|
|
|
|
self.log_dict['info'].append((args, kwargs))
|
|
|
|
|
|
|
|
def warning(self, *args, **kwargs):
|
|
|
|
self.log_dict['warning'].append((args, kwargs))
|
|
|
|
|
2011-05-10 15:36:01 -07:00
|
|
|
def debug(self, *args, **kwargs):
|
|
|
|
self.log_dict['debug'].append((args, kwargs))
|
|
|
|
|
2012-04-30 16:38:15 -04:00
|
|
|
def setFormatter(self, obj):
|
|
|
|
self.formatter = obj
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.log_dict = dict(error=[], info=[], warning=[], debug=[])
|
|
|
|
|
|
|
|
def set_name(self, name):
|
|
|
|
# don't touch _handlers
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
def acquire(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def release(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def createLock(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle(self, record):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handleError(self, record):
|
|
|
|
pass
|
|
|
|
|
|
|
|
original_syslog_handler = logging.handlers.SysLogHandler
|
|
|
|
|
|
|
|
|
|
|
|
def fake_syslog_handler():
|
|
|
|
for attr in dir(original_syslog_handler):
|
|
|
|
if attr.startswith('LOG'):
|
|
|
|
setattr(FakeLogger, attr,
|
|
|
|
copy.copy(getattr(logging.handlers.SysLogHandler, attr)))
|
|
|
|
FakeLogger.priority_map = \
|
|
|
|
copy.deepcopy(logging.handlers.SysLogHandler.priority_map)
|
|
|
|
|
|
|
|
logging.handlers.SysLogHandler = FakeLogger
|
|
|
|
|
|
|
|
|
|
|
|
if get_config('unit_test').get('fake_syslog', 'False').lower() in TRUE_VALUES:
|
|
|
|
fake_syslog_handler()
|
|
|
|
|
2011-03-15 22:12:03 -07:00
|
|
|
|
2010-07-29 13:30:16 -05:00
|
|
|
class MockTrue(object):
|
|
|
|
"""
|
2010-07-29 13:31:27 -05:00
|
|
|
Instances of MockTrue evaluate like True
|
2012-04-30 16:38:15 -04:00
|
|
|
Any attr accessed on an instance of MockTrue will return a MockTrue
|
|
|
|
instance. Any method called on an instance of MockTrue will return
|
|
|
|
a MockTrue instance.
|
2010-07-29 13:30:16 -05:00
|
|
|
|
|
|
|
>>> thing = MockTrue()
|
|
|
|
>>> thing
|
|
|
|
True
|
|
|
|
>>> thing == True # True == True
|
|
|
|
True
|
|
|
|
>>> thing == False # True == False
|
|
|
|
False
|
|
|
|
>>> thing != True # True != True
|
|
|
|
False
|
|
|
|
>>> thing != False # True != False
|
|
|
|
True
|
|
|
|
>>> thing.attribute
|
|
|
|
True
|
|
|
|
>>> thing.method()
|
|
|
|
True
|
|
|
|
>>> thing.attribute.method()
|
|
|
|
True
|
|
|
|
>>> thing.method().attribute
|
|
|
|
True
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __getattribute__(self, *args, **kwargs):
|
|
|
|
return self
|
2012-04-30 16:38:15 -04:00
|
|
|
|
2010-07-29 13:30:16 -05:00
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
return self
|
2012-04-30 16:38:15 -04:00
|
|
|
|
2010-07-29 13:30:16 -05:00
|
|
|
def __repr__(*args, **kwargs):
|
|
|
|
return repr(True)
|
2012-04-30 16:38:15 -04:00
|
|
|
|
2010-07-29 13:30:16 -05:00
|
|
|
def __eq__(self, other):
|
2012-01-04 14:43:16 +08:00
|
|
|
return other is True
|
2012-04-30 16:38:15 -04:00
|
|
|
|
2010-07-29 13:30:16 -05:00
|
|
|
def __ne__(self, other):
|
2012-01-04 14:43:16 +08:00
|
|
|
return other is not True
|