1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
"""
Topotest conftest.py file.
"""
import glob
import os
import pdb
import re
import pytest
from lib.topogen import get_topogen, diagnose_env
from lib.topotest import json_cmp_result
from lib.topotest import g_extra_config as topotest_extra_config
from lib.topolog import logger
try:
from _pytest._code.code import ExceptionInfo
leak_check_ok = True
except ImportError:
leak_check_ok = False
def pytest_addoption(parser):
"""
Add topology-only option to the topology tester. This option makes pytest
only run the setup_module() to setup the topology without running any tests.
"""
parser.addoption(
"--gdb-breakpoints",
metavar="SYMBOL[,SYMBOL...]",
help="Comma-separated list of functions to set gdb breakpoints on",
)
parser.addoption(
"--gdb-daemons",
metavar="DAEMON[,DAEMON...]",
help="Comma-separated list of daemons to spawn gdb on, or 'all'",
)
parser.addoption(
"--gdb-routers",
metavar="ROUTER[,ROUTER...]",
help="Comma-separated list of routers to spawn gdb on, or 'all'",
)
parser.addoption(
"--mininet-on-error",
action="store_true",
help="Mininet cli on test failure",
)
parser.addoption(
"--pause-after",
action="store_true",
help="Pause after each test",
)
parser.addoption(
"--shell",
metavar="ROUTER[,ROUTER...]",
help="Comma-separated list of routers to spawn shell on, or 'all'",
)
parser.addoption(
"--shell-on-error",
action="store_true",
help="Spawn shell on all routers on test failure",
)
parser.addoption(
"--topology-only",
action="store_true",
default=False,
help="Only set up this topology, don't run tests",
)
parser.addoption(
"--valgrind-extra",
action="store_true",
help="Generate suppression file, and enable more precise (slower) valgrind checks",
)
parser.addoption(
"--valgrind-memleaks",
action="store_true",
help="Run all daemons under valgrind for memleak detection",
)
parser.addoption(
"--vtysh",
metavar="ROUTER[,ROUTER...]",
help="Comma-separated list of routers to spawn vtysh on, or 'all'",
)
parser.addoption(
"--vtysh-on-error",
action="store_true",
help="Spawn vtysh on all routers on test failure",
)
def check_for_memleaks():
if not topotest_extra_config["valgrind_memleaks"]:
return
leaks = []
tgen = get_topogen()
latest = []
existing = []
if tgen is not None:
logdir = "/tmp/topotests/{}".format(tgen.modname)
if hasattr(tgen, "valgrind_existing_files"):
existing = tgen.valgrind_existing_files
latest = glob.glob(os.path.join(logdir, "*.valgrind.*"))
for vfile in latest:
if vfile in existing:
continue
with open(vfile) as vf:
vfcontent = vf.read()
match = re.search(r"ERROR SUMMARY: (\d+) errors", vfcontent)
if match and match.group(1) != "0":
emsg = '{} in {}'.format(match.group(1), vfile)
leaks.append(emsg)
if leaks:
if leak_check_ok:
pytest.fail("Memleaks found:\n\t" + "\n\t".join(leaks))
else:
logger.error("Memleaks found:\n\t" + "\n\t".join(leaks))
def pytest_runtest_call():
"""
This function must be run after setup_module(), it does standarized post
setup routines. It is only being used for the 'topology-only' option.
"""
if topotest_extra_config["topology_only"]:
tgen = get_topogen()
if tgen is not None:
# Allow user to play with the setup.
tgen.mininet_cli()
pytest.exit("the topology executed successfully")
def pytest_assertrepr_compare(op, left, right):
"""
Show proper assertion error message for json_cmp results.
"""
del op
json_result = left
if not isinstance(json_result, json_cmp_result):
json_result = right
if not isinstance(json_result, json_cmp_result):
return None
return json_result.gen_report()
def pytest_configure(config):
"""
Assert that the environment is correctly configured, and get extra config.
"""
if not diagnose_env():
pytest.exit("environment has errors, please read the logs")
gdb_routers = config.getoption("--gdb-routers")
gdb_routers = gdb_routers.split(",") if gdb_routers else []
topotest_extra_config["gdb_routers"] = gdb_routers
gdb_daemons = config.getoption("--gdb-daemons")
gdb_daemons = gdb_daemons.split(",") if gdb_daemons else []
topotest_extra_config["gdb_daemons"] = gdb_daemons
gdb_breakpoints = config.getoption("--gdb-breakpoints")
gdb_breakpoints = gdb_breakpoints.split(",") if gdb_breakpoints else []
topotest_extra_config["gdb_breakpoints"] = gdb_breakpoints
mincli_on_error = config.getoption("--mininet-on-error")
topotest_extra_config["mininet_on_error"] = mincli_on_error
shell = config.getoption("--shell")
topotest_extra_config["shell"] = shell.split(",") if shell else []
pause_after = config.getoption("--pause-after")
shell_on_error = config.getoption("--shell-on-error")
topotest_extra_config["shell_on_error"] = shell_on_error
topotest_extra_config["valgrind_extra"] = config.getoption("--valgrind-extra")
topotest_extra_config["valgrind_memleaks"] = config.getoption("--valgrind-memleaks")
vtysh = config.getoption("--vtysh")
topotest_extra_config["vtysh"] = vtysh.split(",") if vtysh else []
vtysh_on_error = config.getoption("--vtysh-on-error")
topotest_extra_config["vtysh_on_error"] = vtysh_on_error
topotest_extra_config["pause_after"] = pause_after or shell or vtysh
topotest_extra_config["topology_only"] = config.getoption("--topology-only")
def pytest_runtest_makereport(item, call):
"Log all assert messages to default logger with error level"
# Nothing happened
if call.when == "call":
pause = topotest_extra_config["pause_after"]
else:
pause = False
if call.excinfo is None and call.when == "call":
try:
check_for_memleaks()
except:
call.excinfo = ExceptionInfo()
if call.excinfo is None:
error = False
else:
parent = item.parent
modname = parent.module.__name__
# Treat skips as non errors, don't pause after
if call.excinfo.typename != "AssertionError":
pause = False
error = False
logger.info(
'assert skipped at "{}/{}": {}'.format(
modname, item.name, call.excinfo.value
)
)
else:
error = True
# Handle assert failures
parent._previousfailed = item # pylint: disable=W0212
logger.error(
'assert failed at "{}/{}": {}'.format(
modname, item.name, call.excinfo.value
)
)
# (topogen) Set topology error to avoid advancing in the test.
tgen = get_topogen()
if tgen is not None:
# This will cause topogen to report error on `routers_have_failure`.
tgen.set_error("{}/{}".format(modname, item.name))
if error and topotest_extra_config["shell_on_error"]:
for router in tgen.routers():
pause = True
tgen.net[router].runInWindow(os.getenv("SHELL", "bash"))
if error and topotest_extra_config["vtysh_on_error"]:
for router in tgen.routers():
pause = True
tgen.net[router].runInWindow("vtysh")
if error and topotest_extra_config["mininet_on_error"]:
tgen.mininet_cli()
if pause:
try:
user = raw_input('Testing paused, "pdb" to debug, "Enter" to continue: ')
except NameError:
user = input('Testing paused, "pdb" to debug, "Enter" to continue: ')
if user.strip() == "pdb":
pdb.set_trace()
|