blob: be6e8dac027829a57b4298ab92189e739bbf69a6 [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 Pedrol644cb412020-03-04 16:14:31 +010046 self._report_stdout = None
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010047
48 def get_run_dir(self):
49 if self._run_dir is None:
50 self._run_dir = util.Dir(self.suite_run.get_run_dir().new_dir(self._name))
51 return self._run_dir
52
53 def run(self):
54 try:
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020055 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 +010056 log.large_separator(self.suite_run.trial.name(), self.suite_run.name(), self.name(), sublevel=3)
57 self.status = Test.UNKNOWN
58 self.start_timestamp = time.time()
Pau Espin Pedrol878b2c62018-05-18 15:57:06 +020059 from . import suite, sms, process
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020060 from .event_loop import MainLoop
Pau Espin Pedrol878b2c62018-05-18 15:57:06 +020061 testenv.setup(self.suite_run, self, suite, MainLoop, sms, process)
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010062 with self.redirect_stdout():
63 util.run_python_file('%s.%s' % (self.suite_run.definition.name(), self.basename),
64 self.path)
65 if self.status == Test.UNKNOWN:
66 self.set_pass()
67 except Exception as e:
68 if hasattr(e, 'msg'):
69 msg = e.msg
70 else:
71 msg = str(e)
72 if isinstance(e, AssertionError):
73 # AssertionError lacks further information on what was
74 # asserted. Find the line where the code asserted:
75 msg += log.get_src_from_exc_info(sys.exc_info())
76 # add source file information to failure report
77 if hasattr(e, 'origins'):
78 msg += ' [%s]' % e.origins
79 tb_str = traceback.format_exc()
80 if isinstance(e, resource.NoResourceExn):
81 tb_str += self.suite_run.resource_status_str()
82 self.set_fail(type(e).__name__, msg, tb_str, log.get_src_from_exc_info())
83 except BaseException as e:
84 # when the program is aborted by a signal (like Ctrl-C), escalate to abort all.
85 self.err('TEST RUN ABORTED: %s' % type(e).__name__)
86 raise
Pau Espin Pedrol5eae2c52019-09-18 16:50:38 +020087 finally:
88 if self.log_target:
89 self.log_target.remove()
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
Pau Espin Pedrol644cb412020-03-04 16:14:31 +0100122 def set_report_stdout(self, text):
123 'Overwrite stdout text stored in report from inside a test'
124 self._report_stdout = text
125
126 def report_stdout(self):
127 # If test overwrote the text, provide it:
128 if self._report_stdout is not None:
129 return self._report_stdout
130 # Otherwise vy default provide the entire test log:
131 if self.log_target is not None and self.log_target.log_file_path() is not None:
132 with open(self.log_target.log_file_path(), 'r') as myfile:
133 return myfile.read()
134 else:
135 return 'test log file not available'
Pau Espin Pedrol5bbdab82020-02-24 18:19:10 +0100136
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100137# vim: expandtab tabstop=4 shiftwidth=4