blob: 516d992da85ff049ce7385bd5d2d577dc6301e86 [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
23from . import config, util, template, process, bts_osmo
24
25class OsmoBtsVirtual(bts_osmo.OsmoBtsMainUnit):
26##############
27# PROTECTED
28##############
29
30 BIN_BTS = 'osmo-bts-virtual'
31 BIN_PCU = 'osmo-pcu'
32
33 CONF_BTS = 'osmo-bts-virtual.cfg'
34
35 def __init__(self, suite_run, conf):
36 """Initializes the OsmoBtsVirtual."""
37 super().__init__(suite_run, conf, OsmoBtsVirtual.BIN_BTS, 'osmo_bts_virtual')
38 self.run_dir = None
39 self.inst = None
40 self.env = {}
41
42 def launch_process(self, keepalive, binary_name, *args):
43 """Launches the osmo-bts-virtual process."""
44
45 binary = os.path.abspath(self.inst.child('bin', binary_name))
46 run_dir = self.run_dir.new_dir(binary_name)
47 if not os.path.isfile(binary):
48 raise RuntimeError('Binary missing: %r' % binary)
49 proc = process.Process(binary_name, run_dir,
50 (binary,) + args,
51 env=self.env)
52 self.suite_run.remember_to_stop(proc, keepalive)
53 proc.launch()
54 return proc
55
56 def configure(self):
57 """Builds the configuration for osmo-bts-virtual and writes it to a file."""
58
59 if self.bsc is None:
60 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
61 self.config_file = self.run_dir.new_file(OsmoBtsVirtual.CONF_BTS)
62 self.dbg(config_file=self.config_file)
63
64 values = dict(osmo_bts_virtual=config.get_defaults('osmo_bts_virtual'))
65 config.overlay(values, self.suite_run.config())
66 config.overlay(values, {
67 'osmo_bts_virtual': {
68 'oml_remote_ip': self.bsc.addr(),
69 'pcu_socket_path': self.pcu_socket_path(),
70 }
71 })
72 config.overlay(values, { 'osmo_bts_virtual': self.conf })
73
74 self.dbg('OSMO-BTS-VIRTUAL CONFIG:\n' + pprint.pformat(values))
75
76 with open(self.config_file, 'w') as f:
77 r = template.render(OsmoBtsVirtual.CONF_BTS, values)
78 self.dbg(r)
79 f.write(r)
80
81########################
82# PUBLIC - INTERNAL API
83########################
84 def conf_for_bsc(self):
85 """Returns the configuration for the BSC (including the BSC/NITB IP)."""
86 values = self.conf_for_bsc_prepare()
87 self.dbg(conf=values)
88 return values
89
90###################
91# PUBLIC (test API included)
92###################
93 def start(self, keepalive=False):
94 """Handles starting/turning-up the osmo-bts-virtual process."""
95 if self.bsc is None:
96 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
97 self.suite_run.poll()
98
99 self.log('Starting to connect to', self.bsc)
100 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
101 self.configure()
102
103 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
104 lib = self.inst.child('lib')
105 if not os.path.isdir(lib):
106 raise RuntimeError('No lib/ in %r' % self.inst)
107 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
108
109 self.proc_bts = self.launch_process(keepalive, OsmoBtsVirtual.BIN_BTS, '-r', '1',
110 '-c', os.path.abspath(self.config_file),
111 '-i', self.bsc.addr())
112 self.suite_run.poll()
113
114# vim: expandtab tabstop=4 shiftwidth=4
115