Pau Espin Pedrol | feb66e7 | 2019-03-04 18:37:04 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running a osmo-bts-oc2g |
| 2 | # |
| 3 | # Copyright (C) 2019 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as |
| 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 |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import pprint |
| 22 | from . import log, config, util, template, process, pcu_oc2g, bts_osmo |
| 23 | |
| 24 | class OsmoBtsOC2G(bts_osmo.OsmoBts): |
| 25 | ############## |
| 26 | # PROTECTED |
| 27 | ############## |
| 28 | |
| 29 | REMOTE_DIR = '/osmo-gsm-tester-bts' |
| 30 | BTS_OC2G_BIN = 'osmo-bts-oc2g' |
| 31 | BTS_OC2G_CFG = 'osmo-bts-oc2g.cfg' |
| 32 | |
| 33 | def __init__(self, suite_run, conf): |
| 34 | super().__init__(suite_run, conf, OsmoBtsOC2G.BTS_OC2G_BIN, 'osmo_bts_oc2g') |
| 35 | self.run_dir = None |
| 36 | self.inst = None |
| 37 | self.remote_inst = None |
| 38 | self.remote_dir = None |
| 39 | self.remote_user = 'root' |
| 40 | |
| 41 | def _direct_pcu_enabled(self): |
| 42 | return util.str2bool(self.conf.get('direct_pcu')) |
| 43 | |
| 44 | def launch_remote(self, name, popen_args, remote_cwd=None, keepalive=False): |
| 45 | run_dir = self.run_dir.new_dir(name) |
| 46 | proc = process.RemoteProcess(name, run_dir, self.remote_user, self.remote_addr(), remote_cwd, |
| 47 | popen_args) |
| 48 | self.suite_run.remember_to_stop(proc, keepalive) |
| 49 | proc.launch() |
| 50 | return proc |
| 51 | |
| 52 | def create_pcu(self): |
| 53 | return pcu_oc2g.OsmoPcuOC2G(self.suite_run, self, self.conf) |
| 54 | |
| 55 | def configure(self): |
| 56 | if self.bsc is None: |
| 57 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured') |
| 58 | |
| 59 | self.config_file = self.run_dir.new_file(OsmoBtsOC2G.BTS_OC2G_CFG) |
| 60 | self.dbg(config_file=self.config_file) |
| 61 | |
| 62 | values = { 'osmo_bts_oc2g': config.get_defaults('osmo_bts_oc2g') } |
| 63 | config.overlay(values, self.suite_run.config()) |
| 64 | config.overlay(values, { |
| 65 | 'osmo_bts_oc2g': { |
| 66 | 'oml_remote_ip': self.bsc.addr(), |
| 67 | 'pcu_socket_path': self.pcu_socket_path(), |
| 68 | } |
| 69 | }) |
| 70 | config.overlay(values, { 'osmo_bts_oc2g': self.conf }) |
| 71 | |
| 72 | self.dbg('OSMO-BTS-OC2G CONFIG:\n' + pprint.pformat(values)) |
| 73 | |
| 74 | with open(self.config_file, 'w') as f: |
| 75 | r = template.render(OsmoBtsOC2G.BTS_OC2G_CFG, values) |
| 76 | self.dbg(r) |
| 77 | f.write(r) |
| 78 | |
| 79 | ######################## |
| 80 | # PUBLIC - INTERNAL API |
| 81 | ######################## |
| 82 | def pcu_socket_path(self): |
| 83 | return os.path.join(OsmoBtsOC2G.REMOTE_DIR, 'pcu_bts') |
| 84 | |
| 85 | def conf_for_bsc(self): |
| 86 | values = self.conf_for_bsc_prepare() |
| 87 | # Hack until we have proper ARFCN resource allocation support (OS#2230) |
| 88 | band = values.get('band') |
| 89 | trx_list = values.get('trx_list') |
| 90 | if band == 'GSM-900': |
| 91 | for trx_i in range(len(trx_list)): |
| 92 | config.overlay(trx_list[trx_i], { 'arfcn' : str(50 + trx_i * 2) }) |
| 93 | self.dbg(conf=values) |
| 94 | return values |
| 95 | |
| 96 | ################### |
| 97 | # PUBLIC (test API included) |
| 98 | ################### |
| 99 | # We get log from ssh stdout instead of usual stderr. |
| 100 | def ready_for_pcu(self): |
| 101 | if not self.proc_bts or not self.proc_bts.is_running: |
| 102 | return False |
| 103 | return 'BTS is up' in (self.proc_bts.get_stdout() or '') |
| 104 | |
| 105 | def start(self, keepalive=False): |
| 106 | if self.bsc is None: |
| 107 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started') |
| 108 | log.log('Starting OsmoBtsOC2G to connect to', self.bsc) |
| 109 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 110 | self.configure() |
| 111 | |
| 112 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(OsmoBtsOC2G.BTS_OC2G_BIN))) |
| 113 | lib = self.inst.child('lib') |
| 114 | if not os.path.isdir(lib): |
| 115 | raise log.Error('No lib/ in', self.inst) |
| 116 | if not self.inst.isfile('bin', OsmoBtsOC2G.BTS_OC2G_BIN): |
| 117 | raise log.Error('No osmo-bts-oc2g binary in', self.inst) |
| 118 | |
| 119 | remote_run_dir = util.Dir(OsmoBtsOC2G.REMOTE_DIR) |
| 120 | |
| 121 | self.remote_inst = process.copy_inst_ssh(self.run_dir, self.inst, remote_run_dir, self.remote_user, |
| 122 | self.remote_addr(), OsmoBtsOC2G.BTS_OC2G_BIN, self.config_file) |
| 123 | |
| 124 | remote_config_file = remote_run_dir.child(OsmoBtsOC2G.BTS_OC2G_CFG) |
| 125 | remote_lib = self.remote_inst.child('lib') |
| 126 | remote_binary = self.remote_inst.child('bin', 'osmo-bts-oc2g') |
| 127 | |
| 128 | args = ('LD_LIBRARY_PATH=%s' % remote_lib, |
| 129 | remote_binary, '-c', remote_config_file, '-r', '1', |
| 130 | '-i', self.bsc.addr()) |
| 131 | |
| 132 | if self._direct_pcu_enabled(): |
| 133 | args += ('-M',) |
| 134 | |
| 135 | self.proc_bts = self.launch_remote('osmo-bts-oc2g', args, remote_cwd=remote_run_dir, keepalive=keepalive) |
| 136 | |
| 137 | # vim: expandtab tabstop=4 shiftwidth=4 |