blob: a284d2b40a033da6abf1286402b40fec1c92d858 [file] [log] [blame]
Roger Meier41ad4342015-03-24 22:30:40 +01001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20import datetime
21import json
22import multiprocessing
23import os
24import platform
25import re
26import subprocess
27import sys
28import time
29import traceback
30
31from crossrunner.test import TestEntry
32
33LOG_DIR = 'log'
34RESULT_HTML = 'result.html'
35RESULT_JSON = 'results.json'
36FAIL_JSON = 'known_failures_%s.json'
37
38
39def generate_known_failures(testdir, overwrite, save, out):
40 def collect_failures(results):
41 success_index = 5
42 for r in results:
43 if not r[success_index]:
44 yield TestEntry.get_name(*r)
45 try:
46 with open(os.path.join(testdir, RESULT_JSON), 'r') as fp:
47 results = json.load(fp)
48 except IOError:
49 sys.stderr.write('Unable to load last result. Did you run tests ?\n')
50 return False
51 fails = collect_failures(results['results'])
52 if not overwrite:
53 known = load_known_failures(testdir)
54 known.extend(fails)
55 fails = known
Nobuaki Sukegawaf5b795d2015-03-29 14:48:48 +090056 fails_json = json.dumps(sorted(set(fails)), indent=2, separators=(',', ': '))
Roger Meier41ad4342015-03-24 22:30:40 +010057 if save:
58 with open(os.path.join(testdir, FAIL_JSON % platform.system()), 'w+') as fp:
59 fp.write(fails_json)
60 sys.stdout.write('Successfully updated known failures.\n')
61 if out:
62 sys.stdout.write(fails_json)
63 sys.stdout.write('\n')
64 return True
65
66
67def load_known_failures(testdir):
68 try:
69 with open(os.path.join(testdir, FAIL_JSON % platform.system()), 'r') as fp:
70 return json.load(fp)
71 except IOError:
72 return []
73
74
75class TestReporter(object):
76 # Unfortunately, standard library doesn't handle timezone well
77 # DATETIME_FORMAT = '%a %b %d %H:%M:%S %Z %Y'
78 DATETIME_FORMAT = '%a %b %d %H:%M:%S %Y'
79
80 def __init__(self):
81 self._log = multiprocessing.get_logger()
82 self._lock = multiprocessing.Lock()
83
84 @classmethod
Nobuaki Sukegawa783660a2015-04-12 00:32:40 +090085 def test_logfile(cls, test_name, prog_kind, dir=None):
86 relpath = os.path.join('log', '%s_%s.log' % (test_name, prog_kind))
87 return relpath if not dir else os.path.realpath(os.path.join(dir, relpath))
Roger Meier41ad4342015-03-24 22:30:40 +010088
89 def _start(self):
90 self._start_time = time.time()
91
92 @property
93 def _elapsed(self):
94 return time.time() - self._start_time
95
96 @classmethod
97 def _format_date(cls):
98 return '%s' % datetime.datetime.now().strftime(cls.DATETIME_FORMAT)
99
100 def _print_date(self):
101 self.out.write('%s\n' % self._format_date())
102
103 def _print_bar(self, out=None):
104 (out or self.out).write(
105 '======================================================================\n')
106
107 def _print_exec_time(self):
108 self.out.write('Test execution took {:.1f} seconds.\n'.format(self._elapsed))
109
110
111class ExecReporter(TestReporter):
112 def __init__(self, testdir, test, prog):
113 super(ExecReporter, self).__init__()
114 self._test = test
115 self._prog = prog
Nobuaki Sukegawa783660a2015-04-12 00:32:40 +0900116 self.logpath = self.test_logfile(test.name, prog.kind, testdir)
Roger Meier41ad4342015-03-24 22:30:40 +0100117 self.out = None
118
119 def begin(self):
120 self._start()
121 self._open()
122 if self.out and not self.out.closed:
123 self._print_header()
124 else:
125 self._log.debug('Output stream is not available.')
126
127 def end(self, returncode):
128 self._lock.acquire()
129 try:
130 if self.out and not self.out.closed:
131 self._print_footer(returncode)
132 self._close()
133 self.out = None
134 else:
135 self._log.debug('Output stream is not available.')
136 finally:
137 self._lock.release()
138
139 def killed(self):
140 self._lock.acquire()
141 try:
142 if self.out and not self.out.closed:
143 self._print_footer()
144 self._close()
145 self.out = None
146 else:
147 self._log.debug('Output stream is not available.')
148 finally:
149 self._lock.release()
150
151 _init_failure_exprs = {
152 'server': list(map(re.compile, [
153 '[Aa]ddress already in use',
154 'Could not bind',
155 'EADDRINUSE',
156 ])),
157 'client': list(map(re.compile, [
158 '[Cc]onnection refused',
159 'Could not connect to localhost',
160 'ECONNREFUSED',
161 'No such file or directory', # domain socket
162 ])),
163 }
164
165 def maybe_false_positive(self):
166 """Searches through log file for socket bind error.
167 Returns True if suspicious expression is found, otherwise False"""
168 def match(line):
169 for expr in exprs:
170 if expr.search(line):
171 return True
172 try:
173 if self.out and not self.out.closed:
174 self.out.flush()
175 exprs = list(map(re.compile, self._init_failure_exprs[self._prog.kind]))
176
177 server_logfile = self.logpath
178 # need to handle unicode errors on Python 3
179 kwargs = {} if sys.version_info[0] < 3 else {'errors': 'replace'}
180 with open(server_logfile, 'r', **kwargs) as fp:
181 if any(map(match, fp)):
182 return True
183 except (KeyboardInterrupt, SystemExit):
184 raise
185 except Exception as ex:
186 self._log.warn('[%s]: Error while detecting false positive: %s' % (self._test.name, str(ex)))
187 self._log.info(traceback.print_exc())
188 return False
189
190 def _open(self):
191 self.out = open(self.logpath, 'w+')
192
193 def _close(self):
194 self.out.close()
195
196 def _print_header(self):
197 self._print_date()
198 self.out.write('Executing: %s\n' % ' '.join(self._prog.command))
199 self.out.write('Directory: %s\n' % self._prog.workdir)
200 self.out.write('config:delay: %s\n' % self._test.delay)
201 self.out.write('config:timeout: %s\n' % self._test.timeout)
202 self._print_bar()
203 self.out.flush()
204
205 def _print_footer(self, returncode=None):
206 self._print_bar()
207 if returncode is not None:
208 self.out.write('Return code: %d\n' % returncode)
209 else:
210 self.out.write('Process is killed.\n')
211 self._print_exec_time()
212 self._print_date()
213
214
215class SummaryReporter(TestReporter):
216 def __init__(self, testdir, concurrent=True):
217 super(SummaryReporter, self).__init__()
218 self.testdir = testdir
219 self.logdir = os.path.join(testdir, LOG_DIR)
220 self.out_path = os.path.join(testdir, RESULT_JSON)
221 self.concurrent = concurrent
222 self.out = sys.stdout
223 self._platform = platform.system()
224 self._revision = self._get_revision()
225 self._tests = []
226 if not os.path.exists(self.logdir):
227 os.mkdir(self.logdir)
228 self._known_failures = load_known_failures(testdir)
229 self._unexpected_success = []
230 self._unexpected_failure = []
231 self._expected_failure = []
232 self._print_header()
233
234 def _get_revision(self):
235 p = subprocess.Popen(['git', 'rev-parse', '--short', 'HEAD'],
236 cwd=self.testdir, stdout=subprocess.PIPE)
237 out, _ = p.communicate()
238 return out.strip()
239
240 def _format_test(self, test, with_result=True):
241 name = '%s-%s' % (test.server.name, test.client.name)
242 trans = '%s-%s' % (test.transport, test.socket)
243 if not with_result:
244 return '{:19s}{:13s}{:25s}'.format(name[:18], test.protocol[:12], trans[:24])
245 else:
246 result = 'success' if test.success else (
247 'timeout' if test.expired else 'failure')
248 result_string = '%s(%d)' % (result, test.returncode)
249 return '{:19s}{:13s}{:25s}{:s}\n'.format(name[:18], test.protocol[:12], trans[:24], result_string)
250
251 def _print_test_header(self):
252 self._print_bar()
253 self.out.write(
254 '{:19s}{:13s}{:25s}{:s}\n'.format('server-client:', 'protocol:', 'transport:', 'result:'))
255
256 def _print_header(self):
257 self._start()
258 self.out.writelines([
259 'Apache Thrift - Integration Test Suite\n',
260 ])
261 self._print_date()
262 self._print_test_header()
263
264 def _print_unexpected_failure(self):
265 if len(self._unexpected_failure) > 0:
266 self.out.writelines([
267 '*** Following %d failures were unexpected ***:\n' % len(self._unexpected_failure),
268 'If it is introduced by you, please fix it before submitting the code.\n',
269 # 'If not, please report at https://issues.apache.org/jira/browse/THRIFT\n',
270 ])
271 self._print_test_header()
272 for i in self._unexpected_failure:
273 self.out.write(self._format_test(self._tests[i]))
274 self._print_bar()
275 else:
276 self.out.write('No unexpected failures.\n')
277
278 def _print_unexpected_success(self):
279 if len(self._unexpected_success) > 0:
280 self.out.write(
281 'Following %d tests were known to fail but succeeded (it\'s normal):\n' % len(self._unexpected_success))
282 self._print_test_header()
283 for i in self._unexpected_success:
284 self.out.write(self._format_test(self._tests[i]))
285 self._print_bar()
286
Nobuaki Sukegawaf5b795d2015-03-29 14:48:48 +0900287 def _http_server_command(self, port):
288 if sys.version_info[0] < 3:
289 return 'python -m SimpleHTTPServer %d' % port
290 else:
291 return 'python -m http.server %d' % port
292
Roger Meier41ad4342015-03-24 22:30:40 +0100293 def _print_footer(self):
294 fail_count = len(self._expected_failure) + len(self._unexpected_failure)
295 self._print_bar()
296 self._print_unexpected_success()
297 self._print_unexpected_failure()
298 self._write_html_data()
299 self._assemble_log('unexpected failures', self._unexpected_failure)
300 self._assemble_log('known failures', self._expected_failure)
301 self.out.writelines([
302 'You can browse results at:\n',
303 '\tfile://%s/%s\n' % (self.testdir, RESULT_HTML),
Nobuaki Sukegawaf5b795d2015-03-29 14:48:48 +0900304 '# If you use Chrome, run:\n',
305 '# \tcd %s\n#\t%s\n' % (self.testdir, self._http_server_command(8001)),
306 '# then browse:\n',
307 '# \thttp://localhost:%d/%s\n' % (8001, RESULT_HTML),
Roger Meier41ad4342015-03-24 22:30:40 +0100308 'Full log for each test is here:\n',
309 '\ttest/log/client_server_protocol_transport_client.log\n',
310 '\ttest/log/client_server_protocol_transport_server.log\n',
311 '%d failed of %d tests in total.\n' % (fail_count, len(self._tests)),
312 ])
313 self._print_exec_time()
314 self._print_date()
315
316 def _render_result(self, test):
317 return [
318 test.server.name,
319 test.client.name,
320 test.protocol,
321 test.transport,
322 test.socket,
323 test.success,
324 test.as_expected,
325 test.returncode,
326 {
Nobuaki Sukegawa783660a2015-04-12 00:32:40 +0900327 'server': self.test_logfile(test.name, test.server.kind),
328 'client': self.test_logfile(test.name, test.client.kind),
Roger Meier41ad4342015-03-24 22:30:40 +0100329 },
330 ]
331
332 def _write_html_data(self):
333 """Writes JSON data to be read by result html"""
334 results = [self._render_result(r) for r in self._tests]
335 with open(self.out_path, 'w+') as fp:
336 fp.write(json.dumps({
337 'date': self._format_date(),
338 'revision': str(self._revision),
339 'platform': self._platform,
340 'duration': '{:.1f}'.format(self._elapsed),
341 'results': results,
342 }, indent=2))
343
344 def _assemble_log(self, title, indexes):
345 if len(indexes) > 0:
346 def add_prog_log(fp, test, prog_kind):
347 fp.write('*************************** %s message ***************************\n'
348 % prog_kind)
Nobuaki Sukegawa783660a2015-04-12 00:32:40 +0900349 path = self.test_logfile(test.name, prog_kind, self.testdir)
Roger Meier41ad4342015-03-24 22:30:40 +0100350 kwargs = {} if sys.version_info[0] < 3 else {'errors': 'replace'}
351 with open(path, 'r', **kwargs) as prog_fp:
352 fp.write(prog_fp.read())
353 filename = title.replace(' ', '_') + '.log'
354 with open(os.path.join(self.logdir, filename), 'w+') as fp:
355 for test in map(self._tests.__getitem__, indexes):
356 fp.write('TEST: [%s]\n' % test.name)
357 add_prog_log(fp, test, test.server.kind)
358 add_prog_log(fp, test, test.client.kind)
359 fp.write('**********************************************************************\n\n')
360 self.out.write('%s are logged to test/%s/%s\n' % (title.capitalize(), LOG_DIR, filename))
361
362 def end(self):
363 self._print_footer()
364 return len(self._unexpected_failure) == 0
365
366 def add_test(self, test_dict):
367 test = TestEntry(self.testdir, **test_dict)
368 self._lock.acquire()
369 try:
370 if not self.concurrent:
371 self.out.write(self._format_test(test, False))
372 self.out.flush()
373 self._tests.append(test)
374 return len(self._tests) - 1
375 finally:
376 self._lock.release()
377
378 def add_result(self, index, returncode, expired):
379 self._lock.acquire()
380 try:
381 failed = returncode is None or returncode != 0
382 test = self._tests[index]
383 known = test.name in self._known_failures
384 if failed:
385 if known:
386 self._log.debug('%s failed as expected' % test.name)
387 self._expected_failure.append(index)
388 else:
389 self._log.info('unexpected failure: %s' % test.name)
390 self._unexpected_failure.append(index)
391 elif known:
392 self._log.info('unexpected success: %s' % test.name)
393 self._unexpected_success.append(index)
394 test.success = not failed
395 test.returncode = returncode
396 test.expired = expired
397 test.as_expected = known == failed
398 if not self.concurrent:
399 result = 'success' if not failed else 'failure'
400 result_string = '%s(%d)' % (result, returncode)
401 self.out.write(result_string + '\n')
402 else:
403 self.out.write(self._format_test(test))
404 finally:
405 self._lock.release()