Skip to content

Commit cbdf030

Browse files
authored
Merge pull request #9670 from LabNConsulting/chopps/fix-valgrind-fail-check
Chopps/fix valgrind fail check
2 parents 230e696 + a15e5ac commit cbdf030

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

tests/topotests/conftest.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@
2121
from lib.topotest import g_extra_config as topotest_extra_config
2222
from lib.topotest import json_cmp_result
2323

24-
try:
25-
from _pytest._code.code import ExceptionInfo
26-
27-
leak_check_ok = True
28-
except ImportError:
29-
leak_check_ok = False
30-
3124

3225
def pytest_addoption(parser):
3326
"""
@@ -138,8 +131,7 @@ def pytest_addoption(parser):
138131

139132

140133
def check_for_memleaks():
141-
if not topotest_extra_config["valgrind_memleaks"]:
142-
return
134+
assert topotest_extra_config["valgrind_memleaks"]
143135

144136
leaks = []
145137
tgen = get_topogen()
@@ -151,21 +143,25 @@ def check_for_memleaks():
151143
existing = tgen.valgrind_existing_files
152144
latest = glob.glob(os.path.join(logdir, "*.valgrind.*"))
153145

146+
daemons = set()
154147
for vfile in latest:
155148
if vfile in existing:
156149
continue
157-
with open(vfile) as vf:
150+
existing.append(vfile)
151+
with open(vfile, encoding="ascii") as vf:
158152
vfcontent = vf.read()
159153
match = re.search(r"ERROR SUMMARY: (\d+) errors", vfcontent)
160154
if match and match.group(1) != "0":
161155
emsg = "{} in {}".format(match.group(1), vfile)
162156
leaks.append(emsg)
157+
daemons.add(re.match(r".*\.valgrind\.(.*)\.\d+", vfile).group(1))
158+
159+
if tgen is not None:
160+
tgen.valgrind_existing_files = existing
163161

164162
if leaks:
165-
if leak_check_ok:
166-
pytest.fail("Memleaks found:\n\t" + "\n\t".join(leaks))
167-
else:
168-
logger.error("Memleaks found:\n\t" + "\n\t".join(leaks))
163+
logger.error("valgrind memleaks found:\n\t%s", "\n\t".join(leaks))
164+
pytest.fail("valgrind memleaks found for daemons: " + " ".join(daemons))
169165

170166

171167
def pytest_runtest_logstart(nodeid, location):
@@ -178,18 +174,21 @@ def pytest_runtest_logfinish(nodeid, location):
178174
topolog.logfinish(nodeid, location)
179175

180176

181-
def pytest_runtest_call():
182-
"""
183-
This function must be run after setup_module(), it does standarized post
184-
setup routines. It is only being used for the 'topology-only' option.
185-
"""
177+
@pytest.hookimpl(hookwrapper=True)
178+
def pytest_runtest_call(item: pytest.Item) -> None:
179+
"Hook the function that is called to execute the test."
180+
181+
# For topology only run the CLI then exit
186182
if topotest_extra_config["topology_only"]:
187-
tgen = get_topogen()
188-
if tgen is not None:
189-
# Allow user to play with the setup.
190-
tgen.cli()
183+
get_topogen().cli()
184+
pytest.exit("exiting after --topology-only")
185+
186+
# Let the default pytest_runtest_call execute the test function
187+
yield
191188

192-
pytest.exit("the topology executed successfully")
189+
# Check for leaks if requested
190+
if topotest_extra_config["valgrind_memleaks"]:
191+
check_for_memleaks()
193192

194193

195194
def pytest_assertrepr_compare(op, left, right):
@@ -333,7 +332,10 @@ def assert_feature_windows(b, feature):
333332
topotest_extra_config["pause"] = pause
334333
assert_feature_windows(pause, "--pause")
335334

336-
topotest_extra_config["topology_only"] = config.getoption("--topology-only")
335+
topology_only = config.getoption("--topology-only")
336+
if topology_only and is_xdist:
337+
pytest.exit("Cannot use --topology-only with distributed test mode")
338+
topotest_extra_config["topology_only"] = topology_only
337339

338340
# Check environment now that we have config
339341
if not diagnose_env(rundir):
@@ -373,12 +375,6 @@ def pytest_runtest_makereport(item, call):
373375
else:
374376
pause = False
375377

376-
if call.excinfo is None and call.when == "call":
377-
try:
378-
check_for_memleaks()
379-
except:
380-
call.excinfo = ExceptionInfo()
381-
382378
title = "unset"
383379

384380
if call.excinfo is None:

tests/topotests/lib/topotest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ def start_daemon(daemon, extra_opts=None):
17251725
)
17261726
if valgrind_extra:
17271727
cmdenv += (
1728-
"--gen-suppressions=all --expensive-definedness-checks=yes"
1728+
" --gen-suppressions=all --expensive-definedness-checks=yes"
17291729
)
17301730
elif daemon in strace_daemons or "all" in strace_daemons:
17311731
cmdenv = "strace -f -D -o {1}/{2}.strace.{0} ".format(

tests/topotests/pytest.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,3 @@ markers =
8181
# memleak_path = /tmp/memleak_
8282
# Output files will be named after the testname:
8383
# /tmp/memleak_test_ospf_topo1.txt
84-
memleak_path = /tmp/memleak_

0 commit comments

Comments
 (0)