Merged in msabramo/pytest/refactor_LsofFdLeakChecker (pull request #138)
testing/conftest.py: Refactor lsof fd leak checking
This commit is contained in:
commit
30a9debaf1
|
@ -4,48 +4,71 @@ import sys
|
||||||
pytest_plugins = "pytester",
|
pytest_plugins = "pytester",
|
||||||
|
|
||||||
import os, py
|
import os, py
|
||||||
pid = os.getpid()
|
|
||||||
|
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()
|
||||||
|
return py.process.cmdexec("lsof -Ffn0 -p %d" % pid)
|
||||||
|
|
||||||
|
def _parse_lsof_output(self, out):
|
||||||
|
def isopen(line):
|
||||||
|
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')
|
||||||
|
fd = fields[0][1:]
|
||||||
|
filename = fields[1][1:]
|
||||||
|
if filename.startswith('/'):
|
||||||
|
open_files.append((fd, filename))
|
||||||
|
|
||||||
|
return open_files
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
parser.addoption('--lsof',
|
parser.addoption('--lsof',
|
||||||
action="store_true", dest="lsof", default=False,
|
action="store_true", dest="lsof", default=False,
|
||||||
help=("run FD checks if lsof is available"))
|
help=("run FD checks if lsof is available"))
|
||||||
|
|
||||||
def pytest_configure(config):
|
def pytest_runtest_setup(item):
|
||||||
|
config = item.config
|
||||||
config._basedir = py.path.local()
|
config._basedir = py.path.local()
|
||||||
if config.getvalue("lsof"):
|
if config.getvalue("lsof"):
|
||||||
try:
|
try:
|
||||||
out = py.process.cmdexec("lsof -p %d" % pid)
|
config._fd_leak_checker = LsofFdLeakChecker()
|
||||||
|
config._openfiles = config._fd_leak_checker.get_open_files()
|
||||||
except py.process.cmdexec.Error:
|
except py.process.cmdexec.Error:
|
||||||
pass
|
pass
|
||||||
else:
|
|
||||||
config._numfiles = len(getopenfiles(out))
|
|
||||||
|
|
||||||
#def pytest_report_header():
|
#def pytest_report_header():
|
||||||
# return "pid: %s" % os.getpid()
|
# return "pid: %s" % os.getpid()
|
||||||
|
|
||||||
def getopenfiles(out):
|
|
||||||
def isopen(line):
|
|
||||||
return ("REG" in line or "CHR" in line) and (
|
|
||||||
"deleted" not in line and 'mem' not in line and "txt" not in line)
|
|
||||||
return [x for x in out.split("\n") if isopen(x)]
|
|
||||||
|
|
||||||
def check_open_files(config):
|
def check_open_files(config):
|
||||||
out2 = py.process.cmdexec("lsof -p %d" % pid)
|
lines2 = config._fd_leak_checker.get_open_files()
|
||||||
lines2 = getopenfiles(out2)
|
new_fds = set([t[0] for t in lines2]) - set([t[0] for t in config._openfiles])
|
||||||
if len(lines2) > config._numfiles + 3:
|
open_files = [t for t in lines2 if t[0] in new_fds]
|
||||||
|
if open_files:
|
||||||
error = []
|
error = []
|
||||||
error.append("***** %s FD leackage detected" %
|
error.append("***** %s FD leackage detected" % len(open_files))
|
||||||
(len(lines2)-config._numfiles))
|
error.extend([str(f) for f in open_files])
|
||||||
error.extend(lines2)
|
error.append("*** Before:")
|
||||||
|
error.extend([str(f) for f in config._openfiles])
|
||||||
|
error.append("*** After:")
|
||||||
|
error.extend([str(f) for f in lines2])
|
||||||
error.append(error[0])
|
error.append(error[0])
|
||||||
# update numfile so that the overall test run continuess
|
|
||||||
config._numfiles = len(lines2)
|
|
||||||
raise AssertionError("\n".join(error))
|
raise AssertionError("\n".join(error))
|
||||||
|
|
||||||
def pytest_runtest_teardown(item, __multicall__):
|
def pytest_runtest_teardown(item, __multicall__):
|
||||||
item.config._basedir.chdir()
|
item.config._basedir.chdir()
|
||||||
if hasattr(item.config, '_numfiles'):
|
if hasattr(item.config, '_openfiles'):
|
||||||
x = __multicall__.execute()
|
x = __multicall__.execute()
|
||||||
check_open_files(item.config)
|
check_open_files(item.config)
|
||||||
return x
|
return x
|
||||||
|
|
Loading…
Reference in New Issue