blob: 2c1c153e034aa19e489527996302367e3827296b [file] [log] [blame]
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +00001# 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
21import os
22import pprint
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020023from ..core import config, util, template, process
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020024from . import bts_osmo
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +000025
26class 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 Pedrola442cb82020-05-05 12:54:37 +020036 def __init__(self, testenv, conf):
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +000037 """Initializes the OsmoBtsVirtual."""
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020038 super().__init__(testenv, conf, OsmoBtsVirtual.BIN_BTS, 'osmo_bts_virtual')
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +000039 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 Pedrola442cb82020-05-05 12:54:37 +020053 self.testenv.remember_to_stop(proc, keepalive)
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +000054 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 Pedrola442cb82020-05-05 12:54:37 +020066 config.overlay(values, self.testenv.suite().config())
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +000067 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 Pedrola442cb82020-05-05 12:54:37 +020098 self.testenv.poll()
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +000099
100 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +0200101 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +0000102 self.configure()
103
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200104 self.inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-bts')))
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +0000105 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 Pedrola442cb82020-05-05 12:54:37 +0200113 self.testenv.poll()
Holger Hans Peter Freytherd2db10d2018-12-06 18:34:53 +0000114
115# vim: expandtab tabstop=4 shiftwidth=4