Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: test class |
| 2 | # |
| 3 | # Copyright (C) 2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as |
| 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import sys |
| 22 | import time |
| 23 | import traceback |
| 24 | from . import testenv |
| 25 | |
| 26 | from . import log, util, resource |
| 27 | |
| 28 | class Test(log.Origin): |
| 29 | UNKNOWN = 'UNKNOWN' |
| 30 | SKIP = 'skip' |
| 31 | PASS = 'pass' |
| 32 | FAIL = 'FAIL' |
| 33 | |
| 34 | _run_dir = None |
| 35 | |
| 36 | def __init__(self, suite_run, test_basename): |
| 37 | self.basename = test_basename |
| 38 | super().__init__(log.C_TST, self.basename) |
| 39 | self.suite_run = suite_run |
| 40 | self.path = os.path.join(self.suite_run.definition.suite_dir, self.basename) |
| 41 | self.status = Test.UNKNOWN |
| 42 | self.start_timestamp = 0 |
| 43 | self.duration = 0 |
| 44 | self.fail_type = None |
| 45 | self.fail_message = None |
| 46 | |
| 47 | def get_run_dir(self): |
| 48 | if self._run_dir is None: |
| 49 | self._run_dir = util.Dir(self.suite_run.get_run_dir().new_dir(self._name)) |
| 50 | return self._run_dir |
| 51 | |
| 52 | def run(self): |
| 53 | try: |
| 54 | log.large_separator(self.suite_run.trial.name(), self.suite_run.name(), self.name(), sublevel=3) |
| 55 | self.status = Test.UNKNOWN |
| 56 | self.start_timestamp = time.time() |
| 57 | from . import suite, event_loop, sms |
| 58 | testenv.setup(self.suite_run, self, suite, event_loop, sms) |
| 59 | with self.redirect_stdout(): |
| 60 | util.run_python_file('%s.%s' % (self.suite_run.definition.name(), self.basename), |
| 61 | self.path) |
| 62 | if self.status == Test.UNKNOWN: |
| 63 | self.set_pass() |
| 64 | except Exception as e: |
| 65 | if hasattr(e, 'msg'): |
| 66 | msg = e.msg |
| 67 | else: |
| 68 | msg = str(e) |
| 69 | if isinstance(e, AssertionError): |
| 70 | # AssertionError lacks further information on what was |
| 71 | # asserted. Find the line where the code asserted: |
| 72 | msg += log.get_src_from_exc_info(sys.exc_info()) |
| 73 | # add source file information to failure report |
| 74 | if hasattr(e, 'origins'): |
| 75 | msg += ' [%s]' % e.origins |
| 76 | tb_str = traceback.format_exc() |
| 77 | if isinstance(e, resource.NoResourceExn): |
| 78 | tb_str += self.suite_run.resource_status_str() |
| 79 | self.set_fail(type(e).__name__, msg, tb_str, log.get_src_from_exc_info()) |
| 80 | except BaseException as e: |
| 81 | # when the program is aborted by a signal (like Ctrl-C), escalate to abort all. |
| 82 | self.err('TEST RUN ABORTED: %s' % type(e).__name__) |
| 83 | raise |
| 84 | |
| 85 | def name(self): |
| 86 | l = log.get_line_for_src(self.path) |
| 87 | if l is not None: |
| 88 | return '%s:%s' % (self._name, l) |
| 89 | return super().name() |
| 90 | |
| 91 | def set_fail(self, fail_type, fail_message, tb_str=None, src=4): |
| 92 | self.status = Test.FAIL |
| 93 | self.duration = time.time() - self.start_timestamp |
| 94 | self.fail_type = fail_type |
| 95 | self.fail_message = fail_message |
| 96 | |
| 97 | if tb_str is None: |
| 98 | # populate an exception-less call to set_fail() with traceback info |
| 99 | tb_str = ''.join(traceback.format_stack()[:-1]) |
| 100 | |
| 101 | self.fail_tb = tb_str |
| 102 | self.err('%s: %s' % (self.fail_type, self.fail_message), _src=src) |
| 103 | if self.fail_tb: |
| 104 | self.log(self.fail_tb, _level=log.L_TRACEBACK) |
| 105 | self.log('Test FAILED (%.1f sec)' % self.duration) |
| 106 | |
| 107 | def set_pass(self): |
| 108 | self.status = Test.PASS |
| 109 | self.duration = time.time() - self.start_timestamp |
| 110 | self.log('Test passed (%.1f sec)' % self.duration) |
| 111 | |
| 112 | def set_skip(self): |
| 113 | self.status = Test.SKIP |
| 114 | self.duration = 0 |
| 115 | |
| 116 | # vim: expandtab tabstop=4 shiftwidth=4 |