blob: 7496ce8f1be504a56c9f77e9cbf72aef2b0d584a [file] [log] [blame]
Neels Hofmeyr17c139e2017-04-12 02:42:02 +02001# osmo_gsm_tester: specifics for running an osmo-bts-trx
Neels Hofmeyr3531a192017-03-28 14:30:28 +02002#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +02009# 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
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020021import pprint
Neels Hofmeyr206803d2017-05-29 03:45:51 +020022from . import log, config, util, template, process, event_loop
Neels Hofmeyr3531a192017-03-28 14:30:28 +020023
24class OsmoBtsTrx(log.Origin):
25 suite_run = None
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020026 bsc = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020027 run_dir = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020028 inst = None
29 env = None
Pau Espin Pedrol47738532017-08-15 16:20:07 +020030 trx = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020031
Neels Hofmeyr3531a192017-03-28 14:30:28 +020032 BIN_BTS_TRX = 'osmo-bts-trx'
33 BIN_PCU = 'osmo-pcu'
34
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020035 CONF_BTS_TRX = 'osmo-bts-trx.cfg'
36
Neels Hofmeyr3531a192017-03-28 14:30:28 +020037 def __init__(self, suite_run, conf):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020038 super().__init__(log.C_RUN, OsmoBtsTrx.BIN_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020039 self.suite_run = suite_run
40 self.conf = conf
Neels Hofmeyr3531a192017-03-28 14:30:28 +020041 self.env = {}
42
Neels Hofmeyrd41dae92017-05-22 19:58:41 +020043 def remote_addr(self):
44 # FIXME
45 return '127.0.0.1'
46
Neels Hofmeyr3531a192017-03-28 14:30:28 +020047 def start(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020048 if self.bsc is None:
49 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020050 self.suite_run.poll()
51
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020052 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrold0912332017-06-14 13:27:08 +020053 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020054 self.configure()
55
Pau Espin Pedrol47738532017-08-15 16:20:07 +020056 self.trx = OsmoTrx(self.suite_run)
57 self.trx.start()
58 self.log('Waiting for osmo-trx to start up...')
59 event_loop.wait(self, self.trx.trx_ready)
60
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020061 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(OsmoBtsTrx.BIN_BTS_TRX)))
Your Name3c6673a2017-04-08 18:52:39 +020062 lib = self.inst.child('lib')
63 if not os.path.isdir(lib):
64 raise RuntimeError('No lib/ in %r' % self.inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020065 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Neels Hofmeyr3531a192017-03-28 14:30:28 +020066
Pau Espin Pedrol6ab98982017-05-12 16:07:35 +020067 self.launch_process(OsmoBtsTrx.BIN_BTS_TRX, '-r', '1',
68 '-c', os.path.abspath(self.config_file),
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020069 '-i', self.bsc.addr())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020070 #self.launch_process(OsmoBtsTrx.BIN_PCU, '-r', '1')
71 self.suite_run.poll()
72
73 def launch_process(self, binary_name, *args):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020074 binary = os.path.abspath(self.inst.child('bin', binary_name))
75 run_dir = self.run_dir.new_dir(binary_name)
76 if not os.path.isfile(binary):
77 raise RuntimeError('Binary missing: %r' % binary)
78 proc = process.Process(binary_name, run_dir,
79 (binary,) + args,
80 env=self.env)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020081 self.suite_run.remember_to_stop(proc)
82 proc.launch()
Neels Hofmeyrfd39f3e2017-05-07 02:02:35 +020083 return proc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020084
85 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020086 if self.bsc is None:
87 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020088 self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020089 self.dbg(config_file=self.config_file)
90
91 values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx'))
92 config.overlay(values, self.suite_run.config())
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020093 config.overlay(values, {
94 'osmo_bts_trx': {
95 'oml_remote_ip': self.bsc.addr(),
96 'pcu_socket_path': os.path.join(str(self.run_dir), 'pcu_bts')
97 }
98 })
99 config.overlay(values, { 'osmo_bts_trx': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +0200100
101 self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200102
103 with open(self.config_file, 'w') as f:
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200104 r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200105 self.dbg(r)
106 f.write(r)
107
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200108 def conf_for_bsc(self):
109 values = config.get_defaults('bsc_bts')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200110 config.overlay(values, config.get_defaults('osmo_bts_trx'))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200111 config.overlay(values, self.conf)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200112 self.dbg(conf=values)
113 return values
114
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200115 def set_bsc(self, bsc):
116 self.bsc = bsc
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200117
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200118class OsmoTrx(log.Origin):
119 suite_run = None
120 run_dir = None
121 inst = None
122 env = None
123 proc_trx = None
124
125 BIN_TRX = 'osmo-trx'
126
127 def __init__(self, suite_run):
128 super().__init__(log.C_RUN, OsmoTrx.BIN_TRX)
129 self.suite_run = suite_run
130 self.env = {}
131
132 def start(self):
133 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
134 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(OsmoTrx.BIN_TRX)))
135 self.proc_trx = self.launch_process(OsmoTrx.BIN_TRX, '-x')
136
137 def launch_process(self, binary_name, *args):
138 binary = os.path.abspath(self.inst.child('bin', binary_name))
139 run_dir = self.run_dir.new_dir(binary_name)
140 if not os.path.isfile(binary):
141 raise RuntimeError('Binary missing: %r' % binary)
142 proc = process.Process(binary_name, run_dir,
143 (binary,) + args,
144 env=self.env)
145 self.suite_run.remember_to_stop(proc)
146 proc.launch()
147 return proc
148
149 def trx_ready(self):
150 if not self.proc_trx or not self.proc_trx.is_running:
151 return False
152 return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200153# vim: expandtab tabstop=4 shiftwidth=4