blob: 53a1dabf856bb3809bd3bf0eb3cd1d8ddddbf973 [file] [log] [blame]
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02001#!/usr/bin/env python3
2
3# osmo_gsm_tester: invoke a single test run
4#
5# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
6#
7# Author: Neels Hofmeyr <neels@hofmeyr.de>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU Affero General Public License as
11# published by the Free Software Foundation, either version 3 of the
12# License, or (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU Affero General Public License for more details.
18#
19# You should have received a copy of the GNU Affero General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22'''osmo_gsm_tester: invoke a single test run.
23
Neels Hofmeyr3531a192017-03-28 14:30:28 +020024Examples:
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020025
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026./run_once.py ~/my_trial_package/ -s osmo_trx
27./run_once.py ~/my_trial_package/ -c sms_tests:dyn_ts+eu_band+bts_sysmo
28./run_once.py ~/my_trial_package/ -c sms_tests/mo_mt_sms:bts_trx
29
30(The names for test suite, scenario and series names used in these examples
31must be defined by the osmo-gsm-tester configuration.)
32
33A trial package contains binaries (usually built by a jenkins job) of GSM
34software, including the core network programs as well as binaries for the
35various BTS models.
36
37A test suite defines specific actions to be taken and verifies their outcome.
38Such a test suite may leave certain aspects of a setup undefined, e.g. it may
39be BTS model agnostic or does not care which voice codecs are chosen.
40
41A test scenario completes the picture in that it defines which specific choices
42shall be made to run a test suite. Any one test suite may thus run on any
43number of different scenarios, e.g. to test various voice codecs.
44
45Test scenarios may be combined. For example, one scenario may define a timeslot
46configuration to use, while another scenario may define the voice codec
47configuration.
48
49There may still be aspects that are neither required by a test suite nor
50strictly defined by a scenario, which will be resolved automatically, e.g. by
51choosing the first available item that matches the other constraints.
52
53A test run thus needs to define: a trial package containing built binaries, a
54combination of scenarios to run a suite in, and a test suite to launch in the
55given scenario with the given binaries.
56
57The osmo-gsm-tester configuration may define one or more series as a number of
58suite:scenario combinations. So instead of a specific suite:scenario
59combination, the name of such a series can be passed.
60
61If neither a combination or series is specified, the default series will be run
62as defined in the osmo-gsm-tester configuration.
63
64The scenarios and suites run for a given trial will be recorded in a trial
65package's directory: Upon launch, a 'test_package/run.<date>' directory will be
66created, which will collect logs and reports.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020067'''
68
Neels Hofmeyre3528442017-04-08 21:18:30 +020069import sys
Neels Hofmeyre60df692017-05-14 03:05:48 +020070import argparse
Pau Espin Pedrol469316f2017-05-17 14:51:31 +020071from signal import *
Neels Hofmeyrd46ea132017-04-08 15:56:31 +020072from osmo_gsm_tester import __version__
73from osmo_gsm_tester import trial, suite, log, config
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020074
Pau Espin Pedrol469316f2017-05-17 14:51:31 +020075def sig_handler_cleanup(signum, frame):
76 print("killed by signal %d" % signum)
77 # This sys.exit() will raise a SystemExit base exception at the current
78 # point of execution. Code must be prepared to clean system-wide resources
79 # by using the "finally" section. This allows at the end 'atexit' hooks to
80 # be called before exiting.
81 sys.exit(1)
82
Neels Hofmeyr21235412017-05-14 03:00:21 +020083def main():
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020084
Pau Espin Pedrol469316f2017-05-17 14:51:31 +020085 for sig in (SIGINT, SIGTERM, SIGQUIT, SIGPIPE, SIGHUP):
86 signal(sig, sig_handler_cleanup)
87
Neels Hofmeyr3531a192017-03-28 14:30:28 +020088 parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawTextHelpFormatter)
Neels Hofmeyrcf0304f2017-05-06 22:07:39 +020089 # Note: since we're using RawTextHelpFormatter to keep nicely separate
90 # paragraphs in the long help text, we unfortunately also need to take care
91 # of line wraps in the shorter cmdline options help.
92 # The line width here is what remains of screen width after the list of
93 # options placed by ArgumentParser. That's unfortunately subject to change
94 # and undefined, so when things change, just run a local
95 # ./osmo-gsm-tester.py --help and try to keep everything in 80 chars width.
96 # The help text is indented automatically, but line width is manual.
97 # Using multi-line strings here -- doesn't look nice in the python flow but
98 # is easiest to maintain.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020099 parser.add_argument('-V', '--version', action='store_true',
100 help='Show version')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200101 parser.add_argument('trial_package', nargs='+',
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200102 help='Directory containing binaries to test')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200103 parser.add_argument('-s', '--suite-scenario', dest='suite_scenario', action='append',
Neels Hofmeyrcf0304f2017-05-06 22:07:39 +0200104 help='''A suite-scenarios combination
105like suite:scenario+scenario''')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200106 parser.add_argument('-S', '--series', dest='series', action='append',
Neels Hofmeyrcf0304f2017-05-06 22:07:39 +0200107 help='''A series of suite-scenarios combinations
108as defined in the osmo-gsm-tester configuration''')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200109 parser.add_argument('-t', '--test', dest='test', action='append',
Neels Hofmeyrcf0304f2017-05-06 22:07:39 +0200110 help='''Run only tests matching this name.
111Any test name that contains the given string is run.
112To get an exact match, prepend a "=" like
113"-t =my_exact_name". The ".py" suffix is always
114optional.''')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200115 parser.add_argument('-l', '--log-level', dest='log_level', choices=log.LEVEL_STRS.keys(),
116 default=None,
117 help='Set logging level for all categories (on stdout)')
118 parser.add_argument('-T', '--traceback', dest='trace', action='store_true',
119 help='Enable logging of tracebacks')
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200120 args = parser.parse_args()
121
122 if args.version:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200123 print(__version__)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200124 exit(0)
125
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200126 print('combinations:', repr(args.suite_scenario))
127 print('series:', repr(args.series))
128 print('trials:', repr(args.trial_package))
129 print('tests:', repr(args.test))
130
Neels Hofmeyrf8166882017-05-05 19:48:35 +0200131 # create a default log to stdout
Neels Hofmeyr9576f5f2017-05-24 18:31:01 +0200132 log.LogTarget().style(all_origins_on_levels=(log.L_ERR, log.L_TRACEBACK), src=False)
Neels Hofmeyrf8166882017-05-05 19:48:35 +0200133
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200134 if args.log_level:
135 log.set_all_levels(log.LEVEL_STRS.get(args.log_level))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200136 if args.trace:
137 log.style_change(trace=True)
138
139 combination_strs = list(args.suite_scenario or [])
140 # for series in args.series:
141 # combination_strs.extend(config.get_series(series))
142
143 if not combination_strs:
Neels Hofmeyrd46ea132017-04-08 15:56:31 +0200144 combination_strs = config.read_config_file(config.DEFAULT_SUITES_CONF, if_missing_return=[])
145
146 if combination_strs:
147 print('Running default suites:\n ' + ('\n '.join(combination_strs)))
Your Name3c6673a2017-04-08 18:52:39 +0200148 else:
149 print('No default suites configured (%r)' % config.DEFAULT_SUITES_CONF)
150
Neels Hofmeyrd46ea132017-04-08 15:56:31 +0200151
152 if not combination_strs:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200153 raise RuntimeError('Need at least one suite:scenario or series to run')
154
155 suite_scenarios = []
156 for combination_str in combination_strs:
157 suite_scenarios.append(suite.load_suite_scenario_str(combination_str))
158
159 test_names = []
160 for test_name in (args.test or []):
161 found = False
Neels Hofmeyrf9de78f2017-05-06 16:04:37 +0200162 if test_name.startswith('=') and not test_name.endswith('.py'):
163 test_name = test_name + '.py'
Neels Hofmeyr930ac952017-05-06 15:49:53 +0200164 for suite_scenario_str, suite_def, scenarios in suite_scenarios:
165 for test in suite_def.tests:
Neels Hofmeyrf9de78f2017-05-06 16:04:37 +0200166 if test_name.startswith('='):
167 match = test_name[1:] == test.name()
168 else:
169 match = test_name in test.name()
170 if match:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200171 found = True
172 test_names.append(test.name())
173 if not found:
174 raise RuntimeError('No test found for %r' % test_name)
175 if test_names:
176 print(repr(test_names))
177
178 trials = []
179 for trial_package in args.trial_package:
180 t = trial.Trial(trial_package)
Neels Hofmeyr21235412017-05-14 03:00:21 +0200181 t.verify()
182 trials.append(t)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200183
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200184 trials_run = []
185 any_failed = False
Neels Hofmeyref42cb52017-04-08 19:38:58 +0200186
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200187 for current_trial in trials:
Your Name44af3412017-04-13 03:11:59 +0200188 try:
189 with current_trial:
Your Name44af3412017-04-13 03:11:59 +0200190 for suite_scenario_str, suite_def, scenarios in suite_scenarios:
191 log.large_separator(current_trial.name(), suite_scenario_str)
192 suite_run = suite.SuiteRun(current_trial, suite_scenario_str, suite_def, scenarios)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200193 current_trial.add_suite(suite_run)
194
195 status = current_trial.run_suites(test_names)
196 if status == trial.Trial.FAIL:
197 any_failed = True
198 trials_run.append(current_trial)
Pau Espin Pedrol469316f2017-05-17 14:51:31 +0200199 except Exception:
200 # Do not catch here subclasses of BaseException such as SystemExit, let them finish the program
Pau Espin Pedrol98df3942017-05-23 17:18:29 +0200201 any_failed = True
Your Name44af3412017-04-13 03:11:59 +0200202 current_trial.log_exn()
Neels Hofmeyref42cb52017-04-08 19:38:58 +0200203
Neels Hofmeyr2ef9b002017-04-08 21:16:27 +0200204 sys.stderr.flush()
205 sys.stdout.flush()
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200206 if not any_failed:
207 log.large_separator('All trials passed:\n ' + ('\n '.join(mytrial.name() for mytrial in trials_run)))
208 else:
209 for mytrial in trials_run:
210 log.large_separator('Trial Report for %s' % mytrial.name())
211 mytrial.log_report()
Neels Hofmeyref42cb52017-04-08 19:38:58 +0200212 exit(1)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200213
Neels Hofmeyr21235412017-05-14 03:00:21 +0200214if __name__ == '__main__':
215 try:
216 main()
217 except:
218 # Tell the log to show the exception, then terminate the program with the exception anyway.
219 # Since exceptions within test runs should be caught and evaluated, this is basically about
220 # exceptions during command line parsing and such, so it's appropriate to abort immediately.
221 log.log_exn()
222 raise
223
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200224# vim: expandtab tabstop=4 shiftwidth=4