-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression.py
308 lines (268 loc) · 11.3 KB
/
regression.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# python lib modules
import os
import sys
import shutil
import pproc as subprocess
import errno
# WAF modules
import Options
import Utils
import Task
# local modules
import wutils
def dev_null():
if sys.platform == 'win32':
return open("NUL:", "w")
else:
return open("/dev/null", "w")
def _find_tests(testdir):
"""Return a list of test modules in the test directory
Arguments:
testdir -- the directory to look in for tests
"""
names = os.listdir(testdir)
tests = []
for name in names:
if name[:5] == "test-" and name[-3:] == ".py":
testname = name[:-3]
tests.append(testname)
tests.sort()
return tests
def diff(dir1, dir2, verbose):
import filecmp
comp = filecmp.dircmp(dir1, dir2)
differ = (comp.left_only or comp.right_only or comp.diff_files)
if differ:
# ok, stupid binary comparison reports differences, but maybe
# only text files differ, in which case we should compare
# again while ignoring newline differences between
# windows/mac/unix.
if not comp.left_only and not comp.right_only:
for diff_fname in comp.diff_files:
if not (diff_fname.endswith(".tr") or diff_fname.endswith(".mob")):
# doesn't look like a text file; it has to differ
break
diff_file1 = open(os.path.join(dir1, diff_fname), "rtU").readlines()
diff_file2 = open(os.path.join(dir2, diff_fname), "rtU").readlines()
if diff_file1 != diff_file2:
break
#else:
# print ">>>>>>>> %s file does not really differ!" % (diff_fname)
else:
differ = False
if differ:
if verbose:
comp.report()
import difflib
for diff_fname in comp.diff_files:
if not (diff_fname.endswith(".tr") or diff_fname.endswith(".mob")):
print "The different file %r does not sound like a text file, not compared." % (diff_fname,)
diff_file1 = open(os.path.join(dir1, diff_fname), "rt").readlines()
diff_file2 = open(os.path.join(dir2, diff_fname), "rt").readlines()
diff = difflib.unified_diff(diff_file1, diff_file2)
count = 0
print "Differences in file %r" % (diff_fname,)
for line in diff:
print line
count += 1
if count > 100:
break
return 1
else:
return 0
class regression_test_task(Task.TaskBase):
after = 'cc cxx cc_link cxx_link'
color = 'BLUE'
def __init__(self, bld, env, test_name, test_scripts_dir, build_traces_dir, reference_traces):
self.bld = bld
self.generator = self
self.env = env
super(regression_test_task, self).__init__(generator=self, env=env)
self.test_name = test_name
assert self.test_name.startswith('test-')
short_name = self.test_name[len('test-'):]
self.test_scripts_dir = test_scripts_dir
self.build_traces_dir = build_traces_dir
self.reference_traces_dir = reference_traces
sys.path.insert(0, self.test_scripts_dir)
try:
mod = __import__(self.test_name, globals(), locals(), [])
finally:
sys.path.remove(self.test_scripts_dir)
self.mod = mod
if hasattr(mod, 'may_run'):
reason_cannot_run = mod.may_run(self.env, Options.options)
else:
reason_cannot_run = None
if not reason_cannot_run:
pyscript = getattr(mod, "pyscript", None)
if pyscript:
Options.options.compile_targets += ',ns3module,pybindgen-command'
else:
program = getattr(mod, "program", short_name)
Options.options.compile_targets += ',' + program
def __str__(self):
return 'regression-test (%s)\n' % self.test_name
def runnable_status(self):
return Task.RUN_ME
def run(self):
"""Run a single test"""
assert self.test_name.startswith('test-')
short_name = self.test_name[len('test-'):]
mod = self.mod
trace_dir_name = getattr(mod, "trace_dir_name", None)
if trace_dir_name is None:
trace_dir_name = "%s.ref" % short_name
trace_output_path = os.path.join(self.build_traces_dir, trace_dir_name)
reference_traces_path = os.path.join(self.reference_traces_dir, trace_dir_name)
if hasattr(mod, 'get_arguments'):
arguments = mod.get_arguments(self.env, '..')
else:
arguments = getattr(mod, "arguments", [])
pyscript = getattr(mod, "pyscript", None)
if pyscript:
is_pyscript = True
program = pyscript
else:
is_pyscript = False
program = getattr(mod, "program", short_name)
if hasattr(mod, 'may_run'):
reason_cannot_run = mod.may_run(self.env, Options.options)
else:
reason_cannot_run = None
if reason_cannot_run:
print "SKIP %s (%s)" % (self.test_name, reason_cannot_run)
self.result = None
return 0
if Options.options.regression_generate:
# clean the target dir
try:
shutil.rmtree(reference_traces_path)
except OSError, ex:
if ex.errno not in [errno.ENOENT]:
raise
os.makedirs(reference_traces_path)
result = self.run_reference_generate(reference_traces_path, program, arguments, is_pyscript)
if result == 0:
print "GENERATE " + self.test_name
else:
print "GENERATE FAIL " + self.test_name
else:
# clean the target dir
try:
shutil.rmtree(trace_output_path)
except OSError, ex:
if ex.errno not in [errno.ENOENT]:
raise
os.makedirs(trace_output_path)
# run it
#print "self.run_reference_test:(%r, %r, %r, %r, %r)" \
# % (reference_traces_path, trace_output_path, program, arguments, is_pyscript)
result = self.run_reference_test(reference_traces_path, trace_output_path, program, arguments, is_pyscript)
if result == 0:
print "PASS " + self.test_name
else:
print "FAIL " + self.test_name
self.result = result
return 0
def run_reference_test(self, reference_traces_path, trace_output_path, program, arguments, is_pyscript):
if not os.path.exists(reference_traces_path):
print "Cannot locate reference traces in " + reference_traces_path
return 1
if is_pyscript:
script = os.path.abspath(os.path.join('..', *os.path.split(program)))
argv = [self.env['PYTHON'], script] + arguments
try:
wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
except Utils.WafError, ex:
print >> sys.stderr, ex
return 1
else:
try:
wutils.run_program(program, self.env,
command_template=wutils.get_command_template(self.env, arguments),
cwd=trace_output_path)
except Utils.WafError, ex:
print >> sys.stderr, ex
return 1
rc = diff(trace_output_path, reference_traces_path, Options.options.verbose)
if rc:
print "----------"
print "Traces differ in test: ", self.test_name
print "Reference traces in directory: " + reference_traces_path
print "Traces in directory: " + trace_output_path
print "Run the following command for details:"
print "\tdiff -u %s %s" % (reference_traces_path, trace_output_path)
if not Options.options.verbose:
print "Or re-run regression testing with option -v"
print "----------"
return rc
def run_reference_generate(self, trace_output_path, program, arguments, is_pyscript):
if is_pyscript:
script = os.path.abspath(os.path.join('..', *os.path.split(program)))
argv = [self.env['PYTHON'], script] + arguments
try:
retval = wutils.run_argv(argv, self.env, cwd=trace_output_path, force_no_valgrind=True)
except Utils.WafError, ex:
print >> sys.stderr, ex
return 1
else:
try:
retval = wutils.run_program(program, self.env,
command_template=wutils.get_command_template(self.env, arguments),
cwd=trace_output_path)
except Utils.WafError, ex:
print >> sys.stderr, ex
return 1
return retval
class regression_test_collector_task(Task.TaskBase):
after = 'regression_test_task'
color = 'BLUE'
def __init__(self, bld, test_tasks):
self.bld = bld
super(regression_test_collector_task, self).__init__(generator=self)
self.test_tasks = test_tasks
def __str__(self):
return 'regression-test-collector\n'
def runnable_status(self):
return Task.RUN_ME
def run(self):
failed_tests = [test for test in self.test_tasks if test.result is not None and test.result != 0]
skipped_tests = [test for test in self.test_tasks if test.result is None]
print "Regression testing summary:"
if skipped_tests:
print "SKIP: %i of %i tests have been skipped (%s)" % (
len(skipped_tests), len(self.test_tasks),
', '.join([test.test_name for test in skipped_tests]))
if failed_tests:
print "FAIL: %i of %i tests have failed (%s)" % (
len(failed_tests), len(self.test_tasks),
', '.join([test.test_name for test in failed_tests]))
return 1
else:
print "PASS: %i of %i tests passed" % (len(self.test_tasks) - len(skipped_tests),
len(self.test_tasks))
return 0
def run_regression(bld, reference_traces):
"""Execute regression tests. Called with cwd set to the 'regression' subdir of ns-3.
@param reference_traces: reference traces directory.
"""
testdir = os.path.join("regression", "tests")
if not os.path.exists(testdir):
print "Tests directory does not exist"
sys.exit(3)
if Options.options.regression_tests:
tests = Options.options.regression_tests.split(',')
else:
tests = _find_tests(testdir)
if not os.path.exists(reference_traces):
print "Reference traces directory (%s) does not exist" % reference_traces
return 3
test_scripts_dir = bld.path.find_dir('regression/tests').abspath()
build_traces_dir = bld.path.find_or_declare('regression/traces').abspath(bld.env)
tasks = []
for test in tests:
task = regression_test_task(bld, bld.env, test, test_scripts_dir, build_traces_dir, reference_traces)
#bld.task_manager.add_task(task)
tasks.append(task)
regression_test_collector_task(bld, tasks)