blob: 25aef3529281871335469fdaceb8bada278ee34e [file] [log] [blame]
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02001# osmo_gsm_tester: test suite
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02009# 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
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Neels Hofmeyr3531a192017-03-28 14:30:28 +020021import sys
22import time
Neels Hofmeyr2d1d5612017-05-22 20:02:41 +020023import pprint
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010024from . import config, log, template, util, resource, schema, event_loop, test
25from . import osmo_nitb, osmo_hlr, osmo_mgcpgw, osmo_mgw, osmo_msc, osmo_bsc, osmo_stp, modem, esme
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020026
Neels Hofmeyr1ffc3fe2017-05-07 02:15:21 +020027class Timeout(Exception):
28 pass
29
Neels Hofmeyr3531a192017-03-28 14:30:28 +020030class SuiteDefinition(log.Origin):
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +020031 '''A test suite reserves resources for a number of tests.
32 Each test requires a specific number of modems, BTSs etc., which are
33 reserved beforehand by a test suite. This way several test suites can be
34 scheduled dynamically without resource conflicts arising halfway through
35 the tests.'''
36
37 CONF_FILENAME = 'suite.conf'
38
Neels Hofmeyr3531a192017-03-28 14:30:28 +020039 def __init__(self, suite_dir):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020040 self.suite_dir = suite_dir
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020041 super().__init__(log.C_CNF, os.path.basename(self.suite_dir))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020042 self.read_conf()
43
44 def read_conf(self):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020045 self.dbg('reading %s' % SuiteDefinition.CONF_FILENAME)
46 if not os.path.isdir(self.suite_dir):
47 raise RuntimeError('No such directory: %r' % self.suite_dir)
48 self.conf = config.read(os.path.join(self.suite_dir,
49 SuiteDefinition.CONF_FILENAME),
Pau Espin Pedrol0b302792017-09-10 16:33:10 +020050 resource.CONF_SCHEMA)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020051 self.load_test_basenames()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020052
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020053 def load_test_basenames(self):
54 self.test_basenames = []
55 for basename in sorted(os.listdir(self.suite_dir)):
56 if not basename.endswith('.py'):
57 continue
58 self.test_basenames.append(basename)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020059
Neels Hofmeyr3531a192017-03-28 14:30:28 +020060class SuiteRun(log.Origin):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020061 UNKNOWN = 'UNKNOWN'
62 PASS = 'PASS'
63 FAIL = 'FAIL'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020064
65 trial = None
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020066 status = None
Neels Hofmeyrf8e61862017-06-06 23:08:07 +020067 start_timestamp = None
68 duration = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020069 resources_pool = None
70 reserved_resources = None
Neels Hofmeyr4d688c22017-05-29 04:13:58 +020071 objects_to_clean_up = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020072 _resource_requirements = None
73 _config = None
74 _processes = None
Pau Espin Pedrold0912332017-06-14 13:27:08 +020075 _run_dir = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020076
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020077 def __init__(self, trial, suite_scenario_str, suite_definition, scenarios=[]):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020078 super().__init__(log.C_TST, suite_scenario_str)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020079 self.trial = trial
Neels Hofmeyr3531a192017-03-28 14:30:28 +020080 self.definition = suite_definition
81 self.scenarios = scenarios
Neels Hofmeyr3531a192017-03-28 14:30:28 +020082 self.resources_pool = resource.ResourcesPool()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020083 self.status = SuiteRun.UNKNOWN
84 self.load_tests()
85
86 def load_tests(self):
87 self.tests = []
88 for test_basename in self.definition.test_basenames:
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010089 self.tests.append(test.Test(self, test_basename))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020090
Neels Hofmeyr4d688c22017-05-29 04:13:58 +020091 def register_for_cleanup(self, *obj):
92 assert all([hasattr(o, 'cleanup') for o in obj])
93 self.objects_to_clean_up = self.objects_to_clean_up or []
94 self.objects_to_clean_up.extend(obj)
95
96 def objects_cleanup(self):
97 while self.objects_to_clean_up:
98 obj = self.objects_to_clean_up.pop()
Pau Espin Pedrol6100b622017-07-31 18:19:06 +020099 try:
100 obj.cleanup()
101 except Exception:
102 log.log_exn()
Neels Hofmeyr4d688c22017-05-29 04:13:58 +0200103
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200104 def mark_start(self):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200105 self.start_timestamp = time.time()
106 self.duration = 0
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200107 self.status = SuiteRun.UNKNOWN
108
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200109 def combined(self, conf_name):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200110 log.dbg(combining=conf_name)
111 log.ctx(combining_scenarios=conf_name)
Pau Espin Pedrol0b302792017-09-10 16:33:10 +0200112 combination = config.replicate_times(self.definition.conf.get(conf_name, {}))
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200113 log.dbg(definition_conf=combination)
114 for scenario in self.scenarios:
115 log.ctx(combining_scenarios=conf_name, scenario=scenario.name())
Pau Espin Pedrol0b302792017-09-10 16:33:10 +0200116 c = config.replicate_times(scenario.get(conf_name, {}))
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200117 log.dbg(scenario=scenario.name(), conf=c)
118 if c is None:
119 continue
120 config.combine(combination, c)
121 return combination
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200122
Pau Espin Pedrold0912332017-06-14 13:27:08 +0200123 def get_run_dir(self):
124 if self._run_dir is None:
125 self._run_dir = util.Dir(self.trial.get_run_dir().new_dir(self.name()))
126 return self._run_dir
127
128 def get_test_run_dir(self):
129 if self.current_test:
130 return self.current_test.get_run_dir()
131 return self.get_run_dir()
132
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200133 def resource_requirements(self):
134 if self._resource_requirements is None:
135 self._resource_requirements = self.combined('resources')
136 return self._resource_requirements
137
138 def config(self):
139 if self._config is None:
140 self._config = self.combined('config')
141 return self._config
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200142
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200143 def reserve_resources(self):
144 if self.reserved_resources:
145 raise RuntimeError('Attempt to reserve resources twice for a SuiteRun')
Neels Hofmeyr7e2e8f12017-05-14 03:37:13 +0200146 self.log('reserving resources in', self.resources_pool.state_dir, '...')
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200147 self.reserved_resources = self.resources_pool.reserve(self, self.resource_requirements())
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200148
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200149 def run_tests(self, names=None):
Pau Espin Pedrol469316f2017-05-17 14:51:31 +0200150 try:
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200151 log.large_separator(self.trial.name(), self.name(), sublevel=2)
152 self.mark_start()
153 event_loop.register_poll_func(self.poll)
154 if not self.reserved_resources:
155 self.reserve_resources()
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100156 for t in self.tests:
157 if names and not t.name() in names:
158 t.set_skip()
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200159 continue
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100160 self.current_test = t
161 t.run()
Pau Espin Pedrol1dd29552017-06-13 18:07:57 +0200162 self.stop_processes()
163 self.objects_cleanup()
164 self.reserved_resources.put_all()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200165 except Exception:
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200166 log.log_exn()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200167 except BaseException as e:
168 # when the program is aborted by a signal (like Ctrl-C), escalate to abort all.
169 self.err('SUITE RUN ABORTED: %s' % type(e).__name__)
170 raise
Pau Espin Pedrol469316f2017-05-17 14:51:31 +0200171 finally:
172 # if sys.exit() called from signal handler (e.g. SIGINT), SystemExit
173 # base exception is raised. Make sure to stop processes in this
174 # finally section. Resources are automatically freed with 'atexit'.
175 self.stop_processes()
Neels Hofmeyr4d688c22017-05-29 04:13:58 +0200176 self.objects_cleanup()
Neels Hofmeyred4e5282017-05-29 02:53:54 +0200177 self.free_resources()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200178 event_loop.unregister_poll_func(self.poll)
179 self.duration = time.time() - self.start_timestamp
180
181 passed, skipped, failed = self.count_test_results()
182 # if no tests ran, count it as failure
183 if passed and not failed:
184 self.status = SuiteRun.PASS
185 else:
186 self.status = SuiteRun.FAIL
187
188 log.large_separator(self.trial.name(), self.name(), self.status, sublevel=2, space_above=False)
189
190 def passed(self):
191 return self.status == SuiteRun.PASS
192
193 def count_test_results(self):
194 passed = 0
195 skipped = 0
196 failed = 0
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100197 for t in self.tests:
198 if t.status == test.Test.PASS:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200199 passed += 1
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100200 elif t.status == test.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200201 failed += 1
202 else:
203 skipped += 1
204 return (passed, skipped, failed)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200205
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200206 def remember_to_stop(self, process):
207 if self._processes is None:
208 self._processes = []
Pau Espin Pedrolecf10792017-05-08 16:56:38 +0200209 self._processes.insert(0, process)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200210
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200211 def stop_processes(self):
Pau Espin Pedrol1dd29552017-06-13 18:07:57 +0200212 while self._processes:
213 self._processes.pop().terminate()
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200214
Neels Hofmeyred4e5282017-05-29 02:53:54 +0200215 def free_resources(self):
216 if self.reserved_resources is None:
217 return
218 self.reserved_resources.free()
219
Neels Hofmeyrb902b292017-06-06 21:52:03 +0200220 def ip_address(self, specifics=None):
221 return self.reserved_resources.get(resource.R_IP_ADDRESS, specifics=specifics)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200222
Neels Hofmeyr76d81032017-05-18 18:35:32 +0200223 def nitb(self, ip_address=None):
224 if ip_address is None:
225 ip_address = self.ip_address()
226 return osmo_nitb.OsmoNitb(self, ip_address)
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200227
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200228 def hlr(self, ip_address=None):
229 if ip_address is None:
230 ip_address = self.ip_address()
231 return osmo_hlr.OsmoHlr(self, ip_address)
232
233 def mgcpgw(self, ip_address=None, bts_ip=None):
234 if ip_address is None:
235 ip_address = self.ip_address()
236 return osmo_mgcpgw.OsmoMgcpgw(self, ip_address, bts_ip)
237
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +0100238 def mgw(self, ip_address=None):
239 if ip_address is None:
240 ip_address = self.ip_address()
241 return osmo_mgw.OsmoMgw(self, ip_address)
242
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200243 def msc(self, hlr, mgcpgw, ip_address=None):
244 if ip_address is None:
245 ip_address = self.ip_address()
246 return osmo_msc.OsmoMsc(self, hlr, mgcpgw, ip_address)
247
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +0100248 def bsc(self, msc, mgw, ip_address=None):
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200249 if ip_address is None:
250 ip_address = self.ip_address()
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +0100251 return osmo_bsc.OsmoBsc(self, msc, mgw, ip_address)
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200252
Neels Hofmeyr38b051c2017-06-13 16:26:06 +0200253 def stp(self, ip_address=None):
254 if ip_address is None:
255 ip_address = self.ip_address()
256 return osmo_stp.OsmoStp(self, ip_address)
257
Neels Hofmeyrb902b292017-06-06 21:52:03 +0200258 def bts(self, specifics=None):
Pau Espin Pedrol15aae982017-09-08 13:55:54 +0200259 bts = bts_obj(self, self.reserved_resources.get(resource.R_BTS, specifics=specifics))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100260 bts.set_lac(self.lac())
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100261 bts.set_cellid(self.cellid())
Pau Espin Pedrol15aae982017-09-08 13:55:54 +0200262 self.register_for_cleanup(bts)
263 return bts
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200264
Neels Hofmeyrb902b292017-06-06 21:52:03 +0200265 def modem(self, specifics=None):
266 conf = self.reserved_resources.get(resource.R_MODEM, specifics=specifics)
Neels Hofmeyr4d688c22017-05-29 04:13:58 +0200267 self.dbg('create Modem object', conf=conf)
Pau Espin Pedrol6cdd2fd2017-11-07 11:57:42 +0100268 ms = modem.Modem(conf)
269 self.register_for_cleanup(ms)
270 return ms
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200271
Neels Hofmeyrf2d279c2017-05-06 15:05:02 +0200272 def modems(self, count):
273 l = []
274 for i in range(count):
275 l.append(self.modem())
276 return l
277
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +0200278 def esme(self):
279 esme_obj = esme.Esme(self.msisdn())
Pau Espin Pedrolac9c1bb2017-08-10 10:59:40 +0200280 self.register_for_cleanup(esme_obj)
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +0200281 return esme_obj
282
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200283 def msisdn(self):
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +0200284 msisdn = self.resources_pool.next_msisdn(self)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200285 self.log('using MSISDN', msisdn)
286 return msisdn
287
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100288 def lac(self):
289 lac = self.resources_pool.next_lac(self)
290 self.log('using LAC', lac)
291 return lac
292
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100293 def cellid(self):
294 cellid = self.resources_pool.next_cellid(self)
295 self.log('using CellId', cellid)
296 return cellid
297
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200298 def poll(self):
299 if self._processes:
300 for process in self._processes:
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200301 if process.terminated():
Neels Hofmeyr85eb3242017-04-09 22:01:16 +0200302 process.log_stdout_tail()
303 process.log_stderr_tail()
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200304 log.ctx(process)
Pau Espin Pedrol800a6972017-07-03 18:34:09 +0200305 raise log.Error('Process ended prematurely: %s' % process.name())
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200306
307 def prompt(self, *msgs, **msg_details):
308 'ask for user interaction. Do not use in tests that should run automatically!'
309 if msg_details:
310 msgs = list(msgs)
311 msgs.append('{%s}' %
312 (', '.join(['%s=%r' % (k,v)
313 for k,v in sorted(msg_details.items())])))
314 msg = ' '.join(msgs) or 'Hit Enter to continue'
315 self.log('prompt:', msg)
Neels Hofmeyracf0c932017-05-06 16:05:33 +0200316 sys.__stdout__.write('\n\n--- PROMPT ---\n')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200317 sys.__stdout__.write(msg)
Neels Hofmeyracf0c932017-05-06 16:05:33 +0200318 sys.__stdout__.write('\n')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200319 sys.__stdout__.flush()
Pau Espin Pedrol90c23cc2017-07-03 13:12:37 +0200320 entered = util.input_polling('> ', event_loop.poll)
Neels Hofmeyracf0c932017-05-06 16:05:33 +0200321 self.log('prompt entered:', repr(entered))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200322 return entered
323
Neels Hofmeyr2d1d5612017-05-22 20:02:41 +0200324 def resource_status_str(self):
325 return '\n'.join(('',
326 'SUITE RUN: %s' % self.origin_id(),
327 'ASKED FOR:', pprint.pformat(self._resource_requirements),
328 'RESERVED COUNT:', pprint.pformat(self.reserved_resources.counts()),
329 'RESOURCES STATE:', repr(self.reserved_resources)))
330
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200331loaded_suite_definitions = {}
332
333def load(suite_name):
334 global loaded_suite_definitions
335
336 suite = loaded_suite_definitions.get(suite_name)
337 if suite is not None:
338 return suite
339
340 suites_dir = config.get_suites_dir()
341 suite_dir = suites_dir.child(suite_name)
342 if not suites_dir.exists(suite_name):
343 raise RuntimeError('Suite not found: %r in %r' % (suite_name, suites_dir))
344 if not suites_dir.isdir(suite_name):
345 raise RuntimeError('Suite name found, but not a directory: %r' % (suite_dir))
346
347 suite_def = SuiteDefinition(suite_dir)
348 loaded_suite_definitions[suite_name] = suite_def
349 return suite_def
350
351def parse_suite_scenario_str(suite_scenario_str):
352 tokens = suite_scenario_str.split(':')
353 if len(tokens) > 2:
354 raise RuntimeError('invalid combination string: %r' % suite_scenario_str)
355
356 suite_name = tokens[0]
357 if len(tokens) <= 1:
358 scenario_names = []
359 else:
360 scenario_names = tokens[1].split('+')
361
362 return suite_name, scenario_names
363
364def load_suite_scenario_str(suite_scenario_str):
365 suite_name, scenario_names = parse_suite_scenario_str(suite_scenario_str)
366 suite = load(suite_name)
Pau Espin Pedrol0b302792017-09-10 16:33:10 +0200367 scenarios = [config.get_scenario(scenario_name, resource.CONF_SCHEMA) for scenario_name in scenario_names]
Your Name44af3412017-04-13 03:11:59 +0200368 return (suite_scenario_str, suite, scenarios)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200369
370def bts_obj(suite_run, conf):
371 bts_type = conf.get('type')
Neels Hofmeyrd28d1a72017-06-14 02:59:55 +0200372 log.dbg('create BTS object', type=bts_type)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200373 bts_class = resource.KNOWN_BTS_TYPES.get(bts_type)
374 if bts_class is None:
375 raise RuntimeError('No such BTS type is defined: %r' % bts_type)
376 return bts_class(suite_run, conf)
377
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +0200378# vim: expandtab tabstop=4 shiftwidth=4