Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 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 |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 21 | import sys |
| 22 | import time |
Neels Hofmeyr | 2d1d561 | 2017-05-22 20:02:41 +0200 | [diff] [blame] | 23 | import pprint |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 24 | from . import config, log, template, util, resource, schema, event_loop, test |
| 25 | from . import osmo_nitb, osmo_hlr, osmo_mgcpgw, osmo_mgw, osmo_msc, osmo_bsc, osmo_stp, modem, esme |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 26 | |
Neels Hofmeyr | 1ffc3fe | 2017-05-07 02:15:21 +0200 | [diff] [blame] | 27 | class Timeout(Exception): |
| 28 | pass |
| 29 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 30 | class SuiteDefinition(log.Origin): |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 31 | '''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 Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 39 | def __init__(self, suite_dir): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 40 | self.suite_dir = suite_dir |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 41 | super().__init__(log.C_CNF, os.path.basename(self.suite_dir)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 42 | self.read_conf() |
| 43 | |
| 44 | def read_conf(self): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 45 | 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 Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 50 | resource.CONF_SCHEMA) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 51 | self.load_test_basenames() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 52 | |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 53 | 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 Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 59 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 60 | class SuiteRun(log.Origin): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 61 | UNKNOWN = 'UNKNOWN' |
| 62 | PASS = 'PASS' |
| 63 | FAIL = 'FAIL' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 64 | |
| 65 | trial = None |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 66 | status = None |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 67 | start_timestamp = None |
| 68 | duration = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 69 | resources_pool = None |
| 70 | reserved_resources = None |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 71 | objects_to_clean_up = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 72 | _resource_requirements = None |
| 73 | _config = None |
| 74 | _processes = None |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 75 | _run_dir = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 76 | |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 77 | def __init__(self, trial, suite_scenario_str, suite_definition, scenarios=[]): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 78 | super().__init__(log.C_TST, suite_scenario_str) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 79 | self.trial = trial |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 80 | self.definition = suite_definition |
| 81 | self.scenarios = scenarios |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 82 | self.resources_pool = resource.ResourcesPool() |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 83 | 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 Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 89 | self.tests.append(test.Test(self, test_basename)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 90 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 91 | 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 Pedrol | 6100b62 | 2017-07-31 18:19:06 +0200 | [diff] [blame] | 99 | try: |
| 100 | obj.cleanup() |
| 101 | except Exception: |
| 102 | log.log_exn() |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 103 | |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 104 | def mark_start(self): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 105 | self.start_timestamp = time.time() |
| 106 | self.duration = 0 |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 107 | self.status = SuiteRun.UNKNOWN |
| 108 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 109 | def combined(self, conf_name): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 110 | log.dbg(combining=conf_name) |
| 111 | log.ctx(combining_scenarios=conf_name) |
Pau Espin Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 112 | combination = config.replicate_times(self.definition.conf.get(conf_name, {})) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 113 | log.dbg(definition_conf=combination) |
| 114 | for scenario in self.scenarios: |
| 115 | log.ctx(combining_scenarios=conf_name, scenario=scenario.name()) |
Pau Espin Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 116 | c = config.replicate_times(scenario.get(conf_name, {})) |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 117 | log.dbg(scenario=scenario.name(), conf=c) |
| 118 | if c is None: |
| 119 | continue |
| 120 | config.combine(combination, c) |
| 121 | return combination |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 122 | |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 123 | 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 Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 133 | 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 Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 142 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 143 | def reserve_resources(self): |
| 144 | if self.reserved_resources: |
| 145 | raise RuntimeError('Attempt to reserve resources twice for a SuiteRun') |
Neels Hofmeyr | 7e2e8f1 | 2017-05-14 03:37:13 +0200 | [diff] [blame] | 146 | self.log('reserving resources in', self.resources_pool.state_dir, '...') |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 147 | self.reserved_resources = self.resources_pool.reserve(self, self.resource_requirements()) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 148 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 149 | def run_tests(self, names=None): |
Pau Espin Pedrol | 469316f | 2017-05-17 14:51:31 +0200 | [diff] [blame] | 150 | try: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 151 | 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 Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 156 | for t in self.tests: |
| 157 | if names and not t.name() in names: |
| 158 | t.set_skip() |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 159 | continue |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 160 | self.current_test = t |
| 161 | t.run() |
Pau Espin Pedrol | 1dd2955 | 2017-06-13 18:07:57 +0200 | [diff] [blame] | 162 | self.stop_processes() |
| 163 | self.objects_cleanup() |
| 164 | self.reserved_resources.put_all() |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 165 | except Exception: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 166 | log.log_exn() |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 167 | 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 Pedrol | 469316f | 2017-05-17 14:51:31 +0200 | [diff] [blame] | 171 | 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 Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 176 | self.objects_cleanup() |
Neels Hofmeyr | ed4e528 | 2017-05-29 02:53:54 +0200 | [diff] [blame] | 177 | self.free_resources() |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 178 | 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 Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 197 | for t in self.tests: |
| 198 | if t.status == test.Test.PASS: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 199 | passed += 1 |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 200 | elif t.status == test.Test.FAIL: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 201 | failed += 1 |
| 202 | else: |
| 203 | skipped += 1 |
| 204 | return (passed, skipped, failed) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 205 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 206 | def remember_to_stop(self, process): |
| 207 | if self._processes is None: |
| 208 | self._processes = [] |
Pau Espin Pedrol | ecf1079 | 2017-05-08 16:56:38 +0200 | [diff] [blame] | 209 | self._processes.insert(0, process) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 210 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 211 | def stop_processes(self): |
Pau Espin Pedrol | 1dd2955 | 2017-06-13 18:07:57 +0200 | [diff] [blame] | 212 | while self._processes: |
| 213 | self._processes.pop().terminate() |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 214 | |
Neels Hofmeyr | ed4e528 | 2017-05-29 02:53:54 +0200 | [diff] [blame] | 215 | def free_resources(self): |
| 216 | if self.reserved_resources is None: |
| 217 | return |
| 218 | self.reserved_resources.free() |
| 219 | |
Neels Hofmeyr | b902b29 | 2017-06-06 21:52:03 +0200 | [diff] [blame] | 220 | def ip_address(self, specifics=None): |
| 221 | return self.reserved_resources.get(resource.R_IP_ADDRESS, specifics=specifics) |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 222 | |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 223 | 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 Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 227 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 228 | 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 Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 238 | 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 | |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 243 | def msc(self, hlr, mgcpgw, stp, ip_address=None): |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 244 | if ip_address is None: |
| 245 | ip_address = self.ip_address() |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 246 | return osmo_msc.OsmoMsc(self, hlr, mgcpgw, stp, ip_address) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 247 | |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 248 | def bsc(self, msc, mgw, stp, ip_address=None): |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 249 | if ip_address is None: |
| 250 | ip_address = self.ip_address() |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 251 | return osmo_bsc.OsmoBsc(self, msc, mgw, stp, ip_address) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 252 | |
Neels Hofmeyr | 38b051c | 2017-06-13 16:26:06 +0200 | [diff] [blame] | 253 | 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 Hofmeyr | b902b29 | 2017-06-06 21:52:03 +0200 | [diff] [blame] | 258 | def bts(self, specifics=None): |
Pau Espin Pedrol | 15aae98 | 2017-09-08 13:55:54 +0200 | [diff] [blame] | 259 | bts = bts_obj(self, self.reserved_resources.get(resource.R_BTS, specifics=specifics)) |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 260 | bts.set_lac(self.lac()) |
Pau Espin Pedrol | 4ccce7c | 2017-11-07 11:13:20 +0100 | [diff] [blame] | 261 | bts.set_cellid(self.cellid()) |
Pau Espin Pedrol | 15aae98 | 2017-09-08 13:55:54 +0200 | [diff] [blame] | 262 | self.register_for_cleanup(bts) |
| 263 | return bts |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 264 | |
Neels Hofmeyr | b902b29 | 2017-06-06 21:52:03 +0200 | [diff] [blame] | 265 | def modem(self, specifics=None): |
| 266 | conf = self.reserved_resources.get(resource.R_MODEM, specifics=specifics) |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 267 | self.dbg('create Modem object', conf=conf) |
Pau Espin Pedrol | 6cdd2fd | 2017-11-07 11:57:42 +0100 | [diff] [blame] | 268 | ms = modem.Modem(conf) |
| 269 | self.register_for_cleanup(ms) |
| 270 | return ms |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 271 | |
Neels Hofmeyr | f2d279c | 2017-05-06 15:05:02 +0200 | [diff] [blame] | 272 | def modems(self, count): |
| 273 | l = [] |
| 274 | for i in range(count): |
| 275 | l.append(self.modem()) |
| 276 | return l |
| 277 | |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 278 | def esme(self): |
| 279 | esme_obj = esme.Esme(self.msisdn()) |
Pau Espin Pedrol | ac9c1bb | 2017-08-10 10:59:40 +0200 | [diff] [blame] | 280 | self.register_for_cleanup(esme_obj) |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 281 | return esme_obj |
| 282 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 283 | def msisdn(self): |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 284 | msisdn = self.resources_pool.next_msisdn(self) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 285 | self.log('using MSISDN', msisdn) |
| 286 | return msisdn |
| 287 | |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 288 | def lac(self): |
| 289 | lac = self.resources_pool.next_lac(self) |
| 290 | self.log('using LAC', lac) |
| 291 | return lac |
| 292 | |
Pau Espin Pedrol | 4ccce7c | 2017-11-07 11:13:20 +0100 | [diff] [blame] | 293 | def cellid(self): |
| 294 | cellid = self.resources_pool.next_cellid(self) |
| 295 | self.log('using CellId', cellid) |
| 296 | return cellid |
| 297 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 298 | def poll(self): |
| 299 | if self._processes: |
| 300 | for process in self._processes: |
Neels Hofmeyr | 5356d0a | 2017-04-10 03:45:30 +0200 | [diff] [blame] | 301 | if process.terminated(): |
Neels Hofmeyr | 85eb324 | 2017-04-09 22:01:16 +0200 | [diff] [blame] | 302 | process.log_stdout_tail() |
| 303 | process.log_stderr_tail() |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 304 | log.ctx(process) |
Pau Espin Pedrol | 800a697 | 2017-07-03 18:34:09 +0200 | [diff] [blame] | 305 | raise log.Error('Process ended prematurely: %s' % process.name()) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 306 | |
| 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 Hofmeyr | acf0c93 | 2017-05-06 16:05:33 +0200 | [diff] [blame] | 316 | sys.__stdout__.write('\n\n--- PROMPT ---\n') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 317 | sys.__stdout__.write(msg) |
Neels Hofmeyr | acf0c93 | 2017-05-06 16:05:33 +0200 | [diff] [blame] | 318 | sys.__stdout__.write('\n') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 319 | sys.__stdout__.flush() |
Pau Espin Pedrol | 90c23cc | 2017-07-03 13:12:37 +0200 | [diff] [blame] | 320 | entered = util.input_polling('> ', event_loop.poll) |
Neels Hofmeyr | acf0c93 | 2017-05-06 16:05:33 +0200 | [diff] [blame] | 321 | self.log('prompt entered:', repr(entered)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 322 | return entered |
| 323 | |
Neels Hofmeyr | 2d1d561 | 2017-05-22 20:02:41 +0200 | [diff] [blame] | 324 | 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 Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 331 | loaded_suite_definitions = {} |
| 332 | |
| 333 | def 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 | |
| 351 | def 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 | |
| 364 | def 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 Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 367 | scenarios = [config.get_scenario(scenario_name, resource.CONF_SCHEMA) for scenario_name in scenario_names] |
Your Name | 44af341 | 2017-04-13 03:11:59 +0200 | [diff] [blame] | 368 | return (suite_scenario_str, suite, scenarios) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 369 | |
| 370 | def bts_obj(suite_run, conf): |
| 371 | bts_type = conf.get('type') |
Neels Hofmeyr | d28d1a7 | 2017-06-14 02:59:55 +0200 | [diff] [blame] | 372 | log.dbg('create BTS object', type=bts_type) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 373 | 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 Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 378 | # vim: expandtab tabstop=4 shiftwidth=4 |