-
Notifications
You must be signed in to change notification settings - Fork 368
/
testsuite.py
313 lines (269 loc) · 10.8 KB
/
testsuite.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
309
310
311
312
313
#
# This file is part of CasADi.
#
# CasADi -- A symbolic framework for dynamic optimization.
# Copyright (C) 2010 by Joel Andersson, Moritz Diehl, K.U.Leuven. All rights reserved.
#
# CasADi is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# CasADi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with CasADi; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
import os
import sys
from subprocess import *
import time
import re
import tempfile
class TimeoutEvent(Exception):
pass
def alarm_handler(signum, frame):
raise TimeoutEvent
def is_exe(root,name):
fpath = os.path.join(root,name)
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
from os import kill
from signal import signal
try:
from signal import alarm, SIGALRM, SIGKILL
alarm_available = True
except:
def alarm(amount):
pass
alarm_available = False
from subprocess import PIPE, Popen
# Snippet from http://stackoverflow.com/questions/1191374/subprocess-with-timeout
def run(args, input=None, cwd = None, shell = False, kill_tree = True, timeout = -1, env = None):
'''
Run a command with a timeout after which it will be forcibly
killed.
'''
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
p = Popen(args, shell = shell, cwd = cwd, stdout = PIPE, stderr = PIPE, env = env)
if timeout != -1:
if alarm_available:
signal(SIGALRM, alarm_handler)
alarm(timeout)
try:
stdout, stderr = p.communicate()
if timeout != -1:
alarm(0)
except Alarm:
pids = [p.pid]
if kill_tree:
pids.extend(get_process_children(p.pid))
for pid in pids:
# process might have died before getting to this line
# so wrap to avoid OSError: no such process
try:
kill(pid, SIGKILL)
except OSError:
pass
return -9, '', ''
return p.returncode, stdout, stderr
def killProcess(pid):
pids = [pid]
if kill_tree:
pids.extend(get_process_children(p.pid))
for pid in pids:
# process might have died before getting to this line
# so wrap to avoid OSError: no such process
try:
kill(pid, SIGKILL)
except OSError:
pass
def get_process_children(pid):
p = Popen('ps --no-headers -o pid --ppid %d' % pid, shell = True,
stdout = PIPE, stderr = PIPE)
stdout, stderr = p.communicate()
return [int(p) for p in stdout.split()]
if __name__ == '__main__':
print run('find /', shell = True, timeout = 3)
print run('find', shell = True)
deprecated = re.compile("[dD]epr[ei]c[ie]?at[ei]")
warning = re.compile("warning")
class TestSuite:
def __init__(self,suffix=None,dirname=None,preRun=None,postRun=None,command=None,skipdirs=[],skipfiles=[],inputs={},workingdir = lambda x: x,allowable_returncodes=[],args=[],stderr_trigger=[],stdout_trigger=[],check_depreciation=True,check_warning=False):
"""
dirname: The directory that should be crawled for test problems.
command: A mapping of (dirname,filename) -> Popen command list.
e.g lambda dir,fn: ['./'+fn] for executable files
skipdirs: A list of directories that should be skipped during recursive traversal of dirname.
skipfiles: A list of matched files that should be skipped during recursive traversal of dirname.
suffix: Only the files that end with this suffix will be tested.
Omit argument or set to None to test any file.
inputs: Can be a dictionary of filename -> string to pass numbers to STDIN of the process
workingdir: Specify the working directory for the process. The path may be relative to the /trunk/test path
stderr_trigger: list of strings/regexes. If any string is found in std_err, it is considered an error, regardless of the return_code. A message may be attached to a trigger by packing the string/regex in a 2-tuple, with the second element the message.
stdout_trigger: list of strings/regexes. If any string is found in std_out, it is considered an error, regardless of the return_code. A message may be attached to a trigger by packing the string/regex in a 2-tuple, with the second element the message.
check_depreciation: raise an error if the output contains a depreciation warning
check_warning: raise an error if the output contains a warning
args: a list of command line options:
-skipfiles="file1 file2" get's added to skipfiles
-memcheck Include a check for memory leaks
-passoptions="option1 option2" get's passed onto the command constructor as third argument
"""
if alarm_available:
signal(SIGALRM, alarm_handler)
# Don't buffer
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)
self.stats={'numtests':0,'numfails':0}
self.suffix = suffix
self.dirname = dirname
self.preRun = preRun
self.postRun = postRun
self.command = command
self.skipdirs = skipdirs
self.skipfiles = skipfiles
self.inputs = inputs
self.allowable_returncodes = allowable_returncodes
self.workingdir = workingdir
self.memcheck = False
self.passoptions = []
self.stderr_trigger = stderr_trigger
self.stdout_trigger = stdout_trigger
if check_depreciation:
self.stderr_trigger.append((deprecated,"deprecated"))
self.stdout_trigger.append((deprecated,"deprecated"))
if check_warning:
self.stderr_trigger.append((warning,"warning"))
self.stdout_trigger.append((warning,"warning"))
self.args=args
for arg in args:
okay = False
m = re.search('-skipfiles=(.*)', arg)
if m:
self.skipfiles+=m.group(1).split(' ')
okay = True
m = re.search('-memcheck', arg)
if m:
self.memcheck = True
okay = True
m = re.search('-passoptions=(.*)', arg)
if m:
self.passoptions+=m.group(1).split(' ')
okay = True
if not(okay):
print "Unknown argument: ", arg
def run(self):
print "Running test in " + self.dirname
for root, dirs, files in os.walk(self.dirname):
if not(self.preRun is None):
self.preRun(root)
for name in files:
for skip in self.skipdirs:
if skip in dirs:
dirs.remove(skip)
if (self.suffix is None and is_exe(root,name)) or (not(self.suffix is None) and name.endswith('.'+self.suffix)):
if not(name in self.skipfiles):
self.test(root,name,self.command)
if not(self.postRun is None):
self.postRun(root)
print "Ran %d tests, %d fails." % (self.stats['numtests'],self.stats['numfails'])
if self.stats['numfails']>0:
sys.exit(1)
def test(self,dir,fn,commands):
self.stats['numtests']+=1
print ("%02d. " % self.stats['numtests']) + fn
t0 = time.clock()
p=Popen(self.command(dir,fn,self.passoptions),cwd=self.workingdir(dir),stdout=PIPE, stderr=PIPE, stdin=PIPE)
inp = None
if fn in self.inputs:
inp = self.inputs[fn]
alarm(60*60) # 1 hour
try:
stdoutdata, stderrdata = p.communicate(inp)
except TimeoutEvent:
killProcess(p.pid)
raise Exception("Timout.")
alarm(0) # Remove alarm
t = time.clock() - t0
stderr_trigger = False
for trigger in self.stderr_trigger:
trigger_message = str(trigger)
if isinstance(trigger,tuple):
trigger_message = trigger[1]
trigger = trigger[0]
trigger_raised = re.search(trigger, stderrdata)
if trigger_raised:
print "stderr_trigger '%s' was raised." % trigger_message
stderr_trigger = True
stdout_trigger = False
for trigger in self.stdout_trigger:
trigger_message = str(trigger)
if isinstance(trigger,tuple):
trigger_message = trigger[1]
trigger = trigger[0]
trigger_raised = re.search(trigger, stdoutdata)
if trigger_raised:
print "stdout_trigger '%s' was raised." % trigger_message
stdout_trigger = True
if (not(stderr_trigger) and (p.returncode==0 or (p.returncode in self.allowable_returncodes))):
pass
#print " > Succes: %0.2f [ms]" % (t*1000)
else :
self.stats['numfails']+=1
print "In %s, %s failed:" % (dir,fn)
print "="*30
print "returncode: ", p.returncode
print stdoutdata
print stderrdata
print "="*30
return
if self.memcheck:
# --suppressions=../internal/valgrind-python.supp
suppressions = ["internal/valgrind-python.supp"]
supps = ":".join([os.path.join(os.getcwd(),s) for s in suppressions])
stdoutfile = tempfile.TemporaryFile()
p=Popen(['valgrind','--leak-check=full','--suppressions='+supps]+self.command(dir,fn,self.passoptions),cwd=self.workingdir(dir),stdout=stdoutfile, stderr=PIPE, stdin=PIPE)
f=Popen(['grep','-E','-A','10', "definitely lost|leaks|ERROR SUMMARY|Invalid read"],stdin=p.stderr,stdout=PIPE)
p.stderr.close()
alarm(60*60) # 1 hour
try:
stdoutdata, stderrdata = f.communicate()
stdoutdata = stdoutfile.read() + "\n"+ stdoutdata
except TimeoutEvent:
killProcess(p.pid)
killProcess(f.pid)
raise Exception("Timeout.")
alarm(0) # Remove alarm
m = re.search('definitely lost: (.*) bytes', stdoutdata)
lost = "0"
if m:
lost = m.group(1)
else:
m = re.search("no leaks are possible",stdoutdata)
if not(m):
print stdoutdata
raise Exception("valgrind output is not like expected: %s")
m = re.search('ERROR SUMMARY: (.*) errors', stdoutdata)
errors = "0"
if m:
errors = m.group(1)
else:
print stdoutdata
raise Exception("valgrind output is not like expected: %s")
errors = "0" # disabling valgrind error-checking for now: samples are flooded with errors
if not(lost=="0" and errors=="0"):
if not(lost=="0"):
print "Memory leak: lost %s bytes" % (lost)
if not(errors=="0"):
print "Valgrind errors: %s" % (errors)
print "="*30
print stdoutdata
print "="*30
self.stats['numfails']+=1