blob: 4d4353aff49f87eba1000286e67e395bab91652e [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
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020045 self.log_target = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010046
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:
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020054 self.log_target = log.FileLogTarget(self.get_run_dir().new_child('log')).set_all_levels(log.L_DBG).style_change(trace=True)
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010055 log.large_separator(self.suite_run.trial.name(), self.suite_run.name(), self.name(), sublevel=3)
56 self.status = Test.UNKNOWN
57 self.start_timestamp = time.time()
Pau Espin Pedrol878b2c62018-05-18 15:57:06 +020058 from . import suite, sms, process
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020059 from .event_loop import MainLoop
Pau Espin Pedrol878b2c62018-05-18 15:57:06 +020060 testenv.setup(self.suite_run, self, suite, MainLoop, sms, process)
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010061 with self.redirect_stdout():
62 util.run_python_file('%s.%s' % (self.suite_run.definition.name(), self.basename),
63 self.path)
64 if self.status == Test.UNKNOWN:
65 self.set_pass()
66 except Exception as e:
67 if hasattr(e, 'msg'):
68 msg = e.msg
69 else:
70 msg = str(e)
71 if isinstance(e, AssertionError):
72 # AssertionError lacks further information on what was
73 # asserted. Find the line where the code asserted:
74 msg += log.get_src_from_exc_info(sys.exc_info())
75 # add source file information to failure report
76 if hasattr(e, 'origins'):
77 msg += ' [%s]' % e.origins
78 tb_str = traceback.format_exc()
79 if isinstance(e, resource.NoResourceExn):
80 tb_str += self.suite_run.resource_status_str()
81 self.set_fail(type(e).__name__, msg, tb_str, log.get_src_from_exc_info())
82 except BaseException as e:
83 # when the program is aborted by a signal (like Ctrl-C), escalate to abort all.
84 self.err('TEST RUN ABORTED: %s' % type(e).__name__)
85 raise
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020086 finally:
87 if self.log_target:
88 self.log_target.remove()
89 self.log_target = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010090
91 def name(self):
92 l = log.get_line_for_src(self.path)
93 if l is not None:
94 return '%s:%s' % (self._name, l)
95 return super().name()
96
97 def set_fail(self, fail_type, fail_message, tb_str=None, src=4):
98 self.status = Test.FAIL
99 self.duration = time.time() - self.start_timestamp
100 self.fail_type = fail_type
101 self.fail_message = fail_message
102
103 if tb_str is None:
104 # populate an exception-less call to set_fail() with traceback info
105 tb_str = ''.join(traceback.format_stack()[:-1])
106
107 self.fail_tb = tb_str
108 self.err('%s: %s' % (self.fail_type, self.fail_message), _src=src)
109 if self.fail_tb:
110 self.log(self.fail_tb, _level=log.L_TRACEBACK)
111 self.log('Test FAILED (%.1f sec)' % self.duration)
112
113 def set_pass(self):
114 self.status = Test.PASS
115 self.duration = time.time() - self.start_timestamp
116 self.log('Test passed (%.1f sec)' % self.duration)
117
118 def set_skip(self):
119 self.status = Test.SKIP
120 self.duration = 0
121
122# vim: expandtab tabstop=4 shiftwidth=4