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 |
Holger Hans Peter Freyther | f16a8f7 | 2019-04-30 12:01:57 +0100 | [diff] [blame] | 23 | from osmo_ms_driver.location_update_test import MassUpdateLocationTest |
| 24 | from osmo_ms_driver.starter import BinaryOptions, MobileTestStarter |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 25 | from osmo_ms_driver.test_support import TestExecutor |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 26 | |
| 27 | import os.path |
| 28 | import shutil |
| 29 | import tempfile |
| 30 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 31 | class MsDriver(log.Origin): |
| 32 | |
| 33 | def __init__(self, suite_run): |
| 34 | super().__init__(log.C_RUN, 'ms-driver') |
| 35 | self._suite_run = suite_run |
| 36 | |
| 37 | # TODO: take config out of the test scenario |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 38 | self._time_start = timedelta(seconds=60) |
| 39 | self._time_step = timedelta(milliseconds=100) |
| 40 | self._test_duration = timedelta(seconds=120) |
| 41 | self._cdf = cdfs["ease_in_out"](self._time_start, self._time_step) |
| 42 | self._loop = SimpleLoop() |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 43 | self._executor = TestExecutor() |
Holger Hans Peter Freyther | d6d3206 | 2018-11-05 03:41:38 +0000 | [diff] [blame] | 44 | self.event_server_sk_tmp_dir = None |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 45 | self._subscribers = [] |
| 46 | self._configured = False |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 47 | self._results = {} |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 48 | |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 49 | # Set-up and start the event server |
| 50 | event_server_path = self.event_server_path() |
| 51 | if len(event_server_path.encode()) > 107: |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 52 | raise log.Error('Path for event_server socket is longer than max allowed len for unix socket path (107):', self.event_server_path()) |
| 53 | |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 54 | self._ev_server = EventServer("ev_server", event_server_path) |
| 55 | self._ev_server.listen(self._loop) |
| 56 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 57 | def event_server_path(self): |
| 58 | if self.event_server_sk_tmp_dir is None: |
| 59 | self.event_server_sk_tmp_dir = tempfile.mkdtemp('', 'ogteventserversk') |
| 60 | return os.path.join(self.event_server_sk_tmp_dir, 'osmo_ms_driver.unix') |
| 61 | |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 62 | def build_binary_options(self): |
| 63 | """Builds an instance of BinaryOptions. |
| 64 | |
| 65 | Populates the BinaryOptions by searching the virtphy and mobile |
| 66 | application within the trial directory. |
| 67 | """ |
| 68 | |
| 69 | # Get the base directory for the virtphy/mobile application |
Holger Hans Peter Freyther | 51ae2b5 | 2018-12-02 15:52:17 +0000 | [diff] [blame] | 70 | 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] | 71 | |
| 72 | # Assume these are dynamically linked and verify there is a lib dir. |
| 73 | lib = inst.child('lib') |
| 74 | if not os.path.isdir(lib): |
| 75 | raise RuntimeError('No lib/ in %r' % inst) |
| 76 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 77 | |
| 78 | def check_and_return_binary(name): |
| 79 | """Checks the binary exists and returns the path.""" |
| 80 | binary = inst.child('bin', name) |
Holger Hans Peter Freyther | 6bf8822 | 2018-12-03 14:10:05 +0000 | [diff] [blame] | 81 | if not os.path.isfile(binary): |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 82 | raise RuntimeError('Binary missing: %r' % binary) |
| 83 | return binary |
| 84 | |
| 85 | virtphy = check_and_return_binary('virtphy') |
| 86 | mobile = check_and_return_binary('mobile') |
| 87 | return BinaryOptions(virtphy, mobile, env) |
| 88 | |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 89 | def subscriber_add(self, subscriber): |
| 90 | """Adds a subscriber to the list of subscribers.""" |
| 91 | self._subscribers.append(subscriber) |
| 92 | |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 93 | def add_test(self, test_name, **kwargs): |
| 94 | """ |
| 95 | Instantiates and returns a test for the given name. |
| 96 | |
| 97 | The instance created and added will be returned. |
| 98 | """ |
| 99 | if test_name == 'ul_test': |
| 100 | test = MassUpdateLocationTest("mass", |
| 101 | self._ev_server, self._results) |
| 102 | |
| 103 | # Verify that a test was instantiated. |
| 104 | if test_name is None: |
| 105 | raise log.Error("Unknown test_name: " + test_name) |
| 106 | |
| 107 | # Add it to the executor and return it. |
| 108 | self._executor.add_test(test) |
| 109 | return test |
| 110 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 111 | def configure(self): |
| 112 | """ |
| 113 | Configures the subscribers, tests and registration server. Needs to be |
| 114 | called after the complete configuration of this driver. |
| 115 | """ |
Holger Hans Peter Freyther | bdc18d9 | 2018-11-05 06:49:48 +0000 | [diff] [blame] | 116 | options = self.build_binary_options() |
Holger Hans Peter Freyther | 92cc0c9 | 2019-04-28 16:55:30 +0100 | [diff] [blame] | 117 | self._starter = MobileTestStarter("mass", options, self._cdf, |
| 118 | self._ev_server, |
| 119 | util.Dir(self.event_server_sk_tmp_dir), |
| 120 | self._results, suite_run=self._suite_run) |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 121 | |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 122 | for sub in self._subscribers: |
Holger Hans Peter Freyther | 92cc0c9 | 2019-04-28 16:55:30 +0100 | [diff] [blame] | 123 | self._starter.subscriber_add(sub) |
| 124 | |
Holger Hans Peter Freyther | dbb16e8 | 2019-04-30 22:03:12 +0100 | [diff] [blame] | 125 | self._starter.configure_tasks() |
| 126 | self._executor.configure(self._subscribers, self._starter.mobiles()) |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 127 | self._configured = True |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 128 | |
| 129 | def run_test(self): |
| 130 | """ |
| 131 | Runs the configured tests by starting the configured amount of mobile |
| 132 | devices according to their schedule. Returns once all tests succeeded |
| 133 | or the configured timeout has passed. |
| 134 | """ |
Holger Hans Peter Freyther | 5e67ed4 | 2019-02-25 09:48:50 +0000 | [diff] [blame] | 135 | if not self._configured: |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 136 | self.configure() |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 137 | self._executor.before_start() |
Holger Hans Peter Freyther | 92cc0c9 | 2019-04-28 16:55:30 +0100 | [diff] [blame] | 138 | deadline = self._starter.start_all(self._loop, self._test_duration) |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 139 | self._executor.after_start() |
| 140 | self._executor.wait_for_test(self._loop, deadline) |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 141 | |
| 142 | def print_stats(self): |
| 143 | """ |
| 144 | Prints statistics about the test run. |
| 145 | """ |
Holger Hans Peter Freyther | 2e7e843 | 2019-04-30 18:55:59 +0100 | [diff] [blame] | 146 | self._executor.print_stats() |
Holger Hans Peter Freyther | 61f2877 | 2019-04-27 14:25:22 +0100 | [diff] [blame] | 147 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 148 | def cleanup(self): |
| 149 | """ |
Holger Hans Peter Freyther | f743afb | 2018-11-05 06:07:57 +0000 | [diff] [blame] | 150 | Cleans up the driver (e.g. AF_UNIX files). |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 151 | """ |
| 152 | |
| 153 | # Clean-up the temporary directory. |
| 154 | if self.event_server_sk_tmp_dir: |
| 155 | shutil.rmtree(path=self.event_server_sk_tmp_dir) |
| 156 | |
Holger Hans Peter Freyther | b484aab | 2018-08-29 04:28:33 +0100 | [diff] [blame] | 157 | # vim: expandtab tabstop=4 shiftwidth=4 |