blob: d50690ab37f8a32d144dfc8318919acd6cbd4651 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002# Copyright (C) 2016 Intel Corporation
Brad Bishopc342db32019-05-15 21:57:59 -04003#
4# SPDX-License-Identifier: MIT
5#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006
7import os
8import time
9import unittest
10import logging
Brad Bishopd7bf8c12018-02-25 22:55:05 -050011import re
Brad Bishopf86d0552018-12-04 14:18:15 -080012import json
Brad Bishopc342db32019-05-15 21:57:59 -040013import sys
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014
Brad Bishopf86d0552018-12-04 14:18:15 -080015from unittest import TextTestResult as _TestResult
16from unittest import TextTestRunner as _TestRunner
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017
18class OEStreamLogger(object):
19 def __init__(self, logger):
20 self.logger = logger
21 self.buffer = ""
22
23 def write(self, msg):
24 if len(msg) > 1 and msg[0] != '\n':
Brad Bishopd7bf8c12018-02-25 22:55:05 -050025 if '...' in msg:
26 self.buffer += msg
27 elif self.buffer:
28 self.buffer += msg
29 self.logger.log(logging.INFO, self.buffer)
30 self.buffer = ""
31 else:
32 self.logger.log(logging.INFO, msg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033
34 def flush(self):
35 for handler in self.logger.handlers:
36 handler.flush()
37
38class OETestResult(_TestResult):
39 def __init__(self, tc, *args, **kwargs):
40 super(OETestResult, self).__init__(*args, **kwargs)
41
Brad Bishopf86d0552018-12-04 14:18:15 -080042 self.successes = []
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080043 self.starttime = {}
44 self.endtime = {}
45 self.progressinfo = {}
Brad Bishop79641f22019-09-10 07:20:22 -040046 self.extraresults = {}
Brad Bishopf86d0552018-12-04 14:18:15 -080047
48 # Inject into tc so that TestDepends decorator can see results
49 tc.results = self
50
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 self.tc = tc
52
Brad Bishopc342db32019-05-15 21:57:59 -040053 # stdout and stderr for each test case
54 self.logged_output = {}
55
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056 def startTest(self, test):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080057 # May have been set by concurrencytest
58 if test.id() not in self.starttime:
59 self.starttime[test.id()] = time.time()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050060 super(OETestResult, self).startTest(test)
61
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080062 def stopTest(self, test):
63 self.endtime[test.id()] = time.time()
Brad Bishopc342db32019-05-15 21:57:59 -040064 if self.buffer:
65 self.logged_output[test.id()] = (
66 sys.stdout.getvalue(), sys.stderr.getvalue())
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080067 super(OETestResult, self).stopTest(test)
68 if test.id() in self.progressinfo:
69 self.tc.logger.info(self.progressinfo[test.id()])
70
71 # Print the errors/failures early to aid/speed debugging, its a pain
72 # to wait until selftest finishes to see them.
73 for t in ['failures', 'errors', 'skipped', 'expectedFailures']:
74 for (scase, msg) in getattr(self, t):
75 if test.id() == scase.id():
76 self.tc.logger.info(str(msg))
77 break
78
Brad Bishopd7bf8c12018-02-25 22:55:05 -050079 def logSummary(self, component, context_msg=''):
80 elapsed_time = self.tc._run_end_time - self.tc._run_start_time
81 self.tc.logger.info("SUMMARY:")
82 self.tc.logger.info("%s (%s) - Ran %d test%s in %.3fs" % (component,
83 context_msg, self.testsRun, self.testsRun != 1 and "s" or "",
84 elapsed_time))
85
86 if self.wasSuccessful():
87 msg = "%s - OK - All required tests passed" % component
88 else:
89 msg = "%s - FAIL - Required tests failed" % component
Brad Bishopf86d0552018-12-04 14:18:15 -080090 msg += " (successes=%d, skipped=%d, failures=%d, errors=%d)" % (len(self.successes), len(self.skipped), len(self.failures), len(self.errors))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050091 self.tc.logger.info(msg)
92
Brad Bishopf86d0552018-12-04 14:18:15 -080093 def _getTestResultDetails(self, case):
94 result_types = {'failures': 'FAILED', 'errors': 'ERROR', 'skipped': 'SKIPPED',
Brad Bishopc342db32019-05-15 21:57:59 -040095 'expectedFailures': 'EXPECTEDFAIL', 'successes': 'PASSED',
96 'unexpectedSuccesses' : 'PASSED'}
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097
Brad Bishopf86d0552018-12-04 14:18:15 -080098 for rtype in result_types:
99 found = False
Brad Bishopc342db32019-05-15 21:57:59 -0400100 for resultclass in getattr(self, rtype):
101 # unexpectedSuccesses are just lists, not lists of tuples
102 if isinstance(resultclass, tuple):
103 scase, msg = resultclass
104 else:
105 scase, msg = resultclass, None
Brad Bishopf86d0552018-12-04 14:18:15 -0800106 if case.id() == scase.id():
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 found = True
108 break
Brad Bishopf86d0552018-12-04 14:18:15 -0800109 scase_str = str(scase.id())
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110
Brad Bishopf86d0552018-12-04 14:18:15 -0800111 # When fails at module or class level the class name is passed as string
112 # so figure out to see if match
Brad Bishopc342db32019-05-15 21:57:59 -0400113 m = re.search(r"^setUpModule \((?P<module_name>.*)\).*$", scase_str)
Brad Bishopf86d0552018-12-04 14:18:15 -0800114 if m:
115 if case.__class__.__module__ == m.group('module_name'):
116 found = True
117 break
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118
Brad Bishopc342db32019-05-15 21:57:59 -0400119 m = re.search(r"^setUpClass \((?P<class_name>.*)\).*$", scase_str)
Brad Bishopf86d0552018-12-04 14:18:15 -0800120 if m:
121 class_name = "%s.%s" % (case.__class__.__module__,
122 case.__class__.__name__)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123
Brad Bishopf86d0552018-12-04 14:18:15 -0800124 if class_name == m.group('class_name'):
125 found = True
126 break
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127
Brad Bishopf86d0552018-12-04 14:18:15 -0800128 if found:
129 return result_types[rtype], msg
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130
Brad Bishopf86d0552018-12-04 14:18:15 -0800131 return 'UNKNOWN', None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132
Brad Bishop79641f22019-09-10 07:20:22 -0400133 def extractExtraResults(self, test, details = None):
134 extraresults = None
135 if details is not None and "extraresults" in details:
136 extraresults = details.get("extraresults", {})
137 elif hasattr(test, "extraresults"):
138 extraresults = test.extraresults
139
140 if extraresults is not None:
141 for k, v in extraresults.items():
142 # handle updating already existing entries (e.g. ptestresults.sections)
143 if k in self.extraresults:
144 self.extraresults[k].update(v)
145 else:
146 self.extraresults[k] = v
147
148 def addError(self, test, *args, details = None):
149 self.extractExtraResults(test, details = details)
150 return super(OETestResult, self).addError(test, *args)
151
152 def addFailure(self, test, *args, details = None):
153 self.extractExtraResults(test, details = details)
154 return super(OETestResult, self).addFailure(test, *args)
155
156 def addSuccess(self, test, details = None):
Brad Bishopf86d0552018-12-04 14:18:15 -0800157 #Added so we can keep track of successes too
158 self.successes.append((test, None))
Brad Bishop79641f22019-09-10 07:20:22 -0400159 self.extractExtraResults(test, details = details)
160 return super(OETestResult, self).addSuccess(test)
161
162 def addExpectedFailure(self, test, *args, details = None):
163 self.extractExtraResults(test, details = details)
164 return super(OETestResult, self).addExpectedFailure(test, *args)
165
166 def addUnexpectedSuccess(self, test, details = None):
167 self.extractExtraResults(test, details = details)
168 return super(OETestResult, self).addUnexpectedSuccess(test)
Brad Bishopf86d0552018-12-04 14:18:15 -0800169
Brad Bishopc342db32019-05-15 21:57:59 -0400170 def logDetails(self, json_file_dir=None, configuration=None, result_id=None,
171 dump_streams=False):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500172 self.tc.logger.info("RESULTS:")
Brad Bishopf86d0552018-12-04 14:18:15 -0800173
Brad Bishop79641f22019-09-10 07:20:22 -0400174 result = self.extraresults
Brad Bishopf86d0552018-12-04 14:18:15 -0800175 logs = {}
176 if hasattr(self.tc, "extraresults"):
Brad Bishop79641f22019-09-10 07:20:22 -0400177 result.update(self.tc.extraresults)
Brad Bishopf86d0552018-12-04 14:18:15 -0800178
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 for case_name in self.tc._registry['cases']:
180 case = self.tc._registry['cases'][case_name]
181
Brad Bishopf86d0552018-12-04 14:18:15 -0800182 (status, log) = self._getTestResultDetails(case)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500183
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800184 t = ""
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500185 duration = 0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800186 if case.id() in self.starttime and case.id() in self.endtime:
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500187 duration = self.endtime[case.id()] - self.starttime[case.id()]
188 t = " (" + "{0:.2f}".format(duration) + "s)"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800189
Brad Bishopf86d0552018-12-04 14:18:15 -0800190 if status not in logs:
191 logs[status] = []
Brad Bishopc342db32019-05-15 21:57:59 -0400192 logs[status].append("RESULTS - %s: %s%s" % (case.id(), status, t))
193 report = {'status': status}
Brad Bishopf86d0552018-12-04 14:18:15 -0800194 if log:
Brad Bishopc342db32019-05-15 21:57:59 -0400195 report['log'] = log
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500196 if duration:
197 report['duration'] = duration
Andrew Geissler635e0e42020-08-21 15:58:33 -0500198
199 alltags = []
200 # pull tags from the case class
201 if hasattr(case, "__oeqa_testtags"):
202 alltags.extend(getattr(case, "__oeqa_testtags"))
203 # pull tags from the method itself
204 test_name = case._testMethodName
205 if hasattr(case, test_name):
206 method = getattr(case, test_name)
207 if hasattr(method, "__oeqa_testtags"):
208 alltags.extend(getattr(method, "__oeqa_testtags"))
209 if alltags:
210 report['oetags'] = alltags
211
Brad Bishopc342db32019-05-15 21:57:59 -0400212 if dump_streams and case.id() in self.logged_output:
213 (stdout, stderr) = self.logged_output[case.id()]
214 report['stdout'] = stdout
215 report['stderr'] = stderr
216 result[case.id()] = report
Brad Bishopf86d0552018-12-04 14:18:15 -0800217
218 for i in ['PASSED', 'SKIPPED', 'EXPECTEDFAIL', 'ERROR', 'FAILED', 'UNKNOWN']:
219 if i not in logs:
220 continue
221 for l in logs[i]:
222 self.tc.logger.info(l)
223
224 if json_file_dir:
225 tresultjsonhelper = OETestResultJSONHelper()
226 tresultjsonhelper.dump_testresult_file(json_file_dir, configuration, result_id, result)
227
228 def wasSuccessful(self):
229 # Override as we unexpected successes aren't failures for us
230 return (len(self.failures) == len(self.errors) == 0)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500231
232class OEListTestsResult(object):
233 def wasSuccessful(self):
234 return True
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500235
236class OETestRunner(_TestRunner):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500237 streamLoggerClass = OEStreamLogger
238
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239 def __init__(self, tc, *args, **kwargs):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500240 kwargs['stream'] = self.streamLoggerClass(tc.logger)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241 super(OETestRunner, self).__init__(*args, **kwargs)
242 self.tc = tc
243 self.resultclass = OETestResult
244
Brad Bishopf86d0552018-12-04 14:18:15 -0800245 def _makeResult(self):
246 return self.resultclass(self.tc, self.stream, self.descriptions,
247 self.verbosity)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500248
249 def _walk_suite(self, suite, func):
250 for obj in suite:
251 if isinstance(obj, unittest.suite.TestSuite):
252 if len(obj._tests):
253 self._walk_suite(obj, func)
254 elif isinstance(obj, unittest.case.TestCase):
255 func(self.tc.logger, obj)
256 self._walked_cases = self._walked_cases + 1
257
258 def _list_tests_name(self, suite):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259 self._walked_cases = 0
260
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500261 def _list_cases(logger, case):
Brad Bishop79641f22019-09-10 07:20:22 -0400262 oetags = []
263 if hasattr(case, '__oeqa_testtags'):
264 oetags = getattr(case, '__oeqa_testtags')
265 if oetags:
266 logger.info("%s (%s)" % (case.id(), ",".join(oetags)))
267 else:
268 logger.info("%s" % (case.id()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269
270 self.tc.logger.info("Listing all available tests:")
271 self._walked_cases = 0
Brad Bishop79641f22019-09-10 07:20:22 -0400272 self.tc.logger.info("test (tags)")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500273 self.tc.logger.info("-" * 80)
274 self._walk_suite(suite, _list_cases)
275 self.tc.logger.info("-" * 80)
276 self.tc.logger.info("Total found:\t%s" % self._walked_cases)
277
278 def _list_tests_class(self, suite):
279 self._walked_cases = 0
280
281 curr = {}
282 def _list_classes(logger, case):
283 if not 'module' in curr or curr['module'] != case.__module__:
284 curr['module'] = case.__module__
285 logger.info(curr['module'])
286
287 if not 'class' in curr or curr['class'] != \
288 case.__class__.__name__:
289 curr['class'] = case.__class__.__name__
290 logger.info(" -- %s" % curr['class'])
291
292 logger.info(" -- -- %s" % case._testMethodName)
293
294 self.tc.logger.info("Listing all available test classes:")
295 self._walk_suite(suite, _list_classes)
296
297 def _list_tests_module(self, suite):
298 self._walked_cases = 0
299
300 listed = []
301 def _list_modules(logger, case):
302 if not case.__module__ in listed:
303 if case.__module__.startswith('_'):
304 logger.info("%s (hidden)" % case.__module__)
305 else:
306 logger.info(case.__module__)
307 listed.append(case.__module__)
308
309 self.tc.logger.info("Listing all available test modules:")
310 self._walk_suite(suite, _list_modules)
311
312 def list_tests(self, suite, display_type):
313 if display_type == 'name':
314 self._list_tests_name(suite)
315 elif display_type == 'class':
316 self._list_tests_class(suite)
317 elif display_type == 'module':
318 self._list_tests_module(suite)
319
320 return OEListTestsResult()
Brad Bishopf86d0552018-12-04 14:18:15 -0800321
322class OETestResultJSONHelper(object):
323
324 testresult_filename = 'testresults.json'
325
326 def _get_existing_testresults_if_available(self, write_dir):
327 testresults = {}
328 file = os.path.join(write_dir, self.testresult_filename)
329 if os.path.exists(file):
330 with open(file, "r") as f:
331 testresults = json.load(f)
332 return testresults
333
334 def _write_file(self, write_dir, file_name, file_content):
335 file_path = os.path.join(write_dir, file_name)
336 with open(file_path, 'w') as the_file:
337 the_file.write(file_content)
338
339 def dump_testresult_file(self, write_dir, configuration, result_id, test_result):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500340 try:
341 import bb
342 has_bb = True
343 bb.utils.mkdirhier(write_dir)
344 lf = bb.utils.lockfile(os.path.join(write_dir, 'jsontestresult.lock'))
345 except ImportError:
346 has_bb = False
347 os.makedirs(write_dir, exist_ok=True)
Brad Bishopf86d0552018-12-04 14:18:15 -0800348 test_results = self._get_existing_testresults_if_available(write_dir)
349 test_results[result_id] = {'configuration': configuration, 'result': test_result}
350 json_testresults = json.dumps(test_results, sort_keys=True, indent=4)
351 self._write_file(write_dir, self.testresult_filename, json_testresults)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500352 if has_bb:
353 bb.utils.unlockfile(lf)