Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 1 | # ms_driver: Launch OsmocomBB mobile's virtually connected to a BTS |
| 2 | # |
| 3 | # Copyright (C) 2018 by Holger Hans Peter Freyther |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as |
| 7 | # published by the Free Software Foundation, either version 3 of the |
| 8 | # License, or (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | from datetime import timedelta |
Holger Hans Peter Freyther | 792614f | 2018-11-05 06:23:39 +0000 | [diff] [blame] | 19 | from . import log, util |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 20 | from osmo_ms_driver.cdf import cdfs |
| 21 | from osmo_ms_driver.event_server import EventServer |
| 22 | from osmo_ms_driver.simple_loop import SimpleLoop |
| 23 | from osmo_ms_driver.location_update_test import MassUpdateLocationTest |
Holger Hans Peter Freyther | f658b83 | 2018-11-05 05:05:43 +0000 | [diff] [blame] | 24 | from osmo_ms_driver.starter import BinaryOptions |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 25 | |
| 26 | import os.path |
| 27 | import shutil |
| 28 | import tempfile |
| 29 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 30 | class MsDriver(log.Origin): |
| 31 | |
| 32 | def __init__(self, suite_run): |
| 33 | super().__init__(log.C_RUN, 'ms-driver') |
| 34 | self._suite_run = suite_run |
| 35 | |
| 36 | # TODO: take config out of the test scenario |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 37 | self._time_start = timedelta(seconds=60) |
| 38 | self._time_step = timedelta(milliseconds=100) |
| 39 | self._test_duration = timedelta(seconds=120) |
| 40 | self._cdf = cdfs["ease_in_out"](self._time_start, self._time_step) |
| 41 | self._loop = SimpleLoop() |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 42 | self._test_case = None |
Holger Hans Peter Freyther | d6d3206 | 2018-11-05 03:41:38 +0000 | [diff] [blame] | 43 | self.event_server_sk_tmp_dir = None |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 44 | self._subscribers = [] |
| 45 | self._configured = False |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 46 | |
| 47 | if len(self.event_server_path().encode()) > 107: |
| 48 | raise log.Error('Path for event_server socket is longer than max allowed len for unix socket path (107):', self.event_server_path()) |
| 49 | |
| 50 | def event_server_path(self): |
| 51 | if self.event_server_sk_tmp_dir is None: |
| 52 | self.event_server_sk_tmp_dir = tempfile.mkdtemp('', 'ogteventserversk') |
| 53 | return os.path.join(self.event_server_sk_tmp_dir, 'osmo_ms_driver.unix') |
| 54 | |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 55 | def build_binary_options(self): |
| 56 | """Builds an instance of BinaryOptions. |
| 57 | |
| 58 | Populates the BinaryOptions by searching the virtphy and mobile |
| 59 | application within the trial directory. |
| 60 | """ |
| 61 | |
| 62 | # Get the base directory for the virtphy/mobile application |
Holger Hans Peter Freyther | 51ae2b5 | 2018-12-02 15:52:17 +0000 | [diff] [blame] | 63 | inst = util.Dir(os.path.abspath(self._suite_run.trial.get_inst('osmocom-bb'))) |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 64 | |
| 65 | # Assume these are dynamically linked and verify there is a lib dir. |
| 66 | lib = inst.child('lib') |
| 67 | if not os.path.isdir(lib): |
| 68 | raise RuntimeError('No lib/ in %r' % inst) |
| 69 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 70 | |
| 71 | def check_and_return_binary(name): |
| 72 | """Checks the binary exists and returns the path.""" |
| 73 | binary = inst.child('bin', name) |
Holger Hans Peter Freyther | 6bf8822 | 2018-12-03 14:10:05 +0000 | [diff] [blame] | 74 | if not os.path.isfile(binary): |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 75 | raise RuntimeError('Binary missing: %r' % binary) |
| 76 | return binary |
| 77 | |
| 78 | virtphy = check_and_return_binary('virtphy') |
| 79 | mobile = check_and_return_binary('mobile') |
| 80 | return BinaryOptions(virtphy, mobile, env) |
| 81 | |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 82 | def subscriber_add(self, subscriber): |
| 83 | """Adds a subscriber to the list of subscribers.""" |
| 84 | self._subscribers.append(subscriber) |
| 85 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 86 | def configure(self): |
| 87 | """ |
| 88 | Configures the subscribers, tests and registration server. Needs to be |
| 89 | called after the complete configuration of this driver. |
| 90 | """ |
| 91 | event_server_path = self.event_server_path() |
| 92 | |
| 93 | self._ev_server = EventServer("ev_server", event_server_path) |
| 94 | self._ev_server.listen(self._loop) |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 95 | options = self.build_binary_options() |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 96 | self._test_case = MassUpdateLocationTest("mass", options, self._cdf, |
Holger Hans Peter Freyther | 792614f | 2018-11-05 06:23:39 +0000 | [diff] [blame] | 97 | self._ev_server, |
Holger Hans Peter Freyther | d51c2ea | 2018-12-03 14:38:06 +0000 | [diff] [blame] | 98 | util.Dir(self.event_server_sk_tmp_dir), |
Holger Hans Peter Freyther | f743afb | 2018-11-05 06:07:57 +0000 | [diff] [blame] | 99 | suite_run=self._suite_run) |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 100 | |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 101 | for sub in self._subscribers: |
| 102 | self._test_case.subscriber_add(sub) |
| 103 | self._configured = True |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 104 | |
| 105 | def run_test(self): |
| 106 | """ |
| 107 | Runs the configured tests by starting the configured amount of mobile |
| 108 | devices according to their schedule. Returns once all tests succeeded |
| 109 | or the configured timeout has passed. |
| 110 | """ |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 111 | if not self._configured: |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 112 | self.configure() |
| 113 | self._test_case.run_test(self._loop, self._test_duration) |
| 114 | |
| 115 | def print_stats(self): |
| 116 | """ |
| 117 | Prints statistics about the test run. |
| 118 | """ |
| 119 | self._test_case.print_stats() |
| 120 | |
Holger Hans Peter Freyther | 337141f | 2019-02-23 09:58:59 +0000 | [diff] [blame] | 121 | def get_stats(self): |
| 122 | """ |
| 123 | Returns a statistical summary of the test. |
| 124 | """ |
| 125 | return self._test_case.get_stats() |
| 126 | |
| 127 | def get_result_values(self): |
| 128 | """ |
| 129 | Returns the raw result values of the test run in any order. |
| 130 | """ |
| 131 | return self._test_case.get_result_values() |
| 132 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 133 | def cleanup(self): |
| 134 | """ |
Holger Hans Peter Freyther | f743afb | 2018-11-05 06:07:57 +0000 | [diff] [blame] | 135 | Cleans up the driver (e.g. AF_UNIX files). |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 136 | """ |
| 137 | |
| 138 | # Clean-up the temporary directory. |
| 139 | if self.event_server_sk_tmp_dir: |
| 140 | shutil.rmtree(path=self.event_server_sk_tmp_dir) |
| 141 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 142 | # vim: expandtab tabstop=4 shiftwidth=4 |