.. | .. |
---|
23 | 23 | from tdc_helper import * |
---|
24 | 24 | |
---|
25 | 25 | import TdcPlugin |
---|
| 26 | +from TdcResults import * |
---|
26 | 27 | |
---|
| 28 | +class PluginDependencyException(Exception): |
---|
| 29 | + def __init__(self, missing_pg): |
---|
| 30 | + self.missing_pg = missing_pg |
---|
27 | 31 | |
---|
28 | 32 | class PluginMgrTestFail(Exception): |
---|
29 | 33 | def __init__(self, stage, output, message): |
---|
.. | .. |
---|
36 | 40 | super().__init__() |
---|
37 | 41 | self.plugins = {} |
---|
38 | 42 | self.plugin_instances = [] |
---|
39 | | - self.args = [] |
---|
| 43 | + self.failed_plugins = {} |
---|
40 | 44 | self.argparser = argparser |
---|
41 | 45 | |
---|
42 | 46 | # TODO, put plugins in order |
---|
.. | .. |
---|
52 | 56 | self.plugins[mn] = foo |
---|
53 | 57 | self.plugin_instances.append(foo.SubPlugin()) |
---|
54 | 58 | |
---|
| 59 | + def load_plugin(self, pgdir, pgname): |
---|
| 60 | + pgname = pgname[0:-3] |
---|
| 61 | + foo = importlib.import_module('{}.{}'.format(pgdir, pgname)) |
---|
| 62 | + self.plugins[pgname] = foo |
---|
| 63 | + self.plugin_instances.append(foo.SubPlugin()) |
---|
| 64 | + self.plugin_instances[-1].check_args(self.args, None) |
---|
| 65 | + |
---|
| 66 | + def get_required_plugins(self, testlist): |
---|
| 67 | + ''' |
---|
| 68 | + Get all required plugins from the list of test cases and return |
---|
| 69 | + all unique items. |
---|
| 70 | + ''' |
---|
| 71 | + reqs = [] |
---|
| 72 | + for t in testlist: |
---|
| 73 | + try: |
---|
| 74 | + if 'requires' in t['plugins']: |
---|
| 75 | + if isinstance(t['plugins']['requires'], list): |
---|
| 76 | + reqs.extend(t['plugins']['requires']) |
---|
| 77 | + else: |
---|
| 78 | + reqs.append(t['plugins']['requires']) |
---|
| 79 | + except KeyError: |
---|
| 80 | + continue |
---|
| 81 | + reqs = get_unique_item(reqs) |
---|
| 82 | + return reqs |
---|
| 83 | + |
---|
| 84 | + def load_required_plugins(self, reqs, parser, args, remaining): |
---|
| 85 | + ''' |
---|
| 86 | + Get all required plugins from the list of test cases and load any plugin |
---|
| 87 | + that is not already enabled. |
---|
| 88 | + ''' |
---|
| 89 | + pgd = ['plugin-lib', 'plugin-lib-custom'] |
---|
| 90 | + pnf = [] |
---|
| 91 | + |
---|
| 92 | + for r in reqs: |
---|
| 93 | + if r not in self.plugins: |
---|
| 94 | + fname = '{}.py'.format(r) |
---|
| 95 | + source_path = [] |
---|
| 96 | + for d in pgd: |
---|
| 97 | + pgpath = '{}/{}'.format(d, fname) |
---|
| 98 | + if os.path.isfile(pgpath): |
---|
| 99 | + source_path.append(pgpath) |
---|
| 100 | + if len(source_path) == 0: |
---|
| 101 | + print('ERROR: unable to find required plugin {}'.format(r)) |
---|
| 102 | + pnf.append(fname) |
---|
| 103 | + continue |
---|
| 104 | + elif len(source_path) > 1: |
---|
| 105 | + print('WARNING: multiple copies of plugin {} found, using version found') |
---|
| 106 | + print('at {}'.format(source_path[0])) |
---|
| 107 | + pgdir = source_path[0] |
---|
| 108 | + pgdir = pgdir.split('/')[0] |
---|
| 109 | + self.load_plugin(pgdir, fname) |
---|
| 110 | + if len(pnf) > 0: |
---|
| 111 | + raise PluginDependencyException(pnf) |
---|
| 112 | + |
---|
| 113 | + parser = self.call_add_args(parser) |
---|
| 114 | + (args, remaining) = parser.parse_known_args(args=remaining, namespace=args) |
---|
| 115 | + return args |
---|
| 116 | + |
---|
55 | 117 | def call_pre_suite(self, testcount, testidlist): |
---|
56 | 118 | for pgn_inst in self.plugin_instances: |
---|
57 | 119 | pgn_inst.pre_suite(testcount, testidlist) |
---|
.. | .. |
---|
60 | 122 | for pgn_inst in reversed(self.plugin_instances): |
---|
61 | 123 | pgn_inst.post_suite(index) |
---|
62 | 124 | |
---|
63 | | - def call_pre_case(self, test_ordinal, testid): |
---|
| 125 | + def call_pre_case(self, caseinfo, *, test_skip=False): |
---|
64 | 126 | for pgn_inst in self.plugin_instances: |
---|
65 | 127 | try: |
---|
66 | | - pgn_inst.pre_case(test_ordinal, testid) |
---|
| 128 | + pgn_inst.pre_case(caseinfo, test_skip) |
---|
67 | 129 | except Exception as ee: |
---|
68 | 130 | print('exception {} in call to pre_case for {} plugin'. |
---|
69 | 131 | format(ee, pgn_inst.__class__)) |
---|
70 | 132 | print('test_ordinal is {}'.format(test_ordinal)) |
---|
71 | | - print('testid is {}'.format(testid)) |
---|
| 133 | + print('testid is {}'.format(caseinfo['id'])) |
---|
72 | 134 | raise |
---|
73 | 135 | |
---|
74 | 136 | def call_post_case(self): |
---|
.. | .. |
---|
97 | 159 | command = pgn_inst.adjust_command(stage, command) |
---|
98 | 160 | return command |
---|
99 | 161 | |
---|
| 162 | + def set_args(self, args): |
---|
| 163 | + self.args = args |
---|
| 164 | + |
---|
100 | 165 | @staticmethod |
---|
101 | 166 | def _make_argparser(args): |
---|
102 | 167 | self.argparser = argparse.ArgumentParser( |
---|
103 | 168 | description='Linux TC unit tests') |
---|
104 | | - |
---|
105 | 169 | |
---|
106 | 170 | def replace_keywords(cmd): |
---|
107 | 171 | """ |
---|
.. | .. |
---|
131 | 195 | stdout=subprocess.PIPE, |
---|
132 | 196 | stderr=subprocess.PIPE, |
---|
133 | 197 | env=ENVIR) |
---|
134 | | - (rawout, serr) = proc.communicate() |
---|
135 | 198 | |
---|
136 | | - if proc.returncode != 0 and len(serr) > 0: |
---|
137 | | - foutput = serr.decode("utf-8", errors="ignore") |
---|
138 | | - else: |
---|
139 | | - foutput = rawout.decode("utf-8", errors="ignore") |
---|
| 199 | + try: |
---|
| 200 | + (rawout, serr) = proc.communicate(timeout=NAMES['TIMEOUT']) |
---|
| 201 | + if proc.returncode != 0 and len(serr) > 0: |
---|
| 202 | + foutput = serr.decode("utf-8", errors="ignore") |
---|
| 203 | + else: |
---|
| 204 | + foutput = rawout.decode("utf-8", errors="ignore") |
---|
| 205 | + except subprocess.TimeoutExpired: |
---|
| 206 | + foutput = "Command \"{}\" timed out\n".format(command) |
---|
| 207 | + proc.returncode = 255 |
---|
140 | 208 | |
---|
141 | 209 | proc.stdout.close() |
---|
142 | 210 | proc.stderr.close() |
---|
.. | .. |
---|
183 | 251 | result = True |
---|
184 | 252 | tresult = "" |
---|
185 | 253 | tap = "" |
---|
| 254 | + res = TestResult(tidx['id'], tidx['name']) |
---|
186 | 255 | if args.verbose > 0: |
---|
187 | 256 | print("\t====================\n=====> ", end="") |
---|
188 | 257 | print("Test " + tidx["id"] + ": " + tidx["name"]) |
---|
189 | 258 | |
---|
| 259 | + if 'skip' in tidx: |
---|
| 260 | + if tidx['skip'] == 'yes': |
---|
| 261 | + res = TestResult(tidx['id'], tidx['name']) |
---|
| 262 | + res.set_result(ResultState.skip) |
---|
| 263 | + res.set_errormsg('Test case designated as skipped.') |
---|
| 264 | + pm.call_pre_case(tidx, test_skip=True) |
---|
| 265 | + pm.call_post_execute() |
---|
| 266 | + return res |
---|
| 267 | + |
---|
190 | 268 | # populate NAMES with TESTID for this test |
---|
191 | 269 | NAMES['TESTID'] = tidx['id'] |
---|
192 | 270 | |
---|
193 | | - pm.call_pre_case(index, tidx['id']) |
---|
| 271 | + pm.call_pre_case(tidx) |
---|
194 | 272 | prepare_env(args, pm, 'setup', "-----> prepare stage", tidx["setup"]) |
---|
195 | 273 | |
---|
196 | 274 | if (args.verbose > 0): |
---|
.. | .. |
---|
205 | 283 | pm.call_post_execute() |
---|
206 | 284 | |
---|
207 | 285 | if (exit_code is None or exit_code != int(tidx["expExitCode"])): |
---|
208 | | - result = False |
---|
209 | 286 | print("exit: {!r}".format(exit_code)) |
---|
210 | 287 | print("exit: {}".format(int(tidx["expExitCode"]))) |
---|
211 | 288 | #print("exit: {!r} {}".format(exit_code, int(tidx["expExitCode"]))) |
---|
| 289 | + res.set_result(ResultState.fail) |
---|
| 290 | + res.set_failmsg('Command exited with {}, expected {}\n{}'.format(exit_code, tidx["expExitCode"], procout)) |
---|
212 | 291 | print(procout) |
---|
213 | 292 | else: |
---|
214 | 293 | if args.verbose > 0: |
---|
.. | .. |
---|
219 | 298 | if procout: |
---|
220 | 299 | match_index = re.findall(match_pattern, procout) |
---|
221 | 300 | if len(match_index) != int(tidx["matchCount"]): |
---|
222 | | - result = False |
---|
| 301 | + res.set_result(ResultState.fail) |
---|
| 302 | + res.set_failmsg('Could not match regex pattern. Verify command output:\n{}'.format(procout)) |
---|
| 303 | + else: |
---|
| 304 | + res.set_result(ResultState.success) |
---|
223 | 305 | elif int(tidx["matchCount"]) != 0: |
---|
224 | | - result = False |
---|
225 | | - |
---|
226 | | - if not result: |
---|
227 | | - tresult += 'not ' |
---|
228 | | - tresult += 'ok {} - {} # {}\n'.format(str(index), tidx['id'], tidx['name']) |
---|
229 | | - tap += tresult |
---|
230 | | - |
---|
231 | | - if result == False: |
---|
232 | | - if procout: |
---|
233 | | - tap += procout |
---|
| 306 | + res.set_result(ResultState.fail) |
---|
| 307 | + res.set_failmsg('No output generated by verify command.') |
---|
234 | 308 | else: |
---|
235 | | - tap += 'No output!\n' |
---|
| 309 | + res.set_result(ResultState.success) |
---|
236 | 310 | |
---|
237 | 311 | prepare_env(args, pm, 'teardown', '-----> teardown stage', tidx['teardown'], procout) |
---|
238 | 312 | pm.call_post_case() |
---|
.. | .. |
---|
241 | 315 | |
---|
242 | 316 | # remove TESTID from NAMES |
---|
243 | 317 | del(NAMES['TESTID']) |
---|
244 | | - return tap |
---|
| 318 | + return res |
---|
245 | 319 | |
---|
246 | 320 | def test_runner(pm, args, filtered_tests): |
---|
247 | 321 | """ |
---|
.. | .. |
---|
261 | 335 | emergency_exit = False |
---|
262 | 336 | emergency_exit_message = '' |
---|
263 | 337 | |
---|
264 | | - if args.notap: |
---|
265 | | - if args.verbose: |
---|
266 | | - tap = 'notap requested: omitting test plan\n' |
---|
267 | | - else: |
---|
268 | | - tap = str(index) + ".." + str(tcount) + "\n" |
---|
| 338 | + tsr = TestSuiteReport() |
---|
| 339 | + |
---|
269 | 340 | try: |
---|
270 | 341 | pm.call_pre_suite(tcount, [tidx['id'] for tidx in testlist]) |
---|
271 | 342 | except Exception as ee: |
---|
272 | 343 | ex_type, ex, ex_tb = sys.exc_info() |
---|
273 | 344 | print('Exception {} {} (caught in pre_suite).'. |
---|
274 | 345 | format(ex_type, ex)) |
---|
275 | | - # when the extra print statements are uncommented, |
---|
276 | | - # the traceback does not appear between them |
---|
277 | | - # (it appears way earlier in the tdc.py output) |
---|
278 | | - # so don't bother ... |
---|
279 | | - # print('--------------------(') |
---|
280 | | - # print('traceback') |
---|
281 | 346 | traceback.print_tb(ex_tb) |
---|
282 | | - # print('--------------------)') |
---|
283 | 347 | emergency_exit_message = 'EMERGENCY EXIT, call_pre_suite failed with exception {} {}\n'.format(ex_type, ex) |
---|
284 | 348 | emergency_exit = True |
---|
285 | 349 | stage = 'pre-SUITE' |
---|
.. | .. |
---|
292 | 356 | time.sleep(2) |
---|
293 | 357 | for tidx in testlist: |
---|
294 | 358 | if "flower" in tidx["category"] and args.device == None: |
---|
| 359 | + errmsg = "Tests using the DEV2 variable must define the name of a " |
---|
| 360 | + errmsg += "physical NIC with the -d option when running tdc.\n" |
---|
| 361 | + errmsg += "Test has been skipped." |
---|
295 | 362 | if args.verbose > 1: |
---|
296 | | - print('Not executing test {} {} because DEV2 not defined'. |
---|
297 | | - format(tidx['id'], tidx['name'])) |
---|
| 363 | + print(errmsg) |
---|
| 364 | + res = TestResult(tidx['id'], tidx['name']) |
---|
| 365 | + res.set_result(ResultState.skip) |
---|
| 366 | + res.set_errormsg(errmsg) |
---|
| 367 | + tsr.add_resultdata(res) |
---|
298 | 368 | continue |
---|
299 | 369 | try: |
---|
300 | 370 | badtest = tidx # in case it goes bad |
---|
301 | | - tap += run_one_test(pm, args, index, tidx) |
---|
| 371 | + res = run_one_test(pm, args, index, tidx) |
---|
| 372 | + tsr.add_resultdata(res) |
---|
302 | 373 | except PluginMgrTestFail as pmtf: |
---|
303 | 374 | ex_type, ex, ex_tb = sys.exc_info() |
---|
304 | 375 | stage = pmtf.stage |
---|
305 | 376 | message = pmtf.message |
---|
306 | 377 | output = pmtf.output |
---|
| 378 | + res = TestResult(tidx['id'], tidx['name']) |
---|
| 379 | + res.set_result(ResultState.skip) |
---|
| 380 | + res.set_errormsg(pmtf.message) |
---|
| 381 | + res.set_failmsg(pmtf.output) |
---|
| 382 | + tsr.add_resultdata(res) |
---|
| 383 | + index += 1 |
---|
307 | 384 | print(message) |
---|
308 | 385 | print('Exception {} {} (caught in test_runner, running test {} {} {} stage {})'. |
---|
309 | 386 | format(ex_type, ex, index, tidx['id'], tidx['name'], stage)) |
---|
.. | .. |
---|
322 | 399 | # if we failed in setup or teardown, |
---|
323 | 400 | # fill in the remaining tests with ok-skipped |
---|
324 | 401 | count = index |
---|
325 | | - if not args.notap: |
---|
326 | | - tap += 'about to flush the tap output if tests need to be skipped\n' |
---|
327 | | - if tcount + 1 != index: |
---|
328 | | - for tidx in testlist[index - 1:]: |
---|
329 | | - msg = 'skipped - previous {} failed'.format(stage) |
---|
330 | | - tap += 'ok {} - {} # {} {} {}\n'.format( |
---|
331 | | - count, tidx['id'], msg, index, badtest.get('id', '--Unknown--')) |
---|
332 | | - count += 1 |
---|
333 | 402 | |
---|
334 | | - tap += 'done flushing skipped test tap output\n' |
---|
| 403 | + if tcount + 1 != count: |
---|
| 404 | + for tidx in testlist[count - 1:]: |
---|
| 405 | + res = TestResult(tidx['id'], tidx['name']) |
---|
| 406 | + res.set_result(ResultState.skip) |
---|
| 407 | + msg = 'skipped - previous {} failed {} {}'.format(stage, |
---|
| 408 | + index, badtest.get('id', '--Unknown--')) |
---|
| 409 | + res.set_errormsg(msg) |
---|
| 410 | + tsr.add_resultdata(res) |
---|
| 411 | + count += 1 |
---|
335 | 412 | |
---|
336 | 413 | if args.pause: |
---|
337 | 414 | print('Want to pause\nPress enter to continue ...') |
---|
.. | .. |
---|
340 | 417 | |
---|
341 | 418 | pm.call_post_suite(index) |
---|
342 | 419 | |
---|
343 | | - return tap |
---|
| 420 | + return tsr |
---|
344 | 421 | |
---|
345 | 422 | def has_blank_ids(idlist): |
---|
346 | 423 | """ |
---|
.. | .. |
---|
381 | 458 | Set the command line arguments for tdc. |
---|
382 | 459 | """ |
---|
383 | 460 | parser.add_argument( |
---|
| 461 | + '--outfile', type=str, |
---|
| 462 | + help='Path to the file in which results should be saved. ' + |
---|
| 463 | + 'Default target is the current directory.') |
---|
| 464 | + parser.add_argument( |
---|
384 | 465 | '-p', '--path', type=str, |
---|
385 | 466 | help='The full path to the tc executable to use') |
---|
386 | 467 | sg = parser.add_argument_group( |
---|
.. | .. |
---|
416 | 497 | '-v', '--verbose', action='count', default=0, |
---|
417 | 498 | help='Show the commands that are being run') |
---|
418 | 499 | parser.add_argument( |
---|
419 | | - '-N', '--notap', action='store_true', |
---|
420 | | - help='Suppress tap results for command under test') |
---|
| 500 | + '--format', default='tap', const='tap', nargs='?', |
---|
| 501 | + choices=['none', 'xunit', 'tap'], |
---|
| 502 | + help='Specify the format for test results. (Default: TAP)') |
---|
421 | 503 | parser.add_argument('-d', '--device', |
---|
422 | | - help='Execute the test case in flower category') |
---|
| 504 | + help='Execute test cases that use a physical device, ' + |
---|
| 505 | + 'where DEVICE is its name. (If not defined, tests ' + |
---|
| 506 | + 'that require a physical device will be skipped)') |
---|
423 | 507 | parser.add_argument( |
---|
424 | 508 | '-P', '--pause', action='store_true', |
---|
425 | 509 | help='Pause execution just before post-suite stage') |
---|
.. | .. |
---|
438 | 522 | NAMES['TC'] = args.path |
---|
439 | 523 | if args.device != None: |
---|
440 | 524 | NAMES['DEV2'] = args.device |
---|
| 525 | + if 'TIMEOUT' not in NAMES: |
---|
| 526 | + NAMES['TIMEOUT'] = None |
---|
441 | 527 | if not os.path.isfile(NAMES['TC']): |
---|
442 | 528 | print("The specified tc path " + NAMES['TC'] + " does not exist.") |
---|
443 | 529 | exit(1) |
---|
.. | .. |
---|
532 | 618 | |
---|
533 | 619 | return answer |
---|
534 | 620 | |
---|
| 621 | + |
---|
535 | 622 | def get_test_cases(args): |
---|
536 | 623 | """ |
---|
537 | 624 | If a test case file is specified, retrieve tests from that file. |
---|
.. | .. |
---|
593 | 680 | return allcatlist, allidlist, testcases_by_cats, alltestcases |
---|
594 | 681 | |
---|
595 | 682 | |
---|
596 | | -def set_operation_mode(pm, args): |
---|
| 683 | +def set_operation_mode(pm, parser, args, remaining): |
---|
597 | 684 | """ |
---|
598 | 685 | Load the test case data and process remaining arguments to determine |
---|
599 | 686 | what the script should do for this run, and call the appropriate |
---|
.. | .. |
---|
626 | 713 | exit(0) |
---|
627 | 714 | |
---|
628 | 715 | if args.list: |
---|
629 | | - if args.list: |
---|
630 | | - list_test_cases(alltests) |
---|
631 | | - exit(0) |
---|
| 716 | + list_test_cases(alltests) |
---|
| 717 | + exit(0) |
---|
632 | 718 | |
---|
633 | 719 | if len(alltests): |
---|
| 720 | + req_plugins = pm.get_required_plugins(alltests) |
---|
| 721 | + try: |
---|
| 722 | + args = pm.load_required_plugins(req_plugins, parser, args, remaining) |
---|
| 723 | + except PluginDependencyException as pde: |
---|
| 724 | + print('The following plugins were not found:') |
---|
| 725 | + print('{}'.format(pde.missing_pg)) |
---|
634 | 726 | catresults = test_runner(pm, args, alltests) |
---|
| 727 | + if args.format == 'none': |
---|
| 728 | + print('Test results output suppression requested\n') |
---|
| 729 | + else: |
---|
| 730 | + print('\nAll test results: \n') |
---|
| 731 | + if args.format == 'xunit': |
---|
| 732 | + suffix = 'xml' |
---|
| 733 | + res = catresults.format_xunit() |
---|
| 734 | + elif args.format == 'tap': |
---|
| 735 | + suffix = 'tap' |
---|
| 736 | + res = catresults.format_tap() |
---|
| 737 | + print(res) |
---|
| 738 | + print('\n\n') |
---|
| 739 | + if not args.outfile: |
---|
| 740 | + fname = 'test-results.{}'.format(suffix) |
---|
| 741 | + else: |
---|
| 742 | + fname = args.outfile |
---|
| 743 | + with open(fname, 'w') as fh: |
---|
| 744 | + fh.write(res) |
---|
| 745 | + fh.close() |
---|
| 746 | + if os.getenv('SUDO_UID') is not None: |
---|
| 747 | + os.chown(fname, uid=int(os.getenv('SUDO_UID')), |
---|
| 748 | + gid=int(os.getenv('SUDO_GID'))) |
---|
635 | 749 | else: |
---|
636 | | - catresults = 'No tests found\n' |
---|
637 | | - if args.notap: |
---|
638 | | - print('Tap output suppression requested\n') |
---|
639 | | - else: |
---|
640 | | - print('All test results: \n\n{}'.format(catresults)) |
---|
| 750 | + print('No tests found\n') |
---|
641 | 751 | |
---|
642 | 752 | def main(): |
---|
643 | 753 | """ |
---|
.. | .. |
---|
650 | 760 | parser = pm.call_add_args(parser) |
---|
651 | 761 | (args, remaining) = parser.parse_known_args() |
---|
652 | 762 | args.NAMES = NAMES |
---|
| 763 | + pm.set_args(args) |
---|
653 | 764 | check_default_settings(args, remaining, pm) |
---|
654 | 765 | if args.verbose > 2: |
---|
655 | 766 | print('args is {}'.format(args)) |
---|
656 | 767 | |
---|
657 | | - set_operation_mode(pm, args) |
---|
| 768 | + set_operation_mode(pm, parser, args, remaining) |
---|
658 | 769 | |
---|
659 | 770 | exit(0) |
---|
660 | 771 | |
---|