blob: 6f141f18aa37be29f65d20cafbcca26cc480c5ae [file] [log] [blame]
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +01001# 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
20import os
21import sys
22import time
23import traceback
24from . import testenv
25
26from . import log, util, resource
27
28class Test(log.Origin):
29 UNKNOWN = 'UNKNOWN'
30 SKIP = 'skip'
31 PASS = 'pass'
32 FAIL = 'FAIL'
33
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010034 def __init__(self, suite_run, test_basename):
35 self.basename = test_basename
36 super().__init__(log.C_TST, self.basename)
Pau Espin Pedrol58603672018-08-09 13:45:55 +020037 self._run_dir = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010038 self.suite_run = suite_run
39 self.path = os.path.join(self.suite_run.definition.suite_dir, self.basename)
40 self.status = Test.UNKNOWN
41 self.start_timestamp = 0
42 self.duration = 0
43 self.fail_type = None
44 self.fail_message = None
45
46 def get_run_dir(self):
47 if self._run_dir is None:
48 self._run_dir = util.Dir(self.suite_run.get_run_dir().new_dir(self._name))
49 return self._run_dir
50
51 def run(self):
52 try:
53 log.large_separator(self.suite_run.trial.name(), self.suite_run.name(), self.name(), sublevel=3)
54 self.status = Test.UNKNOWN
55 self.start_timestamp = time.time()
Pau Espin Pedrol878b2c62018-05-18 15:57:06 +020056 from . import suite, sms, process
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020057 from .event_loop import MainLoop
Pau Espin Pedrol878b2c62018-05-18 15:57:06 +020058 testenv.setup(self.suite_run, self, suite, MainLoop, sms, process)
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010059 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