blob: 8ab124bd288e1abfe42ac529a83ba70d812ca9a7 [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
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010024
Pau Espin Pedrolaa1cbdc2020-05-04 20:21:31 +020025from . import log
26from . import util
27from . import resource
28from .event_loop import MainLoop
29
30from .. import testenv
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010031
32class Test(log.Origin):
Pau Espin Pedrol02e8a8d2020-03-05 17:22:40 +010033 UNKNOWN = 'UNKNOWN' # matches junit 'error'
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010034 SKIP = 'skip'
35 PASS = 'pass'
36 FAIL = 'FAIL'
37
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010038 def __init__(self, suite_run, test_basename):
39 self.basename = test_basename
40 super().__init__(log.C_TST, self.basename)
Pau Espin Pedrol58603672018-08-09 13:45:55 +020041 self._run_dir = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010042 self.suite_run = suite_run
43 self.path = os.path.join(self.suite_run.definition.suite_dir, self.basename)
44 self.status = Test.UNKNOWN
45 self.start_timestamp = 0
46 self.duration = 0
47 self.fail_type = None
48 self.fail_message = None
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020049 self.log_target = None
Pau Espin Pedrol644cb412020-03-04 16:14:31 +010050 self._report_stdout = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010051
52 def get_run_dir(self):
53 if self._run_dir is None:
54 self._run_dir = util.Dir(self.suite_run.get_run_dir().new_dir(self._name))
55 return self._run_dir
56
57 def run(self):
Pau Espin Pedrolaa1cbdc2020-05-04 20:21:31 +020058 testenv_obj = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010059 try:
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020060 self.log_target = log.FileLogTarget(self.get_run_dir().new_child('log')).set_all_levels(log.L_DBG).style_change(trace=True)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020061 log.large_separator(self.suite_run.trial().name(), self.suite_run.name(), self.name(), sublevel=3)
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010062 self.status = Test.UNKNOWN
63 self.start_timestamp = time.time()
Pau Espin Pedrolaa1cbdc2020-05-04 20:21:31 +020064 testenv_obj = testenv.setup(self.suite_run, self)
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010065 with self.redirect_stdout():
66 util.run_python_file('%s.%s' % (self.suite_run.definition.name(), self.basename),
67 self.path)
68 if self.status == Test.UNKNOWN:
69 self.set_pass()
70 except Exception as e:
71 if hasattr(e, 'msg'):
72 msg = e.msg
73 else:
74 msg = str(e)
75 if isinstance(e, AssertionError):
76 # AssertionError lacks further information on what was
77 # asserted. Find the line where the code asserted:
78 msg += log.get_src_from_exc_info(sys.exc_info())
79 # add source file information to failure report
80 if hasattr(e, 'origins'):
81 msg += ' [%s]' % e.origins
82 tb_str = traceback.format_exc()
83 if isinstance(e, resource.NoResourceExn):
84 tb_str += self.suite_run.resource_status_str()
85 self.set_fail(type(e).__name__, msg, tb_str, log.get_src_from_exc_info())
86 except BaseException as e:
87 # when the program is aborted by a signal (like Ctrl-C), escalate to abort all.
88 self.err('TEST RUN ABORTED: %s' % type(e).__name__)
89 raise
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020090 finally:
Pau Espin Pedrolaa1cbdc2020-05-04 20:21:31 +020091 if testenv_obj:
92 testenv_obj.stop()
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020093 if self.log_target:
94 self.log_target.remove()
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010095
96 def name(self):
97 l = log.get_line_for_src(self.path)
98 if l is not None:
99 return '%s:%s' % (self._name, l)
100 return super().name()
101
102 def set_fail(self, fail_type, fail_message, tb_str=None, src=4):
103 self.status = Test.FAIL
104 self.duration = time.time() - self.start_timestamp
105 self.fail_type = fail_type
106 self.fail_message = fail_message
107
108 if tb_str is None:
109 # populate an exception-less call to set_fail() with traceback info
110 tb_str = ''.join(traceback.format_stack()[:-1])
111
112 self.fail_tb = tb_str
113 self.err('%s: %s' % (self.fail_type, self.fail_message), _src=src)
114 if self.fail_tb:
115 self.log(self.fail_tb, _level=log.L_TRACEBACK)
116 self.log('Test FAILED (%.1f sec)' % self.duration)
117
118 def set_pass(self):
119 self.status = Test.PASS
120 self.duration = time.time() - self.start_timestamp
121 self.log('Test passed (%.1f sec)' % self.duration)
122
123 def set_skip(self):
124 self.status = Test.SKIP
125 self.duration = 0
126
Pau Espin Pedrol644cb412020-03-04 16:14:31 +0100127 def set_report_stdout(self, text):
128 'Overwrite stdout text stored in report from inside a test'
129 self._report_stdout = text
130
131 def report_stdout(self):
132 # If test overwrote the text, provide it:
133 if self._report_stdout is not None:
134 return self._report_stdout
135 # Otherwise vy default provide the entire test log:
136 if self.log_target is not None and self.log_target.log_file_path() is not None:
137 with open(self.log_target.log_file_path(), 'r') as myfile:
138 return myfile.read()
139 else:
140 return 'test log file not available'
Pau Espin Pedrol5bbdab82020-02-24 18:19:10 +0100141
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100142# vim: expandtab tabstop=4 shiftwidth=4