Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-bts-virtual |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # Copyright (C) 2018 Holger Hans Peter Freyther |
| 5 | # |
| 6 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 7 | # |
| 8 | # This program is free software: you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as |
| 10 | # published by the Free Software Foundation, either version 3 of the |
| 11 | # License, or (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | import os |
| 22 | import pprint |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 23 | from ..core import config, util, template, process |
Pau Espin Pedrol | e8bbcbf | 2020-04-10 19:51:31 +0200 | [diff] [blame] | 24 | from . import bts_osmo |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 25 | |
| 26 | class OsmoBtsVirtual(bts_osmo.OsmoBtsMainUnit): |
| 27 | ############## |
| 28 | # PROTECTED |
| 29 | ############## |
| 30 | |
| 31 | BIN_BTS = 'osmo-bts-virtual' |
| 32 | BIN_PCU = 'osmo-pcu' |
| 33 | |
| 34 | CONF_BTS = 'osmo-bts-virtual.cfg' |
| 35 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 36 | def __init__(self, testenv, conf): |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 37 | """Initializes the OsmoBtsVirtual.""" |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 38 | super().__init__(testenv, conf, OsmoBtsVirtual.BIN_BTS, 'osmo_bts_virtual') |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 39 | self.run_dir = None |
| 40 | self.inst = None |
| 41 | self.env = {} |
| 42 | |
| 43 | def launch_process(self, keepalive, binary_name, *args): |
| 44 | """Launches the osmo-bts-virtual process.""" |
| 45 | |
| 46 | binary = os.path.abspath(self.inst.child('bin', binary_name)) |
| 47 | run_dir = self.run_dir.new_dir(binary_name) |
| 48 | if not os.path.isfile(binary): |
| 49 | raise RuntimeError('Binary missing: %r' % binary) |
| 50 | proc = process.Process(binary_name, run_dir, |
| 51 | (binary,) + args, |
| 52 | env=self.env) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 53 | self.testenv.remember_to_stop(proc, keepalive) |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 54 | proc.launch() |
| 55 | return proc |
| 56 | |
| 57 | def configure(self): |
| 58 | """Builds the configuration for osmo-bts-virtual and writes it to a file.""" |
| 59 | |
| 60 | if self.bsc is None: |
| 61 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured') |
| 62 | self.config_file = self.run_dir.new_file(OsmoBtsVirtual.CONF_BTS) |
| 63 | self.dbg(config_file=self.config_file) |
| 64 | |
| 65 | values = dict(osmo_bts_virtual=config.get_defaults('osmo_bts_virtual')) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 66 | config.overlay(values, self.testenv.suite().config()) |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 67 | config.overlay(values, { |
| 68 | 'osmo_bts_virtual': { |
| 69 | 'oml_remote_ip': self.bsc.addr(), |
| 70 | 'pcu_socket_path': self.pcu_socket_path(), |
| 71 | } |
| 72 | }) |
| 73 | config.overlay(values, { 'osmo_bts_virtual': self.conf }) |
| 74 | |
| 75 | self.dbg('OSMO-BTS-VIRTUAL CONFIG:\n' + pprint.pformat(values)) |
| 76 | |
| 77 | with open(self.config_file, 'w') as f: |
| 78 | r = template.render(OsmoBtsVirtual.CONF_BTS, values) |
| 79 | self.dbg(r) |
| 80 | f.write(r) |
| 81 | |
| 82 | ######################## |
| 83 | # PUBLIC - INTERNAL API |
| 84 | ######################## |
| 85 | def conf_for_bsc(self): |
| 86 | """Returns the configuration for the BSC (including the BSC/NITB IP).""" |
| 87 | values = self.conf_for_bsc_prepare() |
| 88 | self.dbg(conf=values) |
| 89 | return values |
| 90 | |
| 91 | ################### |
| 92 | # PUBLIC (test API included) |
| 93 | ################### |
| 94 | def start(self, keepalive=False): |
| 95 | """Handles starting/turning-up the osmo-bts-virtual process.""" |
| 96 | if self.bsc is None: |
| 97 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started') |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 98 | self.testenv.poll() |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 99 | |
| 100 | self.log('Starting to connect to', self.bsc) |
Pau Espin Pedrol | 2a2d846 | 2020-05-11 10:56:52 +0200 | [diff] [blame] | 101 | self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name())) |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 102 | self.configure() |
| 103 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 104 | self.inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-bts'))) |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 105 | lib = self.inst.child('lib') |
| 106 | if not os.path.isdir(lib): |
| 107 | raise RuntimeError('No lib/ in %r' % self.inst) |
| 108 | self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 109 | |
| 110 | self.proc_bts = self.launch_process(keepalive, OsmoBtsVirtual.BIN_BTS, '-r', '1', |
| 111 | '-c', os.path.abspath(self.config_file), |
| 112 | '-i', self.bsc.addr()) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 113 | self.testenv.poll() |
Holger Hans Peter Freyther | d2db10d | 2018-12-06 18:34:53 +0000 | [diff] [blame] | 114 | |
| 115 | # vim: expandtab tabstop=4 shiftwidth=4 |