forked from sfepy/sfepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_install.py
executable file
·287 lines (209 loc) · 9.46 KB
/
test_install.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
#!/usr/bin/env python
"""
Simple script for testing various SfePy functionality, examples not
covered by tests, and running the tests.
The script just runs the commands specified in its main() using the
`subprocess` module, captures the output and compares one or more key
words to the expected ones.
The output of failed commands is saved to 'test_install.log' file.
"""
from __future__ import print_function
from __future__ import absolute_import
import time
import sys
from argparse import ArgumentParser, RawDescriptionHelpFormatter
import shlex
import subprocess
import logging
import re
DEBUG_FMT = '*' * 55 + '\n%s\n' + '*' * 55
def _get_logger(filename='test_install.log'):
"""
Convenience function to set-up output and logging.
"""
logger = logging.getLogger('test_install.py')
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
file_handler = logging.FileHandler(filename)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
return logger
logger = _get_logger()
def check_output(cmd):
"""
Run the specified command and capture its outputs.
Returns
-------
out : tuple
The (stdout, stderr) output tuple.
"""
logger.info(cmd)
args = shlex.split(cmd)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = [ii.decode() for ii in p.communicate()]
return out
def report(out, name, line, item, value, eps=None, return_item=False,
match_numbers=False):
"""
Check that `item` at `line` of the output string `out` is equal
to `value`. If not, print the output.
"""
try:
if match_numbers:
status = out.split('\n')[line]
else:
status = out.split('\n')[line].split()
except IndexError:
logger.error(' not enough output from command!')
ok = False
else:
try:
if match_numbers:
pat = '([-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?[jJ]?)'
matches = re.findall(pat, status)
status_item = matches[item]
else:
status_item = status[item]
logger.info(' comparing: %s %s', status_item, value)
if eps is None:
ok = (status_item == value)
else:
try:
ok = abs(float(status_item) - float(value)) < eps
except:
ok = False
except IndexError:
ok = False
logger.info(' %s: %s', name, ok)
if not ok:
logger.debug(DEBUG_FMT, out)
if return_item:
return ok, status[item]
else:
return ok
def report2(out, name, items, return_item=False):
"""
Check that `items` are in the output string `out`.
If not, print the output.
"""
ok = True
for s in items:
logger.info(' checking: %s', s)
if s not in out:
ok = False
break
logger.info(' %s: %s', name, ok)
if not ok:
logger.debug(DEBUG_FMT, out)
if return_item:
return ok, s
else:
return ok
def report_tests(out, return_item=False):
"""
Check that all tests in the output string `out` passed.
If not, print the output.
"""
search = re.compile('([0-9]+) test file\(s\) executed in ([0-9.]+) s, ([0-9]+) failure\(s\) of ([0-9]+) test\(s\)').search
try:
stats = search(out).groups()
except AttributeError:
stats = '0', '0', '-1', '0'
ok = False
ok = stats[2] == '0'
logger.info(' %s test file(s) executed in %s s, %s failure(s) of %s test(s)'
% (stats[0], stats[1], stats[2], stats[3]))
if not ok:
logger.debug(DEBUG_FMT, out)
if return_item:
return ok, stats[2]
else:
return ok
def main():
parser = ArgumentParser(description=__doc__,
formatter_class=RawDescriptionHelpFormatter)
parser.add_argument('--version', action='version', version='%(prog)s')
parser.parse_args()
fd = open('test_install.log', 'w')
fd.close()
if sys.version_info[0] < 3:
cmd = 'python2'
else:
cmd = 'python3'
eok = 0
t0 = time.time()
out, err = check_output('%s ./script/blockgen.py' % cmd)
eok += report(out, '...', -2, 1, '...done')
out, err = check_output('%s ./script/cylindergen.py' % cmd)
eok += report(out, '...', -2, 1, '...done')
out, err = check_output('%s ./script/convert_mesh.py meshes/3d/cylinder.vtk out.mesh' % cmd)
eok += report(out, '...', -2, 1, '...done')
out, err = check_output('%s ./script/tile_periodic_mesh.py -r 2,2 meshes/elements/2_4_2.mesh out-per.mesh' % cmd)
eok += report(out, '...', -2, 1, 'done.')
out, err = check_output('%s ./script/extract_surface.py meshes/various_formats/octahedron.node -' % cmd)
eok += report(out, '...', -2, 0, '1185')
out, err = check_output('%s ./simple.py examples/diffusion/poisson.py' % cmd)
eok += report(out, '...', -3, 5, '1.173819e-16', eps=1e-15)
out, err = check_output("""%s ./simple.py -c "ebc_2 : {'name' : 't2', 'region' : 'Gamma_Right', 'dofs' : {'t.0' : -5.0}}" examples/diffusion/poisson.py""" %cmd)
eok += report(out, '...', -3, 5, '2.308051e-16', eps=1e-15)
out, err = check_output('%s ./simple.py examples/diffusion/poisson_iga.py' % cmd)
eok += report(out, '...', -3, 5, '3.373487e-15', eps=1e-14)
out, err = check_output('%s ./simple.py examples/navier_stokes/stokes.py' % cmd)
eok += report(out, '...', -3, 5, '1.210678e-13', eps=1e-11)
out, err = check_output('%s ./simple.py examples/diffusion/poisson_parametric_study.py' % cmd)
eok += report(out, '...', -3, 5, '1.606408e-14', eps=1e-13)
out, err = check_output('%s ./simple.py examples/linear_elasticity/its2D_3.py' % cmd)
eok += report(out, '...', -24, 5, '3.964886e-12', eps=1e-11)
eok += report(out, '...', -4, 4, '2.58660e+01', eps=1e-5)
out, err = check_output('%s ./simple.py examples/linear_elasticity/linear_elastic.py --format h5' % cmd)
eok += report(out, '...', -3, 5, '4.638192e-18', eps=1e-15)
out, err = check_output('%s ./extractor.py -d cylinder.h5' % cmd)
eok += report(out, '...', -2, 1, '...done')
out, err = check_output('%s ./postproc.py -n --no-offscreen -o cylinder.png cylinder.h5' % cmd)
eok += report(out, '...', -3, 2, 'cylinder.png...')
out, err = check_output('%s ./phonon.py examples/phononic/band_gaps.py' % cmd)
eok += report(out, '...', -9, 0, '2.08545116e+08', match_numbers=True)
eok += report(out, '...', -8, 1, '1.16309223e+11', match_numbers=True)
out, err = check_output('%s ./phonon.py examples/phononic/band_gaps.py --phase-velocity' % cmd)
eok += report(out, '...', -2, 0, '4189.41229592', match_numbers=True)
eok += report(out, '...', -2, 1, '2620.55608256', match_numbers=True)
out, err = check_output('%s ./phonon.py examples/phononic/band_gaps.py -d' % cmd)
eok += report(out, '...', -6, 1, '[0,')
out, err = check_output('%s ./phonon.py examples/phononic/band_gaps_rigid.py' % cmd)
eok += report(out, '...', -9, 0, '4.58709531e+07', match_numbers=True)
eok += report(out, '...', -8, 1, '1.13929200e+11', match_numbers=True)
out, err = check_output('%s ./simple.py examples/quantum/hydrogen.py' % cmd)
eok += report(out, '...', -2, -2, '-0.01913506', eps=1e-4)
out, err = check_output('%s ./homogen.py examples/homogenization/perfusion_micro.py' % cmd)
eok += report2(out, '...', ['computing EpA', 'computing PA_3',
'computing GA', 'computing EmA',
'computing KA'])
out, err = check_output('%s examples/homogenization/rs_correctors.py -n' % cmd)
eok += report(out, '...', -2, -1, '1.644e-01', match_numbers=True)
out, err = check_output('%s examples/large_deformation/compare_elastic_materials.py -n' % cmd)
eok += report(out, '...', -3, 5, '1.068759e-14', eps=1e-13)
out, err = check_output('%s examples/linear_elasticity/linear_elastic_interactive.py' % cmd)
eok += report(out, '...', -16, 0, '1.62128841139e-14', eps=1e-13)
out, err = check_output('%s examples/linear_elasticity/modal_analysis.py' % cmd)
eok += report(out, '...', -12, 5, '12142.11470773', eps=1e-13)
out, err = check_output('%s examples/multi_physics/thermal_electric.py' % cmd)
eok += report(out, '...', -4, 5, '2.612933e-14', eps=1e-13)
out, err = check_output('%s examples/diffusion/laplace_refine_interactive.py output' % cmd)
eok += report(out, '...', -3, 5, '2.675866e-15', eps=1e-13)
out, err = check_output('mpiexec -n 2 %s examples/diffusion/poisson_parallel_interactive.py output-parallel -2 --silent -ksp_monitor' % cmd)
eok += report(out, '...', -2, 4, '8.021313824020e-07', eps=1e-6)
out, err = check_output('mpiexec -n 2 %s examples/multi_physics/biot_parallel_interactive.py output-parallel -2 --silent -ksp_monitor' % cmd)
eok += report(out, '...', -2, 4, '3.787214380277e-09', eps=1e-8)
t1 = time.time()
out, err = check_output('%s ./run_tests.py' % cmd)
tok, failed = report_tests(out, return_item=True)
tok = {True : 'ok', False : 'fail'}[tok]
t2 = time.time()
fd = open('test_install_times.log', 'a+')
fd.write('%s: examples: %.2f [s] (%d), tests: %.2f [s] (%s: %s)\n'
% (time.ctime(t0), t1 - t0, eok, t2 - t1, tok, failed))
fd.close()
if __name__ == '__main__':
main()