2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-10-07 17:59:00 +08:00
|
|
|
import sys
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-10-07 17:59:00 +08:00
|
|
|
pytest_plugins = "pytester",
|
|
|
|
|
|
|
|
import os, py
|
2014-04-02 01:15:27 +08:00
|
|
|
|
|
|
|
class LsofFdLeakChecker(object):
|
|
|
|
def get_open_files(self):
|
|
|
|
out = self._exec_lsof()
|
|
|
|
open_files = self._parse_lsof_output(out)
|
|
|
|
return open_files
|
|
|
|
|
|
|
|
def _exec_lsof(self):
|
|
|
|
pid = os.getpid()
|
2014-04-02 04:41:35 +08:00
|
|
|
return py.process.cmdexec("lsof -Ffn0 -p %d" % pid)
|
2014-04-02 01:15:27 +08:00
|
|
|
|
|
|
|
def _parse_lsof_output(self, out):
|
|
|
|
def isopen(line):
|
2014-04-02 04:41:35 +08:00
|
|
|
return line.startswith('f') and (
|
|
|
|
"deleted" not in line and 'mem' not in line and "txt" not in line and 'cwd' not in line)
|
|
|
|
|
|
|
|
open_files = []
|
|
|
|
|
|
|
|
for line in out.split("\n"):
|
|
|
|
if isopen(line):
|
|
|
|
fields = line.split('\0')
|
2014-04-02 06:36:54 +08:00
|
|
|
fd = fields[0][1:]
|
2014-04-02 04:41:35 +08:00
|
|
|
filename = fields[1][1:]
|
|
|
|
if filename.startswith('/'):
|
|
|
|
open_files.append((fd, filename))
|
|
|
|
|
|
|
|
return open_files
|
2014-04-02 01:15:27 +08:00
|
|
|
|
2010-10-26 15:11:53 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption('--lsof',
|
|
|
|
action="store_true", dest="lsof", default=False,
|
|
|
|
help=("run FD checks if lsof is available"))
|
|
|
|
|
2014-04-02 05:13:11 +08:00
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
config = item.config
|
2013-11-21 21:54:46 +08:00
|
|
|
config._basedir = py.path.local()
|
2010-10-26 15:11:53 +08:00
|
|
|
if config.getvalue("lsof"):
|
|
|
|
try:
|
2014-04-02 01:15:27 +08:00
|
|
|
config._fd_leak_checker = LsofFdLeakChecker()
|
|
|
|
config._openfiles = config._fd_leak_checker.get_open_files()
|
2010-10-26 15:11:53 +08:00
|
|
|
except py.process.cmdexec.Error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
#def pytest_report_header():
|
|
|
|
# return "pid: %s" % os.getpid()
|
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def check_open_files(config):
|
2014-04-02 01:15:27 +08:00
|
|
|
lines2 = config._fd_leak_checker.get_open_files()
|
2014-04-02 06:36:54 +08:00
|
|
|
new_fds = set([t[0] for t in lines2]) - set([t[0] for t in config._openfiles])
|
2014-04-02 04:41:35 +08:00
|
|
|
open_files = [t for t in lines2 if t[0] in new_fds]
|
|
|
|
if open_files:
|
2011-11-08 02:08:41 +08:00
|
|
|
error = []
|
2014-04-02 15:51:24 +08:00
|
|
|
error.append("***** %s FD leakage detected" % len(open_files))
|
2014-04-02 04:41:35 +08:00
|
|
|
error.extend([str(f) for f in open_files])
|
|
|
|
error.append("*** Before:")
|
|
|
|
error.extend([str(f) for f in config._openfiles])
|
|
|
|
error.append("*** After:")
|
|
|
|
error.extend([str(f) for f in lines2])
|
2011-11-08 02:08:41 +08:00
|
|
|
error.append(error[0])
|
|
|
|
raise AssertionError("\n".join(error))
|
2010-10-26 05:08:41 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def pytest_runtest_teardown(item, __multicall__):
|
2013-11-21 21:54:46 +08:00
|
|
|
item.config._basedir.chdir()
|
2014-04-02 05:13:11 +08:00
|
|
|
if hasattr(item.config, '_openfiles'):
|
2011-11-08 02:08:41 +08:00
|
|
|
x = __multicall__.execute()
|
|
|
|
check_open_files(item.config)
|
|
|
|
return x
|
2010-10-28 01:35:27 +08:00
|
|
|
|
2010-10-07 17:59:00 +08:00
|
|
|
# XXX copied from execnet's conftest.py - needs to be merged
|
|
|
|
winpymap = {
|
|
|
|
'python2.7': r'C:\Python27\python.exe',
|
|
|
|
'python2.6': r'C:\Python26\python.exe',
|
|
|
|
'python2.5': r'C:\Python25\python.exe',
|
|
|
|
'python2.4': r'C:\Python24\python.exe',
|
|
|
|
'python3.1': r'C:\Python31\python.exe',
|
2013-07-26 14:59:31 +08:00
|
|
|
'python3.2': r'C:\Python32\python.exe',
|
|
|
|
'python3.3': r'C:\Python33\python.exe',
|
|
|
|
'python3.4': r'C:\Python34\python.exe',
|
2010-10-07 17:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def getexecutable(name, cache={}):
|
|
|
|
try:
|
|
|
|
return cache[name]
|
|
|
|
except KeyError:
|
|
|
|
executable = py.path.local.sysfind(name)
|
|
|
|
if executable:
|
|
|
|
if name == "jython":
|
|
|
|
import subprocess
|
|
|
|
popen = subprocess.Popen([str(executable), "--version"],
|
|
|
|
universal_newlines=True, stderr=subprocess.PIPE)
|
|
|
|
out, err = popen.communicate()
|
|
|
|
if not err or "2.5" not in err:
|
|
|
|
executable = None
|
2011-08-20 01:25:52 +08:00
|
|
|
if "2.5.2" in err:
|
|
|
|
executable = None # http://bugs.jython.org/issue1790
|
2010-10-07 17:59:00 +08:00
|
|
|
cache[name] = executable
|
|
|
|
return executable
|
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.fixture(params=['python2.5', 'python2.6',
|
|
|
|
'python2.7', 'python3.2', "python3.3",
|
|
|
|
'pypy', 'jython'])
|
|
|
|
def anypython(request):
|
2010-10-07 17:59:00 +08:00
|
|
|
name = request.param
|
|
|
|
executable = getexecutable(name)
|
|
|
|
if executable is None:
|
|
|
|
if sys.platform == "win32":
|
|
|
|
executable = winpymap.get(name, None)
|
|
|
|
if executable:
|
|
|
|
executable = py.path.local(executable)
|
|
|
|
if executable.check():
|
|
|
|
return executable
|
2011-08-20 01:25:52 +08:00
|
|
|
pytest.skip("no suitable %s found" % (name,))
|
2010-10-07 17:59:00 +08:00
|
|
|
return executable
|