blob: f1ad6fc431c516c57db81bc3d6b46671801fbebd [file] [log] [blame]
Neels Hofmeyr6743c0c2017-06-13 16:26:06 +02001# osmo_gsm_tester: specifics for running an osmo-stp
2#
3# Copyright (C) 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
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
20import os
21import pprint
22
23from . import log, util, config, template, process, pcap_recorder
24
25class OsmoStp(log.Origin):
26 suite_run = None
27 ip_address = None
28 run_dir = None
29 config_file = None
30 process = None
31
32 def __init__(self, suite_run, ip_address):
33 super().__init__(log.C_RUN, 'osmo-stp_%s' % ip_address.get('addr'))
34 self.suite_run = suite_run
35 self.ip_address = ip_address
36
37 def start(self):
38 self.log('Starting osmo-stp')
39 self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name()))
40 self.configure()
41
42 # NOTE: libosmo-sccp provides osmo-stp and is built as a dependency of
43 # OsmoMSC.
44 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-msc')))
45
46 binary = inst.child('bin', 'osmo-stp')
47 if not os.path.isfile(binary):
48 raise RuntimeError('Binary missing: %r' % binary)
49 lib = inst.child('lib')
50 if not os.path.isdir(lib):
51 raise RuntimeError('No lib/ in %r' % inst)
52
53 # TODO: osmo-stp is not yet configurable to a specific IP address
54 #iface = util.ip_to_iface(self.addr())
55 #pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
56 # 'host %s and port not 22' % self.addr())
57
58 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
59
60 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
61 self.process = process.Process(self.name(), self.run_dir,
62 (binary, '-c',
63 os.path.abspath(self.config_file)),
64 env=env)
65 self.suite_run.remember_to_stop(self.process)
66 self.process.launch()
67
68 def configure(self):
69 self.config_file = self.run_dir.new_file('osmo-stp.cfg')
70 self.dbg(config_file=self.config_file)
71
72 values = dict(stp=config.get_defaults('stp'))
73 config.overlay(values, self.suite_run.config())
74 config.overlay(values, dict(stp=dict(ip_address=self.ip_address)))
75
76 self.dbg('STP CONFIG:\n' + pprint.pformat(values))
77
78 with open(self.config_file, 'w') as f:
79 r = template.render('osmo-stp.cfg', values)
80 self.dbg(r)
81 f.write(r)
82
83 def addr(self):
84 return self.ip_address.get('addr')
85
86 def running(self):
87 return not self.process.terminated()
88
89# vim: expandtab tabstop=4 shiftwidth=4